| 1 | package scripts |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | ) |
| 14 | |
| 15 | func TestSelfUpdatePostsWithoutWaiting(t *testing.T) { |
| 16 | if _, err := exec.LookPath("bash"); err != nil { |
| 17 | t.Skip("bash ontbreekt") |
| 18 | } |
| 19 | if _, err := exec.LookPath("curl"); err != nil { |
| 20 | t.Skip("curl ontbreekt") |
| 21 | } |
| 22 | script := filepath.Join("..", "scripts", "self-update.sh") |
| 23 | sha := "0123456789abcdef0123456789abcdef01234567" |
| 24 | var posts, gets int |
| 25 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 26 | if r.URL.Path != "/api/v1/admin/self-update" { |
| 27 | http.NotFound(w, r) |
| 28 | return |
| 29 | } |
| 30 | if r.Method == http.MethodPost { |
| 31 | posts++ |
| 32 | w.WriteHeader(http.StatusAccepted) |
| 33 | _, _ = w.Write([]byte(`{"status":"accepted","ref":"` + sha + `"}`)) |
| 34 | return |
| 35 | } |
| 36 | gets++ |
| 37 | select {} |
| 38 | })) |
| 39 | t.Cleanup(srv.Close) |
| 40 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 41 | t.Cleanup(cancel) |
| 42 | cmd := exec.CommandContext(ctx, "bash", script) |
| 43 | cmd.Env = append(os.Environ(), |
| 44 | "CI_API_URL="+srv.URL+"/api/v1", |
| 45 | "BLUEGIT_UPDATE_TOKEN=test-token", |
| 46 | "CI_COMMIT_SHA="+sha, |
| 47 | ) |
| 48 | out, err := cmd.CombinedOutput() |
| 49 | if err != nil { |
| 50 | t.Fatalf("post: %v %s", err, out) |
| 51 | } |
| 52 | if posts != 1 || gets != 0 || !strings.Contains(string(out), "Self-update gestart: "+sha) { |
| 53 | t.Fatalf("posts %d gets %d\n%s", posts, gets, out) |
| 54 | } |
| 55 | raw, err := os.ReadFile(script) |
| 56 | if err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | if strings.Contains(string(raw), "jq") || strings.Contains(string(raw), "while true") { |
| 60 | t.Fatal("self-update.sh polt nog") |
| 61 | } |
| 62 | } |