| 1 | package provision |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/carlosframework/platform/internal/release" |
| 9 | ) |
| 10 | |
| 11 | func TestPlan14ProofRecordLookup(t *testing.T) { |
| 12 | ctx := context.Background() |
| 13 | s := release.NewDirStore(t.TempDir()) |
| 14 | rec := Record{Schema: 1, Host: "console.example.com", Account: "bab", App: "console", Addr: "127.0.0.1:4000", Unit: "console.service"} |
| 15 | raw, err := json.Marshal(rec) |
| 16 | if err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | key := RecordKey(rec.Account, rec.App, rec.Host) |
| 20 | etag, err := s.Put(ctx, key, raw, "") |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | got, tag, err := FindRecordByHost(ctx, s, rec.Host) |
| 25 | if err != nil || got.Account != rec.Account || got.App != rec.App || tag != etag { |
| 26 | t.Fatalf("lookup: %+v, %q, %v", got, tag, err) |
| 27 | } |
| 28 | if _, _, err := FindRecordByHost(ctx, s, "missing.example.com"); err == nil { |
| 29 | t.Fatal("missing record bound") |
| 30 | } |
| 31 | duplicate := RecordKey("bab", "other", rec.Host) |
| 32 | if _, err := s.Put(ctx, duplicate, []byte("malformed"), ""); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | if _, _, err := FindRecordByHost(ctx, s, rec.Host); err == nil { |
| 36 | t.Fatal("malformed competing key was ignored") |
| 37 | } |
| 38 | if err := s.Delete(ctx, duplicate); err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | rec.Account = "bap" |
| 42 | raw, err = json.Marshal(rec) |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | if _, err := s.Put(ctx, key, raw, etag); err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | if _, _, err := FindRecordByHost(ctx, s, rec.Host); err == nil { |
| 50 | t.Fatal("key/body mismatch bound") |
| 51 | } |
| 52 | } |
| 53 | |