package moves import ( "strings" "testing" "time" "github.com/carlosframework/platform/internal/hostclaim" ) // Every scope helper's EvidenceAt is the bound it was handed, never the // clock (claim rev 6.23 ยง2, INV-22): a bound years from now and one years // ago, in a zone that is not UTC, both come back as the same instant in UTC. // Each scope is a valid park for the host, names the incarnation it was // given, and carries the staging request limit. func TestScopeHelpersUseTheGenuineBound(t *testing.T) { const host = "shop.bab.example.com" inc := strings.Repeat("ab", 32) zone := time.FixedZone("plus-one", 3600) for _, bound := range []time.Time{ time.Date(2031, 3, 4, 5, 6, 7, 8, zone), time.Date(2001, 9, 10, 11, 12, 13, 14, zone), } { for _, c := range []struct { name string got hostclaim.ParkScope location string }{ {"source location, holder with a slash", SourceLocationScope(hostclaim.Holder{Location: "instances/" + host + "/" + inc + "/", Incarnation: inc}, bound), "instances/" + host + "/" + inc}, {"source location, holder without one", SourceLocationScope(hostclaim.Holder{Location: "instances/" + host, Incarnation: inc}, bound), "instances/" + host}, {"target location, holder with a slash", TargetLocationScope(hostclaim.Holder{Location: "instances/" + host + "/" + inc + "/", Incarnation: inc}, bound), "instances/" + host + "/" + inc}, {"target location, holder without one", TargetLocationScope(hostclaim.Holder{Location: "instances/" + host, Incarnation: inc}, bound), "instances/" + host}, {"residue, holder with a slash", ResidueScope(hostclaim.Holder{Location: "instances/" + host + "/" + inc + "/", Incarnation: inc}, bound), "instances/" + host + "/" + inc}, {"residue, holder without one", ResidueScope(hostclaim.Holder{Location: "instances/" + host, Incarnation: inc}, bound), "instances/" + host}, {"staging", StagingScope(host, "move-1", inc, bound), "moves/" + host + "/move-1"}, {"abort", AbortScope(host, "move-1", inc, bound), "moves/" + host + "/move-1"}, {"return lane", ReturnLaneScope(host, "rb-1", inc, bound), "moves/" + host + "/rb-1"}, } { t.Run(c.name+"/"+bound.Format("2006"), func(t *testing.T) { if !c.got.EvidenceAt.Equal(bound) || c.got.EvidenceAt.Location() != time.UTC { t.Fatalf("EvidenceAt = %v, want %v in UTC", c.got.EvidenceAt, bound) } if c.got.RequestLimit != 120*time.Second { t.Fatalf("RequestLimit = %v, want 120s", c.got.RequestLimit) } if c.got.Location != c.location || c.got.Incarnation != inc { t.Fatalf("scope = %q/%q, want %q/%q", c.got.Location, c.got.Incarnation, c.location, inc) } if err := c.got.Validate(host); err != nil { t.Fatalf("Validate(%q): %v", host, err) } }) } } if zero := StagingScope(host, "move-1", inc, time.Time{}); zero.Validate(host) == nil { t.Fatal("a zero bound made a valid scope; the helper must not fill one in") } } // CommitScopes (Plan 5 Task 3, K9) is the one builder of a commit's two // parks, for the box and the console alike: the source's location from the // flip (SourceLocationScope) and the move's lane to the seal's bound // (StagingScope), read from the claim and the record's FlippedAt only. It // refuses without the flip's time, without a holder, and without a seal. func TestCommitScopes(t *testing.T) { const host = "shop.bab.example.com" inc := strings.Repeat("ab", 32) seal := sealForTest() flipped := seal.SealedAt.Add(40 * time.Second) c := &hostclaim.Claim{Host: host, Holder: &hostclaim.Holder{Location: "instances/" + host + "/" + inc + "/", Incarnation: inc}, Op: hostclaim.Op{ID: "move-1", Kind: hostclaim.KindLanding, Journal: map[string]string{hostclaim.JournalSeal: seal.Encode()}}} source, staging, err := CommitScopes(c, &flipped) if err != nil { t.Fatal(err) } if want := SourceLocationScope(*c.Holder, flipped); source != want { t.Fatalf("source %+v, want %+v", source, want) } if want := StagingScope(host, "move-1", inc, seal.Bound()); staging != want { t.Fatalf("staging %+v, want %+v", staging, want) } for name, edit := range map[string]func(c *hostclaim.Claim) *time.Time{ "no flip": func(*hostclaim.Claim) *time.Time { return nil }, "no holder": func(c *hostclaim.Claim) *time.Time { c.Holder = nil; return &flipped }, "no seal": func(c *hostclaim.Claim) *time.Time { delete(c.Op.Journal, hostclaim.JournalSeal); return &flipped }, "no claim": nil, } { t.Run(name, func(t *testing.T) { cc := *c cc.Op.Journal = map[string]string{hostclaim.JournalSeal: seal.Encode()} if edit == nil { if _, _, err := CommitScopes(nil, &flipped); err == nil { t.Fatal("scopes without a claim") } return } at := edit(&cc) if _, _, err := CommitScopes(&cc, at); err == nil { t.Fatal("scopes built") } }) } }