Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes : #1177 support variable values on open libery alizer's port de… #111

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
golang.org/x/mod v0.20.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
gotest.tools v1.4.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just need to run go mod tidy as @thepetk mentioned

)

require (
Expand All @@ -32,6 +33,8 @@ require (
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/gotestyourself/gotestyourself v1.4.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gotestyourself/gotestyourself v1.4.0 h1:CDSlSIuRL/Fsc72Ln5lMybtrCvSRDddsHsDRG/nP7Rg=
github.com/gotestyourself/gotestyourself v1.4.0/go.mod h1:zZKM6oeNM8k+FRljX1mnzVYeS8wiGgQyvST1/GafPbY=
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=
github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
Expand Down Expand Up @@ -196,3 +198,5 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v1.4.0 h1:BjtEgfuw8Qyd+jPvQz8CfoxiO/UjFEidWinwEXZiWv0=
gotest.tools v1.4.0/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
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)
}
}
Loading