| new file mode 100644 |
| index 000000000..21060a91c |
| --- /dev/null |
| +++ b/internal/ops/hibernate_verify_home_test.go |
| @@ -0,0 +1,109 @@ |
| +package ops |
| + |
| +import ( |
| + "context" |
| + "strings" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/dbverify" |
| + "github.com/carlosframework/platform/internal/lease" |
| + "github.com/carlosframework/platform/internal/provision" |
| +) |
| + |
| +// Member backups 2a, R3: a home-region box has no CARLOS_REGION, and its |
| +// instances' records carry region "". An empty region is the home region: |
| +// the drill runs there and binds verdicts to records whose region is "" |
| +// exactly. Nothing else is loosened: a malformed region still refuses, and |
| +// a record in another region still does not match. |
| + |
| +// A home box (region "") drills a home instance end to end through the |
| +// real run path, restore included, and publishes its verdict, bound to the |
| +// home record. A record in another region on the same box is no_record. |
| +func TestRunHibernateVerifyHomeBoxDrillsHomeInstances(t *testing.T) { |
| + const home, away = "home.example.com", "away.example.com" |
| + fx := newRunFx(t, home, away) |
| + fx.putRecord(fxAccount, home, fxCreated, func(r *provision.Record) { r.Region = "" }) |
| + fx.putRecord(fxAccount, away, fxCreated, func(r *provision.Record) { r.Region = "uk" }) |
| + swap(t, &verifyAt, lease.VerifyReaderAt) |
| + fx.writeSelf(fxSelf, false) |
| + r := fx.runner() |
| + r.Region = "" |
| + if code := r.run(context.Background()); code != 0 { |
| + t.Fatalf("a home box exited %d:\n%s", code, fx.logs) |
| + } |
| + if strings.Contains(fx.logs.String(), "NOT RUN") { |
| + t.Fatalf("a home box did not run:\n%s", fx.logs) |
| + } |
| + rec := fx.published(fxSelf) |
| + v := rec.Hosts[home] |
| + if !v.OK() || v.TXID != fx.plan().TXID { |
| + t.Fatalf("home instance: published %+v, want passed at txid %d", v, fx.plan().TXID) |
| + } |
| + if want := idFor(t, home, "", fxCreated); v.Identity != want { |
| + t.Fatalf("home instance: identity %+v, want %+v", v.Identity, want) |
| + } |
| + if a := rec.Hosts[away]; a.Result != dbverify.ResultUnverified || a.Reason != dbverify.ReasonNoRecord { |
| + t.Fatalf("a record in another region on a home box: %+v, want unverified/no_record", a) |
| + } |
| + fx.scratchEmpty() |
| +} |
| + |
| +// The exact match runs both ways: a region box does not take a home |
| +// record, and a home record that leaves for a region while it is tested |
| +// is record_changed at the close. |
| +func TestDrillHomeRegionMatchesExactly(t *testing.T) { |
| + const host = "home.example.com" |
| + t.Run("a region box and a home record", func(t *testing.T) { |
| + fx := newDrillFx(t, host) |
| + fx.putRecord(fxAccount, host, fxCreated, func(r *provision.Record) { r.Region = "" }) |
| + d := fx.drill() // region uk |
| + v := fx.run(context.Background(), d, fx.routes(host), nil)[host] |
| + if v.Result != dbverify.ResultUnverified || v.Reason != dbverify.ReasonNoRecord { |
| + t.Fatalf("verdict %+v, want unverified/no_record", v) |
| + } |
| + }) |
| + t.Run("a home record leaving during the test", func(t *testing.T) { |
| + fx := newDrillFx(t, host) |
| + fx.putRecord(fxAccount, host, fxCreated, func(r *provision.Record) { r.Region = "" }) |
| + fx.stubVerify(func(context.Context, string, string, uint64) (uint32, error) { |
| + fx.putRecord(fxAccount, host, fxCreated, func(r *provision.Record) { r.Region = "uk" }) |
| + return 1, nil |
| + }) |
| + d := fx.drill() |
| + d.Region = "" |
| + v := fx.run(context.Background(), d, fx.routes(host), nil)[host] |
| + if v.Result != dbverify.ResultUnverified || v.Reason != ReasonRecordChanged { |
| + t.Fatalf("verdict %+v, want unverified/record_changed", v) |
| + } |
| + }) |
| +} |
| + |
| +// A malformed region still refuses: nothing drilled, nothing published. |
| +func TestRunHibernateVerifyRefusesAMalformedRegion(t *testing.T) { |
| + for _, region := range []string{" ", "UK", "eu west", "fleet/", "uk/", "-uk"} { |
| + t.Run(region, func(t *testing.T) { |
| + fx := newRunFx(t, "a.example.com") |
| + fx.writeSelf(fxSelf, false) |
| + restored := false |
| + fx.stubVerify(func(context.Context, string, string, uint64) (uint32, error) { |
| + restored = true |
| + return 1, nil |
| + }) |
| + r := fx.runner() |
| + r.Region = region |
| + if code := r.run(context.Background()); code != 1 { |
| + t.Fatalf("CARLOS_REGION %q: exited %d, want 1", region, code) |
| + } |
| + if restored || !strings.Contains(fx.logs.String(), "is not a region") { |
| + t.Fatalf("CARLOS_REGION %q: restored %v, log:\n%s", region, restored, fx.logs) |
| + } |
| + keys, err := fx.store.List(context.Background(), "control/") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if len(keys) != 0 { |
| + t.Fatalf("CARLOS_REGION %q: published %v", region, keys) |
| + } |
| + }) |
| + } |
| +} |