region-ha p2a: wire ingress and refusal witnesses
Exercise ingress and refusal doors through real authentication, store wrappers and S3 clients. Count protected mutations, bounded listings and SDK retries, and retain successful exclusions and duplicate precedence. Date the deleted-target fixture before its heartbeats so settling cannot mask its missing-serving-box assertion at a second boundary.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+734
−1
internal/console/hap2_wiring_test.go+733 −0internal/console/regioningress_test.go+1 −1
CI cancelled — superseded by a newer push — run details
diff --git a/internal/console/hap2_wiring_test.go b/internal/console/hap2_wiring_test.go| new file mode 100644 |
| index 000000000..0702e3fd4 |
| --- /dev/null |
| +++ b/internal/console/hap2_wiring_test.go |
| @@ -0,0 +1,733 @@ |
| +package console |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "fmt" |
| + "html" |
| + "io" |
| + "net/http" |
| + "net/http/httptest" |
| + "net/url" |
| + "reflect" |
| + "strings" |
| + "sync" |
| + "testing" |
| + "time" |
| + |
| + "github.com/aws/aws-sdk-go-v2/aws/retry" |
| + awss3 "github.com/aws/aws-sdk-go-v2/service/s3" |
| + |
| + "github.com/carlosframework/platform/internal/domains" |
| + "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/release" |
| + "github.com/carlosframework/platform/internal/s3fake" |
| +) |
| + |
| +// Count HTTP attempts below the SDK, including failed calls, pagination, |
| +// conditional writes and change-index bumps. All clients (including the real |
| +// Litestream reader) reach this transport through the loopback S3 endpoint. |
| +// Fixture seeding stays on the underlying DirStore and is not charged. |
| +type hap2Attempt struct { |
| + method, bucket, key string |
| + query url.Values |
| +} |
| +type hap2Transport struct { |
| + mu sync.Mutex |
| + attempts []hap2Attempt |
| + control http.Handler |
| + data *fakeDataBucket |
| + fail func(hap2Attempt) bool |
| + failStatus int |
| +} |
| + |
| +func (tr *hap2Transport) RoundTrip(r *http.Request) (*http.Response, error) { |
| + bucket, key, _ := strings.Cut(strings.TrimPrefix(r.URL.Path, "/"), "/") |
| + a := hap2Attempt{r.Method, bucket, key, r.URL.Query()} |
| + tr.mu.Lock() |
| + tr.attempts = append(tr.attempts, a) |
| + fail := tr.fail != nil && tr.fail(a) |
| + failStatus := tr.failStatus |
| + tr.mu.Unlock() |
| + w := httptest.NewRecorder() |
| + switch { |
| + case fail: |
| + w.Header().Set("Content-Type", "application/xml") |
| + code := "AccessDenied" |
| + if failStatus == 0 { |
| + failStatus = http.StatusForbidden |
| + } |
| + if failStatus == http.StatusServiceUnavailable { |
| + code = "SlowDown" |
| + } |
| + w.WriteHeader(failStatus) |
| + fmt.Fprintf(w, "<Error><Code>%s</Code><Message>secret-bucket/secret-key</Message></Error>", code) |
| + case bucket == "carlos-fixture": |
| + tr.control.ServeHTTP(w, r) |
| + case bucket == "data-test": |
| + tr.data.ServeHTTP(w, r) |
| + default: |
| + http.Error(w, "unexpected test bucket", http.StatusBadRequest) |
| + } |
| + return w.Result(), nil |
| +} |
| +func (tr *hap2Transport) reset() { |
| + tr.mu.Lock() |
| + defer tr.mu.Unlock() |
| + tr.attempts = nil |
| +} |
| +func (tr *hap2Transport) setFailure(fail func(hap2Attempt) bool) { |
| + tr.mu.Lock() |
| + defer tr.mu.Unlock() |
| + tr.fail = fail |
| +} |
| +func (tr *hap2Transport) setData(keys ...string) { |
| + tr.data.mu.Lock() |
| + defer tr.data.mu.Unlock() |
| + tr.data.keys = keys |
| +} |
| +func (tr *hap2Transport) all() []hap2Attempt { |
| + tr.mu.Lock() |
| + defer tr.mu.Unlock() |
| + return append([]hap2Attempt(nil), tr.attempts...) |
| +} |
| +func (tr *hap2Transport) noWrites(t *testing.T) { |
| + t.Helper() |
| + for _, a := range tr.all() { |
| + if a.method != http.MethodGet && a.method != http.MethodHead { |
| + t.Errorf("refusal attempted protected mutation: %+v", a) |
| + } |
| + } |
| +} |
| +func (tr *hap2Transport) noData(t *testing.T) { |
| + t.Helper() |
| + for _, a := range tr.all() { |
| + if a.bucket == "data-test" { |
| + t.Errorf("refused transfer attempted data I/O: %+v", a) |
| + } |
| + } |
| +} |
| + |
| +func hap2S3(t *testing.T, fx *consoleFixture, keys ...string) *hap2Transport { |
| + t.Helper() |
| + seeded, ok := fx.rel.(*deletableStore) |
| + if !ok { |
| + t.Fatalf("fixture store %T", fx.rel) |
| + } |
| + dir, ok := seeded.Store.(*release.DirStore) |
| + if !ok { |
| + t.Fatalf("fixture backing store %T", seeded.Store) |
| + } |
| + fake := s3fake.New(dir) |
| + fake.ListFrom(seeded.root) |
| + tr := &hap2Transport{control: fake, data: &fakeDataBucket{keys: keys}} |
| + endpoint := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + resp, err := tr.RoundTrip(r) |
| + if err != nil { |
| + t.Errorf("S3 transport: %v", err) |
| + w.WriteHeader(500) |
| + return |
| + } |
| + defer resp.Body.Close() |
| + for k, vs := range resp.Header { |
| + w.Header()[k] = vs |
| + } |
| + w.WriteHeader(resp.StatusCode) |
| + _, _ = io.Copy(w, resp.Body) |
| + })) |
| + t.Cleanup(endpoint.Close) |
| + t.Setenv("CARLOS_S3_ENDPOINT", endpoint.URL) |
| + t.Setenv("AWS_ACCESS_KEY_ID", "fixture") |
| + t.Setenv("AWS_SECRET_ACCESS_KEY", "fixture") |
| + t.Setenv("AWS_SESSION_TOKEN", "") |
| + t.Setenv("AWS_REGION", "us-east-1") |
| + t.Setenv("AWS_EC2_METADATA_DISABLED", "true") |
| + control, err := release.NewDeploymentS3Store(context.Background(), "carlos-fixture") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + data, err := release.NewS3Store(context.Background(), "data-test") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + fx.srv.cfg.Releases = TimeS3Store(control, fx.srv.cfg.Perf) |
| + fx.srv.cfg.SeedData = data |
| + fx.srv.cfg.HibernateBucket = "data-test" |
| + fx.srv.cfg.HibernateRegion = "us-east-1" |
| + fx.srv.cfg.HibernatePrefix = "instances" |
| + fx.srv.dbReplica = fx.srv.cachedReadReplica |
| + return tr |
| +} |
| + |
| +func hap2Refusal(t *testing.T, tr *hap2Transport, status int, out string, wantStatus int, want string) { |
| + t.Helper() |
| + if status != wantStatus || errorText(out) != want { |
| + t.Errorf("door = %d %s; want %d %q", status, out, wantStatus, want) |
| + } |
| + tr.noWrites(t) |
| + for _, private := range []string{"secret-bucket", "secret-key", "data-test", "instances/", "moves/"} { |
| + if strings.Contains(out, private) { |
| + t.Errorf("private storage detail %q in %s", private, out) |
| + } |
| + } |
| +} |
| + |
| +func TestHAP2ProductionDoors(t *testing.T) { |
| + t.Run("ingress", func(t *testing.T) { |
| + w := newRegionalWorld(t, "hap2-ingress") |
| + w.putRegionalWorldClaim(t, time.Now()) |
| + attachShopClaim(t, w.fx, w.fx.sqid, "shop", "plain.example.com", rdIE) |
| + attachShopClaim(t, w.fx, w.fx.sqid, "shop", "example.com", rdIE) |
| + w.fx.srv.cfg.EdgeIPs = []string{"192.0.2.1"} |
| + w.fx.srv.cfg.RegionIngress = []string{ingressIP, "192.0.2.1"} |
| + w.fx.srv.cfg.HARegions = []string{"ie"} |
| + w.fx.srv.steeringHost = "edge.example.net" |
| + tr := hap2S3(t, w.fx) |
| + union := "192.0.2.1, " + ingressIP |
| + want := "Placement readiness for " + rdHost + " is unavailable here. For DNS, use the configured public ingress addresses: " + union + "." |
| + for _, method := range []string{http.MethodGet, http.MethodPut} { |
| + tr.reset() |
| + body := "" |
| + if method == http.MethodPut { |
| + raw, err := json.Marshal(rdBoth()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + body = string(raw) |
| + } |
| + code, _, out := rawReq(t, method, placementsURL(w.fx, "shop", rdHost), w.token, body) |
| + hap2Refusal(t, tr, code, out, 409, want) |
| + for _, bad := range []string{`"placements"`, `"ready"`, "203.0.113.10", "203.0.113.20"} { |
| + if strings.Contains(out, bad) { |
| + t.Errorf("placements refusal leaked %s: %s", bad, out) |
| + } |
| + } |
| + } |
| + for _, path := range []string{domainsURL(w.fx, w.fx.sqid, "shop"), domainGetURL(w.fx, w.fx.sqid, "shop", rdHost)} { |
| + code, _, out := rawReq(t, http.MethodGet, path, w.token, "") |
| + if code != 200 { |
| + t.Fatalf("domain sink = %d %s", code, out) |
| + } |
| + var payload map[string]any |
| + if err := json.Unmarshal([]byte(out), &payload); err != nil { |
| + t.Fatal(err) |
| + } |
| + rows, ok := payload["domains"].([]any) |
| + if !ok { |
| + rows = []any{payload["domain"]} |
| + } |
| + seen := false |
| + for _, raw := range rows { |
| + row := raw.(map[string]any) |
| + host := row["host"].(string) |
| + guidance := want |
| + switch host { |
| + case "plain.example.com": |
| + guidance = "Point an A record for " + host + " at " + union + ". Checks run every few minutes." |
| + case "example.com": |
| + guidance = host + " is an apex domain and cannot use a CNAME. Point its A record at the configured public ingress addresses: " + union + ". Checks run every few minutes." |
| + case rdHost: |
| + seen = true |
| + default: |
| + continue |
| + } |
| + if row["guidance"] != guidance { |
| + t.Errorf("guidance %s = %v; want %q", host, row["guidance"], guidance) |
| + } |
| + if host == rdHost && row["detail"] != ingressDetail { |
| + t.Errorf("regional detail = %v", row) |
| + } |
| + if host != rdHost { |
| + records, ok := row["records"].([]any) |
| + if !ok || len(records) != 2 { |
| + t.Fatalf("public DNS records = %v", row) |
| + } |
| + for i, record := range records { |
| + rr := record.(map[string]any) |
| + if rr["type"] != "A" || rr["value"] != []string{"192.0.2.1", ingressIP}[i] { |
| + t.Errorf("DNS record = %v", rr) |
| + } |
| + } |
| + } |
| + } |
| + if !seen { |
| + t.Error("regional domain row missing") |
| + } |
| + for _, bad := range []string{"203.0.113.10", "203.0.113.20", "edge.example.net"} { |
| + if strings.Contains(out, bad) { |
| + t.Errorf("configured domain sink leaked %s", bad) |
| + } |
| + } |
| + } |
| + c := signInPaul(t, w.fx) |
| + resp, out := perfGet(t, c, w.fx.u("/apps/shop/settings/domains")) |
| + if resp.StatusCode != 200 || !strings.Contains(html.UnescapeString(out), want) || !strings.Contains(out, ingressDetail) { |
| + t.Errorf("rendered settings = %d; approved regional copy missing", resp.StatusCode) |
| + } |
| + // Auth still precedes guidance and all of its target reads. |
| + stranger := cliToken(t, w.fx, "eve@keymail.dev", "hap2-stranger") |
| + tr.reset() |
| + code, _, out := rawReq(t, http.MethodGet, placementsURL(w.fx, "shop", rdHost), stranger, "") |
| + nc, _, nb := rawReq(t, http.MethodGet, placementsURL(w.fx, "shop", "missing.example.com"), stranger, "") |
| + if code != 404 || nc != code || nb != out { |
| + t.Errorf("ownership oracle: %d %s / %d %s", code, out, nc, nb) |
| + } |
| + if len(tr.all()) != 0 { |
| + t.Errorf("unauthorized request read deployment: %+v", tr.all()) |
| + } |
| + }) |
| + |
| + for _, lifetimes := range []bool{false, true} { |
| + for _, private := range []bool{false, true} { |
| + t.Run(fmt.Sprintf("create/lifetimes=%v/private=%v", lifetimes, private), func(t *testing.T) { |
| + clock := useLifetimesClock(t) |
| + f := newFromFixture(t, 0) |
| + if lifetimes { |
| + turnLifetimesOn(t, f.fx.srv, clock) |
| + } |
| + f.fx.srv.cfg.HomeRegion, f.fx.srv.cfg.HARegions = "ie", []string{"ie", "uk"} |
| + enableInstances(t, f.fx, "shop", fromManifest(private)) |
| + tr := hap2S3(t, f.fx) |
| + buckets, users := len(f.fake.Buckets), len(f.fake.Users) |
| + for _, mode := range []string{"lock", "files", "replica", "other", "unreadable", "remote", "pool", "missing-home-wiring"} { |
| + t.Run(mode, func(t *testing.T) { |
| + tr.reset() |
| + tr.setFailure(nil) |
| + tr.setData() |
| + enableInstances(t, f.fx, "shop", fromManifest(private)) |
| + body := `{"host":"` + fromHost + `"}` |
| + status := 409 |
| + want := fromHost + " still holds " + mode + " from a deleted instance. choose another address, or ask an operator to purge it." |
| + switch mode { |
| + case "lock": |
| + tr.setData("instances/" + fromHost + "/lock.json") |
| + case "files": |
| + tr.setData("instances/" + fromHost + "/files/0/objects/secret-key") |
| + case "replica": |
| + tr.setData(ltxKey("instances/"+fromHost, 9)) |
| + case "other": |
| + tr.setData("instances/" + fromHost + "/secret-key") |
| + case "unreadable", "missing-home-wiring": |
| + status = 503 |
| + want = "could not check this instance's data in ie. nothing was created. try again. persistent failures may require an operator to check home data access." |
| + if mode == "unreadable" { |
| + tr.setFailure(func(a hap2Attempt) bool { return a.bucket == "data-test" }) |
| + } else { |
| + f.fx.srv.cfg.SeedData = nil |
| + } |
| + case "remote", "pool": |
| + region := "ie" |
| + if mode == "remote" { |
| + region = "uk" |
| + body = `{"host":"` + fromHost + `","region":"uk"}` |
| + } else { |
| + m := fromManifest(private) |
| + m.Pool = "gold" |
| + enableInstances(t, f.fx, "shop", m) |
| + } |
| + want = "high availability data creation is not available yet in " + region + ". use the console's home region without a pool." |
| + } |
| + before := bucketState(t, f.fx.rel) |
| + code, headers, out := rawReq(t, http.MethodPost, f.url, f.token, body) |
| + hap2Refusal(t, tr, code, out, status, want) |
| + if headers.Get("Retry-After") != "" { |
| + t.Error("unexpected Retry-After") |
| + } |
| + if !reflect.DeepEqual(before, bucketState(t, f.fx.rel)) { |
| + t.Error("refused create changed control storage") |
| + } |
| + if len(f.fake.Buckets) != buckets || len(f.fake.Users) != users { |
| + t.Error("refusal provisioned private storage") |
| + } |
| + }) |
| + } |
| + }) |
| + } |
| + } |
| + |
| + for _, door := range []string{"offline start", "offline stage", "legacy", "phase 1", "staged start"} { |
| + for _, active := range []string{"ie", "uk", "immutable"} { |
| + t.Run("move/"+door+"/"+active, func(t *testing.T) { |
| + w := newOfflineWorld(t, oneEach...) |
| + if door == "staged start" { |
| + w.ok("/stage", `{"region":"uk"}`, provision.OfflineStaging) |
| + } |
| + if active == "immutable" { |
| + markHA(t, w.fx.rel, "bab", "shop", offlineHost) |
| + } else { |
| + w.fx.srv.cfg.HARegions = []string{active} |
| + } |
| + u := haTransferDoor(w, door) |
| + before := bucketState(t, w.fx.rel) |
| + tr := hap2S3(t, w.fx) |
| + code, _, out := rawReq(t, http.MethodPost, u, w.token, `{"region":"uk"}`) |
| + region := active |
| + if active == "immutable" { |
| + region = "ie" |
| + } |
| + hap2Refusal(t, tr, code, out, 409, strings.Replace(haMoveC1, "ie ", region+" ", 1)) |
| + tr.noData(t) |
| + if !reflect.DeepEqual(before, bucketState(t, w.fx.rel)) { |
| + t.Error("refused move changed control storage") |
| + } |
| + }) |
| + } |
| + } |
| + t.Run("place", func(t *testing.T) { |
| + fx, token := placeSeedFixture(t, "hap2-place") |
| + fx.srv.cfg.HomeRegion, fx.srv.cfg.HARegions = "ie", []string{"ie"} |
| + putHARecord(t, fx.rel, fx.sqid, seedMoveHost, "ie") |
| + // This is a legacy record in an active region, not an immutable-flag test. |
| + haTransferEdit(t, fx.rel, fx.sqid, "shop", seedMoveHost, func(r *provision.Record) { r.HA = false }) |
| + before := bucketState(t, fx.rel) |
| + placement, err := fx.st.AppPlacement(OpsAccountID, "shop") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + tr := hap2S3(t, fx) |
| + code, _, out := rawReq(t, http.MethodPost, placeURL(fx, "shop"), token, `{"target":"fleet/pi"}`) |
| + hap2Refusal(t, tr, code, out, 409, haMoveC1) |
| + tr.noData(t) |
| + if got, err := fx.st.AppPlacement(OpsAccountID, "shop"); err != nil || got != placement { |
| + t.Error("refused place changed console placement") |
| + } |
| + if !reflect.DeepEqual(before, bucketState(t, fx.rel)) { |
| + t.Error("refused place changed control storage") |
| + } |
| + }) |
| + for _, door := range []string{"begin", "upload", "stopped"} { |
| + t.Run("import/"+door, func(t *testing.T) { |
| + f := newImportFixture(t) |
| + if door != "begin" { |
| + body, sha := sqliteBytes(t) |
| + code, out := beginImport(t, f, importHost, sha, int64(len(body))) |
| + f.requireBegun(t, code, out, importHost) |
| + if door == "stopped" { |
| + f.uploaded(t, importHost, body) |
| + } |
| + markHA(t, f.fx.rel, "bab", fromApp, importHost) |
| + } |
| + f.fx.srv.cfg.HomeRegion = "ie" |
| + if door == "begin" { |
| + f.fx.srv.cfg.HARegions = []string{"ie"} |
| + } else { |
| + f.fx.srv.cfg.HARegions = nil |
| + } |
| + before := bucketState(t, f.fx.rel) |
| + tr := hap2S3(t, f.fx) |
| + var code int |
| + var out map[string]any |
| + switch door { |
| + case "begin": |
| + code, out = beginImport(t, f, importHost, importSHA, 10) |
| + case "upload": |
| + code, out = uploadImport(t, f, importHost, []byte("x")) |
| + case "stopped": |
| + code, out = postImport(t, f, importHost, door) |
| + } |
| + raw, _ := json.Marshal(out) |
| + hap2Refusal(t, tr, code, string(raw), 409, haImportC2) |
| + if door == "begin" { |
| + // The create door retains main's leftover rail before dispatch |
| + // to import. The regional refusal adds no reads of its own. |
| + var got, want []string |
| + for _, a := range tr.all() { |
| + if a.bucket != "data-test" { |
| + continue |
| + } |
| + if a.method != http.MethodGet || a.query.Get("list-type") != "2" { |
| + t.Errorf("import begin data request: %+v", a) |
| + } |
| + got = append(got, a.query.Get("prefix")) |
| + } |
| + for _, prefix := range []string{"instances/", "moves/"} { |
| + for level := 0; level < 10; level++ { |
| + want = append(want, fmt.Sprintf("%s%s/%04x/", prefix, importHost, level)) |
| + } |
| + } |
| + if !reflect.DeepEqual(got, want) { |
| + t.Errorf("import begin retained rail = %v, want %v", got, want) |
| + } |
| + } else { |
| + tr.noData(t) |
| + } |
| + if !reflect.DeepEqual(before, bucketState(t, f.fx.rel)) { |
| + t.Error("refused import changed control storage") |
| + } |
| + }) |
| + } |
| + t.Run("resident rootfs", func(t *testing.T) { |
| + fx, token := unplacedRootfsApp(t) |
| + fx.srv.cfg.DeploymentSingleTenant = true |
| + fx.srv.cfg.HomeRegion, fx.srv.cfg.HARegions = "ie", []string{"ie"} |
| + writeFleetBox(t, fx, "", "box-1", "ie", true, time.Now()) |
| + tr := hap2S3(t, fx) |
| + code, out := createShop(t, fx, token, `{"host":"shop.example.com"}`) |
| + if code != 201 { |
| + t.Fatalf("resident rootfs = %d %s", code, out) |
| + } |
| + rec, _ := storedShop(t, fx, rootfsShopHost) |
| + if rec.Runtime != provision.RuntimeRootfsV1 || rec.Hibernate == nil || *rec.Hibernate || rec.HA { |
| + t.Errorf("rootfs exclusion = %+v", rec) |
| + } |
| + for _, a := range tr.all() { |
| + if a.bucket == "data-test" && a.query.Get("max-keys") == "1" { |
| + t.Error("rootfs entered HA prefix proof") |
| + } |
| + } |
| + }) |
| + t.Run("first-domain static", func(t *testing.T) { |
| + fx, token := instanceFixture(t, "hap2-static") |
| + shipVersion(t, fx, "titogo", "v1") |
| + seedChannel(t, fx, "titogo", "canary/live", "v1") |
| + fx.srv.cfg.HomeRegion, fx.srv.cfg.HARegions = "ie", []string{"ie"} |
| + tr := hap2S3(t, fx) |
| + code, _, out := rawReq(t, http.MethodPost, domainsURL(fx, fx.sqid, "titogo"), token, `{"host":"site.example.com","kind":"static"}`) |
| + if code != 200 && code != 201 { |
| + t.Fatalf("first-domain static = %d %s", code, out) |
| + } |
| + rec, err := provision.GetRecord(context.Background(), fx.rel, fx.sqid, "titogo", "site.example.com") |
| + if err != nil || rec.KindOrDefault() != "static" || rec.HA { |
| + t.Fatalf("static exclusion = %+v, %v", rec, err) |
| + } |
| + for _, a := range tr.all() { |
| + if a.bucket == "data-test" { |
| + t.Errorf("static data request: %+v", a) |
| + } |
| + } |
| + }) |
| +} |
| + |
| +func TestHAP2RequestBudget(t *testing.T) { |
| + for _, mode := range []string{"HA home", "HA explicit home", "legacy", "sidecar", "object", "list error", "list retry", "staging", "remote", "pool"} { |
| + t.Run(mode, func(t *testing.T) { |
| + fx := haCreateHome(t) |
| + body, host, u := leftoverDoors[0].body, leftoverDoors[0].host, leftoverDoors[0].url(fx.consoleFixture) |
| + keys := []string(nil) |
| + wantStatus, levels, proof := 201, 10, true |
| + switch mode { |
| + case "HA home": |
| + body = `{"host":"` + host + `"}` |
| + case "legacy": |
| + fx.srv.cfg.HARegions = nil |
| + proof = false |
| + case "sidecar": |
| + body, host, u = leftoverDoors[1].body, leftoverDoors[1].host, leftoverDoors[1].url(fx.consoleFixture) |
| + m := titoSidecarManifest() |
| + m.Pool = "" |
| + m.Regions = nil |
| + enableSidecars(t, fx.consoleFixture, "titogo", m) |
| + proof = false |
| + case "object": |
| + keys = []string{"instances/" + host + "/lock.json"} |
| + wantStatus, levels = 409, 0 |
| + case "list error": |
| + wantStatus, levels = 503, 0 |
| + case "staging": |
| + keys = []string{ltxKey("moves/"+host, 9)} |
| + wantStatus = 409 |
| + case "remote": |
| + fx.srv.cfg.HARegions = []string{"ie", "uk"} |
| + body = `{"host":"` + host + `","region":"uk"}` |
| + wantStatus, levels, proof = 409, 0, false |
| + case "pool": |
| + m := titoManifest() |
| + m.Regions = nil |
| + enableInstances(t, fx.consoleFixture, "titogo", m) |
| + wantStatus, levels, proof = 409, 0, false |
| + } |
| + tr := hap2S3(t, fx.consoleFixture, keys...) |
| + if mode == "list error" { |
| + tr.setFailure(func(a hap2Attempt) bool { return a.bucket == "data-test" }) |
| + } |
| + if mode == "list retry" { |
| + attempts := 0 |
| + tr.setFailure(func(a hap2Attempt) bool { |
| + if a.bucket != "data-test" { |
| + return false |
| + } |
| + attempts++ |
| + return attempts == 1 |
| + }) |
| + tr.mu.Lock() |
| + tr.failStatus = http.StatusServiceUnavailable |
| + tr.mu.Unlock() |
| + data := fx.srv.cfg.SeedData.(*release.S3Store) |
| + data.Client = awss3.New(data.Client.Options(), func(o *awss3.Options) { |
| + o.Retryer = retry.NewStandard(func(o *retry.StandardOptions) { |
| + o.MaxAttempts = 2 |
| + o.Backoff = retry.BackoffDelayerFunc(func(int, error) (time.Duration, error) { return 0, nil }) |
| + }) |
| + }) |
| + } |
| + code, _, out := rawReq(t, http.MethodPost, u, fx.token, body) |
| + if code != wantStatus { |
| + t.Errorf("create = %d %s, want %d", code, out, wantStatus) |
| + } |
| + if wantStatus != 201 { |
| + tr.noWrites(t) |
| + } else { |
| + if mode == "sidecar" { |
| + rec, err := provision.GetSidecarRecord(context.Background(), fx.rel, "bab", "titogo", "bot") |
| + if err != nil || rec == nil { |
| + t.Fatalf("created sidecar: %+v, %v", rec, err) |
| + } |
| + raw, _, err := fx.rel.Get(context.Background(), leftoverDoors[1].recordKey()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var fields map[string]json.RawMessage |
| + if err := json.Unmarshal(raw, &fields); err != nil { |
| + t.Fatal(err) |
| + } |
| + if fields["ha"] != nil { |
| + t.Error("standalone sidecar was stamped HA") |
| + } |
| + } else { |
| + rec, err := provision.GetRecord(context.Background(), fx.rel, "bab", "titogo", host) |
| + if err != nil || rec == nil { |
| + t.Fatalf("created record: %+v, %v", rec, err) |
| + } |
| + if rec.HA != proof { |
| + t.Errorf("HA stamp=%v; want %v", rec.HA, proof) |
| + } |
| + } |
| + } |
| + var got []string |
| + existence, dataGets, writes, beforeProof := 0, 0, 0, 0 |
| + dataStarted := false |
| + recordKey := provision.RecordKey("bab", "titogo", host) |
| + if mode == "sidecar" { |
| + recordKey = leftoverDoors[1].recordKey() |
| + } |
| + for _, a := range tr.all() { |
| + if a.bucket == "carlos-fixture" && a.key == recordKey && a.method == "GET" { |
| + existence++ |
| + if !dataStarted { |
| + beforeProof++ |
| + } |
| + } |
| + if a.method != "GET" && a.method != "HEAD" { |
| + writes++ |
| + } |
| + if a.bucket != "data-test" { |
| + continue |
| + } |
| + if a.method != "GET" || a.query.Get("list-type") != "2" { |
| + dataGets++ |
| + continue |
| + } |
| + dataStarted = true |
| + got = append(got, a.query.Get("prefix")) |
| + if a.query.Get("continuation-token") != "" || a.query.Get("start-after") != "" { |
| + t.Errorf("unexpected pagination: %+v", a) |
| + } |
| + if a.query.Get("prefix") == "instances/"+host+"/" && a.query.Get("max-keys") != "1" { |
| + t.Errorf("unbounded proof: %+v", a) |
| + } |
| + } |
| + var want []string |
| + if proof { |
| + want = append(want, "instances/"+host+"/") |
| + if mode == "list retry" { |
| + want = append(want, "instances/"+host+"/") |
| + } |
| + } |
| + if mode == "legacy" || mode == "sidecar" { |
| + for level := 0; level < 10; level++ { |
| + want = append(want, fmt.Sprintf("instances/%s/%04x/", host, level)) |
| + } |
| + } |
| + for level := 0; level < levels; level++ { |
| + want = append(want, fmt.Sprintf("moves/%s/%04x/", host, level)) |
| + } |
| + if !reflect.DeepEqual(got, want) || dataGets != 0 { |
| + t.Errorf("data HTTP attempts=%v + %d non-LIST; want %v", got, dataGets, want) |
| + } |
| + // On refusal the reused existence read is exactly one; successful writes |
| + // may reread the record under the existing writer's locks. |
| + if proof && beforeProof != 1 { |
| + t.Errorf("record GETs before prefix proof=%d, want 1", beforeProof) |
| + } |
| + if code != 201 && existence != 1 { |
| + t.Errorf("existence GET attempts=%d, want 1", existence) |
| + } |
| + if code == 201 && writes == 0 { |
| + t.Error("successful create did not reach S3 writes") |
| + } |
| + if wantStatus == 201 { |
| + tr.reset() |
| + code, _, out = rawReq(t, http.MethodPost, u, fx.token, body) |
| + want := "instance " + host + " already exists" |
| + if mode == "sidecar" { |
| + want = "sidecar bot already exists" |
| + } |
| + hap2Refusal(t, tr, code, out, 409, want) |
| + n := 0 |
| + for _, a := range tr.all() { |
| + if a.bucket == "data-test" { |
| + t.Errorf("duplicate inspected data: %+v", a) |
| + } |
| + if a.key == recordKey && a.method == "GET" { |
| + n++ |
| + } |
| + } |
| + if n != 1 { |
| + t.Errorf("duplicate record GETs=%d, want 1", n) |
| + } |
| + } |
| + }) |
| + } |
| + t.Run("ingress zero fold and target memo", func(t *testing.T) { |
| + w := newRegionalWorld(t, "hap2-budget-ingress") |
| + w.putRegionalWorldClaim(t, time.Now()) |
| + sibling := *w.claim(t, rdHost) |
| + sibling.Host = "second.example.com" |
| + if changed, err := domains.SetClaimPlacements(context.Background(), w.fx.rel, sibling, nil, ""); err != nil || !changed { |
| + t.Fatalf("sibling claim: %v", err) |
| + } |
| + attachShopClaim(t, w.fx, w.fx.sqid, "shop", "plain.example.com", rdIE) |
| + w.fx.srv.cfg.RegionIngress = []string{ingressIP} |
| + tr := hap2S3(t, w.fx) |
| + w.fx.srv.dnsTargets(context.Background()) |
| + if len(tr.all()) != 0 { |
| + t.Errorf("configured DNS selection used S3: %+v", tr.all()) |
| + } |
| + for _, failed := range []bool{false, true} { |
| + tr.setFailure(func(a hap2Attempt) bool { return failed && a.key == provision.RecordKey(w.fx.sqid, "shop", rdUK) }) |
| + for _, path := range []string{domainsURL(w.fx, w.fx.sqid, "shop"), domainGetURL(w.fx, w.fx.sqid, "shop", rdHost), domainGetURL(w.fx, w.fx.sqid, "shop", "plain.example.com"), placementsURL(w.fx, "shop", rdHost)} { |
| + tr.reset() |
| + code, _, out := rawReq(t, http.MethodGet, path, w.token, "") |
| + wantStatus := 200 |
| + if failed && strings.HasSuffix(path, "/placements") { |
| + wantStatus = 503 |
| + } |
| + if code != wantStatus { |
| + t.Fatalf("ingress read = %d %s", code, out) |
| + } |
| + reads := map[string]int{} |
| + for _, a := range tr.all() { |
| + if !strings.HasSuffix(path, "/placements") && (strings.HasPrefix(a.key, "control/edges/") || strings.HasPrefix(a.query.Get("prefix"), "control/edges/")) { |
| + t.Errorf("configured DNS folded edges: %+v", a) |
| + } |
| + if a.key == provision.RecordKey(w.fx.sqid, "shop", rdIE) || a.key == provision.RecordKey(w.fx.sqid, "shop", rdUK) { |
| + reads[a.key]++ |
| + } |
| + } |
| + single := strings.HasSuffix(path, "/plain.example.com") |
| + if single && len(reads) != 0 { |
| + t.Errorf("single domain added target reads: %v", reads) |
| + } |
| + if !single && len(reads) != 2 { |
| + t.Errorf("regional target reads=%v, want both targets", reads) |
| + } |
| + for key, n := range reads { |
| + if n != 1 { |
| + t.Errorf("target %s GETs=%d, want 1", key, n) |
| + } |
| + } |
| + tr.noWrites(t) |
| + } |
| + } |
| + }) |
| +} |
diff --git a/internal/console/regioningress_test.go b/internal/console/regioningress_test.go| index 4d769b8e3..d165bbfaa 100644 |
| --- a/internal/console/regioningress_test.go |
| +++ b/internal/console/regioningress_test.go |
| @@ -253,7 +253,7 @@ func TestConfiguredIngressDeletedTarget(t *testing.T) { |
| for _, active := range []bool{false, true} { |
| t.Run(map[bool]string{false: "inactive", true: "active"}[active], func(t *testing.T) { |
| w := newRegionalWorld(t, "ingress-deleted") |
| - w.putRegionalWorldClaim(t, time.Now()) |
| + w.putRegionalWorldClaim(t, time.Now().Add(-time.Minute)) |
| w.fx.srv.cfg.RegionIngress = []string{ingressIP} |
| if active { |
| w.fx.srv.cfg.HARegions = []string{"ie"} |