carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package hostclaim
2
3import (
4 "errors"
5 "fmt"
6 "strings"
7 "testing"
8 "time"
9)
10
11func TestPlan13ResidueAliasQueue(t *testing.T) {
12 for _, home := range []string{"ie", "", "uk"} {
13 for _, different := range []bool{false, true} {
14 t.Run(home+"/"+map[bool]string{false: "same", true: "different"}[different], func(t *testing.T) {
15 c, scope := parkFixture(t)
16 c.Op.Intent["home_region"] = home
17 inState(c, KindLanding, PhaseProbing)
18 id := plan13IDs()
19 first := scope(c.Holder.Location)
20 if err := QueueResidueParks(moveA, "ie", first, []string{"g1"}, id)(c); err != nil {
21 t.Fatal(err)
22 }
23 second := first
24 if different {
25 second.EvidenceAt = second.EvidenceAt.Add(time.Second)
26 }
27 before := string(mustJSON(t, c))
28 err := QueueResidueParks(moveA, "@home", second, []string{"g1"}, id)(c)
29 if home == "ie" {
30 if different && (!errors.Is(err, ErrWrongState) || !strings.Contains(err.Error(), "another scope")) {
31 t.Fatalf("scope conflict: %v", err)
32 }
33 if !different && err != nil {
34 t.Fatal(err)
35 }
36 if string(mustJSON(t, c)) != before {
37 t.Fatal("alias replay changed claim")
38 }
39 } else if err != nil || len(c.Obligations) != 2 {
40 t.Fatalf("unbound region: %v %+v", err, c.Obligations)
41 }
42 })
43 }
44 }
45}
46
47func TestPlan13ReplicaAliasQueue(t *testing.T) {
48 for _, home := range []string{"ie", "", "uk"} {
49 c, scope := parkFixture(t)
50 c.Op.Intent["home_region"] = home
51 s := scope("moves/" + c.Host + "/" + moveA)
52 id := plan13IDs()
53 if _, err := queueReplicaPark(c, moveA, "ie", s, id); err != nil {
54 t.Fatal(err)
55 }
56 if got := owesStagingPark(c, moveA, "@home"); got != (home == "ie") {
57 t.Fatalf("home %q: staging owed %v", home, got)
58 }
59 if _, err := queueReplicaPark(c, moveA, "@home", s, id); err != nil {
60 t.Fatal(err)
61 }
62 want := 2
63 if home == "ie" {
64 want = 1
65 }
66 if len(c.Obligations) != want {
67 t.Fatalf("home %q: %+v", home, c.Obligations)
68 }
69 }
70}
71
72func plan13IDs() func() string {
73 n := 0
74 return func() string { n++; return fmt.Sprintf("chore%d", n) }
75}
76
77func TestPlan13AbortAndRevertRetainHomeChore(t *testing.T) {
78 for _, action := range []string{"abort", "revert", "revert-replay"} {
79 t.Run(action, func(t *testing.T) {
80 c, scope := parkFixture(t)
81 c.Op.Intent["home_region"], c.Op.Intent["to"] = "ie", "@home"
82 s := scope("moves/" + c.Host + "/" + moveA)
83 id := plan13IDs()
84 if _, err := queueReplicaPark(c, moveA, "ie", s, id); err != nil {
85 t.Fatal(err)
86 }
87 if action == "abort" {
88 c.Op.Phase = PhaseAborting
89 changed := s
90 changed.EvidenceAt = changed.EvidenceAt.Add(time.Second)
91 if err := Abort(moveA, "@home", changed, id)(c); !errors.Is(err, ErrWrongState) {
92 t.Fatalf("abort accepted conflicting alias scope: %v", err)
93 }
94 if err := Abort(moveA, "@home", s, id)(c); err != nil {
95 t.Fatal(err)
96 }
97 } else {
98 c.Op.Exec = ""
99 setJournal(c, JournalEvidenceSource, "evidence")
100 if action == "revert-replay" {
101 c.Op.Kind, c.Op.Exec = KindReverting, "ie-box"
102 setJournal(c, JournalRevPhase, RevPhaseEvidence)
103 }
104 if err := Revert(moveA, "ie-box", "@home", &s, id)(c); err != nil {
105 t.Fatal(err)
106 }
107 }
108 if len(c.Obligations) != 1 || c.Obligations[0].Region != "ie" {
109 t.Fatalf("rewrote or duplicated chore: %+v", c.Obligations)
110 }
111 })
112 }
113}
114
115func TestPlan13ReturnLaneAliasScope(t *testing.T) {
116 c, scope := parkFixture(t)
117 c.Op.Intent["home_region"] = "ie"
118 c.Op.Intent["rollback"] = "rb_abcdefghijklmnopqrstuvwxyz"
119 inState(c, KindRestoring, PhaseProbed)
120 s := scope("moves/" + c.Host + "/" + c.Op.Intent["rollback"])
121 id := plan13IDs()
122 if _, err := queueReplicaPark(c, moveA, "ie", s, id); err != nil {
123 t.Fatal(err)
124 }
125 setJournal(c, journalReturnLane, s.Location)
126 changed := s
127 changed.EvidenceAt = changed.EvidenceAt.Add(time.Second)
128 if err := QueueReturnLanePark(moveA, "@home", changed, id)(c); !errors.Is(err, ErrWrongState) {
129 t.Fatalf("journal masked conflicting alias chore: %v", err)
130 }
131 if err := QueueReturnLanePark(moveA, "@home", s, id)(c); err != nil {
132 t.Fatal(err)
133 }
134 if len(c.Obligations) != 1 || c.Obligations[0].Region != "ie" {
135 t.Fatal(c.Obligations)
136 }
137}
138