| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "net/http/cookiejar" |
| 7 | "net/http/httptest" |
| 8 | "net/url" |
| 9 | "os/exec" |
| 10 | "path/filepath" |
| 11 | "regexp" |
| 12 | "strconv" |
| 13 | "strings" |
| 14 | "testing" |
| 15 | |
| 16 | "gitlab.com/Timfa/bluegit/internal/auth" |
| 17 | "gitlab.com/Timfa/bluegit/internal/config" |
| 18 | "gitlab.com/Timfa/bluegit/internal/gitrepo" |
| 19 | "gitlab.com/Timfa/bluegit/internal/store" |
| 20 | ) |
| 21 | |
| 22 | func TestOverviewPipelinePollsWhileRunning(t *testing.T) { |
| 23 | if _, err := exec.LookPath("git"); err != nil { |
| 24 | t.Skip("git ontbreekt") |
| 25 | } |
| 26 | storage := t.TempDir() |
| 27 | st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32()) |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | t.Cleanup(func() { _ = st.Close() }) |
| 32 | cfg := config.Config{ |
| 33 | BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage, |
| 34 | Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"}, |
| 35 | Git: config.Git{Binary: "git", DefaultBranch: "master"}, |
| 36 | } |
| 37 | srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"} |
| 38 | ts := httptest.NewServer(srv.Handler()) |
| 39 | t.Cleanup(ts.Close) |
| 40 | jar, _ := cookiejar.New(nil) |
| 41 | tim := &http.Client{Jar: jar} |
| 42 | page := read(t, mustGet(t, tim, ts.URL+"/login")) |
| 43 | if _, err := tim.PostForm(ts.URL+"/new/repo", url.Values{ |
| 44 | "csrf": {csrfOf(t, page)}, "name": {"livepipe"}, "visibility": {"public"}, |
| 45 | }); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | ctx := context.Background() |
| 49 | repo, err := st.RepoByPath(ctx, "tim", "livepipe") |
| 50 | if err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | dir := gitrepo.Dir(storage, "tim", "livepipe") |
| 54 | work := t.TempDir() |
| 55 | git(t, work, "init", "-b", "master") |
| 56 | git(t, work, "config", "user.email", "tim@users.git.timfalken.com") |
| 57 | git(t, work, "config", "user.name", "tim") |
| 58 | writeFile(t, filepath.Join(work, "README.md"), "hallo\n") |
| 59 | git(t, work, "add", "README.md") |
| 60 | git(t, work, "commit", "-m", "leesmij") |
| 61 | git(t, work, "remote", "add", "origin", dir) |
| 62 | git(t, work, "push", "origin", "master") |
| 63 | sha := strings.TrimSpace(mustGit(t, dir, "rev-parse", "refs/heads/master")) |
| 64 | |
| 65 | id := makePipe(t, st, repo.ID, sha, "wachten", []store.NewJob{ |
| 66 | {Name: "unit", Stage: "test", Script: "exit 0"}, |
| 67 | {Name: "ship", Stage: "deploy", Script: "exit 0"}, |
| 68 | }) |
| 69 | home := read(t, mustGet(t, tim, ts.URL+"/tim/livepipe")) |
| 70 | live := `data-live="/tim/livepipe/-/pipelines/` + strconv.FormatInt(id, 10) + `" data-live-on="1"` |
| 71 | for _, want := range []string{ |
| 72 | live, `data-head-pipe`, `data-pipe-status`, `data-pipe-label`, `data-pipe-duration`, |
| 73 | `data-stage="test"`, `data-stage="deploy"`, `class="stage-dot pending"`, |
| 74 | `class="status pending"`, "Pending", `<script src="/assets/pipeline.js"></script>`, |
| 75 | } { |
| 76 | if !strings.Contains(home, want) { |
| 77 | t.Errorf("lopend overzicht mist %q", want) |
| 78 | } |
| 79 | } |
| 80 | if !regexp.MustCompile(`data-pipe-duration>\d{2}:\d{2}:\d{2}<`).MatchString(home) { |
| 81 | t.Fatalf("duur ontbreekt:\n%s", home) |
| 82 | } |
| 83 | file := read(t, mustGet(t, tim, ts.URL+"/tim/livepipe/-/blob/README.md?rev=master")) |
| 84 | if strings.Contains(file, `data-live-on="1"`) || strings.Contains(file, "/assets/pipeline.js") { |
| 85 | t.Fatal("bestandspagina pollt de pipeline mee") |
| 86 | } |
| 87 | if !strings.Contains(file, `class="status pending"`) { |
| 88 | t.Fatal("bestandspagina mist de statische status") |
| 89 | } |
| 90 | body := overviewJSON(t, tim, ts.URL+"/tim/livepipe/-/pipelines/"+strconv.FormatInt(id, 10)) |
| 91 | for _, want := range []string{`"active":true`, `"status":"pending"`, `"duration":"`, `"name":"test"`, `"name":"deploy"`, "stage-dot"} { |
| 92 | if !strings.Contains(body, want) { |
| 93 | t.Errorf("detail-json mist %q:\n%s", want, body) |
| 94 | } |
| 95 | } |
| 96 | if strings.Contains(body, `"rows"`) { |
| 97 | t.Fatalf("detail-json lijkt op de lijst:\n%s", body) |
| 98 | } |
| 99 | js := read(t, mustGet(t, tim, ts.URL+"/assets/pipeline.js")) |
| 100 | if !strings.Contains(js, "data-head-pipe") || !strings.Contains(js, "3000") { |
| 101 | t.Fatal("pipeline.js mist de overzichtspoll") |
| 102 | } |
| 103 | |
| 104 | finishPipe(t, st, id, "success") |
| 105 | home = read(t, mustGet(t, tim, ts.URL+"/tim/livepipe")) |
| 106 | if strings.Contains(home, `data-live-on="1"`) || strings.Contains(home, "/assets/pipeline.js") { |
| 107 | t.Fatal("geslaagde pipeline blijft pollen") |
| 108 | } |
| 109 | for _, want := range []string{`class="status passed"`, `class="stage-dot passed"`, `data-pipe-duration`, "Passed"} { |
| 110 | if !strings.Contains(home, want) { |
| 111 | t.Errorf("geslaagd overzicht mist %q", want) |
| 112 | } |
| 113 | } |
| 114 | body = overviewJSON(t, tim, ts.URL+"/tim/livepipe/-/pipelines/"+strconv.FormatInt(id, 10)) |
| 115 | if !strings.Contains(body, `"active":false`) || !strings.Contains(body, `"status":"passed"`) || !strings.Contains(body, `"duration":"`) { |
| 116 | t.Fatalf("stille json:\n%s", body) |
| 117 | } |
| 118 | |
| 119 | failed := makePipe(t, st, repo.ID, sha, "kapot", []store.NewJob{{Name: "unit", Stage: "test", Script: "exit 1"}}) |
| 120 | finishPipe(t, st, failed, "failed") |
| 121 | home = read(t, mustGet(t, tim, ts.URL+"/tim/livepipe")) |
| 122 | if strings.Contains(home, `data-live-on="1"`) || !strings.Contains(home, `class="status failed"`) { |
| 123 | t.Fatalf("mislukt overzicht:\n%s", home) |
| 124 | } |
| 125 | |
| 126 | canceled := makePipe(t, st, repo.ID, sha, "stop", []store.NewJob{{Name: "unit", Stage: "test", Script: "exit 0"}}) |
| 127 | finishPipe(t, st, canceled, "canceled") |
| 128 | home = read(t, mustGet(t, tim, ts.URL+"/tim/livepipe")) |
| 129 | if strings.Contains(home, `data-live-on="1"`) || !strings.Contains(home, `class="status canceled"`) { |
| 130 | t.Fatalf("geannuleerd overzicht:\n%s", home) |
| 131 | } |
| 132 | |
| 133 | running := makePipe(t, st, repo.ID, sha, "bezig", []store.NewJob{ |
| 134 | {Name: "unit", Stage: "test", Script: "exit 0"}, |
| 135 | {Name: "ship", Stage: "deploy", Script: "exit 0"}, |
| 136 | }) |
| 137 | jobs, err := st.JobsByPipeline(ctx, running) |
| 138 | if err != nil { |
| 139 | t.Fatal(err) |
| 140 | } |
| 141 | if ok, err := st.MarkJobRunning(ctx, jobs[0].ID); err != nil || !ok { |
| 142 | t.Fatalf("running: %v %v", ok, err) |
| 143 | } |
| 144 | home = read(t, mustGet(t, tim, ts.URL+"/tim/livepipe")) |
| 145 | if !strings.Contains(home, `data-live-on="1"`) || !strings.Contains(home, `class="status running"`) || !strings.Contains(home, `class="stage-dot running"`) { |
| 146 | t.Fatalf("lopend overzicht:\n%s", home) |
| 147 | } |
| 148 | if !strings.Contains(home, `/tim/livepipe/-/pipelines/`+strconv.FormatInt(running, 10)) { |
| 149 | t.Fatal("poll wijst niet naar de lopende pipeline") |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | func makePipe(t *testing.T, st *store.Store, repoID int64, sha, title string, jobs []store.NewJob) int64 { |
| 154 | t.Helper() |
| 155 | id, err := st.CreatePipeline(context.Background(), repoID, sha, "refs/heads/master", "push", "tim", title, jobs) |
| 156 | if err != nil { |
| 157 | t.Fatal(err) |
| 158 | } |
| 159 | return id |
| 160 | } |
| 161 | |
| 162 | func finishPipe(t *testing.T, st *store.Store, id int64, status string) { |
| 163 | t.Helper() |
| 164 | ctx := context.Background() |
| 165 | jobs, err := st.JobsByPipeline(ctx, id) |
| 166 | if err != nil { |
| 167 | t.Fatal(err) |
| 168 | } |
| 169 | for _, j := range jobs { |
| 170 | if err := st.FinishJob(ctx, j.ID, status, ""); err != nil { |
| 171 | t.Fatal(err) |
| 172 | } |
| 173 | } |
| 174 | if _, err := st.RefreshPipelineStatus(ctx, id); err != nil { |
| 175 | t.Fatal(err) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | func overviewJSON(t *testing.T, c *http.Client, raw string) string { |
| 180 | t.Helper() |
| 181 | req, err := http.NewRequest(http.MethodGet, raw, nil) |
| 182 | if err != nil { |
| 183 | t.Fatal(err) |
| 184 | } |
| 185 | req.Header.Set("Accept", "application/json") |
| 186 | res, err := c.Do(req) |
| 187 | if err != nil { |
| 188 | t.Fatal(err) |
| 189 | } |
| 190 | return read(t, res) |
| 191 | } |