| new file mode 100644 |
| index 000000000..7517afbd2 |
| --- /dev/null |
| +++ b/cli_add_regional_test.go |
| @@ -0,0 +1,158 @@ |
| +package main |
| + |
| +import ( |
| + "context" |
| + "os" |
| + "os/exec" |
| + "path/filepath" |
| + "strings" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/domains" |
| + "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// W12: a root `carlos add` of a host a regional claim holds is refused, |
| +// whether the host is the claim's own (X) or a wildcard under it (*.X). The |
| +// claim is read as ONE object through the deployment-store seam. When it |
| +// cannot be read (no store, or a read that fails other than not-found) the |
| +// verb prints the skip line and proceeds; the edge's Reload deny is the |
| +// backstop. |
| +func TestAddRefusesARegionalHost(t *testing.T) { |
| + bin := buildCarlos(t) |
| + storeDir := t.TempDir() |
| + _, pubB64, err := release.GenerateKey() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + store := release.NewDirStore(storeDir) |
| + const claimed = "shop.example.com" |
| + if _, err := domains.SetClaimPlacements(context.Background(), store, domains.Claim{ |
| + Schema: 2, Host: claimed, Account: "bab", App: "shop", |
| + Route: "shop-ie.example.net", |
| + CreatedAt: "2026-10-29T00:00:00Z", CreatedBy: "owner@example.com", |
| + Placements: []domains.Placement{ |
| + {Region: "", Host: "shop-ie.example.net"}, |
| + {Region: "uk", Host: "shop-uk.example.net"}, |
| + }, |
| + }, nil, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + scratch := t.TempDir() |
| + regPath := filepath.Join(t.TempDir(), "registry.db") |
| + run := func(extraEnv []string, host string) (string, error) { |
| + t.Helper() |
| + cmd := exec.Command(bin, "add", "-registry", regPath, "-app", "shop", host) |
| + cmd.Env = carlosTestEnv(t, extraEnv...) |
| + // pick() walks cwd ancestors for .carlos/config, so the subprocess must not run from the real checkout. |
| + cmd.Dir = scratch |
| + out, err := cmd.CombinedOutput() |
| + return string(out), err |
| + } |
| + withStore := []string{"CARLOS_DEPLOYMENT_DIR=" + storeDir, "CARLOS_RELEASE_PUBKEY=" + pubB64} |
| + const refusal = "belongs to a regional domain, so it cannot be a route here. detach the domain first." |
| + const skipLine = "could not read the domain claim for " + claimed + ", so the regional domain check was skipped." |
| + |
| + reg, err := registry.Open(regPath) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer reg.Close() |
| + has := func(h string) bool { |
| + t.Helper() |
| + got, err := reg.Get(h) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return got != nil |
| + } |
| + |
| + for _, h := range []string{claimed, "*." + claimed} { |
| + out, err := run(withStore, h) |
| + if err == nil { |
| + t.Fatalf("add %s under a regional claim: want non-zero exit, got 0\n%s", h, out) |
| + } |
| + if !strings.Contains(out, h+" "+refusal) { |
| + t.Errorf("add %s: not the regional refusal:\n%s", h, out) |
| + } |
| + if strings.Contains(out, skipLine) { |
| + t.Errorf("add %s: printed the skip line though the claim was read:\n%s", h, out) |
| + } |
| + if has(h) { |
| + t.Errorf("add %s: refused, yet the registry has a row", h) |
| + } |
| + } |
| + |
| + // Neighbours the claim does not hold: a sub-host (no wildcard claim of its |
| + // own to read) and a wildcard over a parent with no claim. |
| + for _, h := range []string{"alice." + claimed, "*.example.com"} { |
| + out, err := run(withStore, h) |
| + if err != nil { |
| + t.Fatalf("add %s: %v\n%s", h, err, out) |
| + } |
| + if strings.Contains(out, refusal) || strings.Contains(out, "could not read the domain claim") { |
| + t.Errorf("add %s: printed a regional line:\n%s", h, out) |
| + } |
| + if !has(h) { |
| + t.Errorf("add %s: no registry row", h) |
| + } |
| + } |
| + |
| + // Cannot read the claim: no store at all. |
| + out, err := run(nil, claimed) |
| + if err != nil { |
| + t.Fatalf("add %s with no store: %v\n%s", claimed, err, out) |
| + } |
| + if !strings.Contains(out, skipLine) { |
| + t.Errorf("add %s with no store: no skip line:\n%s", claimed, out) |
| + } |
| + if !has(claimed) { |
| + t.Errorf("add %s with no store: no registry row", claimed) |
| + } |
| + if err := reg.Delete(claimed); err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + // Cannot read the claim: the store is there and the object is not readable. |
| + claimFile := filepath.Join(storeDir, domains.ClaimKey(claimed)) |
| + if os.Geteuid() == 0 { |
| + t.Log("root reads past chmod 000; the unreadable-object case uses a malformed claim instead") |
| + if err := os.WriteFile(claimFile, []byte("not json"), 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + } else if err := os.Chmod(claimFile, 0); err != nil { |
| + t.Fatal(err) |
| + } |
| + out, err = run(withStore, claimed) |
| + if err != nil { |
| + t.Fatalf("add %s over an unreadable claim: %v\n%s", claimed, err, out) |
| + } |
| + if !strings.Contains(out, skipLine) { |
| + t.Errorf("add %s over an unreadable claim: no skip line:\n%s", claimed, out) |
| + } |
| + if !has(claimed) { |
| + t.Errorf("add %s over an unreadable claim: no registry row", claimed) |
| + } |
| +} |
| + |
| +// A claim that is not regional (a plain alias claim) does not stop `carlos |
| +// add`: only a regional one does. |
| +func TestAddRegionalCheckPassesAPlainClaim(t *testing.T) { |
| + store := release.NewDirStore(t.TempDir()) |
| + ctx := context.Background() |
| + if _, _, err := domains.AttachClaim(ctx, store, domains.Claim{ |
| + Schema: 1, Host: "shop.example.com", Account: "bab", App: "shop", |
| + Route: "shop-ie.example.net", CreatedAt: "2026-10-29T00:00:00Z", |
| + }); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, h := range []string{"shop.example.com", "*.shop.example.com"} { |
| + if refused, skipped := addRegionalCheck(ctx, store, h); refused || skipped { |
| + t.Errorf("addRegionalCheck(%s) over a plain claim = refused %v, skipped %v, want neither", h, refused, skipped) |
| + } |
| + } |
| + if refused, skipped := addRegionalCheck(ctx, nil, "shop.example.com"); refused || !skipped { |
| + t.Errorf("addRegionalCheck with no store = refused %v, skipped %v, want skipped only", refused, skipped) |
| + } |
| +} |