Skip to content

Commit 1cfb606

Browse files
authored
fixes : #1177 support variable values on open libery alizer's port de… (#111)
* fixes : #1177 support variable values on open libery alizer's port detection Signed-off-by: Horiodino <[email protected]> * fixes : #1177 added more test cases Signed-off-by: Horiodino <[email protected]> * update: go.mod Signed-off-by: Horiodino <[email protected]> --------- Signed-off-by: Horiodino <[email protected]>
1 parent b6b73d2 commit 1cfb606

File tree

5 files changed

+120
-8
lines changed

5 files changed

+120
-8
lines changed

pkg/apis/enricher/framework/java/openliberty_detector.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ package enricher
1414
import (
1515
"context"
1616
"encoding/xml"
17+
"strings"
1718

1819
"github.com/devfile/alizer/pkg/apis/model"
1920
"github.com/devfile/alizer/pkg/utils"
@@ -67,10 +68,30 @@ func (o OpenLibertyDetector) DoPortsDetection(component *model.Component, ctx *c
6768
if err != nil {
6869
continue
6970
}
70-
ports := utils.GetValidPorts([]string{data.HttpEndpoint.HttpPort, data.HttpEndpoint.HttpsPort})
71+
72+
variables := make(map[string]string)
73+
for _, v := range data.Variables {
74+
variables[v.Name] = v.DefaultValue
75+
}
76+
77+
httpPort := resolvePort(data.HttpEndpoint.HttpPort, variables)
78+
httpsPort := resolvePort(data.HttpEndpoint.HttpsPort, variables)
79+
80+
ports := utils.GetValidPorts([]string{httpPort, httpsPort})
7181
if len(ports) > 0 {
7282
component.Ports = ports
7383
return
7484
}
7585
}
7686
}
87+
88+
// resolvePort resolves the port value by checking if it is a variable and if it is, it returns the value of the variable
89+
func resolvePort(portValue string, variables map[string]string) string {
90+
if strings.HasPrefix(portValue, "${") && strings.HasSuffix(portValue, "}") {
91+
varName := strings.Trim(portValue, "${}")
92+
if value, exists := variables[varName]; exists {
93+
return value
94+
}
95+
}
96+
return portValue
97+
}

pkg/apis/model/model.go

+4
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ type OpenLibertyServerXml struct {
180180
HttpPort string `xml:"httpPort,attr"`
181181
HttpsPort string `xml:"httpsPort,attr"`
182182
} `xml:"httpEndpoint"`
183+
Variables []struct {
184+
Name string `xml:"name,attr"`
185+
DefaultValue string `xml:"defaultValue,attr"`
186+
} `xml:"variable"`
183187
}
184188

185189
// PortDetectionAlgorithm represents one of port detection algorithm values

