chore(test): enhance error handling and avoid repetitions

This commit is contained in:
2024-12-17 18:22:45 +01:00
parent 131ea5d569
commit 41a4292939
2 changed files with 31 additions and 15 deletions

View File

@@ -21,7 +21,7 @@ services:
composeFileContent = fmt.Sprintf(composeFileContent, labels.KatenaryLabelPrefix) composeFileContent = fmt.Sprintf(composeFileContent, labels.KatenaryLabelPrefix)
tmpDir, err := os.MkdirTemp("", "katenary-test-override") tmpDir, err := os.MkdirTemp("", "katenary-test-override")
if err != nil { if err != nil {
t.Fatalf(err.Error()) t.Fatal(err.Error())
} }
composeFile := filepath.Join(tmpDir, "compose.yaml") composeFile := filepath.Join(tmpDir, "compose.yaml")
@@ -38,6 +38,9 @@ services:
cli.WithDefaultConfigPath, cli.WithDefaultConfigPath,
) )
project, err := cli.ProjectFromOptions(options) project, err := cli.ProjectFromOptions(options)
if err != nil {
t.Fatal(err)
}
if err := fixPorts(&project.Services[0]); err != nil { if err := fixPorts(&project.Services[0]); err != nil {
t.Errorf("Expected no error, got %s", err) t.Errorf("Expected no error, got %s", err)
} }
@@ -66,7 +69,7 @@ services:
composeFileContent = fmt.Sprintf(composeFileContent, labels.KatenaryLabelPrefix) composeFileContent = fmt.Sprintf(composeFileContent, labels.KatenaryLabelPrefix)
tmpDir, err := os.MkdirTemp("", "katenary-test-override") tmpDir, err := os.MkdirTemp("", "katenary-test-override")
if err != nil { if err != nil {
t.Fatalf(err.Error()) t.Fatal(err)
} }
composeFile := filepath.Join(tmpDir, "compose.yaml") composeFile := filepath.Join(tmpDir, "compose.yaml")
@@ -83,6 +86,9 @@ services:
cli.WithDefaultConfigPath, cli.WithDefaultConfigPath,
) )
project, err := cli.ProjectFromOptions(options) project, err := cli.ProjectFromOptions(options)
if err != nil {
t.Fatal(err)
}
if err := fixPorts(&project.Services[0]); err != nil { if err := fixPorts(&project.Services[0]); err != nil {
t.Errorf("Expected no error, got %s", err) t.Errorf("Expected no error, got %s", err)
} }

View File

