| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/carlosframework/platform/internal/domains" |
| 11 | "github.com/carlosframework/platform/internal/hostclaim" |
| 12 | "github.com/carlosframework/platform/internal/provision" |
| 13 | "github.com/carlosframework/platform/internal/release" |
| 14 | ) |
| 15 | |
| 16 | // TestRelocationRecordCarriesTheAppUpdatePolicy: the route a relocation |
| 17 | // mints carries the app's update policy as the ordinary mint's does |
| 18 | // (settings spec §3.2). With no manifest the instance keeps today's |
| 19 | // default; with a manual default it lands manual; and a manifest that |
| 20 | // cannot be read refuses rather than minting the instance as auto. |
| 21 | func TestRelocationRecordCarriesTheAppUpdatePolicy(t *testing.T) { |
| 22 | ctx := context.Background() |
| 23 | acct, app, host := domains.Sqid(12), "shop", "shop.example.com" |
| 24 | rec := provision.Record{Host: host, App: app, Account: acct, Channel: "stable"} |
| 25 | key := provision.RecordKey(acct, app, host) |
| 26 | body, err := json.Marshal(rec) |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | obs := hostclaim.Observed{Host: host, Claim: &hostclaim.Claim{Holder: &hostclaim.Holder{Key: key}}} |
| 31 | store := func(t *testing.T, manifest *provision.Manifest) release.Store { |
| 32 | s := release.NewDirStore(t.TempDir()) |
| 33 | if _, err := s.Put(ctx, key, body, ""); err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | if manifest != nil { |
| 37 | if err := provision.PutManifest(ctx, s, acct, app, *manifest); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | } |
| 41 | return s |
| 42 | } |
| 43 | dataDir := t.TempDir() |
| 44 | |
| 45 | t.Run("no manifest", func(t *testing.T) { |
| 46 | got, err := relocationRecord(ctx, dataDir, store(t, nil), obs) |
| 47 | if err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | if got.Route.UpdateSettings != "" { |
| 51 | t.Fatalf("update settings %q, want today's default", got.Route.UpdateSettings) |
| 52 | } |
| 53 | }) |
| 54 | t.Run("manual default", func(t *testing.T) { |
| 55 | got, err := relocationRecord(ctx, dataDir, store(t, &provision.Manifest{Updates: &provision.UpdatePolicy{DefaultUpdates: release.UpdatesManual}}), obs) |
| 56 | if err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | want := provision.EffectiveUpdates(rec, &provision.UpdatePolicy{DefaultUpdates: release.UpdatesManual}).Column() |
| 60 | if want == "" || got.Route.UpdateSettings != want { |
| 61 | t.Fatalf("update settings %q, want %q", got.Route.UpdateSettings, want) |
| 62 | } |
| 63 | if !strings.Contains(got.Route.UpdateSettings, release.UpdatesManual) { |
| 64 | t.Fatalf("update settings %q do not say manual", got.Route.UpdateSettings) |
| 65 | } |
| 66 | }) |
| 67 | t.Run("manifest unreadable", func(t *testing.T) { |
| 68 | boom := errors.New("bucket down") |
| 69 | s := failingGetStore{Store: store(t, &provision.Manifest{}), bad: provision.ManifestKey(acct, app), err: boom} |
| 70 | if _, err := relocationRecord(ctx, dataDir, s, obs); !errors.Is(err, boom) { |
| 71 | t.Fatalf("err = %v, want the manifest read's failure", err) |
| 72 | } |
| 73 | }) |
| 74 | } |
| 75 | |