| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "gitlab.com/Timfa/bluegit/internal/images" |
| 11 | ) |
| 12 | |
| 13 | func TestImageLogPresetAndPlain(t *testing.T) { |
| 14 | lines := imageLog("bluegit/node", "node:22-alpine", "bluegit/node:abc", "sha256:id", nil, []string{"docker.io/library/node@sha256:dead"}) |
| 15 | joined := strings.Join(lines, "\n") |
| 16 | for _, want := range []string{ |
| 17 | "Using image preset bluegit/node based on node:22-alpine", |
| 18 | "Using docker image bluegit/node:abc ...", |
| 19 | "Underlying image node:22-alpine with digest docker.io/library/node@sha256:dead ...", |
| 20 | } { |
| 21 | if !strings.Contains(joined, want) { |
| 22 | t.Fatalf("mist %q in %s", want, joined) |
| 23 | } |
| 24 | } |
| 25 | plain := imageLog("", "", "node:22", "", nil, nil) |
| 26 | if len(plain) != 1 || plain[0] != "Using docker image node:22 ..." || strings.Contains(plain[0], "preset") { |
| 27 | t.Fatalf("gewoon image: %q", plain) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func TestResolveImageBuildsPresetAndLeavesPlainImages(t *testing.T) { |
| 32 | dir := t.TempDir() |
| 33 | logPath := filepath.Join(dir, "log") |
| 34 | builtPath := filepath.Join(dir, "built") |
| 35 | script := filepath.Join(dir, "docker") |
| 36 | body := `#!/bin/sh |
| 37 | echo "$@" >> "` + logPath + `" |
| 38 | tag="" |
| 39 | for a in "$@"; do tag="$a"; done |
| 40 | if [ "$1" = "image" ]; then |
| 41 | case "$tag" in |
| 42 | bluegit/*) |
| 43 | if grep -qx "$tag" "` + builtPath + `" 2>/dev/null; then |
| 44 | printf '%s\n' "sha256:built" |
| 45 | printf '%s\n' "$tag@sha256:builtdigest" |
| 46 | exit 0 |
| 47 | fi |
| 48 | exit 1 |
| 49 | ;; |
| 50 | esac |
| 51 | printf '%s\n' "sha256:base" |
| 52 | printf '%s\n' "docker.io/library/base@sha256:basedigest" |
| 53 | exit 0 |
| 54 | fi |
| 55 | if [ "$1" = "build" ]; then |
| 56 | prev="" |
| 57 | for a in "$@"; do |
| 58 | if [ "$prev" = "-t" ]; then printf '%s\n' "$a" >> "` + builtPath + `"; fi |
| 59 | prev="$a" |
| 60 | done |
| 61 | exit 0 |
| 62 | fi |
| 63 | exit 1 |
| 64 | ` |
| 65 | if err := os.WriteFile(script, []byte(body), 0o755); err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | r := &Runner{Docker: script} |
| 69 | ref, lines, err := r.resolveImage(context.Background(), "bluegit/node") |
| 70 | if err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | if !strings.HasPrefix(ref, "bluegit/node:") { |
| 74 | t.Fatalf("ref %s", ref) |
| 75 | } |
| 76 | joined := strings.Join(lines, "\n") |
| 77 | for _, want := range []string{ |
| 78 | "Building image preset bluegit/node ...", |
| 79 | "Using image preset bluegit/node based on node:22-alpine", |
| 80 | "Using docker image " + ref, |
| 81 | "Underlying image node:22-alpine with digest docker.io/library/base@sha256:basedigest ...", |
| 82 | } { |
| 83 | if !strings.Contains(joined, want) { |
| 84 | t.Fatalf("mist %q in\n%s", want, joined) |
| 85 | } |
| 86 | } |
| 87 | if _, lines, err = r.resolveImage(context.Background(), "bluegit/node"); err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | if strings.Contains(strings.Join(lines, "\n"), "Building image preset") { |
| 91 | t.Fatal("cache bouwt opnieuw") |
| 92 | } |
| 93 | plain, plainLines, err := r.resolveImage(context.Background(), "node:22") |
| 94 | if err != nil || plain != "node:22" { |
| 95 | t.Fatalf("plain %s %v", plain, err) |
| 96 | } |
| 97 | if strings.Contains(strings.Join(plainLines, "\n"), "preset") { |
| 98 | t.Fatalf("node:22 werd een preset: %s", plainLines) |
| 99 | } |
| 100 | log, err := os.ReadFile(logPath) |
| 101 | if err != nil { |
| 102 | t.Fatal(err) |
| 103 | } |
| 104 | if strings.Count(string(log), "\nbuild ")+strings.Count(string(log), "build -t") == 0 && !strings.Contains(string(log), "build ") { |
| 105 | t.Fatalf("geen build:\n%s", log) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func TestDirectPresetDoesNotBuild(t *testing.T) { |
| 110 | dir := t.TempDir() |
| 111 | script := filepath.Join(dir, "docker") |
| 112 | if err := os.WriteFile(script, []byte("#!/bin/sh\nexit 1\n"), 0o755); err != nil { |
| 113 | t.Fatal(err) |
| 114 | } |
| 115 | r := &Runner{Docker: script, Presets: map[string]images.Override{ |
| 116 | "team/plain": {Image: "alpine:3.20"}, |
| 117 | }} |
| 118 | // Docker ontbreekt niet: het script bestaat. inspect faalt, dat is geen fout. |
| 119 | // Een direct gemapte preset roept Ensure aan, dat geen build doet, en daarna inspect. |
| 120 | // inspect faalt stil. De regels noemen wel de preset. |
| 121 | ref, lines, err := r.resolveImage(context.Background(), "team/plain") |
| 122 | if err != nil { |
| 123 | t.Fatal(err) |
| 124 | } |
| 125 | if ref != "alpine:3.20" { |
| 126 | t.Fatalf("ref %s", ref) |
| 127 | } |
| 128 | joined := strings.Join(lines, "\n") |
| 129 | if !strings.Contains(joined, "Using image preset team/plain based on alpine:3.20") { |
| 130 | t.Fatalf("%s", joined) |
| 131 | } |
| 132 | if strings.Contains(joined, "Building image preset") { |
| 133 | t.Fatal("een mapping wordt gebouwd") |
| 134 | } |
| 135 | } |