From eb760d429983b0dcf517af7a101034b4c7b00296 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Tue, 3 Dec 2024 14:03:36 +0100 Subject: [PATCH] test(subdir): Add globally mount binary files --- generator/volume_test.go | 67 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/generator/volume_test.go b/generator/volume_test.go index 700fa6d..9c9a97a 100644 --- a/generator/volume_test.go +++ b/generator/volume_test.go @@ -221,6 +221,73 @@ services: } } +func TestGloballyBinaryMount(t *testing.T) { + composeFile := ` +services: + web: + image: nginx + volumes: + - ./images:/var/www/foo + labels: + %[1]s/configmap-files: |- + - ./images +` + composeFile = fmt.Sprintf(composeFile, labels.KatenaryLabelPrefix) + tmpDir := setup(composeFile) + log.Println(tmpDir) + defer teardown(tmpDir) + + os.Mkdir(filepath.Join(tmpDir, "images"), 0o755) + + // create a png image + pngFile := tmpDir + "/images/foo.png" + w, h := 100, 100 + img := image.NewRGBA(image.Rect(0, 0, w, h)) + red := color.RGBA{255, 0, 0, 255} + for y := 0; y < h; y++ { + for x := 0; x < w; x++ { + img.Set(x, y, red) + } + } + + blue := color.RGBA{0, 0, 255, 255} + for y := 30; y < 70; y++ { + for x := 30; x < 70; x++ { + img.Set(x, y, blue) + } + } + currentDir, _ := os.Getwd() + os.Chdir(tmpDir) + defer os.Chdir(currentDir) + + f, err := os.Create(pngFile) + if err != nil { + t.Fatal(err) + } + png.Encode(f, img) + f.Close() + output := internalCompileTest(t, "-s", "templates/web/deployment.yaml") + d := v1.Deployment{} + yaml.Unmarshal([]byte(output), &d) + volumes := d.Spec.Template.Spec.Volumes + if len(volumes) != 1 { + t.Errorf("Expected 1 volume, got %d", len(volumes)) + } + + cm := corev1.ConfigMap{} + cmContent, err := helmTemplate(ConvertOptions{ + OutputDir: "chart", + }, "-s", "templates/web/statics/images/configmap.yaml") + yaml.Unmarshal([]byte(cmContent), &cm) + if im, ok := cm.BinaryData["foo.png"]; !ok { + t.Errorf("Expected foo.png to be in the configmap") + } else { + if len(im) == 0 { + t.Errorf("Expected image to be non-empty") + } + } +} + func TestBindFrom(t *testing.T) { composeFile := ` services: