carlos / platform Public

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

Plain git — no account needed to clone.

Download

Download this file

1package host
2
3import (
4 "context"
5 "fmt"
6 "testing"
7
8 "github.com/carlosframework/platform/internal/hostbox"
9 "github.com/carlosframework/platform/internal/hostclaim"
10 execlease "github.com/carlosframework/platform/internal/hostexec"
11 "github.com/carlosframework/platform/internal/hostguard"
12 "github.com/carlosframework/platform/internal/provision"
13 "github.com/carlosframework/platform/internal/registry"
14)
15
16func TestPlan14ExecutionPreinstallationAuthority(t *testing.T) {
17 t.Parallel()
18 for _, kind := range []string{hostclaim.KindRelocating, hostclaim.KindLanding, hostclaim.KindMoving} {
19 for _, attack := range []string{"none", "wrong phase", "wrong box", "foreign census", "stale session", "record", "enrollment", "stamp", "incarnation"} {
20 t.Run(kind+"/"+attack, func(t *testing.T) {
21 phase := hostclaim.PhaseInstalling
22 if kind == hostclaim.KindLanding {
23 phase = provision.LandChecking
24 }
25 f, root, iq := plan14InstallRegistration(t, kind, phase)
26 actor, _ := root.InventoryActor(t.Context())
27 if _, err := hostclaim.Update(t.Context(), f.claims, f.read(), func(c *hostclaim.Claim) error {
28 c.Op.Intent["target_enrollment"] = "en"
29 if kind == hostclaim.KindMoving {
30 c.Op.Exec = "box-0"
31 c.Op.Phase = hostclaim.PhaseMoving
32 }
33 if kind == hostclaim.KindRelocating {
34 c.Op.Exec = "box-0"
35 c.Op.Phase = hostclaim.PhaseStarting
36 }
37 return nil
38 }); err != nil {
39 t.Fatal(err)
40 }
41 q := execlease.ExecutionRequest{Schema: 1, ID: "begin-1", Operation: "executionBegin", Host: iq.Target.Host, Incarnation: iq.Target.Incarnation, Stamp: iq.Target.Stamp, Session: iq.Target.Session, Op: iq.Target.Op}
42 switch attack {
43 case "wrong phase":
44 _, err := hostclaim.Update(t.Context(), f.claims, f.read(), func(c *hostclaim.Claim) error { c.Op.Phase = "probing"; c.Op.Exec = "box-1"; return nil })
45 if err != nil {
46 t.Fatal(err)
47 }
48 case "wrong box":
49 actor.Box = "unnamed"
50 case "foreign census":
51 if err := root.Reconcile.Registry.Upsert(registry.Route{Host: q.Host, Account: "foreign", App: "foreign"}); err != nil {
52 t.Fatal(err)
53 }
54 case "stale session":
55 q.Session++
56 case "stamp":
57 q.Stamp++
58 case "incarnation":
59 q.Incarnation = "other"
60 case "enrollment":
61 actor.Enrollment = "other"
62 case "record":
63 raw, etag, err := f.claims.Get(t.Context(), iq.RecordKey)
64 if err != nil {
65 t.Fatal(err)
66 }
67 _ = raw
68 if _, err = f.claims.Put(t.Context(), iq.RecordKey, []byte(`{"schema":1,"host":"other.example.com","account":"bab","app":"jam"}`), etag); err != nil {
69 t.Fatal(err)
70 }
71 }
72 root.InventoryActor = func(context.Context) (hostbox.Actor, error) { return actor, nil }
73 a, err := root.executionAuthority(t.Context(), q, execlease.ControllerProcess{})
74 if attack != "none" {
75 if err == nil {
76 t.Fatalf("admitted %s: %+v", attack, a)
77 }
78 return
79 }
80 if err != nil {
81 t.Fatal(err)
82 }
83 if !a.ParticipationOnly || a.Installation != "" {
84 t.Fatalf("preinstallation gained effects: %+v", a)
85 }
86 if _, err := root.Stop.Coordinator.Guard.Capture(q.Host); err == nil {
87 t.Fatal("participation registered installation")
88 }
89 })
90 }
91 }
92}
93
94func TestPlan14ExecutionPendingRegistration(t *testing.T) {
95 t.Parallel()
96 for _, kind := range []string{hostclaim.KindServing, hostclaim.KindRelocating, hostclaim.KindLanding} {
97 t.Run(kind, func(t *testing.T) {
98 phase := hostclaim.PhaseInstalling
99 if kind == hostclaim.KindLanding {
100 phase = provision.LandChecking
101 }
102 _, root, iq := plan14InstallRegistration(t, kind, phase)
103 store := &plan14RegistrationCrash{rollbackDisk: &rollbackDisk{dir: t.TempDir()}}
104 root.Stop.Coordinator.Guard.Root = hostguard.NewRootAuthority(store)
105 if _, err := root.commitRootInventory(t.Context(), iq); err == nil {
106 t.Fatal("missed registration interruption")
107 }
108 q := execlease.ExecutionRequest{Schema: 1, ID: "pending-registration", Operation: "executionBegin", Host: iq.Target.Host, Incarnation: iq.Target.Incarnation, Stamp: iq.Target.Stamp, Session: iq.Target.Session, Op: iq.Target.Op}
109 a, err := root.executionAuthority(t.Context(), q, execlease.ControllerProcess{})
110 if err != nil || !a.ParticipationOnly || a.Installation == "" {
111 t.Fatalf("pending registration participation: %+v %v", a, err)
112 }
113 if _, err := root.Stop.Coordinator.Guard.Root.View(t.Context(), q.Host, hostguard.Captured{Incarnation: q.Incarnation, Stamp: q.Stamp}); err == nil {
114 t.Fatal("participation admitted installation effects")
115 }
116 q.Session++
117 if _, err := root.executionAuthority(t.Context(), q, execlease.ControllerProcess{}); err == nil {
118 t.Fatal("stale pending registration session admitted")
119 }
120 })
121 }
122}
123
124func TestPlan14ExecutionServingNamesLocalWriter(t *testing.T) {
125 t.Parallel()
126 for _, foreign := range []bool{false, true} {
127 t.Run(fmt.Sprint(foreign), func(t *testing.T) {
128 f, root, iq := plan14InstallRegistration(t, hostclaim.KindServing, "")
129 if _, err := root.commitRootInventory(t.Context(), iq); err != nil {
130 t.Fatal(err)
131 }
132 if foreign {
133 if _, err := hostclaim.Update(t.Context(), f.claims, f.read(), func(c *hostclaim.Claim) error {
134 c.Op.Exec, c.Holder.WriterBox = "box-0", "box-0"
135 return nil
136 }); err != nil {
137 t.Fatal(err)
138 }
139 }
140 q := execlease.ExecutionRequest{Schema: 1, ID: "serving", Operation: "executionView", Host: iq.Target.Host, Incarnation: iq.Target.Incarnation, Stamp: iq.Target.Stamp, Session: iq.Target.Session, Op: iq.Target.Op}
141 _, err := root.executionAuthority(t.Context(), q, execlease.ControllerProcess{})
142 if foreign && err == nil {
143 t.Fatal("retained local installation granted another box's serving session")
144 }
145 if !foreign && err != nil {
146 t.Fatal(err)
147 }
148 })
149 }
150}
151