Skip to content

Commit

Permalink
Sonar complience
Browse files Browse the repository at this point in the history
Use valid names and factorize some constants checks
  • Loading branch information
Your Name committed Oct 18, 2024
1 parent 2fc6742 commit 95e2b47
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 36 deletions.
2 changes: 1 addition & 1 deletion generator/configMap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ services:
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

output := _compile_test(t, "-s", "templates/web/configmap.yaml")
output := internalCompileTest(t, "-s", "templates/web/configmap.yaml")
configMap := v1.ConfigMap{}
if err := yaml.Unmarshal([]byte(output), &configMap); err != nil {
t.Errorf(unmarshalError, err)
Expand Down
4 changes: 2 additions & 2 deletions generator/cronJob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ services:
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

output := _compile_test(t, "-s", "templates/cron/cronjob.yaml")
output := internalCompileTest(t, "-s", "templates/cron/cronjob.yaml")
cronJob := batchv1.CronJob{}
if err := yaml.Unmarshal([]byte(output), &cronJob); err != nil {
t.Errorf(unmarshalError, err)
Expand Down Expand Up @@ -83,7 +83,7 @@ services:
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

output := _compile_test(t, "-s", "templates/cron/cronjob.yaml")
output := internalCompileTest(t, "-s", "templates/cron/cronjob.yaml")
cronJob := batchv1.CronJob{}
if err := yaml.Unmarshal([]byte(output), &cronJob); err != nil {
t.Errorf(unmarshalError, err)
Expand Down
14 changes: 7 additions & 7 deletions generator/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ services:
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

output := _compile_test(t, "-s", webTemplateOutput)
output := internalCompileTest(t, "-s", webTemplateOutput)

// dt := DeploymentTest{}
dt := v1.Deployment{}
Expand Down Expand Up @@ -67,7 +67,7 @@ services:
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

output := _compile_test(t, "-s", webTemplateOutput)
output := internalCompileTest(t, "-s", webTemplateOutput)
dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
t.Errorf(unmarshalError, err)
Expand Down Expand Up @@ -125,7 +125,7 @@ services:
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

output := _compile_test(t, "-s", webTemplateOutput)
output := internalCompileTest(t, "-s", webTemplateOutput)
dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
t.Errorf(unmarshalError, err)
Expand Down Expand Up @@ -167,7 +167,7 @@ services:
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

output := _compile_test(t, "-s", webTemplateOutput)
output := internalCompileTest(t, "-s", webTemplateOutput)
dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
t.Errorf(unmarshalError, err)
Expand Down Expand Up @@ -220,7 +220,7 @@ services:
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

output := _compile_test(t, "-s", webTemplateOutput)
output := internalCompileTest(t, "-s", webTemplateOutput)
dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
t.Errorf(unmarshalError, err)
Expand Down Expand Up @@ -257,7 +257,7 @@ services:
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

output := _compile_test(t, "-s", webTemplateOutput)
output := internalCompileTest(t, "-s", webTemplateOutput)
dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
t.Errorf(unmarshalError, err)
Expand Down Expand Up @@ -303,7 +303,7 @@ services:
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

output := _compile_test(t, "-s", webTemplateOutput)
output := internalCompileTest(t, "-s", webTemplateOutput)
dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
t.Errorf(unmarshalError, err)
Expand Down
2 changes: 1 addition & 1 deletion generator/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ services:
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

output := _compile_test(t, "-s", "templates/web/ingress.yaml", "--set", "web.ingress.enabled=true")
output := internalCompileTest(t, "-s", "templates/web/ingress.yaml", "--set", "web.ingress.enabled=true")
ingress := v1.Ingress{}
if err := yaml.Unmarshal([]byte(output), &ingress); err != nil {
t.Errorf(unmarshalError, err)
Expand Down
2 changes: 1 addition & 1 deletion generator/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ services:
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

output := _compile_test(t, "-s", "templates/web/secret.yaml")
output := internalCompileTest(t, "-s", "templates/web/secret.yaml")
secret := v1.Secret{}
if err := yaml.Unmarshal([]byte(output), &secret); err != nil {
t.Errorf(unmarshalError, err)
Expand Down
2 changes: 1 addition & 1 deletion generator/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ services:
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

output := _compile_test(t, "-s", "templates/web/service.yaml")
output := internalCompileTest(t, "-s", "templates/web/service.yaml")
service := v1.Service{}
if err := yaml.Unmarshal([]byte(output), &service); err != nil {
t.Errorf(unmarshalError, err)
Expand Down
7 changes: 3 additions & 4 deletions generator/tools_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package generator

import (
"katenary/parser"
"log"
"os"
"os/exec"
"testing"

"katenary/parser"
)

const unmarshalError = "Failed to unmarshal the output: %s"
Expand All @@ -29,8 +28,8 @@ func teardown(tmpDir string) {
}
}

func _compile_test(t *testing.T, options ...string) string {
_, err := parser.Parse(nil, "compose.yml")
func internalCompileTest(t *testing.T, options ...string) string {
_, err := parser.Parse(nil, nil, "compose.yml")
if err != nil {
t.Fatalf("Failed to parse the project: %s", err)
}
Expand Down
40 changes: 21 additions & 19 deletions generator/volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
"sigs.k8s.io/yaml"
)

const htmlContent = "<html><body><h1>Hello, World!</h1></body></html>"

func TestGenerateWithBoundVolume(t *testing.T) {
compose_file := `
composeFile := `
services:
web:
image: nginx:1.29
Expand All @@ -20,14 +22,14 @@ services:
volumes:
data:
`
tmpDir := setup(compose_file)
tmpDir := setup(composeFile)
defer teardown(tmpDir)

currentDir, _ := os.Getwd()
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

output := _compile_test(t, "-s", "templates/web/deployment.yaml")
output := internalCompileTest(t, "-s", "templates/web/deployment.yaml")

dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
Expand All @@ -40,7 +42,7 @@ volumes:
}

func TestWithStaticFiles(t *testing.T) {
compose_file := `
composeFile := `
services:
web:
image: nginx:1.29
Expand All @@ -50,8 +52,8 @@ services:
%s/configmap-files: |-
- ./static
`
compose_file = fmt.Sprintf(compose_file, katenaryLabelPrefix)
tmpDir := setup(compose_file)
composeFile = fmt.Sprintf(composeFile, katenaryLabelPrefix)
tmpDir := setup(composeFile)
defer teardown(tmpDir)

// create a static directory with an index.html file
Expand All @@ -61,14 +63,14 @@ services:
if err != nil {
t.Errorf("Failed to create index.html: %s", err)
}
indexFile.WriteString("<html><body><h1>Hello, World!</h1></body></html>")
indexFile.WriteString(htmlContent)
indexFile.Close()

currentDir, _ := os.Getwd()
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

output := _compile_test(t, "-s", "templates/web/deployment.yaml")
output := internalCompileTest(t, "-s", "templates/web/deployment.yaml")
dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
t.Errorf(unmarshalError, err)
Expand All @@ -94,13 +96,13 @@ services:
if len(data) != 1 {
t.Errorf("Expected 1 data, got %d", len(data))
}
if data["index.html"] != "<html><body><h1>Hello, World!</h1></body></html>" {
t.Errorf("Expected index.html to be <html><body><h1>Hello, World!</h1></body></html>, got %s", data["index.html"])
if data["index.html"] != htmlContent {
t.Errorf("Expected index.html to be "+htmlContent+", got %s", data["index.html"])
}
}

func TestWithFileMapping(t *testing.T) {
compose_file := `
composeFile := `
services:
web:
image: nginx:1.29
Expand All @@ -110,8 +112,8 @@ services:
%s/configmap-files: |-
- ./static/index.html
`
compose_file = fmt.Sprintf(compose_file, katenaryLabelPrefix)
tmpDir := setup(compose_file)
composeFile = fmt.Sprintf(composeFile, katenaryLabelPrefix)
tmpDir := setup(composeFile)
defer teardown(tmpDir)

// create a static directory with an index.html file
Expand All @@ -121,14 +123,14 @@ services:
if err != nil {
t.Errorf("Failed to create index.html: %s", err)
}
indexFile.WriteString("<html><body><h1>Hello, World!</h1></body></html>")
indexFile.WriteString(htmlContent)
indexFile.Close()

currentDir, _ := os.Getwd()
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

output := _compile_test(t, "-s", "templates/web/deployment.yaml")
output := internalCompileTest(t, "-s", "templates/web/deployment.yaml")
dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
t.Errorf(unmarshalError, err)
Expand All @@ -147,7 +149,7 @@ services:
}

func TestBindFrom(t *testing.T) {
compose_file := `
composeFile := `
services:
web:
image: nginx:1.29
Expand All @@ -167,15 +169,15 @@ volumes:
data:
`

compose_file = fmt.Sprintf(compose_file, katenaryLabelPrefix)
tmpDir := setup(compose_file)
composeFile = fmt.Sprintf(composeFile, katenaryLabelPrefix)
tmpDir := setup(composeFile)
defer teardown(tmpDir)

currentDir, _ := os.Getwd()
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

output := _compile_test(t, "-s", "templates/web/deployment.yaml")
output := internalCompileTest(t, "-s", "templates/web/deployment.yaml")
dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
t.Errorf(unmarshalError, err)
Expand Down

0 comments on commit 95e2b47

Please sign in to comment.