Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for static volumes #71

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions generator/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,3 @@ services:
t.Errorf("Expected image to be nginx:1.29, got %s", dt.Spec.Template.Spec.Containers[0].Image)
}
}

func TestGenerateWithBoundVolume(t *testing.T) {
_compose_file := `
services:
web:
image: nginx:1.29
volumes:
- data:/var/www
volumes:
data:
`
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("Failed to unmarshal the output: %s", err)
}

if dt.Spec.Template.Spec.Containers[0].VolumeMounts[0].Name != "data" {
t.Errorf("Expected volume name to be data: %v", dt)
}
}
100 changes: 100 additions & 0 deletions generator/volume_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package generator

import (
"fmt"
"os"
"testing"

v1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/yaml"
)

func TestGenerateWithBoundVolume(t *testing.T) {
_compose_file := `
services:
web:
image: nginx:1.29
volumes:
- data:/var/www
volumes:
data:
`
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("Failed to unmarshal the output: %s", err)
}

if dt.Spec.Template.Spec.Containers[0].VolumeMounts[0].Name != "data" {
t.Errorf("Expected volume name to be data: %v", dt)
}
}

func TestWithStaticFiles(t *testing.T) {
_compose_file := `
services:
web:
image: nginx:1.29
volumes:
- ./static:/var/www
labels:
%sconfigmap-files: |-
- ./static
`
_compose_file = fmt.Sprintf(_compose_file, KATENARY_PREFIX)
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("Failed to unmarshal the output: %s", err)
}
// get the volume mount path
volumeMountPath := dt.Spec.Template.Spec.Containers[0].VolumeMounts[0].MountPath
if volumeMountPath != "/var/www" {
t.Errorf("Expected volume mount path to be /var/www, got %s", volumeMountPath)
}

// read the configMap
output, err = helmTemplate(ConvertOptions{
OutputDir: tmpDir + "/chart",
}, "-s", "templates/web/statics/static/configmap.yaml")
if err != nil {
t.Errorf("Failed to run helm template: %s", err)
}
configMap := corev1.ConfigMap{}
if err := yaml.Unmarshal([]byte(output), &configMap); err != nil {
t.Errorf("Failed to unmarshal the output: %s", err)
}
data := configMap.Data
if len(data) != 1 {
t.Errorf("Expected 1 data, got %d", len(data))
}
if data["index.html"] != "<html><body><h1>Hello, World!</h1></body></html>" {
t.Errorf("Expected index.html to be <html><body><h1>Hello, World!</h1></body></html>, got %s", data["index.html"])
}
}
Loading