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 hostexec
4
5import (
6 "context"
7 "encoding/json"
8 "os"
9 "testing"
10
11 "github.com/carlosframework/platform/internal/rootparticipation"
12)
13
14func TestPlan14ExecutionServingView(t *testing.T) {
15 for _, scenario := range []string{"empty", "settled", "active", "lost", "missing census", "incomplete census", "wrong migration", "wrong enrollment", "update", "wrong kind", "participation only", "dead peer"} {
16 t.Run(scenario, func(t *testing.T) {
17 b, disk, remote, _, q, peer := brokerFixture(t)
18 if scenario == "settled" || scenario == "active" || scenario == "lost" {
19 out, err := b.Execute(t.Context(), peer, q)
20 if err != nil {
21 t.Fatal(err)
22 }
23 if scenario == "settled" {
24 settle := q
25 settle.Operation, settle.ID, settle.Token = "executionSettle", "settle", out.Token
26 if _, err := b.Execute(t.Context(), peer, settle); err != nil {
27 t.Fatal(err)
28 }
29 } else if scenario == "lost" {
30 r, _ := b.read(q.Host)
31 if err := b.deny(t.Context(), r, nil); err != nil {
32 t.Fatal(err)
33 }
34 }
35 }
36 census := rootparticipation.Census{Schema: 1, Box: "box", Enrollment: "en", Migration: 1, Revision: 1, LaunchRevision: 1, Complete: true}
37 if scenario == "incomplete census" {
38 census.Complete = false
39 }
40 if scenario == "wrong migration" {
41 census.Migration++
42 }
43 if scenario == "wrong enrollment" {
44 census.Enrollment = "other"
45 }
46 if scenario != "missing census" {
47 raw, _ := json.Marshal(census)
48 if err := disk.Write("census.json", raw); err != nil {
49 t.Fatal(err)
50 }
51 }
52 if scenario == "update" {
53 if err := disk.Write("update.json", []byte(`{}`)); err != nil {
54 t.Fatal(err)
55 }
56 }
57 original := b.cfg.Authority
58 originalQ := q
59 b.cfg.Authority = func(ctx context.Context, _ ExecutionRequest, p ControllerProcess) (ExecutionAuthority, error) {
60 a, err := original(ctx, originalQ, p)
61 a.Kind, a.Phase = "serving", ""
62 if scenario == "wrong kind" {
63 a.Kind = "landing"
64 }
65 if scenario == "participation only" {
66 a.ParticipationOnly = true
67 }
68 return a, err
69 }
70 if scenario == "dead peer" {
71 b.cfg.Alive = func(context.Context, ControllerProcess) error { return os.ErrNotExist }
72 }
73 q.Operation, q.ID, q.Token, q.Op = "executionView", "serving-view", "", ""
74 writes, acquisitions := disk.writes, remote.calls
75 out, err := b.Execute(t.Context(), peer, q)
76 if scenario == "empty" || scenario == "settled" || scenario == "participation only" {
77 if err != nil || out.Status != "serving" || out.Host != q.Host || out.Stamp != q.Stamp || out.Session != q.Session || out.Token != "" || out.SafeUntil != 0 || out.LeaseETag != "" || !out.Expires.IsZero() {
78 t.Fatalf("serving view: %+v %v", out, err)
79 }
80 } else if err == nil {
81 t.Fatalf("%s granted serving: %+v", scenario, out)
82 }
83 if disk.writes != writes || remote.calls != acquisitions {
84 t.Fatal("serving view wrote participation or acquired a lease")
85 }
86 })
87 }
88}
89
90func TestPlan14ExecutionServingCannotAcquire(t *testing.T) {
91 b, _, remote, _, q, peer := brokerFixture(t)
92 original := b.cfg.Authority
93 b.cfg.Authority = func(ctx context.Context, q ExecutionRequest, p ControllerProcess) (ExecutionAuthority, error) {
94 a, err := original(ctx, q, p)
95 a.Kind = "serving"
96 return a, err
97 }
98 if _, err := b.Execute(t.Context(), peer, q); err == nil || remote.calls != 0 {
99 t.Fatalf("serving acquired an operation lease: err=%v acquisitions=%d", err, remote.calls)
100 }
101}
102