| 1 | package scripts |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "os" |
| 10 | "os/exec" |
| 11 | "path/filepath" |
| 12 | "strings" |
| 13 | "sync/atomic" |
| 14 | "testing" |
| 15 | ) |
| 16 | |
| 17 | // fakeBlueGit geeft eerst geen pipeline, dan running, dan final. |
| 18 | func fakeBlueGit(t *testing.T, final string, token string) (*httptest.Server, *atomic.Int32) { |
| 19 | var calls atomic.Int32 |
| 20 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 21 | if token != "" && r.Header.Get("Authorization") != "Bearer "+token { |
| 22 | http.Error(w, `{"message":"no"}`, 401) |
| 23 | return |
| 24 | } |
| 25 | switch { |
| 26 | case r.URL.Path == "/api/v1/projects/tim/app/pipelines": |
| 27 | if r.URL.Query().Get("sha") != "abcdef1" { |
| 28 | t.Errorf("sha-filter ontbreekt: %s", r.URL.RawQuery) |
| 29 | } |
| 30 | n := calls.Add(1) |
| 31 | switch { |
| 32 | case n == 1: |
| 33 | fmt.Fprint(w, `[]`) |
| 34 | case n == 2: |
| 35 | fmt.Fprint(w, `[{"id":9,"status":"running","ref":"refs/heads/x","sha":"abcdef1000","web_url":"u"}]`) |
| 36 | default: |
| 37 | fmt.Fprintf(w, `[{"id":9,"status":%q,"ref":"refs/heads/x","sha":"abcdef1000","web_url":"u"}]`, final) |
| 38 | } |
| 39 | case r.URL.Path == "/api/v1/projects/tim/app/pipelines/9/jobs": |
| 40 | fmt.Fprint(w, `[{"id":1,"name":"test","stage":"test","status":"failed","allow_failure":false},{"id":2,"name":"aur","stage":"release","status":"failed","allow_failure":true}]`) |
| 41 | case r.URL.Path == "/api/v1/projects/tim/app/pipelines/9/jobs/1/log": |
| 42 | fmt.Fprint(w, "regel 1\nFOUT hier\n") |
| 43 | default: |
| 44 | http.NotFound(w, r) |
| 45 | } |
| 46 | })) |
| 47 | t.Cleanup(srv.Close) |
| 48 | return srv, &calls |
| 49 | } |
| 50 | |
| 51 | func runBG(t *testing.T, url, token string, args ...string) (int, string, string) { |
| 52 | t.Helper() |
| 53 | for _, bin := range []string{"bash", "curl", "jq"} { |
| 54 | if _, err := exec.LookPath(bin); err != nil { |
| 55 | t.Skip(bin + " ontbreekt") |
| 56 | } |
| 57 | } |
| 58 | cmd := exec.Command(filepath.Join("..", "scripts", "bg"), args...) |
| 59 | cmd.Env = append(os.Environ(), "BLUEGIT_URL="+url, "BLUEGIT_TOKEN="+token) |
| 60 | var out, errb bytes.Buffer |
| 61 | cmd.Stdout, cmd.Stderr = &out, &errb |
| 62 | err := cmd.Run() |
| 63 | code := 0 |
| 64 | var ee *exec.ExitError |
| 65 | if errors.As(err, &ee) { |
| 66 | code = ee.ExitCode() |
| 67 | } else if err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | return code, out.String(), errb.String() |
| 71 | } |
| 72 | |
| 73 | func TestBGWaitExitCodes(t *testing.T) { |
| 74 | const tok = "bg_geheimtoken123" |
| 75 | srv, _ := fakeBlueGit(t, "success", tok) |
| 76 | code, out, errs := runBG(t, srv.URL, tok, "pipeline", "wait", "tim/app", "--sha", "abcdef1", "--interval", "0") |
| 77 | if code != 0 || !strings.Contains(out, "pipeline #9 success") { |
| 78 | t.Fatalf("success: %d %s %s", code, out, errs) |
| 79 | } |
| 80 | if strings.Contains(out+errs, tok) { |
| 81 | t.Fatal("token geprint") |
| 82 | } |
| 83 | srv, _ = fakeBlueGit(t, "failed", "") |
| 84 | code, out, errs = runBG(t, srv.URL, "", "pipeline", "wait", "tim/app", "--sha", "abcdef1", "--interval", "0") |
| 85 | if code != 1 || !strings.Contains(errs, "FOUT hier") || !strings.Contains(errs, "waarschuwing: aur") { |
| 86 | t.Fatalf("failed: %d %s %s", code, out, errs) |
| 87 | } |
| 88 | srv, _ = fakeBlueGit(t, "running", "") |
| 89 | code, _, errs = runBG(t, srv.URL, "", "pipeline", "wait", "tim/app", "--sha", "abcdef1", "--interval", "1", "--timeout", "2") |
| 90 | if code != 2 || !strings.Contains(errs, "timeout") { |
| 91 | t.Fatalf("timeout: %d %s", code, errs) |
| 92 | } |
| 93 | code, _, errs = runBG(t, srv.URL, "", "mr", "show", "tim/app", "5") |
| 94 | if code != 1 || !strings.Contains(errs, "404") { |
| 95 | t.Fatalf("404: %d %s", code, errs) |
| 96 | } |
| 97 | } |