package releaseupdate import ( "archive/zip" "bytes" "context" "crypto/ed25519" "encoding/base64" "encoding/json" "errors" "fmt" "net/http" "net/http/httptest" "os" "path/filepath" "slices" "strings" "testing" "time" digest "github.com/opencontainers/go-digest" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/samcharles93/archie-core/internal/buildinfo" "github.com/samcharles93/archie-core/internal/domain/distribution" "github.com/samcharles93/archie-core/internal/infrastructure/signedoci" "github.com/samcharles93/archie-core/internal/platform/deletions" ) 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 restoreErr error } 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 } if h.restoreErr != nil { return h.restoreErr } 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) { // The installer resolves the database password from the environment or // the env file beside its config; pin both to this test's own files so // the result never depends on the machine's real archie config. t.Setenv(DatabasePasswordEnv, "test-password") // Stamp this binary as a release, as a host installed from one is, so the // journal's previous daemon version is a real version. stamped := buildinfo.Version buildinfo.Version = "1.0.0" defer func() { buildinfo.Version = stamped }() configPath := filepath.Join(t.TempDir(), "config.toml") if err := os.WriteFile(configPath, []byte("database_url = \"postgres://archie@/archie?host=/tmp/archie-test\"\n"), 0o600); err != nil { t.Fatal(err) } type outcome struct { version, image string report string // health_check, or "" for no report rolledBack bool restoredDatabase bool } old := outcome{version: "1.0.0", image: oldImage, report: "failed", rolledBack: true} tests := []struct { name string host fakeHost candidate string // version the release's archied reports corrupt bool // the registry serves a zip other than the one signed 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", rolledBack: true, restoredDatabase: true}}, {name: "unsafe rollback retains the installed release without leaving a pending update", host: fakeHost{managed: true, unhealthy: true, migrates: true, restoreErr: deletions.ErrUnsafeRollback}, want: outcome{version: "2.0.0", image: newImage, report: "failed"}}, {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: "a tampered zip is refused", host: fakeHost{managed: true}, corrupt: true, prepare: "digest", 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" } handler, key := releaseHandler(t, candidate, tt.corrupt) server := httptest.NewServer(handler) defer server.Close() for _, unit := range testUnits { writeScript(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, ConfigPath: configPath, 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", Source: distribution.Source{Reference: strings.TrimPrefix(server.URL, "http://") + "/archie", Key: key}, 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.rolledBack || 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, ed25519.PublicKey) { 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"]}}`, strings.Join(testUnits, `","`))) if err := writer.Close(); err != nil { t.Fatal(err) } zipDigest := signedoci.Digest(archive.Bytes()) statement, err := json.Marshal(Statement{ Version: "2.0.0", IssuedAt: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC), Platform: Platform, Zip: Blob{Name: name + ".zip", Digest: zipDigest, Size: int64(archive.Len())}, AgentImage: newImage, }) if err != nil { t.Fatal(err) } public, private, err := ed25519.GenerateKey(nil) if err != nil { t.Fatal(err) } served := archive.Bytes() if corrupt { served = append([]byte(nil), served...) served[len(served)/2] ^= 0xff } return fakeRegistry(t, "archie", "v2.0.0", private, statement, archive.Bytes(), served), public } // fakeRegistry serves one signed release artifact at repository:tag, the way // an OCI registry answers oras: the manifest by tag, each layer by digest. // served is the zip the registry returns, which a tampering registry swaps. func fakeRegistry(t *testing.T, repository, tag string, signer ed25519.PrivateKey, statement, archive, served []byte) http.Handler { t.Helper() blobs := map[string][]byte{signedoci.Digest(statement): statement, signedoci.Digest(archive): served} manifest, err := json.Marshal(ocispec.Manifest{ SchemaVersion: 2, MediaType: ocispec.MediaTypeImageManifest, ArtifactType: ArtifactType, Config: ocispec.DescriptorEmptyJSON, Layers: []ocispec.Descriptor{ {MediaType: StatementMediaType, Digest: digest.FromBytes(statement), Size: int64(len(statement))}, {MediaType: "application/zip", Digest: digest.FromBytes(archive), Size: int64(len(archive))}, }, Annotations: map[string]string{Annotation: base64.StdEncoding.EncodeToString(ed25519.Sign(signer, statement))}, }) if err != nil { t.Fatal(err) } mux := http.NewServeMux() mux.HandleFunc("/v2/"+repository+"/manifests/"+tag, func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", ocispec.MediaTypeImageManifest) w.Header().Set("Docker-Content-Digest", signedoci.Digest(manifest)) _, _ = w.Write(manifest) }) mux.HandleFunc("/v2/"+repository+"/blobs/", func(w http.ResponseWriter, r *http.Request) { blob, ok := blobs[strings.TrimPrefix(r.URL.Path, "/v2/"+repository+"/blobs/")] if !ok { http.NotFound(w, r) return } _, _ = w.Write(blob) }) return mux } func versionScript(unit, version string) string { return fmt.Sprintf("#!/bin/sh\n# %s %s\necho '%s %s'\n", unit, version, unit, version) } // writeScript writes a stand-in for a release binary the transaction runs. func writeScript(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), 0o700); err != nil { //nolint:gosec // G306: a script needs the execute bit. 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) }