package host import ( "context" "encoding/json" "errors" "strings" "testing" "github.com/carlosframework/platform/internal/domains" "github.com/carlosframework/platform/internal/hostclaim" "github.com/carlosframework/platform/internal/provision" "github.com/carlosframework/platform/internal/release" ) // TestRelocationRecordCarriesTheAppUpdatePolicy: the route a relocation // mints carries the app's update policy as the ordinary mint's does // (settings spec ยง3.2). With no manifest the instance keeps today's // default; with a manual default it lands manual; and a manifest that // cannot be read refuses rather than minting the instance as auto. func TestRelocationRecordCarriesTheAppUpdatePolicy(t *testing.T) { ctx := context.Background() acct, app, host := domains.Sqid(12), "shop", "shop.example.com" rec := provision.Record{Host: host, App: app, Account: acct, Channel: "stable"} key := provision.RecordKey(acct, app, host) body, err := json.Marshal(rec) if err != nil { t.Fatal(err) } obs := hostclaim.Observed{Host: host, Claim: &hostclaim.Claim{Holder: &hostclaim.Holder{Key: key}}} store := func(t *testing.T, manifest *provision.Manifest) release.Store { s := release.NewDirStore(t.TempDir()) if _, err := s.Put(ctx, key, body, ""); err != nil { t.Fatal(err) } if manifest != nil { if err := provision.PutManifest(ctx, s, acct, app, *manifest); err != nil { t.Fatal(err) } } return s } dataDir := t.TempDir() t.Run("no manifest", func(t *testing.T) { got, err := relocationRecord(ctx, dataDir, store(t, nil), obs) if err != nil { t.Fatal(err) } if got.Route.UpdateSettings != "" { t.Fatalf("update settings %q, want today's default", got.Route.UpdateSettings) } }) t.Run("manual default", func(t *testing.T) { got, err := relocationRecord(ctx, dataDir, store(t, &provision.Manifest{Updates: &provision.UpdatePolicy{DefaultUpdates: release.UpdatesManual}}), obs) if err != nil { t.Fatal(err) } want := provision.EffectiveUpdates(rec, &provision.UpdatePolicy{DefaultUpdates: release.UpdatesManual}).Column() if want == "" || got.Route.UpdateSettings != want { t.Fatalf("update settings %q, want %q", got.Route.UpdateSettings, want) } if !strings.Contains(got.Route.UpdateSettings, release.UpdatesManual) { t.Fatalf("update settings %q do not say manual", got.Route.UpdateSettings) } }) t.Run("manifest unreadable", func(t *testing.T) { boom := errors.New("bucket down") s := failingGetStore{Store: store(t, &provision.Manifest{}), bad: provision.ManifestKey(acct, app), err: boom} if _, err := relocationRecord(ctx, dataDir, s, obs); !errors.Is(err, boom) { t.Fatalf("err = %v, want the manifest read's failure", err) } }) }