| 1 | package plans |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/carlosframework/platform/internal/domains" |
| 10 | "github.com/carlosframework/platform/internal/release" |
| 11 | ) |
| 12 | |
| 13 | func TestRecordKey(t *testing.T) { |
| 14 | if got := LimitsKey("abc"); got != "accounts/abc/limits.json" { |
| 15 | t.Fatalf("LimitsKey = %q", got) |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | func sampleRecord(sqid string) *Record { |
| 20 | return &Record{ |
| 21 | Account: sqid, |
| 22 | CatalogID: "v1@0123456789ab", |
| 23 | Plan: "free", |
| 24 | Source: "default", |
| 25 | Revision: 1, |
| 26 | Limits: Limits{RunningThings: 3, IdleSeconds: 90, MemoryTier: MemorySmall}, |
| 27 | IssuedAt: time.Unix(1_800_000_000, 0).UTC(), |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func TestRecordRoundTrip(t *testing.T) { |
| 32 | ctx := context.Background() |
| 33 | s := release.NewDirStore(t.TempDir()) |
| 34 | sqid := domains.Sqid(42) |
| 35 | want := sampleRecord(sqid) |
| 36 | etag, err := PutLimits(ctx, s, want, "") |
| 37 | if err != nil || etag == "" { |
| 38 | t.Fatalf("put: %q, %v", etag, err) |
| 39 | } |
| 40 | got, getEtag, err := GetLimits(ctx, s, sqid) |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | if getEtag == "" || got.Schema != RecordSchema { |
| 45 | t.Fatalf("etag %q, schema %d", getEtag, got.Schema) |
| 46 | } |
| 47 | want.Schema = RecordSchema |
| 48 | if *got != *want { |
| 49 | t.Fatalf("got %+v, want %+v", got, want) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestGetRecordAbsent(t *testing.T) { |
| 54 | got, etag, err := GetLimits(context.Background(), release.NewDirStore(t.TempDir()), domains.Sqid(42)) |
| 55 | if got != nil || etag != "" || err != nil { |
| 56 | t.Fatalf("got %+v, %q, %v; want nil, \"\", nil", got, etag, err) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestParseRecordRefuses(t *testing.T) { |
| 61 | cases := map[string]string{ |
| 62 | "schema 2": `{"schema": 2, "account": "abc"}`, |
| 63 | "wrong account": `{"schema": 1, "account": "other"}`, |
| 64 | "malformed": `{"schema": 1,`, |
| 65 | } |
| 66 | for name, body := range cases { |
| 67 | if _, err := ParseLimits([]byte(body), "abc"); err == nil { |
| 68 | t.Errorf("%s: accepted", name) |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestParseRecordIgnoresUnknownFields(t *testing.T) { |
| 74 | r, err := ParseLimits([]byte(`{"schema": 1, "account": "abc", "plan": "free", "throttled": true}`), "abc") |
| 75 | if err != nil || r.Plan != "free" { |
| 76 | t.Fatalf("got %+v, %v", r, err) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func TestPutRecordIsCAS(t *testing.T) { |
| 81 | ctx := context.Background() |
| 82 | s := release.NewDirStore(t.TempDir()) |
| 83 | sqid := domains.Sqid(42) |
| 84 | first, err := PutLimits(ctx, s, sampleRecord(sqid), "") |
| 85 | if err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | if _, err := PutLimits(ctx, s, sampleRecord(sqid), ""); !errors.Is(err, release.ErrCASConflict) { |
| 89 | t.Fatalf("second create-only put: %v", err) |
| 90 | } |
| 91 | r := sampleRecord(sqid) |
| 92 | r.Revision = 2 |
| 93 | if _, err := PutLimits(ctx, s, r, first); err != nil { |
| 94 | t.Fatalf("put with the current etag: %v", err) |
| 95 | } |
| 96 | if _, err := PutLimits(ctx, s, r, first); !errors.Is(err, release.ErrCASConflict) { |
| 97 | t.Fatalf("put with a stale etag: %v", err) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func TestPutRecordRefusesAnInvalidAccount(t *testing.T) { |
| 102 | r := sampleRecord("../escape") |
| 103 | if _, err := PutLimits(context.Background(), release.NewDirStore(t.TempDir()), r, ""); err == nil { |
| 104 | t.Fatal("accepted an invalid account") |
| 105 | } |
| 106 | } |
| 107 | |