package server import ( "context" "net/http" "net/http/cookiejar" "net/http/httptest" "net/url" "os/exec" "path/filepath" "regexp" "strconv" "strings" "testing" "gitlab.com/Timfa/bluegit/internal/auth" "gitlab.com/Timfa/bluegit/internal/config" "gitlab.com/Timfa/bluegit/internal/gitrepo" "gitlab.com/Timfa/bluegit/internal/store" ) func TestOverviewPipelinePollsWhileRunning(t *testing.T) { if _, err := exec.LookPath("git"); err != nil { t.Skip("git ontbreekt") } storage := t.TempDir() st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32()) if err != nil { t.Fatal(err) } t.Cleanup(func() { _ = st.Close() }) cfg := config.Config{ BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage, Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"}, Git: config.Git{Binary: "git", DefaultBranch: "master"}, } srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"} ts := httptest.NewServer(srv.Handler()) t.Cleanup(ts.Close) jar, _ := cookiejar.New(nil) tim := &http.Client{Jar: jar} page := read(t, mustGet(t, tim, ts.URL+"/login")) if _, err := tim.PostForm(ts.URL+"/new/repo", url.Values{ "csrf": {csrfOf(t, page)}, "name": {"livepipe"}, "visibility": {"public"}, }); err != nil { t.Fatal(err) } ctx := context.Background() repo, err := st.RepoByPath(ctx, "tim", "livepipe") if err != nil { t.Fatal(err) } dir := gitrepo.Dir(storage, "tim", "livepipe") work := t.TempDir() git(t, work, "init", "-b", "master") git(t, work, "config", "user.email", "tim@users.git.timfalken.com") git(t, work, "config", "user.name", "tim") writeFile(t, filepath.Join(work, "README.md"), "hallo\n") git(t, work, "add", "README.md") git(t, work, "commit", "-m", "leesmij") git(t, work, "remote", "add", "origin", dir) git(t, work, "push", "origin", "master") sha := strings.TrimSpace(mustGit(t, dir, "rev-parse", "refs/heads/master")) id := makePipe(t, st, repo.ID, sha, "wachten", []store.NewJob{ {Name: "unit", Stage: "test", Script: "exit 0"}, {Name: "ship", Stage: "deploy", Script: "exit 0"}, }) home := read(t, mustGet(t, tim, ts.URL+"/tim/livepipe")) live := `data-live="/tim/livepipe/-/pipelines/` + strconv.FormatInt(id, 10) + `" data-live-on="1"` for _, want := range []string{ live, `data-head-pipe`, `data-pipe-status`, `data-pipe-label`, `data-pipe-duration`, `data-stage="test"`, `data-stage="deploy"`, `class="stage-dot pending"`, `class="status pending"`, "Pending", ``, } { if !strings.Contains(home, want) { t.Errorf("lopend overzicht mist %q", want) } } if !regexp.MustCompile(`data-pipe-duration>\d{2}:\d{2}:\d{2}<`).MatchString(home) { t.Fatalf("duur ontbreekt:\n%s", home) } file := read(t, mustGet(t, tim, ts.URL+"/tim/livepipe/-/blob/README.md?rev=master")) if strings.Contains(file, `data-live-on="1"`) || strings.Contains(file, "/assets/pipeline.js") { t.Fatal("bestandspagina pollt de pipeline mee") } if !strings.Contains(file, `class="status pending"`) { t.Fatal("bestandspagina mist de statische status") } body := overviewJSON(t, tim, ts.URL+"/tim/livepipe/-/pipelines/"+strconv.FormatInt(id, 10)) for _, want := range []string{`"active":true`, `"status":"pending"`, `"duration":"`, `"name":"test"`, `"name":"deploy"`, "stage-dot"} { if !strings.Contains(body, want) { t.Errorf("detail-json mist %q:\n%s", want, body) } } if strings.Contains(body, `"rows"`) { t.Fatalf("detail-json lijkt op de lijst:\n%s", body) } js := read(t, mustGet(t, tim, ts.URL+"/assets/pipeline.js")) if !strings.Contains(js, "data-head-pipe") || !strings.Contains(js, "3000") { t.Fatal("pipeline.js mist de overzichtspoll") } finishPipe(t, st, id, "success") home = read(t, mustGet(t, tim, ts.URL+"/tim/livepipe")) if strings.Contains(home, `data-live-on="1"`) || strings.Contains(home, "/assets/pipeline.js") { t.Fatal("geslaagde pipeline blijft pollen") } for _, want := range []string{`class="status passed"`, `class="stage-dot passed"`, `data-pipe-duration`, "Passed"} { if !strings.Contains(home, want) { t.Errorf("geslaagd overzicht mist %q", want) } } body = overviewJSON(t, tim, ts.URL+"/tim/livepipe/-/pipelines/"+strconv.FormatInt(id, 10)) if !strings.Contains(body, `"active":false`) || !strings.Contains(body, `"status":"passed"`) || !strings.Contains(body, `"duration":"`) { t.Fatalf("stille json:\n%s", body) } failed := makePipe(t, st, repo.ID, sha, "kapot", []store.NewJob{{Name: "unit", Stage: "test", Script: "exit 1"}}) finishPipe(t, st, failed, "failed") home = read(t, mustGet(t, tim, ts.URL+"/tim/livepipe")) if strings.Contains(home, `data-live-on="1"`) || !strings.Contains(home, `class="status failed"`) { t.Fatalf("mislukt overzicht:\n%s", home) } canceled := makePipe(t, st, repo.ID, sha, "stop", []store.NewJob{{Name: "unit", Stage: "test", Script: "exit 0"}}) finishPipe(t, st, canceled, "canceled") home = read(t, mustGet(t, tim, ts.URL+"/tim/livepipe")) if strings.Contains(home, `data-live-on="1"`) || !strings.Contains(home, `class="status canceled"`) { t.Fatalf("geannuleerd overzicht:\n%s", home) } running := makePipe(t, st, repo.ID, sha, "bezig", []store.NewJob{ {Name: "unit", Stage: "test", Script: "exit 0"}, {Name: "ship", Stage: "deploy", Script: "exit 0"}, }) jobs, err := st.JobsByPipeline(ctx, running) if err != nil { t.Fatal(err) } if ok, err := st.MarkJobRunning(ctx, jobs[0].ID); err != nil || !ok { t.Fatalf("running: %v %v", ok, err) } home = read(t, mustGet(t, tim, ts.URL+"/tim/livepipe")) if !strings.Contains(home, `data-live-on="1"`) || !strings.Contains(home, `class="status running"`) || !strings.Contains(home, `class="stage-dot running"`) { t.Fatalf("lopend overzicht:\n%s", home) } if !strings.Contains(home, `/tim/livepipe/-/pipelines/`+strconv.FormatInt(running, 10)) { t.Fatal("poll wijst niet naar de lopende pipeline") } } func makePipe(t *testing.T, st *store.Store, repoID int64, sha, title string, jobs []store.NewJob) int64 { t.Helper() id, err := st.CreatePipeline(context.Background(), repoID, sha, "refs/heads/master", "push", "tim", title, jobs) if err != nil { t.Fatal(err) } return id } func finishPipe(t *testing.T, st *store.Store, id int64, status string) { t.Helper() ctx := context.Background() jobs, err := st.JobsByPipeline(ctx, id) if err != nil { t.Fatal(err) } for _, j := range jobs { if err := st.FinishJob(ctx, j.ID, status, ""); err != nil { t.Fatal(err) } } if _, err := st.RefreshPipelineStatus(ctx, id); err != nil { t.Fatal(err) } } func overviewJSON(t *testing.T, c *http.Client, raw string) string { t.Helper() req, err := http.NewRequest(http.MethodGet, raw, nil) if err != nil { t.Fatal(err) } req.Header.Set("Accept", "application/json") res, err := c.Do(req) if err != nil { t.Fatal(err) } return read(t, res) }