diff --git a/internal/app/archied/release_canary.go b/internal/app/archied/release_canary.go index d4fe7636a..13e766d05 100644 --- a/internal/app/archied/release_canary.go +++ b/internal/app/archied/release_canary.go @@ -1,6 +1,7 @@ package archied import ( + "bytes" "context" "errors" "flag" @@ -8,6 +9,7 @@ import ( "io" "os" "os/exec" + "strings" "time" "oras.land/oras-go/v2/errdef" @@ -506,7 +508,11 @@ func rollBackCanary(ctx context.Context, dashboard releaseupdate.CanaryDashboard if err := releaseupdate.Installable(ctx, source, stable); err != nil { return fmt.Errorf("stable %s is not installable, the canary stays on next: %w", stable.Statement.Version, err) } - if migrated(ctx, checkout, stable.Statement.Version, failed) { + changed, err := migrated(ctx, checkout, stable.Statement.Version, failed) + if err != nil { + return holdCanary(ctx, dashboard, failed, err.Error(), out) + } + if changed { return holdCanary(ctx, dashboard, failed, failed+" changed the database schema", out) } if err := dashboard.Follow(ctx, "exact-pin", stable.Statement.Version); err != nil { @@ -527,12 +533,40 @@ func holdCanary(ctx context.Context, dashboard releaseupdate.CanaryDashboard, ve return nil } +// runGitDiff runs git diff --quiet between two release tags and reports its +// exit code and stderr. It is a variable so tests can drive the three +// outcomes without a repository. +var runGitDiff = func(ctx context.Context, checkout, from, to string) (int, string, error) { + cmd := exec.CommandContext(ctx, "git", "-C", checkout, "diff", "--quiet", "v"+from, "v"+to, "--", migrationsDir) + var buf bytes.Buffer + cmd.Stderr = &buf + err := cmd.Run() + if err == nil { + return 0, "", nil + } + if exitErr, ok := errors.AsType[*exec.ExitError](err); ok { + return exitErr.ExitCode(), buf.String(), err + } + return -1, buf.String(), err +} + // migrated reports whether the migrations differ between two release tags. -// If it can't tell, it says they differ, so the canary is held rather than -// rolled back. -func migrated(ctx context.Context, checkout, from, to string) bool { - err := exec.CommandContext(ctx, "git", "-C", checkout, "diff", "--quiet", "v"+from, "v"+to, "--", migrationsDir).Run() - return err != nil +// It returns true, nil when they differ, false, nil when they are identical, +// and true, err when git could not answer, with the reason in err. Every +// non-identical outcome holds the canary rather than rolls it back. +func migrated(ctx context.Context, checkout, from, to string) (bool, error) { + code, stderr, runErr := runGitDiff(ctx, checkout, from, to) + switch code { + case 0: + return false, nil + case 1: + return true, nil + } + reason := strings.TrimSpace(stderr) + if reason == "" && runErr != nil { + reason = runErr.Error() + } + return true, fmt.Errorf("could not tell whether %s changed the database schema: %s", to, reason) } func runCanaryPromote(args []string, out, stderr io.Writer) int { diff --git a/internal/app/archied/release_canary_test.go b/internal/app/archied/release_canary_test.go new file mode 100644 index 000000000..afdbb010b --- /dev/null +++ b/internal/app/archied/release_canary_test.go @@ -0,0 +1,57 @@ +package archied + +import ( + "context" + "strings" + "testing" +) + +func TestMigrated(t *testing.T) { + tests := []struct { + name string + code int + stderr string + wantChanged bool + wantErr bool + wantSubstr string + }{ + { + name: "identical migrations", + code: 0, + wantChanged: false, + }, + { + name: "migrations differ", + code: 1, + wantChanged: true, + }, + { + name: "git cannot answer", + code: 128, + stderr: "fatal: bad revision 'v1.0.0'\n", + wantChanged: true, + wantErr: true, + wantSubstr: "fatal: bad revision 'v1.0.0'", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + old := runGitDiff + runGitDiff = func(context.Context, string, string, string) (int, string, error) { + return tt.code, tt.stderr, nil + } + t.Cleanup(func() { runGitDiff = old }) + + changed, err := migrated(context.Background(), ".", "1.0.0", "1.1.0") + if changed != tt.wantChanged { + t.Fatalf("changed = %v, want %v", changed, tt.wantChanged) + } + if (err != nil) != tt.wantErr { + t.Fatalf("err = %v, want err %v", err, tt.wantErr) + } + if tt.wantErr && !strings.Contains(err.Error(), tt.wantSubstr) { + t.Fatalf("err = %q, want substring %q", err, tt.wantSubstr) + } + }) + } +}