| index 948791e..dd4a024 100644 |
| --- a/adopt/homepreflight_test.go |
| +++ b/adopt/homepreflight_test.go |
| @@ -20,9 +20,10 @@ import ( |
| // inspectHome answers <role>.inspect with whatever state a test sets, |
| // echoing the signed challenge so the client accepts the answer. |
| type inspectHome struct { |
| - mu sync.Mutex |
| - state map[string]homeop.Inspection |
| - status int |
| + mu sync.Mutex |
| + state map[string]homeop.Inspection |
| + // status, when set for a role, is that role's answer instead. |
| + status map[string]int |
| calls map[string]int |
| } |
| |
| @@ -31,8 +32,8 @@ func (f *inspectHome) handler(role string) http.Handler { |
| f.mu.Lock() |
| defer f.mu.Unlock() |
| f.calls[role]++ |
| - if f.status != 0 { |
| - w.WriteHeader(f.status) |
| + if s := f.status[role]; s != 0 { |
| + w.WriteHeader(s) |
| return |
| } |
| raw, _ := io.ReadAll(r.Body) |
| @@ -245,13 +246,58 @@ func TestHomePreflightPrePlanAAcceptsOwnInitialize(t *testing.T) { |
| if r.CredentialSeq != 3 || r.CredentialHead != credHead { |
| t.Fatalf("record = %+v", r) |
| } |
| +} |
| |
| - // Unknown pasted tables are not recorded. |
| - counts["extra"] = 9 |
| - r, codes = p.check(t, counts, PhasePrePlanA) |
| - wantCodes(t, codes) |
| - if _, ok := r.Counts["extra"]; ok { |
| - t.Fatal("record holds a table HomeQueries does not ask for") |
| +// A pasted table HomeQueries does not ask for is a typo or a stray line: |
| +// refused, with one fixed code, and not recorded. |
| +func TestHomePreflightRefusesUnknownTable(t *testing.T) { |
| + for _, phase := range []string{PhasePreInitialize, PhasePrePlanA} { |
| + t.Run(phase, func(t *testing.T) { |
| + p := newPreflightFixture(t) |
| + counts := zeroCounts() |
| + if phase == PhasePrePlanA { |
| + _, counts = p.initialized(t) |
| + } |
| + counts["extra"] = 0 |
| + r, codes := p.check(t, counts, phase) |
| + wantCodes(t, codes, "count-unknown-table") |
| + if _, ok := r.Counts["extra"]; ok { |
| + t.Fatal("record holds a table HomeQueries does not ask for") |
| + } |
| + |
| + delete(counts, "extra") |
| + delete(counts, "teams") |
| + counts["team"] = 0 |
| + _, codes = p.check(t, counts, phase) |
| + wantCodes(t, codes, "count-missing-teams", "count-unknown-table") |
| + }) |
| + } |
| +} |
| + |
| +// Before Plan A, a pending or open plan on either role is refused: the |
| +// spec allows none, and Plan A must be the only plan in flight. |
| +func TestHomePreflightPrePlanARefusesPendingOrOpenPlan(t *testing.T) { |
| + cases := []struct { |
| + name, role string |
| + edit func(*homeop.Inspection) |
| + code string |
| + }{ |
| + {"credential-pending", homeop.Credential, func(in *homeop.Inspection) { in.PendingPlan = homeop.Token() }, "plan-pending"}, |
| + {"directory-pending", homeop.Directory, func(in *homeop.Inspection) { in.PendingPlan = homeop.Token() }, "plan-pending"}, |
| + {"credential-open", homeop.Credential, func(in *homeop.Inspection) { in.OpenPlan = homeop.Token() }, "plan-open"}, |
| + {"directory-open", homeop.Directory, func(in *homeop.Inspection) { in.OpenPlan = homeop.Token() }, "plan-open"}, |
| + } |
| + for _, c := range cases { |
| + t.Run(c.name, func(t *testing.T) { |
| + p := newPreflightFixture(t) |
| + _, counts := p.initialized(t) |
| + p.set(c.role, c.edit) |
| + r, codes := p.check(t, counts, PhasePrePlanA) |
| + wantCodes(t, codes, c.code) |
| + if r.Pending != (c.code == "plan-pending") || r.Open != (c.code == "plan-open") { |
| + t.Fatalf("record pending=%v open=%v", r.Pending, r.Open) |
| + } |
| + }) |
| } |
| } |
| |
| @@ -366,12 +412,21 @@ func TestHomePreflightErrors(t *testing.T) { |
| t.Fatalf("phase %q accepted", phase) |
| } |
| } |
| - p.home.status = http.StatusServiceUnavailable |
| - _, codes, err := CheckHome(context.Background(), p.client, p.ps, zeroCounts(), PhasePreInitialize) |
| - if err == nil || codes != nil { |
| - t.Fatalf("an unavailable Home gave codes %q, err %v", codes, err) |
| - } |
| - if strings.Contains(err.Error(), p.client.Roles[homeop.Directory].URL) { |
| - t.Fatalf("error names the endpoint: %v", err) |
| + // Each role failing alone gives that role's fixed error, naming no |
| + // endpoint of either role. |
| + for _, role := range []string{homeop.Credential, homeop.Directory} { |
| + p.home.status = map[string]int{role: http.StatusServiceUnavailable} |
| + _, codes, err := CheckHome(context.Background(), p.client, p.ps, zeroCounts(), PhasePreInitialize) |
| + if err == nil || codes != nil { |
| + t.Fatalf("%s unavailable gave codes %q, err %v", role, codes, err) |
| + } |
| + if want := "adopt: " + role + " inspect failed"; err.Error() != want { |
| + t.Fatalf("error = %q, want %q", err, want) |
| + } |
| + for _, r := range p.client.Roles { |
| + if strings.Contains(err.Error(), r.URL) || strings.Contains(err.Error(), r.Origin) { |
| + t.Fatalf("error names an endpoint: %v", err) |
| + } |
| + } |
| } |
| } |