Add more tests on probes and dependencies
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package generator
|
package generator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -135,3 +136,198 @@ services:
|
|||||||
t.Errorf("Expected 1 init container, got %d", len(dt.Spec.Template.Spec.InitContainers))
|
t.Errorf("Expected 1 init container, got %d", len(dt.Spec.Template.Spec.InitContainers))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHelmDependencies(t *testing.T) {
|
||||||
|
compose_file := `
|
||||||
|
services:
|
||||||
|
web:
|
||||||
|
image: nginx:1.29
|
||||||
|
ports:
|
||||||
|
- 80:80
|
||||||
|
|
||||||
|
mariadb:
|
||||||
|
image: mariadb:10.5
|
||||||
|
ports:
|
||||||
|
- 3306:3306
|
||||||
|
labels:
|
||||||
|
%s/dependencies: |
|
||||||
|
- name: mariadb
|
||||||
|
repository: oci://registry-1.docker.io/bitnamicharts
|
||||||
|
version: 18.x.X
|
||||||
|
|
||||||
|
`
|
||||||
|
compose_file = fmt.Sprintf(compose_file, Prefix())
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ensure that there is no mariasb deployment
|
||||||
|
_, err := helmTemplate(ConvertOptions{
|
||||||
|
OutputDir: "./chart",
|
||||||
|
}, "-s", "templates/mariadb/deployment.yaml")
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Expected error, got nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
// check that Chart.yaml has the dependency
|
||||||
|
chart := HelmChart{}
|
||||||
|
chartFile := "./chart/Chart.yaml"
|
||||||
|
if _, err := os.Stat(chartFile); os.IsNotExist(err) {
|
||||||
|
t.Errorf("Chart.yaml does not exist")
|
||||||
|
}
|
||||||
|
chartContent, err := os.ReadFile(chartFile)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error reading Chart.yaml: %s", err)
|
||||||
|
}
|
||||||
|
if err := yaml.Unmarshal(chartContent, &chart); err != nil {
|
||||||
|
t.Errorf(unmarshalError, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(chart.Dependencies) != 1 {
|
||||||
|
t.Errorf("Expected 1 dependency, got %d", len(chart.Dependencies))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLivenessProbesFromHealthCheck(t *testing.T) {
|
||||||
|
compose_file := `
|
||||||
|
services:
|
||||||
|
web:
|
||||||
|
image: nginx:1.29
|
||||||
|
ports:
|
||||||
|
- 80:80
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "curl", "-f", "http://localhost"]
|
||||||
|
interval: 5s
|
||||||
|
timeout: 3s
|
||||||
|
retries: 3
|
||||||
|
`
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
if dt.Spec.Template.Spec.Containers[0].LivenessProbe == nil {
|
||||||
|
t.Errorf("Expected liveness probe to be set")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProbesFromLabels(t *testing.T) {
|
||||||
|
compose_file := `
|
||||||
|
services:
|
||||||
|
web:
|
||||||
|
image: nginx:1.29
|
||||||
|
ports:
|
||||||
|
- 80:80
|
||||||
|
labels:
|
||||||
|
%s/health-check: |
|
||||||
|
livenessProbe:
|
||||||
|
httpGet:
|
||||||
|
path: /healthz
|
||||||
|
port: 80
|
||||||
|
readinessProbe:
|
||||||
|
httpGet:
|
||||||
|
path: /ready
|
||||||
|
port: 80
|
||||||
|
`
|
||||||
|
compose_file = fmt.Sprintf(compose_file, Prefix())
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
if dt.Spec.Template.Spec.Containers[0].LivenessProbe == nil {
|
||||||
|
t.Errorf("Expected liveness probe to be set")
|
||||||
|
}
|
||||||
|
if dt.Spec.Template.Spec.Containers[0].ReadinessProbe == nil {
|
||||||
|
t.Errorf("Expected readiness probe to be set")
|
||||||
|
}
|
||||||
|
t.Logf("LivenessProbe: %+v", dt.Spec.Template.Spec.Containers[0].LivenessProbe)
|
||||||
|
|
||||||
|
// ensure that the liveness probe is set to /healthz
|
||||||
|
if dt.Spec.Template.Spec.Containers[0].LivenessProbe.HTTPGet.Path != "/healthz" {
|
||||||
|
t.Errorf("Expected liveness probe path to be /healthz, got %s", dt.Spec.Template.Spec.Containers[0].LivenessProbe.HTTPGet.Path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ensure that the readiness probe is set to /ready
|
||||||
|
if dt.Spec.Template.Spec.Containers[0].ReadinessProbe.HTTPGet.Path != "/ready" {
|
||||||
|
t.Errorf("Expected readiness probe path to be /ready, got %s", dt.Spec.Template.Spec.Containers[0].ReadinessProbe.HTTPGet.Path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSetValues(t *testing.T) {
|
||||||
|
compose_file := `
|
||||||
|
services:
|
||||||
|
web:
|
||||||
|
image: nginx:1.29
|
||||||
|
environment:
|
||||||
|
FOO: bar
|
||||||
|
BAZ: qux
|
||||||
|
labels:
|
||||||
|
%s/values: |
|
||||||
|
- FOO
|
||||||
|
`
|
||||||
|
|
||||||
|
compose_file = fmt.Sprintf(compose_file, Prefix())
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// readh the values.yaml, we must have FOO in web environment but not BAZ
|
||||||
|
valuesFile := "./chart/values.yaml"
|
||||||
|
if _, err := os.Stat(valuesFile); os.IsNotExist(err) {
|
||||||
|
t.Errorf("values.yaml does not exist")
|
||||||
|
}
|
||||||
|
valuesContent, err := os.ReadFile(valuesFile)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error reading values.yaml: %s", err)
|
||||||
|
}
|
||||||
|
mapping := struct {
|
||||||
|
Web struct {
|
||||||
|
Environment map[string]string `yaml:"environment"`
|
||||||
|
} `yaml:"web"`
|
||||||
|
}{}
|
||||||
|
if err := yaml.Unmarshal(valuesContent, &mapping); err != nil {
|
||||||
|
t.Errorf(unmarshalError, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := mapping.Web.Environment["FOO"]; !ok {
|
||||||
|
t.Errorf("Expected FOO in web environment")
|
||||||
|
}
|
||||||
|
if _, ok := mapping.Web.Environment["BAZ"]; ok {
|
||||||
|
t.Errorf("Expected BAZ not in web environment")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user