pkg/utils/detector.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import (
2121
"fmt"
2222
"io"
2323
"io/fs"
24+
"net/http"
2425
"os"
2526
"path/filepath"
2627
"regexp"
2728
"strconv"
2829
"strings"
29-
"net/http"
3030

3131
"github.com/devfile/alizer/pkg/apis/model"
3232
"github.com/devfile/alizer/pkg/schema"
@@ -139,7 +139,7 @@ func GetPomFileContent(pomFilePath string) (schema.Pom, error) {
139139
if err != nil {
140140
return schema.Pom{}, err
141141
}
142-
142+
143143
var pom schema.Pom
144144
err = xml.Unmarshal(byteValue, &pom)
145145
if err != nil {
@@ -568,9 +568,9 @@ func GetAnyApplicationFilePathExactMatch(root string, propsFiles []model.Applica
568568
func GenerateApplicationFileFromFilters(files []string, path string, suffix string, ctx *context.Context) []model.ApplicationFileInfo {
569569
applicationFileInfos := []model.ApplicationFileInfo{}
570570
for _, file := range files {
571-
if strings.HasSuffix(file, suffix) && !strings.HasSuffix(file, "_test.go"){
571+
if strings.HasSuffix(file, suffix) && !strings.HasSuffix(file, "_test.go") {
572572
applicationFileInfos = append(applicationFileInfos, createAppFileInfo(file, path, ctx))
573-
}
573+
}
574574
}
575575
return applicationFileInfos
576576
}
@@ -751,13 +751,13 @@ func NormalizeSplit(file string) (string, string) {
751751
return dir, fileName
752752
}
753753

754-
func CloseHttpResponseBody(resp *http.Response){
754+
func CloseHttpResponseBody(resp *http.Response) {
755755
if err := resp.Body.Close(); err != nil {
756756
fmt.Printf("error closing file: %s", err)
757757
}
758758
}
759759

760-
func CloseFile(file *os.File){
760+
func CloseFile(file *os.File) {
761761
if err := file.Close(); err != nil {
762762
fmt.Printf("error closing file: %s", err)
763763
}

test/apis/component_recognizer_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ func TestPortDetectionJavaMicronautFromDockerfileWithSSLEnabled(t *testing.T) {
158158

159159
func TestPortDetectionOnOpenLiberty(t *testing.T) {
160160
testPortDetectionInProject(t, "open-liberty", []int{9080, 9443})
161+
testOpenLibertyDetector_DoPortsDetection(t, "open-liberty", []int{9080, 9443})
161162
}
162163

163164
func TestPortDetectionJavaQuarkus(t *testing.T) {

test/apis/utils.go

+87-1
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ import (
1010
"strings"
1111
"testing"
1212

13+
framework "github.com/devfile/alizer/pkg/apis/enricher/framework/java"
1314
"github.com/devfile/alizer/pkg/apis/model"
1415
"github.com/devfile/alizer/pkg/apis/recognizer"
16+
"github.com/stretchr/testify/assert"
1517
)
1618

1719
func updateContent(filePath string, data []byte) error {
1820
f, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
1921
if err != nil {
2022
return err
2123
}
22-
defer func(){
24+
defer func() {
2325
if err := f.Close(); err != nil {
2426
fmt.Printf("error closing file: %s", err)
2527
}
@@ -103,3 +105,87 @@ func getTestProjectPath(folder string) string {
103105
basepath := filepath.Dir(b)
104106
return filepath.Join(basepath, "..", "..", "resources/projects", folder)
105107
}
108+
109+
func testOpenLibertyDetector_DoPortsDetection(t *testing.T, projectType string, expectedPorts []int) {
110+
tempDir, err := os.MkdirTemp("", projectType+"-test")
111+
if err != nil {
112+
t.Fatal(err)
113+
}
114+
defer os.RemoveAll(tempDir)
115+
116+
hardcodedXML := `
117+
<server>
118+
<httpEndpoint host="*" httpPort="1234" httpsPort="1235" id="defaultHttpEndpoint"/>
119+
</server>
120+
`
121+
writeTestFile(t, tempDir, "hardcoded_server.xml", hardcodedXML)
122+
123+
variableXML := `
124+
<server>
125+
<variable name="default.http.port" defaultValue="9080"/>
126+
<variable name="default.https.port" defaultValue="9443"/>
127+
<httpEndpoint host="*" httpPort="${default.http.port}" httpsPort="${default.https.port}" id="defaultHttpEndpoint"/>
128+
</server>
129+
`
130+
writeTestFile(t, tempDir, "variable_server.xml", variableXML)
131+
132+
mixedXML := `
133+
<server>
134+
<variable name="default.http.port" defaultValue="9080"/>
135+
<httpEndpoint host="*" httpPort="${default.http.port}" httpsPort="1235" id="defaultHttpEndpoint"/>
136+
</server>
137+
`
138+
writeTestFile(t, tempDir, "mixed_server.xml", mixedXML)
139+
140+
emptyVarXML := `
141+
<server>
142+
<variable name="default.http.port" defaultValue=""/>
143+
<variable name="default.https.port" defaultValue="9443"/>
144+
<httpEndpoint host="*" httpPort="${default.http.port}" httpsPort="${default.https.port}" id="defaultHttpEndpoint"/>
145+
</server>
146+
`
147+
writeTestFile(t, tempDir, "empty_var_server.xml", emptyVarXML)
148+
149+
detector := framework.OpenLibertyDetector{}
150+
ctx := context.TODO()
151+
152+
t.Run("Hardcoded Ports", func(t *testing.T) {
153+
component := &model.Component{
154+
Path: filepath.Join(tempDir, "hardcoded_server.xml"),
155+
}
156+
detector.DoPortsDetection(component, &ctx)
157+
assert.Equal(t, []int{1234, 1235}, component.Ports)
158+
})
159+
160+
t.Run("Variable-based Ports", func(t *testing.T) {
161+
component := &model.Component{
162+
Path: filepath.Join(tempDir, "variable_server.xml"),
163+
}
164+
detector.DoPortsDetection(component, &ctx)
165+
assert.Equal(t, expectedPorts, component.Ports)
166+
})
167+
168+
t.Run("Mixed Hardcoded and Variable Ports", func(t *testing.T) {
169+
component := &model.Component{
170+
Path: filepath.Join(tempDir, "mixed_server.xml"),
171+
}
172+
detector.DoPortsDetection(component, &ctx)
173+
assert.Equal(t, []int{9080, 1235}, component.Ports)
174+
})
175+
176+
t.Run("Empty Variable Port", func(t *testing.T) {
177+
component := &model.Component{
178+
Path: filepath.Join(tempDir, "empty_var_server.xml"),
179+
}
180+
detector.DoPortsDetection(component, &ctx)
181+
assert.Equal(t, []int{9443}, component.Ports)
182+
})
183+
}
184+
185+
func writeTestFile(t *testing.T, dir, filename, content string) {
186+
path := filepath.Join(dir, filename)
187+
err := os.WriteFile(path, []byte(content), 0644)
188+
if err != nil {
189+
t.Fatal(err)
190+
}
191+
}

0 commit comments

Comments
 (0)