Skip to content

Commit

Permalink
fixes : #1177 support variable values on open libery alizer's port de… (
Browse files Browse the repository at this point in the history
#111)

* fixes : #1177 support variable values on open libery alizer's port detection

Signed-off-by: Horiodino <holiodin@gmail.com>

* fixes : #1177 added more test cases

Signed-off-by: Horiodino <holiodin@gmail.com>

* update: go.mod

Signed-off-by: Horiodino <holiodin@gmail.com>

---------

Signed-off-by: Horiodino <holiodin@gmail.com>
  • Loading branch information
Horiodino authored Oct 28, 2024
1 parent b6b73d2 commit 1cfb606
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 8 deletions.
23 changes: 22 additions & 1 deletion pkg/apis/enricher/framework/java/openliberty_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package enricher
import (
"context"
"encoding/xml"
"strings"

"github.com/devfile/alizer/pkg/apis/model"
"github.com/devfile/alizer/pkg/utils"
Expand Down Expand Up @@ -67,10 +68,30 @@ func (o OpenLibertyDetector) DoPortsDetection(component *model.Component, ctx *c
if err != nil {
continue
}
ports := utils.GetValidPorts([]string{data.HttpEndpoint.HttpPort, data.HttpEndpoint.HttpsPort})

variables := make(map[string]string)
for _, v := range data.Variables {
variables[v.Name] = v.DefaultValue
}

httpPort := resolvePort(data.HttpEndpoint.HttpPort, variables)
httpsPort := resolvePort(data.HttpEndpoint.HttpsPort, variables)

ports := utils.GetValidPorts([]string{httpPort, httpsPort})
if len(ports) > 0 {
component.Ports = ports
return
}
}
}

// resolvePort resolves the port value by checking if it is a variable and if it is, it returns the value of the variable
func resolvePort(portValue string, variables map[string]string) string {
if strings.HasPrefix(portValue, "${") && strings.HasSuffix(portValue, "}") {
varName := strings.Trim(portValue, "${}")
if value, exists := variables[varName]; exists {
return value
}
}
return portValue
}
4 changes: 4 additions & 0 deletions pkg/apis/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ type OpenLibertyServerXml struct {
HttpPort string `xml:"httpPort,attr"`
HttpsPort string `xml:"httpsPort,attr"`
} `xml:"httpEndpoint"`
Variables []struct {
Name string `xml:"name,attr"`
DefaultValue string `xml:"defaultValue,attr"`
} `xml:"variable"`
}

// PortDetectionAlgorithm represents one of port detection algorithm values
Expand Down
12 changes: 6 additions & 6 deletions pkg/utils/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"net/http"

