package e2e import ( "bytes" "encoding/json" "net/http" "os/exec" "strings" "testing" ) // Chapter 19.1 has run.failed and not run.succeeded, so a red build has to reach the inbox. func TestAFailedBuildReachesTheInbox(t *testing.T) { if _, err := exec.LookPath("git"); err != nil { t.Skip("git is not installed") } in := newInstance(t) john := in.account("john") seed(t, in, john, "john", "johnbot") jobID, _, runnerToken := takeJobAs(t, in, "john", "john/johnbot") finish(t, in, runnerToken, jobID, 2) body := inboxOf(t, in, "john") if !strings.Contains(body, "build failed") { t.Errorf("a failed build is not in the inbox:\n%s", body) } if !strings.Contains(body, "refs/heads/master") { t.Errorf("the inbox line does not say what was being built:\n%s", body) } } // A green build is not news, and putting it in the inbox is how a feed becomes noise. 19.1. func TestAPassingBuildIsNotAnEvent(t *testing.T) { if _, err := exec.LookPath("git"); err != nil { t.Skip("git is not installed") } in := newInstance(t) john := in.account("john") seed(t, in, john, "john", "johnbot") jobID, _, runnerToken := takeJobAs(t, in, "john", "john/johnbot") finish(t, in, runnerToken, jobID, 0) if body := inboxOf(t, in, "john"); strings.Contains(body, "build failed") { t.Errorf("a build that passed produced an event:\n%s", body) } } func finish(t *testing.T, in *instance, runnerToken string, jobID int64, exit int) { t.Helper() body, err := json.Marshal(map[string]any{ "token": runnerToken, "job_id": jobID, "exit_code": exit, "duration": 3, }) if err != nil { t.Fatal(err) } resp, err := http.Post(in.http.URL+"/runner/done", "application/json", bytes.NewReader(body)) if err != nil { t.Fatal(err) } defer resp.Body.Close() if resp.StatusCode != http.StatusNoContent { t.Fatalf("finishing the job answered %d", resp.StatusCode) } }