| 1 | package hostbox |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "github.com/carlosframework/platform/internal/hostguard" |
| 6 | "testing" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | func TestPlan14InventoryCarriesCapturedRevision(t *testing.T) { |
| 11 | calls := 0 |
| 12 | _, db := RemoteJournals(func(_ context.Context, q JournalRequest) (JournalResponse, error) { |
| 13 | calls++ |
| 14 | if q.Op != "inventory-write" || q.Revision != 7 || q.RecordKey != "record" || q.RecordETag != "etag" || q.Target.ClaimETag != "claim" { |
| 15 | t.Fatalf("uncaptured inventory request: %+v", q) |
| 16 | } |
| 17 | return JournalResponse{V: 1, ID: q.ID, Revision: 8}, nil |
| 18 | }) |
| 19 | expected := InventoryExpectation{Revision: 7, RecordKey: "record", RecordETag: "etag", Target: hostguard.FenceTarget{Host: "shop.example", Incarnation: "inc", Stamp: 1, ClaimETag: "claim"}} |
| 20 | revision, err := db.WriteExpected(t.Context(), StopInventory{Host: "shop.example", Incarnation: "inc", InstallationStamp: 1}, expected) |
| 21 | if err != nil || revision != 8 || calls != 1 { |
| 22 | t.Fatalf("inventory commit: %d %d %v", revision, calls, err) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | func TestPlan14InventoryIdentityCoversRecordExpectations(t *testing.T) { |
| 27 | q, err := journalPrepare(JournalRequest{Op: "inventory-write", RecordKey: "record", RecordETag: "etag"}, time.Now()) |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | if q.V != 3 { |
| 32 | t.Fatalf("inventory expectations use protocol %d", q.V) |
| 33 | } |
| 34 | for _, field := range []string{"key", "etag"} { |
| 35 | changed := q |
| 36 | if field == "key" { |
| 37 | changed.RecordKey = "other" |
| 38 | } else { |
| 39 | changed.RecordETag = "other" |
| 40 | } |
| 41 | if validateJournalRequest(changed) == nil { |
| 42 | t.Fatal("record expectation not bound to request identity") |
| 43 | } |
| 44 | } |
| 45 | q.V = 2 |
| 46 | q.ID = journalRequestID(q) |
| 47 | if validateJournalRequest(q) == nil { |
| 48 | t.Fatal("old protocol accepted unhashed record expectations") |
| 49 | } |
| 50 | } |
| 51 | |