Add more tests, refactor to fix problems

Signed-off-by: Patrice Ferlet <metal3d@gmail.com>
This commit is contained in:
2024-04-24 20:55:27 +02:00
parent 15a2f25e51
commit da7d92bbfa
8 changed files with 429 additions and 20 deletions

View File

@@ -11,7 +11,7 @@ import (
)
func TestGenerateWithBoundVolume(t *testing.T) {
_compose_file := `
compose_file := `
services:
web:
image: nginx:1.29
@@ -20,7 +20,7 @@ services:
volumes:
data:
`
tmpDir := setup(_compose_file)
tmpDir := setup(compose_file)
defer teardown(tmpDir)
currentDir, _ := os.Getwd()
@@ -40,7 +40,7 @@ volumes:
}
func TestWithStaticFiles(t *testing.T) {
_compose_file := `
compose_file := `
services:
web:
image: nginx:1.29
@@ -50,8 +50,8 @@ services:
%s/configmap-files: |-
- ./static
`
_compose_file = fmt.Sprintf(_compose_file, katenaryLabelPrefix)
tmpDir := setup(_compose_file)
compose_file = fmt.Sprintf(compose_file, katenaryLabelPrefix)
tmpDir := setup(compose_file)
defer teardown(tmpDir)
// create a static directory with an index.html file
@@ -98,3 +98,93 @@ services:
t.Errorf("Expected index.html to be <html><body><h1>Hello, World!</h1></body></html>, got %s", data["index.html"])
}
}
func TestWithFileMapping(t *testing.T) {
compose_file := `
services:
web:
image: nginx:1.29
volumes:
- ./static/index.html:/var/www/index.html
labels:
%s/configmap-files: |-
- ./static/index.html
`
compose_file = fmt.Sprintf(compose_file, katenaryLabelPrefix)
tmpDir := setup(compose_file)
defer teardown(tmpDir)
// create a static directory with an index.html file
staticDir := tmpDir + "/static"
os.Mkdir(staticDir, 0o755)
indexFile, err := os.Create(staticDir + "/index.html")
if err != nil {
t.Errorf("Failed to create index.html: %s", err)
}
indexFile.WriteString("<html><body><h1>Hello, World!</h1></body></html>")
indexFile.Close()
currentDir, _ := os.Getwd()
os.Chdir(tmpDir)
defer os.Chdir(currentDir)
output := _compile_test(t, "-s", "templates/web/deployment.yaml")
dt := v1.Deployment{}
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
t.Errorf(unmarshalError, err)
}
// get the volume mount path
volumeMountPath := dt.Spec.Template.Spec.Containers[0].VolumeMounts[0].MountPath
if volumeMountPath != "/var/www/index.html" {
t.Errorf("Expected volume mount path to be /var/www/index.html, got %s", volumeMountPath)
}
// but this time, we need a subpath
subPath := dt.Spec.Template.Spec.Containers[0].VolumeMounts[0].SubPath
if subPath != "index.html" {
t.Errorf("Expected subpath to be index.html, got %s", subPath)
}
}
func TestBindFrom(t *testing.T) {
compose_file := `
services:
web:
image: nginx:1.29
volumes:
- data:/var/www
fpm:
image: php:fpm
volumes:
- data:/var/www
labels:
%[1]s/ports: |
- 9000
%[1]s/same-pod: web
volumes:
data:
`
compose_file = fmt.Sprintf(compose_file, katenaryLabelPrefix)
tmpDir := setup(compose_file)
defer teardown(tmpDir)
currentDir, _ := os.Getwd()
os.Chdir(tmpDir)
defer os.Chdir(currentDir)
output := _compile_test(t, "-s", "templates/web/deployment.yaml")
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)
}
if dt.Spec.Template.Spec.Containers[1].VolumeMounts[0].Name != "data" {
t.Errorf("Expected volume name to be data: %v", dt)
}
}