"github.com/devfile/alizer/pkg/apis/model"
"github.com/devfile/alizer/pkg/schema"
Expand Down Expand Up @@ -139,7 +139,7 @@ func GetPomFileContent(pomFilePath string) (schema.Pom, error) {
if err != nil {
return schema.Pom{}, err
}

var pom schema.Pom
err = xml.Unmarshal(byteValue, &pom)
if err != nil {
Expand Down Expand Up @@ -568,9 +568,9 @@ func GetAnyApplicationFilePathExactMatch(root string, propsFiles []model.Applica
func GenerateApplicationFileFromFilters(files []string, path string, suffix string, ctx *context.Context) []model.ApplicationFileInfo {
applicationFileInfos := []model.ApplicationFileInfo{}
for _, file := range files {
if strings.HasSuffix(file, suffix) && !strings.HasSuffix(file, "_test.go"){
if strings.HasSuffix(file, suffix) && !strings.HasSuffix(file, "_test.go") {
applicationFileInfos = append(applicationFileInfos, createAppFileInfo(file, path, ctx))
}
}
}
return applicationFileInfos
}
Expand Down Expand Up @@ -751,13 +751,13 @@ func NormalizeSplit(file string) (string, string) {
return dir, fileName
}

func CloseHttpResponseBody(resp *http.Response){
func CloseHttpResponseBody(resp *http.Response) {
if err := resp.Body.Close(); err != nil {
fmt.Printf("error closing file: %s", err)
}
}

func CloseFile(file *os.File){
func CloseFile(file *os.File) {
if err := file.Close(); err != nil {
fmt.Printf("error closing file: %s", err)
}
Expand Down
1 change: 1 addition & 0 deletions test/apis/component_recognizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func TestPortDetectionJavaMicronautFromDockerfileWithSSLEnabled(t *testing.T) {

func TestPortDetectionOnOpenLiberty(t *testing.T) {
testPortDetectionInProject(t, "open-liberty", []int{9080, 9443})
testOpenLibertyDetector_DoPortsDetection(t, "open-liberty", []int{9080, 9443})
}

func TestPortDetectionJavaQuarkus(t *testing.T) {
Expand Down
88 changes: 87 additions & 1 deletion test/apis/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ import (
"strings"
"testing"

framework "github.com/devfile/alizer/pkg/apis/enricher/framework/java"
"github.com/devfile/alizer/pkg/apis/model"
"github.com/devfile/alizer/pkg/apis/recognizer"
"github.com/stretchr/testify/assert"
)

func updateContent(filePath string, data []byte) error {
f, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer func(){
defer func() {
if err := f.Close(); err != nil {
fmt.Printf("error closing file: %s", err)
}
Expand Down Expand Up @@ -103,3 +105,87 @@ func getTestProjectPath(folder string) string {
basepath := filepath.Dir(b)
return filepath.Join(basepath, "..", "..", "resources/projects", folder)
}

func testOpenLibertyDetector_DoPortsDetection(t *testing.T, projectType string, expectedPorts []int) {
tempDir, err := os.MkdirTemp("", projectType+"-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)

hardcodedXML := `
<server>
<httpEndpoint host="*" httpPort="1234" httpsPort="1235" id="defaultHttpEndpoint"/>
</server>
`
writeTestFile(t, tempDir, "hardcoded_server.xml", hardcodedXML)

variableXML := `
<server>
<variable name="default.http.port" defaultValue="9080"/>
<variable name="default.https.port" defaultValue="9443"/>
<httpEndpoint host="*" httpPort="${default.http.port}" httpsPort="${default.https.port}" id="defaultHttpEndpoint"/>
</server>
`
writeTestFile(t, tempDir, "variable_server.xml", variableXML)

mixedXML := `
<server>
<variable name="default.http.port" defaultValue="9080"/>
<httpEndpoint host="*" httpPort="${default.http.port}" httpsPort="1235" id="defaultHttpEndpoint"/>
</server>
`
writeTestFile(t, tempDir, "mixed_server.xml", mixedXML)

emptyVarXML := `
<server>
<variable name="default.http.port" defaultValue=""/>
<variable name="default.https.port" defaultValue="9443"/>
<httpEndpoint host="*" httpPort="${default.http.port}" httpsPort="${default.https.port}" id="defaultHttpEndpoint"/>
</server>
`
writeTestFile(t, tempDir, "empty_var_server.xml", emptyVarXML)

detector := framework.OpenLibertyDetector{}
ctx := context.TODO()

t.Run("Hardcoded Ports", func(t *testing.T) {
component := &model.Component{
Path: filepath.Join(tempDir, "hardcoded_server.xml"),
}
detector.DoPortsDetection(component, &ctx)
assert.Equal(t, []int{1234, 1235}, component.Ports)
})

t.Run("Variable-based Ports", func(t *testing.T) {
component := &model.Component{
Path: filepath.Join(tempDir, "variable_server.xml"),
}
detector.DoPortsDetection(component, &ctx)
assert.Equal(t, expectedPorts, component.Ports)
})

t.Run("Mixed Hardcoded and Variable Ports", func(t *testing.T) {
component := &model.Component{
Path: filepath.Join(tempDir, "mixed_server.xml"),
}
detector.DoPortsDetection(component, &ctx)
assert.Equal(t, []int{9080, 1235}, component.Ports)
})

t.Run("Empty Variable Port", func(t *testing.T) {
component := &model.Component{
Path: filepath.Join(tempDir, "empty_var_server.xml"),
}
detector.DoPortsDetection(component, &ctx)
assert.Equal(t, []int{9443}, component.Ports)
})
}

func writeTestFile(t *testing.T, dir, filename, content string) {
path := filepath.Join(dir, filename)
err := os.WriteFile(path, []byte(content), 0644)
if err != nil {
t.Fatal(err)
}
}

0 comments on commit 1cfb606

Please sign in to comment.