Skip to content

Commit

Permalink
chore(test): enhance error handling and avoid repetitions
Browse files Browse the repository at this point in the history
  • Loading branch information
metal3d committed Dec 17, 2024
1 parent 131ea5d commit 41a4292
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
10 changes: 8 additions & 2 deletions generator/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ services:
composeFileContent = fmt.Sprintf(composeFileContent, labels.KatenaryLabelPrefix)
tmpDir, err := os.MkdirTemp("", "katenary-test-override")
if err != nil {
t.Fatalf(err.Error())
t.Fatal(err.Error())
}
composeFile := filepath.Join(tmpDir, "compose.yaml")

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

Expand All @@ -83,6 +86,9 @@ services:
cli.WithDefaultConfigPath,
)
project, err := cli.ProjectFromOptions(options)
if err != nil {
t.Fatal(err)
}
if err := fixPorts(&project.Services[0]); err != nil {
t.Errorf("Expected no error, got %s", err)
}
Expand Down
36 changes: 23 additions & 13 deletions generator/volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import (
"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) {
composeFile := `
Expand All @@ -35,15 +39,15 @@ volumes:
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

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

dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
t.Errorf(unmarshalError, err)
}

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)
}
}

Expand Down Expand Up @@ -76,7 +80,7 @@ services:
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

output := internalCompileTest(t, "-s", "templates/web/deployment.yaml")
output := internalCompileTest(t, "-s", developementFile)
dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
t.Errorf(unmarshalError, err)
Expand All @@ -102,8 +106,8 @@ services:
if len(data) != 1 {
t.Errorf("Expected 1 data, got %d", len(data))
}
if data["index.html"] != htmlContent {
t.Errorf("Expected index.html to be "+htmlContent+", got %s", data["index.html"])
if data[indexHtmlFile] != htmlContent {
t.Errorf("Expected index.html to be "+htmlContent+", got %s", data[indexHtmlFile])
}
}

Expand Down Expand Up @@ -136,7 +140,7 @@ services:
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

output := internalCompileTest(t, "-s", "templates/web/deployment.yaml")
output := internalCompileTest(t, "-s", developementFile)
dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
t.Errorf(unmarshalError, err)
Expand All @@ -149,7 +153,7 @@ services:
}
// but this time, we need a 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)
}
}
Expand Down Expand Up @@ -199,7 +203,7 @@ services:
}
png.Encode(f, img)
f.Close()
output := internalCompileTest(t, "-s", "templates/web/deployment.yaml")
output := internalCompileTest(t, "-s", developementFile)
d := v1.Deployment{}
yaml.Unmarshal([]byte(output), &d)
volumes := d.Spec.Template.Spec.Volumes
Expand All @@ -211,6 +215,9 @@ services:
cmContent, err := helmTemplate(ConvertOptions{
OutputDir: "chart",
}, "-s", "templates/web/statics/images/configmap.yaml")
if err != nil {
t.Fatal(err)
}
yaml.Unmarshal([]byte(cmContent), &cm)
if im, ok := cm.BinaryData["foo.png"]; !ok {
t.Errorf("Expected foo.png to be in the configmap")
Expand Down Expand Up @@ -266,7 +273,7 @@ services:
}
png.Encode(f, img)
f.Close()
output := internalCompileTest(t, "-s", "templates/web/deployment.yaml")
output := internalCompileTest(t, "-s", developementFile)
d := v1.Deployment{}
yaml.Unmarshal([]byte(output), &d)
volumes := d.Spec.Template.Spec.Volumes
Expand All @@ -278,6 +285,9 @@ services:
cmContent, err := helmTemplate(ConvertOptions{
OutputDir: "chart",
}, "-s", "templates/web/statics/images/configmap.yaml")
if err != nil {
t.Fatal(err)
}
yaml.Unmarshal([]byte(cmContent), &cm)
if im, ok := cm.BinaryData["foo.png"]; !ok {
t.Errorf("Expected foo.png to be in the configmap")
Expand Down Expand Up @@ -317,17 +327,17 @@ volumes:
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

output := internalCompileTest(t, "-s", "templates/web/deployment.yaml")
output := internalCompileTest(t, "-s", developementFile)
dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
t.Errorf(unmarshalError, err)
}
// both containers should have the same volume mount
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" {
t.Errorf("Expected volume name to be data: %v", dt)
t.Errorf("Expected container 1 volume name to be data: %v", dt)
}
}

Expand Down

0 comments on commit 41a4292

Please sign in to comment.