| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | // The domains pass is a housekeeper duty since multi-region: N platform boxes |
| 10 | // carry PLATFORM_APPS_DOMAIN, and only the lease holder may reconcile |
| 11 | // deployment-wide domain state (agent.go, domainsDuty). |
| 12 | |
| 13 | func TestDomainsDutyRunsThePassAndPropagatesItsError(t *testing.T) { |
| 14 | quietLog(t) |
| 15 | ran := 0 |
| 16 | boom := errors.New("reconcile failed") |
| 17 | duty := domainsDuty(func(context.Context) error { ran++; return boom }) |
| 18 | |
| 19 | if err := duty.run(context.Background()); !errors.Is(err, boom) { |
| 20 | t.Fatalf("duty error = %v, want the pass's error propagated to the housekeeper log", err) |
| 21 | } |
| 22 | if ran != 1 { |
| 23 | t.Fatalf("pass ran %d times, want 1", ran) |
| 24 | } |
| 25 | if duty.name != "domains reconcile" { |
| 26 | t.Fatalf("duty name = %q, want the housekeeper log label", duty.name) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func TestDomainsDutyWithNoPassDeclines(t *testing.T) { |
| 31 | quietLog(t) |
| 32 | if err := domainsDuty(nil).run(context.Background()); err != nil { |
| 33 | t.Fatalf("duty with a nil pass returned %v, want nil (and no panic)", err) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // The gate pin: a box that does not hold the lease never runs the pass — |
| 38 | // that is what makes PLATFORM_APPS_DOMAIN safe to set on every box. |
| 39 | func TestDomainsDutyNeverRunsWhenLeaseNotHeld(t *testing.T) { |
| 40 | quietLog(t) |
| 41 | ran := 0 |
| 42 | duty := domainsDuty(func(context.Context) error { ran++; return nil }) |
| 43 | notHeld := func() (context.Context, bool) { return nil, false } |
| 44 | held := func() (context.Context, bool) { return context.Background(), true } |
| 45 | |
| 46 | runHousekeeperDuties(notHeld, duty) |
| 47 | if ran != 0 { |
| 48 | t.Fatalf("pass ran %d times without the lease, want 0", ran) |
| 49 | } |
| 50 | runHousekeeperDuties(held, duty) |
| 51 | if ran != 1 { |
| 52 | t.Fatalf("pass ran %d times with the lease, want 1", ran) |
| 53 | } |
| 54 | } |
| 55 | |