Skip to content

Commit d98268f

Browse files
committed
Add more tests on probes and dependencies
1 parent ccfebd1 commit d98268f

File tree

1 file changed

+196
-0
lines changed

1 file changed

+196
-0
lines changed

generator/deployment_test.go

+196
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package generator
22

33
import (
4+
"fmt"
45
"os"
56
"testing"
67

@@ -135,3 +136,198 @@ services:
135136
t.Errorf("Expected 1 init container, got %d", len(dt.Spec.Template.Spec.InitContainers))
136137
}
137138
}
139+
140+
func TestHelmDependencies(t *testing.T) {
141+
compose_file := `
142+
services:
143+
web:
144+
image: nginx:1.29
145+
ports:
146+
- 80:80
147+
148+
mariadb:
149+
image: mariadb:10.5
150+
ports:
151+
- 3306:3306
152+
labels:
153+
%s/dependencies: |
154+
- name: mariadb
155+
repository: oci://registry-1.docker.io/bitnamicharts
156+
version: 18.x.X
157+
158+
`
159+
compose_file = fmt.Sprintf(compose_file, Prefix())
160+
tmpDir := setup(compose_file)
161+
defer teardown(tmpDir)
162+
163+
currentDir, _ := os.Getwd()
164+
os.Chdir(tmpDir)
165+
defer os.Chdir(currentDir)
166+
167+
output := _compile_test(t, "-s", "templates/web/deployment.yaml")
168+
dt := v1.Deployment{}
169+
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
170+
t.Errorf(unmarshalError, err)
171+
}
172+
173+
// ensure that there is no mariasb deployment
174+
_, err := helmTemplate(ConvertOptions{
175+
OutputDir: "./chart",
176+
}, "-s", "templates/mariadb/deployment.yaml")
177+
if err == nil {
178+
t.Errorf("Expected error, got nil")
179+
}
180+
181+
// check that Chart.yaml has the dependency
182+
chart := HelmChart{}
183+
chartFile := "./chart/Chart.yaml"
184+
if _, err := os.Stat(chartFile); os.IsNotExist(err) {
185+
t.Errorf("Chart.yaml does not exist")
186+
}
187+
chartContent, err := os.ReadFile(chartFile)
188+
if err != nil {
189+
t.Errorf("Error reading Chart.yaml: %s", err)
190+
}
191+
if err := yaml.Unmarshal(chartContent, &chart); err != nil {
192+
t.Errorf(unmarshalError, err)
193+
}
194+
195+
if len(chart.Dependencies) != 1 {
196+
t.Errorf("Expected 1 dependency, got %d", len(chart.Dependencies))
197+
}
198+
}
199+
200+
func TestLivenessProbesFromHealthCheck(t *testing.T) {
201+
compose_file := `
202+
services:
203+
web:
204+
image: nginx:1.29
205+
ports:
206+
- 80:80
207+
healthcheck:
208+
test: ["CMD", "curl", "-f", "http://localhost"]
209+
interval: 5s
210+
timeout: 3s
211+
retries: 3
212+
`
213+
tmpDir := setup(compose_file)
214+
defer teardown(tmpDir)
215+
216+
currentDir, _ := os.Getwd()
217+
os.Chdir(tmpDir)
218+
defer os.Chdir(currentDir)
219+
220+
output := _compile_test(t, "-s", "templates/web/deployment.yaml")
221+
dt := v1.Deployment{}
222+
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
223+
t.Errorf(unmarshalError, err)
224+
}
225+
226+
if dt.Spec.Template.Spec.Containers[0].LivenessProbe == nil {
227+
t.Errorf("Expected liveness probe to be set")
228+
}
229+
}
230+
231+
func TestProbesFromLabels(t *testing.T) {
232+
compose_file := `
233+
services:
234+
web:
235+
image: nginx:1.29
236+
ports:
237+
- 80:80
238+
labels:
239+
%s/health-check: |
240+
livenessProbe:
241+
httpGet:
242+
path: /healthz
243+
port: 80
244+
readinessProbe:
245+
httpGet:
246+
path: /ready
247+
port: 80
248+
`
249+
compose_file = fmt.Sprintf(compose_file, Prefix())
250+
tmpDir := setup(compose_file)
251+
defer teardown(tmpDir)
252+
253+
currentDir, _ := os.Getwd()
254+
os.Chdir(tmpDir)
255+
defer os.Chdir(currentDir)
256+
257+
output := _compile_test(t, "-s", "templates/web/deployment.yaml")
258+
dt := v1.Deployment{}
259+
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
260+
t.Errorf(unmarshalError, err)
261+
}
262+
263+
if dt.Spec.Template.Spec.Containers[0].LivenessProbe == nil {
264+
t.Errorf("Expected liveness probe to be set")
265+
}
266+
if dt.Spec.Template.Spec.Containers[0].ReadinessProbe == nil {
267+
t.Errorf("Expected readiness probe to be set")
268+
}
269+
t.Logf("LivenessProbe: %+v", dt.Spec.Template.Spec.Containers[0].LivenessProbe)
270+
271+
// ensure that the liveness probe is set to /healthz
272+
if dt.Spec.Template.Spec.Containers[0].LivenessProbe.HTTPGet.Path != "/healthz" {
273+
t.Errorf("Expected liveness probe path to be /healthz, got %s", dt.Spec.Template.Spec.Containers[0].LivenessProbe.HTTPGet.Path)
274+
}
275+
276+
// ensure that the readiness probe is set to /ready
277+
if dt.Spec.Template.Spec.Containers[0].ReadinessProbe.HTTPGet.Path != "/ready" {
278+
t.Errorf("Expected readiness probe path to be /ready, got %s", dt.Spec.Template.Spec.Containers[0].ReadinessProbe.HTTPGet.Path)
279+
}
280+
}
281+
282+
func TestSetValues(t *testing.T) {
283+
compose_file := `
284+
services:
285+
web:
286+
image: nginx:1.29
287+
environment:
288+
FOO: bar
289+
BAZ: qux
290+
labels:
291+
%s/values: |
292+
- FOO
293+
`
294+
295+
compose_file = fmt.Sprintf(compose_file, Prefix())
296+
tmpDir := setup(compose_file)
297+
defer teardown(tmpDir)
298+
299+
currentDir, _ := os.Getwd()
300+
os.Chdir(tmpDir)
301+
defer os.Chdir(currentDir)
302+
303+
output := _compile_test(t, "-s", "templates/web/deployment.yaml")
304+
dt := v1.Deployment{}
305+
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
306+
t.Errorf(unmarshalError, err)
307+
}
308+
309+
// readh the values.yaml, we must have FOO in web environment but not BAZ
310+
valuesFile := "./chart/values.yaml"
311+
if _, err := os.Stat(valuesFile); os.IsNotExist(err) {
312+
t.Errorf("values.yaml does not exist")
313+
}
314+
valuesContent, err := os.ReadFile(valuesFile)
315+
if err != nil {
316+
t.Errorf("Error reading values.yaml: %s", err)
317+
}
318+
mapping := struct {
319+
Web struct {
320+
Environment map[string]string `yaml:"environment"`
321+
} `yaml:"web"`
322+
}{}
323+
if err := yaml.Unmarshal(valuesContent, &mapping); err != nil {
324+
t.Errorf(unmarshalError, err)
325+
}
326+
327+
if _, ok := mapping.Web.Environment["FOO"]; !ok {
328+
t.Errorf("Expected FOO in web environment")
329+
}
330+
if _, ok := mapping.Web.Environment["BAZ"]; ok {
331+
t.Errorf("Expected BAZ not in web environment")
332+
}
333+
}

0 commit comments

Comments
 (0)