| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "os/exec" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "gitlab.com/Timfa/bluegit/internal/store" |
| 9 | ) |
| 10 | |
| 11 | func TestShellScriptNotesStayInEnvironmentSection(t *testing.T) { |
| 12 | job := store.Job{Before: "echo before\n", Script: "echo run\n", After: "echo after\n"} |
| 13 | note := placeNote{ |
| 14 | Runner: "O'Hara $(touch /tmp/pwn)", Host: "build.example", |
| 15 | WorkDir: "/workspace", PipelineID: 12, JobID: 34, |
| 16 | } |
| 17 | script := shellScript(job, note) |
| 18 | start := strings.Index(script, "section_start:before:Preparing environment") |
| 19 | end := strings.Index(script, "section_end:before:Preparing environment") |
| 20 | if start < 0 || end < start { |
| 21 | t.Fatalf("sectie: %s", script) |
| 22 | } |
| 23 | body := script[start:end] |
| 24 | for _, want := range []string{ |
| 25 | "echo " + shSingle("Running on O'Hara $(touch /tmp/pwn) via build.example..."), |
| 26 | "Working directory: /workspace", |
| 27 | "Pipeline #12, job #34", |
| 28 | } { |
| 29 | if !strings.Contains(body, want) { |
| 30 | t.Fatalf("ontbreekt %q in %s", want, body) |
| 31 | } |
| 32 | } |
| 33 | if strings.Index(body, "Pipeline #12") > strings.Index(body, "echo before") { |
| 34 | t.Fatal("info staat na before_script") |
| 35 | } |
| 36 | cmd := exec.Command("sh", "-n") |
| 37 | cmd.Stdin = strings.NewReader(script) |
| 38 | if out, err := cmd.CombinedOutput(); err != nil { |
| 39 | t.Fatalf("sh -n: %v %s\n%s", err, out, script) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func TestPlaceNoteDefaults(t *testing.T) { |
| 44 | lines := (placeNote{PipelineID: 7, JobID: 8}).lines() |
| 45 | if len(lines) != 3 || lines[0] != "Running on Steve via localhost..." || lines[1] != "Working directory: /workspace" || lines[2] != "Pipeline #7, job #8" { |
| 46 | t.Fatalf("%q", lines) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func TestDockerImageLines(t *testing.T) { |
| 51 | only := dockerImageLines("alpine:latest", "", nil) |
| 52 | if len(only) != 1 || only[0] != "Using docker image alpine:latest ..." { |
| 53 | t.Fatalf("zonder lokaal image: %q", only) |
| 54 | } |
| 55 | idOnly := dockerImageLines("alpine:latest", "sha256:abc", nil) |
| 56 | if len(idOnly) != 2 || idOnly[1] != "Using docker image sha256:abc for alpine:latest ..." { |
| 57 | t.Fatalf("alleen id: %q", idOnly) |
| 58 | } |
| 59 | full := dockerImageLines("alpine:latest", "sha256:abc", []string{ |
| 60 | "other@sha256:nope", |
| 61 | "docker.io/library/alpine@sha256:def", |
| 62 | }) |
| 63 | want := "Using docker image sha256:abc for alpine:latest with digest docker.io/library/alpine@sha256:def ..." |
| 64 | if len(full) != 2 || full[0] != "Using docker image alpine:latest ..." || full[1] != want { |
| 65 | t.Fatalf("digest: %q", full) |
| 66 | } |
| 67 | reg := dockerImageLines("localhost:5000/team/app:1", "sha256:fff", []string{ |
| 68 | "localhost:5000/team/app@sha256:999", |
| 69 | }) |
| 70 | if !strings.Contains(reg[1], "digest localhost:5000/team/app@sha256:999") { |
| 71 | t.Fatalf("registry: %q", reg) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func TestParseImageInspect(t *testing.T) { |
| 76 | id, digests := parseImageInspect("sha256:abc\nalpine@sha256:def\n\n") |
| 77 | if id != "sha256:abc" || len(digests) != 1 || digests[0] != "alpine@sha256:def" { |
| 78 | t.Fatalf("%s %q", id, digests) |
| 79 | } |
| 80 | } |