Sonar complience

Use valid names and factorize some constants checks
This commit is contained in:
2024-10-18 09:34:57 +02:00
parent d2c8d08b7f
commit 533e1422d0
8 changed files with 37 additions and 36 deletions

View File

@@ -24,7 +24,7 @@ services:
os.Chdir(tmpDir) os.Chdir(tmpDir)
defer os.Chdir(currentDir) defer os.Chdir(currentDir)
output := _compile_test(t, "-s", "templates/web/configmap.yaml") output := internalCompileTest(t, "-s", "templates/web/configmap.yaml")
configMap := v1.ConfigMap{} configMap := v1.ConfigMap{}
if err := yaml.Unmarshal([]byte(output), &configMap); err != nil { if err := yaml.Unmarshal([]byte(output), &configMap); err != nil {
t.Errorf(unmarshalError, err) t.Errorf(unmarshalError, err)

View File

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

View File

@@ -25,7 +25,7 @@ services:
os.Chdir(tmpDir) os.Chdir(tmpDir)
defer os.Chdir(currentDir) defer os.Chdir(currentDir)
output := _compile_test(t, "-s", webTemplateOutput) output := internalCompileTest(t, "-s", webTemplateOutput)
// dt := DeploymentTest{} // dt := DeploymentTest{}
dt := v1.Deployment{} dt := v1.Deployment{}
@@ -67,7 +67,7 @@ services:
os.Chdir(tmpDir) os.Chdir(tmpDir)
defer os.Chdir(currentDir) defer os.Chdir(currentDir)
output := _compile_test(t, "-s", webTemplateOutput) output := internalCompileTest(t, "-s", webTemplateOutput)
dt := v1.Deployment{} dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil { if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
t.Errorf(unmarshalError, err) t.Errorf(unmarshalError, err)
@@ -125,7 +125,7 @@ services:
os.Chdir(tmpDir) os.Chdir(tmpDir)
defer os.Chdir(currentDir) defer os.Chdir(currentDir)
output := _compile_test(t, "-s", webTemplateOutput) output := internalCompileTest(t, "-s", webTemplateOutput)
dt := v1.Deployment{} dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil { if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
t.Errorf(unmarshalError, err) t.Errorf(unmarshalError, err)
@@ -167,7 +167,7 @@ services:
os.Chdir(tmpDir) os.Chdir(tmpDir)
defer os.Chdir(currentDir) defer os.Chdir(currentDir)
output := _compile_test(t, "-s", webTemplateOutput) output := internalCompileTest(t, "-s", webTemplateOutput)
dt := v1.Deployment{} dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil { if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
t.Errorf(unmarshalError, err) t.Errorf(unmarshalError, err)
@@ -220,7 +220,7 @@ services:
os.Chdir(tmpDir) os.Chdir(tmpDir)
defer os.Chdir(currentDir) defer os.Chdir(currentDir)
output := _compile_test(t, "-s", webTemplateOutput) output := internalCompileTest(t, "-s", webTemplateOutput)
dt := v1.Deployment{} dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil { if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
t.Errorf(unmarshalError, err) t.Errorf(unmarshalError, err)
@@ -257,7 +257,7 @@ services:
os.Chdir(tmpDir) os.Chdir(tmpDir)
defer os.Chdir(currentDir) defer os.Chdir(currentDir)
output := _compile_test(t, "-s", webTemplateOutput) output := internalCompileTest(t, "-s", webTemplateOutput)
dt := v1.Deployment{} dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil { if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
t.Errorf(unmarshalError, err) t.Errorf(unmarshalError, err)
@@ -303,7 +303,7 @@ services:
os.Chdir(tmpDir) os.Chdir(tmpDir)
defer os.Chdir(currentDir) defer os.Chdir(currentDir)
output := _compile_test(t, "-s", webTemplateOutput) output := internalCompileTest(t, "-s", webTemplateOutput)
dt := v1.Deployment{} dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil { if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
t.Errorf(unmarshalError, err) t.Errorf(unmarshalError, err)

View File

@@ -30,7 +30,7 @@ services:
os.Chdir(tmpDir) os.Chdir(tmpDir)
defer os.Chdir(currentDir) 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{} ingress := v1.Ingress{}
if err := yaml.Unmarshal([]byte(output), &ingress); err != nil { if err := yaml.Unmarshal([]byte(output), &ingress); err != nil {
t.Errorf(unmarshalError, err) t.Errorf(unmarshalError, err)

View File

@@ -29,7 +29,7 @@ services:
os.Chdir(tmpDir) os.Chdir(tmpDir)
defer os.Chdir(currentDir) defer os.Chdir(currentDir)
output := _compile_test(t, "-s", "templates/web/secret.yaml") output := internalCompileTest(t, "-s", "templates/web/secret.yaml")
secret := v1.Secret{} secret := v1.Secret{}
if err := yaml.Unmarshal([]byte(output), &secret); err != nil { if err := yaml.Unmarshal([]byte(output), &secret); err != nil {
t.Errorf(unmarshalError, err) t.Errorf(unmarshalError, err)

View File

@@ -24,7 +24,7 @@ services:
os.Chdir(tmpDir) os.Chdir(tmpDir)
defer os.Chdir(currentDir) defer os.Chdir(currentDir)
output := _compile_test(t, "-s", "templates/web/service.yaml") output := internalCompileTest(t, "-s", "templates/web/service.yaml")
service := v1.Service{} service := v1.Service{}
if err := yaml.Unmarshal([]byte(output), &service); err != nil { if err := yaml.Unmarshal([]byte(output), &service); err != nil {
t.Errorf(unmarshalError, err) t.Errorf(unmarshalError, err)

View File

@@ -1,12 +1,11 @@
package generator package generator
import ( import (
"katenary/parser"
"log" "log"
"os" "os"
"os/exec" "os/exec"
"testing" "testing"
"katenary/parser"
) )
const unmarshalError = "Failed to unmarshal the output: %s" const unmarshalError = "Failed to unmarshal the output: %s"
@@ -29,8 +28,8 @@ func teardown(tmpDir string) {
} }
} }
func _compile_test(t *testing.T, options ...string) string { func internalCompileTest(t *testing.T, options ...string) string {
_, err := parser.Parse(nil, "compose.yml") _, err := parser.Parse(nil, nil, "compose.yml")
if err != nil { if err != nil {
t.Fatalf("Failed to parse the project: %s", err) t.Fatalf("Failed to parse the project: %s", err)
} }

View File

@@ -10,8 +10,10 @@ import (
"sigs.k8s.io/yaml" "sigs.k8s.io/yaml"
) )
const htmlContent = "<html><body><h1>Hello, World!</h1></body></html>"
func TestGenerateWithBoundVolume(t *testing.T) { func TestGenerateWithBoundVolume(t *testing.T) {
compose_file := ` composeFile := `
services: services:
web: web:
image: nginx:1.29 image: nginx:1.29
@@ -20,14 +22,14 @@ services:
volumes: volumes:
data: data:
` `
tmpDir := setup(compose_file) tmpDir := setup(composeFile)
defer teardown(tmpDir) defer teardown(tmpDir)
currentDir, _ := os.Getwd() currentDir, _ := os.Getwd()
os.Chdir(tmpDir) os.Chdir(tmpDir)
defer os.Chdir(currentDir) defer os.Chdir(currentDir)
output := _compile_test(t, "-s", "templates/web/deployment.yaml") output := internalCompileTest(t, "-s", "templates/web/deployment.yaml")
dt := v1.Deployment{} dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil { if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
@@ -40,7 +42,7 @@ volumes:
} }
func TestWithStaticFiles(t *testing.T) { func TestWithStaticFiles(t *testing.T) {
compose_file := ` composeFile := `
services: services:
web: web:
image: nginx:1.29 image: nginx:1.29
@@ -50,8 +52,8 @@ services:
%s/configmap-files: |- %s/configmap-files: |-
- ./static - ./static
` `
compose_file = fmt.Sprintf(compose_file, katenaryLabelPrefix) composeFile = fmt.Sprintf(composeFile, katenaryLabelPrefix)
tmpDir := setup(compose_file) tmpDir := setup(composeFile)
defer teardown(tmpDir) defer teardown(tmpDir)
// create a static directory with an index.html file // create a static directory with an index.html file
@@ -61,14 +63,14 @@ services:
if err != nil { if err != nil {
t.Errorf("Failed to create index.html: %s", err) t.Errorf("Failed to create index.html: %s", err)
} }
indexFile.WriteString("<html><body><h1>Hello, World!</h1></body></html>") indexFile.WriteString(htmlContent)
indexFile.Close() indexFile.Close()
currentDir, _ := os.Getwd() currentDir, _ := os.Getwd()
os.Chdir(tmpDir) os.Chdir(tmpDir)
defer os.Chdir(currentDir) defer os.Chdir(currentDir)
output := _compile_test(t, "-s", "templates/web/deployment.yaml") output := internalCompileTest(t, "-s", "templates/web/deployment.yaml")
dt := v1.Deployment{} dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil { if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
t.Errorf(unmarshalError, err) t.Errorf(unmarshalError, err)
@@ -94,13 +96,13 @@ services:
if len(data) != 1 { if len(data) != 1 {
t.Errorf("Expected 1 data, got %d", len(data)) t.Errorf("Expected 1 data, got %d", len(data))
} }
if data["index.html"] != "<html><body><h1>Hello, World!</h1></body></html>" { if data["index.html"] != htmlContent {
t.Errorf("Expected index.html to be <html><body><h1>Hello, World!</h1></body></html>, got %s", data["index.html"]) t.Errorf("Expected index.html to be "+htmlContent+", got %s", data["index.html"])
} }
} }
func TestWithFileMapping(t *testing.T) { func TestWithFileMapping(t *testing.T) {
compose_file := ` composeFile := `
services: services:
web: web:
image: nginx:1.29 image: nginx:1.29
@@ -110,8 +112,8 @@ services:
%s/configmap-files: |- %s/configmap-files: |-
- ./static/index.html - ./static/index.html
` `
compose_file = fmt.Sprintf(compose_file, katenaryLabelPrefix) composeFile = fmt.Sprintf(composeFile, katenaryLabelPrefix)
tmpDir := setup(compose_file) tmpDir := setup(composeFile)
defer teardown(tmpDir) defer teardown(tmpDir)
// create a static directory with an index.html file // create a static directory with an index.html file
@@ -121,14 +123,14 @@ services:
if err != nil { if err != nil {
t.Errorf("Failed to create index.html: %s", err) t.Errorf("Failed to create index.html: %s", err)
} }
indexFile.WriteString("<html><body><h1>Hello, World!</h1></body></html>") indexFile.WriteString(htmlContent)
indexFile.Close() indexFile.Close()
currentDir, _ := os.Getwd() currentDir, _ := os.Getwd()
os.Chdir(tmpDir) os.Chdir(tmpDir)
defer os.Chdir(currentDir) defer os.Chdir(currentDir)
output := _compile_test(t, "-s", "templates/web/deployment.yaml") output := internalCompileTest(t, "-s", "templates/web/deployment.yaml")
dt := v1.Deployment{} dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil { if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
t.Errorf(unmarshalError, err) t.Errorf(unmarshalError, err)
@@ -147,7 +149,7 @@ services:
} }
func TestBindFrom(t *testing.T) { func TestBindFrom(t *testing.T) {
compose_file := ` composeFile := `
services: services:
web: web:
image: nginx:1.29 image: nginx:1.29
@@ -167,15 +169,15 @@ volumes:
data: data:
` `
compose_file = fmt.Sprintf(compose_file, katenaryLabelPrefix) composeFile = fmt.Sprintf(composeFile, katenaryLabelPrefix)
tmpDir := setup(compose_file) tmpDir := setup(composeFile)
defer teardown(tmpDir) defer teardown(tmpDir)
currentDir, _ := os.Getwd() currentDir, _ := os.Getwd()
os.Chdir(tmpDir) os.Chdir(tmpDir)
defer os.Chdir(currentDir) defer os.Chdir(currentDir)
output := _compile_test(t, "-s", "templates/web/deployment.yaml") output := internalCompileTest(t, "-s", "templates/web/deployment.yaml")
dt := v1.Deployment{} dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil { if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
t.Errorf(unmarshalError, err) t.Errorf(unmarshalError, err)