package releaseupdate import ( "archive/zip" "bytes" "context" "crypto/sha256" "errors" "fmt" "net/http" "net/http/httptest" "os" "path/filepath" "slices" "strings" "testing" "time" ) var testUnits = []string{"archie-state-store", "archie-gateway", "archied"} const ( oldImage = "ghcr.io/example/archie-agent@sha256:1111111111111111111111111111111111111111111111111111111111111111" newImage = "ghcr.io/example/archie-agent@sha256:2222222222222222222222222222222222222222222222222222222222222222" ) // fakeHost stands in for systemd, Docker, the State Store and the daemon. A // started State Store on a migrating release bumps the schema, as a real // candidate's migrations would. type fakeHost struct { bin string managed bool migrates bool unhealthy bool hangs bool // the State Store never answers the switch to the candidate image string backedUp string schema int64 pulled []string started []string restored bool } func (h *fakeHost) Managed(context.Context) bool { return h.managed } func (h *fakeHost) Start(_ context.Context, unit string) error { h.started = append(h.started, unit) if data, _ := os.ReadFile(filepath.Join(h.bin, unit)); h.migrates && unit == stateStoreUnit && bytes.Contains(data, []byte("2.0.0")) { h.schema = 2 } return nil } func (h *fakeHost) Stop(context.Context, string) error { return nil } func (h *fakeHost) Pull(_ context.Context, ref string) error { h.pulled = append(h.pulled, ref) return nil } func (h *fakeHost) Image(context.Context) (string, error) { return h.image, nil } func (h *fakeHost) SetImage(ctx context.Context, expected, desired string) error { if h.hangs && desired == newImage { <-ctx.Done() return ctx.Err() } if h.image != desired && h.image != expected { return errors.New("image policy changed") } h.image = desired return nil } func (h *fakeHost) Backup(_ context.Context, out string) error { h.backedUp = h.image return os.WriteFile(out, []byte("snapshot"), 0o600) } func (h *fakeHost) SchemaVersion(context.Context) (int64, error) { return h.schema, nil } func (h *fakeHost) Restore(_ context.Context, from string) error { if _, err := os.Stat(from); err != nil { return err } h.restored, h.schema, h.image = true, 1, h.backedUp return nil } func (h *fakeHost) Healthy(context.Context) (string, error) { if h.unhealthy { return "", errors.New("503 Service Unavailable") } data, err := os.ReadFile(filepath.Join(h.bin, "archied")) if err != nil { return "", err } return strings.Fields(string(data))[3], nil } // crash leaves the host as a watchdog killed during phase would have. func crash(t *testing.T, tx *Transaction, h *fakeHost, phase string) { t.Helper() j, found, err := tx.load() if err != nil || !found { t.Fatalf("no journal to crash: %v", err) } switched := map[string]int{phasePrepared: 0, phaseSwitching: 1, phaseRolledBack: 0} count, partial := switched[phase] if !partial { count = len(j.Units) h.image = newImage } for _, unit := range j.Units[:count] { if err := os.Rename(tx.staged(unit), filepath.Join(tx.BinDir, unit)); err != nil { t.Fatal(err) } } if phase == phaseRolledBack { for _, unit := range j.Units { if err := os.Remove(tx.staged(unit)); err != nil { t.Fatal(err) } } } if phase == phaseRollingBack { if err := installFile(filepath.Join(JournalPath(tx.StateDir), "previous", j.Units[0]), filepath.Join(tx.BinDir, j.Units[0]), 0o755); err != nil { t.Fatal(err) } } if err := tx.setPhase(&j, phase); err != nil { t.Fatal(err) } } func TestTransaction(t *testing.T) { type outcome struct { version, image string report string // health_check, or "" for no report restoredDatabase bool } old := outcome{version: "1.0.0", image: oldImage, report: "failed"} tests := []struct { name string host fakeHost candidate string // version the release's archied reports corrupt bool // SHA256SUMS does not match the zip crashAt string // phase the watchdog dies in, then boot recovers prepare string // Prepare refusal want outcome }{ {name: "healthy release commits", host: fakeHost{managed: true}, want: outcome{version: "2.0.0", image: newImage, report: "passed"}}, {name: "unhealthy release rolls back", host: fakeHost{managed: true, unhealthy: true}, want: old}, {name: "unhealthy migrated release restores the database", host: fakeHost{managed: true, unhealthy: true, migrates: true}, want: outcome{version: "1.0.0", image: oldImage, report: "failed", restoredDatabase: true}}, {name: "stuck image switch rolls back", host: fakeHost{managed: true, hangs: true}, want: old}, {name: "without systemd it installs without restarting", host: fakeHost{}, want: outcome{version: "2.0.0", image: newImage}}, {name: "checksum mismatch is refused", host: fakeHost{managed: true}, corrupt: true, prepare: "checksum mismatch", want: outcome{version: "1.0.0", image: oldImage}}, {name: "misreported candidate is refused", host: fakeHost{managed: true}, candidate: "1.9.0", prepare: "reports", want: outcome{version: "1.0.0", image: oldImage}}, {name: "crash before the watchdog started is abandoned", host: fakeHost{managed: true}, crashAt: phasePrepared, want: old}, {name: "crash while switching rolls back", host: fakeHost{managed: true}, crashAt: phaseSwitching, want: old}, {name: "crash while observing rolls back", host: fakeHost{managed: true}, crashAt: phaseObserving, want: old}, {name: "crash while rolling back finishes it", host: fakeHost{managed: true}, crashAt: phaseRollingBack, want: old}, {name: "crash after commit keeps the release", host: fakeHost{managed: true}, crashAt: phaseCommitted, want: outcome{version: "2.0.0", image: newImage}}, {name: "crash after rollback keeps the old release", host: fakeHost{managed: true}, crashAt: phaseRolledBack, want: outcome{version: "1.0.0", image: oldImage}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { root := t.TempDir() host := tt.host host.bin, host.image, host.schema = filepath.Join(root, "bin"), oldImage, 1 candidate := tt.candidate if candidate == "" { candidate = "2.0.0" } server := httptest.NewServer(releaseHandler(t, candidate, tt.corrupt)) defer server.Close() for _, unit := range testUnits { writeTestFile(t, filepath.Join(host.bin, unit), versionScript(unit, "1.0.0")) } report := filepath.Join(root, "report.json") tx := &Transaction{ StateDir: filepath.Join(root, "state"), BinDir: host.bin, Units: &host, Images: &host, Policy: &host, Database: &host, Health: &host, HealthTimeout: 20 * time.Millisecond, Observe: 5 * time.Millisecond, Poll: time.Millisecond, } _, err := tx.Prepare(t.Context(), PrepareRequest{Version: "2.0.0", ReleaseURL: server.URL, Meta: InstallMeta{Channel: "webui", ReportPath: report}}) switch { case tt.prepare != "": if err == nil || !strings.Contains(err.Error(), tt.prepare) { t.Fatalf("Prepare error = %v, want %q", err, tt.prepare) } case err != nil: t.Fatalf("Prepare: %v", err) case !strings.Contains(readTestFile(t, filepath.Join(JournalPath(tx.StateDir), WatchdogBinary)), " 2.0.0"): t.Fatal("the frozen watchdog is not the release's archied") case tt.crashAt != "": crash(t, tx, &host, tt.crashAt) err = tx.Run(t.Context(), true) default: ctx, cancel := context.WithTimeout(t.Context(), time.Second) defer cancel() if err = tx.Run(ctx, false); ctx.Err() != nil { t.Fatal("Run outlived HealthTimeout") } } if tt.prepare == "" && err != nil { t.Fatalf("Run: %v", err) } for _, unit := range testUnits { data, _ := os.ReadFile(filepath.Join(host.bin, unit)) if !strings.Contains(string(data), " "+tt.want.version) { t.Errorf("%s = %q, want %s", unit, data, tt.want.version) } if _, err := os.Stat(tx.staged(unit)); !errors.Is(err, os.ErrNotExist) { t.Errorf("%s is still staged", unit) } } if host.image != tt.want.image { t.Errorf("image = %s, want %s", host.image, tt.want.image) } if _, err := os.Stat(JournalPath(tx.StateDir)); !errors.Is(err, os.ErrNotExist) { t.Error("journal left behind") } if host.restored != tt.want.restoredDatabase { t.Errorf("database restored = %v, want %v", host.restored, tt.want.restoredDatabase) } if host.managed && tt.prepare == "" && !slices.Contains(host.started, "archied") { t.Errorf("archied never started: %v", host.started) } if !host.managed && len(host.started) > 0 { t.Errorf("started %v without a service manager", host.started) } got, found, err := ReadPendingReport(report) if err != nil { t.Fatal(err) } if wantReport := tt.want.report != "" && tt.crashAt != phaseCommitted && tt.crashAt != phaseRolledBack; found != wantReport || found && (got.HealthCheck != tt.want.report || got.RolledBack != (tt.want.report == "failed") || got.RestoredDatabase != tt.want.restoredDatabase) { t.Errorf("report = %+v (found %v), want %+v", got, found, tt.want) } }) } } // releaseHandler serves one release's zip and SHA256SUMS the way GitHub // release downloads are laid out. func releaseHandler(t *testing.T, candidate string, corrupt bool) http.Handler { t.Helper() name := "archie-core-archied-v2.0.0-linux-amd64" var archive bytes.Buffer writer := zip.NewWriter(&archive) add := func(path, body string) { header := &zip.FileHeader{Name: name + "/" + path, Method: zip.Deflate} header.SetMode(0o755) file, err := writer.CreateHeader(header) if err != nil { t.Fatal(err) } if _, err := file.Write([]byte(body)); err != nil { t.Fatal(err) } } for _, unit := range testUnits { version := "2.0.0" if unit == "archied" { version = candidate } add(unit, versionScript(unit, version)) } add("release.json", fmt.Sprintf(`{"required_topology":{"units":["%s"]},"agent_image":%q}`, strings.Join(testUnits, `","`), newImage)) if err := writer.Close(); err != nil { t.Fatal(err) } sum := sha256.Sum256(archive.Bytes()) if corrupt { sum[0] ^= 0xff } mux := http.NewServeMux() mux.HandleFunc("/v2.0.0/"+name+".zip", func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write(archive.Bytes()) }) mux.HandleFunc("/v2.0.0/SHA256SUMS", func(w http.ResponseWriter, _ *http.Request) { fmt.Fprintf(w, "%x %s.zip\n", sum, name) }) return mux } func versionScript(unit, version string) string { return fmt.Sprintf("#!/bin/sh\n# %s %s\necho '%s %s'\n", unit, version, unit, version) } func writeTestFile(t *testing.T, path, data string) { t.Helper() if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { t.Fatal(err) } if err := os.WriteFile(path, []byte(data), 0o755); err != nil { t.Fatal(err) } } func readTestFile(t *testing.T, path string) string { t.Helper() data, err := os.ReadFile(path) if err != nil { t.Fatal(err) } return string(data) }