@@ -16,7 +16,11 @@ import (
"sigs.k8s.io/yaml" "sigs.k8s.io/yaml"
) )
const htmlContent = "<html><body><h1>Hello, World!</h1></body></html>" const (
htmlContent = "<html><body><h1>Hello, World!</h1></body></html>"
developementFile = "templates/web/deployment.yaml"
indexHtmlFile = "index.html"
)
func TestGenerateWithBoundVolume(t *testing.T) { func TestGenerateWithBoundVolume(t *testing.T) {
composeFile := ` composeFile := `
@@ -35,7 +39,7 @@ volumes:
os.Chdir(tmpDir) os.Chdir(tmpDir)
defer os.Chdir(currentDir) defer os.Chdir(currentDir)
output := internalCompileTest(t, "-s", "templates/web/deployment.yaml") output := internalCompileTest(t, "-s", developementFile)
dt := v1.Deployment{} dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil { if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
@@ -43,7 +47,7 @@ volumes:
} }
if dt.Spec.Template.Spec.Containers[0].VolumeMounts[0].Name != "data" { if dt.Spec.Template.Spec.Containers[0].VolumeMounts[0].Name != "data" {
t.Errorf("Expected volume name to be data: %v", dt) t.Errorf("Expected container volume name to be data: %v", dt)
} }
} }
@@ -76,7 +80,7 @@ services:
os.Chdir(tmpDir) os.Chdir(tmpDir)
defer os.Chdir(currentDir) defer os.Chdir(currentDir)
output := internalCompileTest(t, "-s", "templates/web/deployment.yaml") output := internalCompileTest(t, "-s", developementFile)
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)
@@ -102,8 +106,8 @@ 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"] != htmlContent { if data[indexHtmlFile] != htmlContent {
t.Errorf("Expected index.html to be "+htmlContent+", got %s", data["index.html"]) t.Errorf("Expected index.html to be "+htmlContent+", got %s", data[indexHtmlFile])
} }
} }
@@ -136,7 +140,7 @@ services:
os.Chdir(tmpDir) os.Chdir(tmpDir)
defer os.Chdir(currentDir) defer os.Chdir(currentDir)
output := internalCompileTest(t, "-s", "templates/web/deployment.yaml") output := internalCompileTest(t, "-s", developementFile)
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)
@@ -149,7 +153,7 @@ services:
} }
// but this time, we need a subpath // but this time, we need a subpath
subPath := dt.Spec.Template.Spec.Containers[0].VolumeMounts[0].SubPath subPath := dt.Spec.Template.Spec.Containers[0].VolumeMounts[0].SubPath
if subPath != "index.html" { if subPath != indexHtmlFile {
t.Errorf("Expected subpath to be index.html, got %s", subPath) t.Errorf("Expected subpath to be index.html, got %s", subPath)
} }
} }
@@ -199,7 +203,7 @@ services:
} }
png.Encode(f, img) png.Encode(f, img)
f.Close() f.Close()
output := internalCompileTest(t, "-s", "templates/web/deployment.yaml") output := internalCompileTest(t, "-s", developementFile)
d := v1.Deployment{} d := v1.Deployment{}
yaml.Unmarshal([]byte(output), &d) yaml.Unmarshal([]byte(output), &d)
volumes := d.Spec.Template.Spec.Volumes volumes := d.Spec.Template.Spec.Volumes
@@ -211,6 +215,9 @@ services:
cmContent, err := helmTemplate(ConvertOptions{ cmContent, err := helmTemplate(ConvertOptions{
OutputDir: "chart", OutputDir: "chart",
}, "-s", "templates/web/statics/images/configmap.yaml") }, "-s", "templates/web/statics/images/configmap.yaml")
if err != nil {
t.Fatal(err)
}
yaml.Unmarshal([]byte(cmContent), &cm) yaml.Unmarshal([]byte(cmContent), &cm)
if im, ok := cm.BinaryData["foo.png"]; !ok { if im, ok := cm.BinaryData["foo.png"]; !ok {
t.Errorf("Expected foo.png to be in the configmap") t.Errorf("Expected foo.png to be in the configmap")
@@ -266,7 +273,7 @@ services:
} }
png.Encode(f, img) png.Encode(f, img)
f.Close() f.Close()
output := internalCompileTest(t, "-s", "templates/web/deployment.yaml") output := internalCompileTest(t, "-s", developementFile)
d := v1.Deployment{} d := v1.Deployment{}
yaml.Unmarshal([]byte(output), &d) yaml.Unmarshal([]byte(output), &d)
volumes := d.Spec.Template.Spec.Volumes volumes := d.Spec.Template.Spec.Volumes
@@ -278,6 +285,9 @@ services:
cmContent, err := helmTemplate(ConvertOptions{ cmContent, err := helmTemplate(ConvertOptions{
OutputDir: "chart", OutputDir: "chart",
}, "-s", "templates/web/statics/images/configmap.yaml") }, "-s", "templates/web/statics/images/configmap.yaml")
if err != nil {
t.Fatal(err)
}
yaml.Unmarshal([]byte(cmContent), &cm) yaml.Unmarshal([]byte(cmContent), &cm)
if im, ok := cm.BinaryData["foo.png"]; !ok { if im, ok := cm.BinaryData["foo.png"]; !ok {
t.Errorf("Expected foo.png to be in the configmap") t.Errorf("Expected foo.png to be in the configmap")
@@ -317,17 +327,17 @@ volumes:
os.Chdir(tmpDir) os.Chdir(tmpDir)
defer os.Chdir(currentDir) defer os.Chdir(currentDir)
output := internalCompileTest(t, "-s", "templates/web/deployment.yaml") output := internalCompileTest(t, "-s", developementFile)
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)
} }
// both containers should have the same volume mount // both containers should have the same volume mount
if dt.Spec.Template.Spec.Containers[0].VolumeMounts[0].Name != "data" { if dt.Spec.Template.Spec.Containers[0].VolumeMounts[0].Name != "data" {
t.Errorf("Expected volume name to be data: %v", dt) t.Errorf("Expected container 0 volume name to be data: %v", dt)
} }
if dt.Spec.Template.Spec.Containers[1].VolumeMounts[0].Name != "data" { if dt.Spec.Template.Spec.Containers[1].VolumeMounts[0].Name != "data" {
t.Errorf("Expected volume name to be data: %v", dt) t.Errorf("Expected container 1 volume name to be data: %v", dt)
} }
} }