Skip to content

Commit a3e7435

Browse files
committed
Add test for static volumes
And moved a test from deployment_test that was not the right place to be created.
1 parent c01cdf5 commit a3e7435

File tree

2 files changed

+100
-29
lines changed

2 files changed

+100
-29
lines changed

generator/deployment_test.go

-29
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,3 @@ services:
3838
t.Errorf("Expected image to be nginx:1.29, got %s", dt.Spec.Template.Spec.Containers[0].Image)
3939
}
4040
}
41-
42-
func TestGenerateWithBoundVolume(t *testing.T) {
43-
_compose_file := `
44-
services:
45-
web:
46-
image: nginx:1.29
47-
volumes:
48-
- data:/var/www
49-
volumes:
50-
data:
51-
`
52-
tmpDir := setup(_compose_file)
53-
defer teardown(tmpDir)
54-
55-
currentDir, _ := os.Getwd()
56-
os.Chdir(tmpDir)
57-
defer os.Chdir(currentDir)
58-
59-
output := _compile_test(t, "-s", "templates/web/deployment.yaml")
60-
61-
dt := v1.Deployment{}
62-
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
63-
t.Errorf("Failed to unmarshal the output: %s", err)
64-
}
65-
66-
if dt.Spec.Template.Spec.Containers[0].VolumeMounts[0].Name != "data" {
67-
t.Errorf("Expected volume name to be data: %v", dt)
68-
}
69-
}

generator/volume_test.go

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package generator
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
v1 "k8s.io/api/apps/v1"
9+
corev1 "k8s.io/api/core/v1"
10+
"sigs.k8s.io/yaml"
11+
)
12+
13+
func TestGenerateWithBoundVolume(t *testing.T) {
14+
_compose_file := `
15+
services:
16+
web:
17+
image: nginx:1.29
18+
volumes:
19+
- data:/var/www
20+
volumes:
21+
data:
22+
`
23+
tmpDir := setup(_compose_file)
24+
defer teardown(tmpDir)
25+
26+
currentDir, _ := os.Getwd()
27+
os.Chdir(tmpDir)
28+
defer os.Chdir(currentDir)
29+
30+
output := _compile_test(t, "-s", "templates/web/deployment.yaml")
31+
32+
dt := v1.Deployment{}
33+
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
34+
t.Errorf("Failed to unmarshal the output: %s", err)
35+
}
36+
37+
if dt.Spec.Template.Spec.Containers[0].VolumeMounts[0].Name != "data" {
38+
t.Errorf("Expected volume name to be data: %v", dt)
39+
}
40+
}
41+
42+
func TestWithStaticFiles(t *testing.T) {
43+
_compose_file := `
44+
services:
45+
web:
46+
image: nginx:1.29
47+
volumes:
48+
- ./static:/var/www
49+
labels:
50+
%sconfigmap-files: |-
51+
- ./static
52+
`
53+
_compose_file = fmt.Sprintf(_compose_file, KATENARY_PREFIX)
54+
tmpDir := setup(_compose_file)
55+
defer teardown(tmpDir)
56+
57+
// create a static directory with an index.html file
58+
staticDir := tmpDir + "/static"
59+
os.Mkdir(staticDir, 0o755)
60+
indexFile, err := os.Create(staticDir + "/index.html")
61+
if err != nil {
62+
t.Errorf("Failed to create index.html: %s", err)
63+
}
64+
indexFile.WriteString("<html><body><h1>Hello, World!</h1></body></html>")
65+
indexFile.Close()
66+
67+
currentDir, _ := os.Getwd()
68+
os.Chdir(tmpDir)
69+
defer os.Chdir(currentDir)
70+
71+
output := _compile_test(t, "-s", "templates/web/deployment.yaml")
72+
dt := v1.Deployment{}
73+
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
74+
t.Errorf("Failed to unmarshal the output: %s", err)
75+
}
76+
// get the volume mount path
77+
volumeMountPath := dt.Spec.Template.Spec.Containers[0].VolumeMounts[0].MountPath
78+
if volumeMountPath != "/var/www" {
79+
t.Errorf("Expected volume mount path to be /var/www, got %s", volumeMountPath)
80+
}
81+
82+
// read the configMap
83+
output, err = helmTemplate(ConvertOptions{
84+
OutputDir: tmpDir + "/chart",
85+
}, "-s", "templates/web/statics/static/configmap.yaml")
86+
if err != nil {
87+
t.Errorf("Failed to run helm template: %s", err)
88+
}
89+
configMap := corev1.ConfigMap{}
90+
if err := yaml.Unmarshal([]byte(output), &configMap); err != nil {
91+
t.Errorf("Failed to unmarshal the output: %s", err)
92+
}
93+
data := configMap.Data
94+
if len(data) != 1 {
95+
t.Errorf("Expected 1 data, got %d", len(data))
96+
}
97+
if data["index.html"] != "<html><body><h1>Hello, World!</h1></body></html>" {
98+
t.Errorf("Expected index.html to be <html><body><h1>Hello, World!</h1></body></html>, got %s", data["index.html"])
99+
}
100+
}

0 commit comments

Comments
 (0)