carlos / platform Public

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

Plain git — no account needed to clone.

Download

Download this file

1//go:build linux
2
3package host
4
5import (
6 "context"
7 "encoding/json"
8 "os"
9 "path/filepath"
10 "testing"
11
12 "github.com/carlosframework/platform/internal/hostbox"
13 "github.com/carlosframework/platform/internal/hostclaim"
14 execlease "github.com/carlosframework/platform/internal/hostexec"
15 "github.com/carlosframework/platform/internal/hostguard"
16 "github.com/carlosframework/platform/internal/provision"
17)
18
19func TestPlan14CreateParticipationBeforeInstallation(t *testing.T) {
20 t.Parallel()
21 f, root, _ := plan14InstallRegistration(t, hostclaim.KindServing, "")
22 before := f.read()
23 h := before.Claim.Holder
24 rec := provision.Record{Schema: 1, Host: before.Host, Account: h.Account, App: h.App, Channel: "stable", Region: "ie", CreatedAt: f.now}
25 body, err := json.Marshal(rec)
26 if err != nil {
27 t.Fatal(err)
28 }
29 created, err := hostclaim.Update(t.Context(), f.claims, before, func(c *hostclaim.Claim) error {
30 *c = hostclaim.Claim{V: c.V, Host: c.Host, Gen: c.Gen}
31 return hostclaim.BeginSelectedCreate(hostclaim.NewHolder{Host: rec.Host, Account: rec.Account, App: rec.App, Family: "instance", Key: h.Key, Region: "ie", RecordRegion: "ie", CreatedAt: rec.CreatedAt}, body, "create", "test", f.now, "box-1", "en", "app-id", 1, "own", "record")(c)
32 })
33 if err != nil {
34 t.Fatal(err)
35 }
36 q := execlease.ExecutionRequest{Schema: 1, ID: "begin", Operation: "executionBegin", Host: created.Host, Incarnation: created.Claim.Holder.Incarnation, Op: created.Claim.Op.ID}
37 if !q.Valid() {
38 t.Fatal("create participation request refused")
39 }
40 a, err := root.executionAuthority(t.Context(), q, execlease.ControllerProcess{})
41 if err != nil || !a.ParticipationOnly || a.Installation != "" || a.Kind != hostclaim.KindCreating {
42 t.Fatalf("create authority: %+v %v", a, err)
43 }
44 if _, err = root.Stop.Coordinator.Guard.Root.View(t.Context(), q.Host, hostguard.Captured{Incarnation: q.Incarnation}); err == nil {
45 t.Fatal("participation installed root authority")
46 }
47 for _, attack := range []string{"session", "stamp", "operation", "enrollment", "artifact"} {
48 t.Run(attack, func(t *testing.T) {
49 request, handler := q, root
50 switch attack {
51 case "session":
52 request.Session = 1
53 case "stamp":
54 request.Stamp = 1
55 case "operation":
56 request.Op = "other"
57 case "enrollment":
58 actor, e := root.InventoryActor(t.Context())
59 if e != nil {
60 t.Fatal(e)
61 }
62 actor.Enrollment = "foreign"
63 handler.InventoryActor = func(context.Context) (hostbox.Actor, error) { return actor, nil }
64 case "artifact":
65 path := filepath.Join(root.Roots["data"], "unaccounted")
66 if err := os.WriteFile(path, []byte("old state"), 0600); err != nil {
67 t.Fatal(err)
68 }
69 defer os.Remove(path)
70 }
71 if _, err := handler.executionAuthority(t.Context(), request, execlease.ControllerProcess{}); err == nil {
72 t.Fatal("unauthorized create participation")
73 }
74 })
75 }
76}
77
78func TestPlan14CreateAcknowledgementProductionClient(t *testing.T) {
79 f, root, _, _, local := productionExecutionFixture(t, hostclaim.KindLanding)
80 before := f.read()
81 h := before.Claim.Holder
82 rec := provision.Record{Schema: 1, Host: before.Host, Account: h.Account, App: h.App, Channel: "stable", Region: "ie", CreatedAt: f.now}
83 body, err := json.Marshal(rec)
84 if err != nil {
85 t.Fatal(err)
86 }
87 _, err = hostclaim.Update(t.Context(), f.claims, before, func(c *hostclaim.Claim) error {
88 *c = hostclaim.Claim{V: c.V, Host: c.Host, Gen: c.Gen}
89 return hostclaim.BeginSelectedCreate(hostclaim.NewHolder{Host: rec.Host, Account: rec.Account, App: rec.App, Family: "instance", Key: h.Key, Region: "ie", RecordRegion: "ie", CreatedAt: rec.CreatedAt}, body, "create", "test", f.now, "box-1", "en", "app-id", 1, "own", "record")(c)
90 })
91 if err != nil {
92 t.Fatal(err)
93 }
94 actor, err := root.InventoryActor(t.Context())
95 if err != nil {
96 t.Fatal(err)
97 }
98 if err = local.participateHost(t.Context(), actor, rec.Host); err != nil {
99 t.Fatal("participate and acknowledge", err)
100 }
101 observed := f.read()
102 var ack hostclaim.ExecutionAcknowledgement
103 if err = json.Unmarshal([]byte(observed.Claim.Op.Journal["exec_ack."+actor.Box]), &ack); err != nil {
104 t.Fatal("missing production acknowledgement", err)
105 }
106 state, err := local.slots[rec.Host].Controller.Ready(t.Context())
107 if err != nil {
108 t.Fatal(err)
109 }
110 if ack.Op != state.Op || ack.Enrollment != state.Enrollment || ack.LeaseETag != state.LeaseETag || ack.ControllerID != state.ControllerID || ack.BootID != state.BootID || !ack.Expires.Equal(state.Expires) {
111 t.Fatal("acknowledgement differs from root lease", ack, state)
112 }
113 if err = local.participateHost(t.Context(), actor, rec.Host); err != nil {
114 t.Fatal("acknowledgement replay", err)
115 }
116 if got := f.read().Claim.Op.Journal["exec_ack."+actor.Box]; got != observed.Claim.Op.Journal["exec_ack."+actor.Box] {
117 t.Fatal("acknowledgement replaced")
118 }
119 completed, err := hostclaim.Update(t.Context(), f.claims, f.read(), hostclaim.CompleteCreate("create", actor.Box))
120 if err != nil {
121 t.Fatal(err)
122 }
123 if err = local.prepareWith(t.Context(), actor, completed); err != nil {
124 t.Fatal("settle create before installation", err)
125 }
126}
127