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 "errors"
6 "github.com/carlosframework/platform/internal/rootparticipation"
7 "go/ast"
8 "go/parser"
9 "go/token"
10 "os"
11 "sync"
12 "testing"
13
14 "github.com/carlosframework/platform/internal/rootstate"
15)
16
17type plan14ReleaseStore struct {
18 rootstate.Store
19 mu sync.Mutex
20 held bool
21 fail error
22}
23
24func (s *plan14ReleaseStore) Lock(ctx context.Context, name string) (func(), error) {
25 if name != "release.lock" {
26 return nil, os.ErrPermission
27 }
28 if s.fail != nil {
29 return nil, s.fail
30 }
31 s.mu.Lock()
32 s.held = true
33 return func() { s.held = false; s.mu.Unlock() }, nil
34}
35func (s *plan14ReleaseStore) Close() error { return nil }
36
37func TestPlan14AdoptReleaseLock(t *testing.T) {
38 s := &plan14ReleaseStore{}
39 called := 0
40 open := func() (rootstate.Store, error) { called++; return s, nil }
41 effects := 0
42 run := func() error {
43 if !s.held {
44 t.Fatal("effect outside release lock")
45 }
46 effects++
47 return nil
48 }
49 if err := withRootReleaseStore(context.Background(), open, run); err != nil {
50 t.Fatal(err)
51 }
52 if effects != 1 || called != 1 {
53 t.Fatal(effects, called)
54 }
55 s.fail = os.ErrPermission
56 if err := withRootReleaseStore(context.Background(), open, run); !errors.Is(err, os.ErrPermission) || effects != 1 {
57 t.Fatal("lock failure admitted effects", err, effects)
58 }
59 if err := withRootReleaseStore(context.Background(), open, run); !errors.Is(err, os.ErrPermission) || effects != 1 {
60 t.Fatal("flag-off lock failure admitted effects", err, effects)
61 }
62 s.fail = nil
63 if err := withRootReleaseStore(context.Background(), open, run); err != nil || effects != 2 || called != 4 {
64 t.Fatal(err, effects)
65 }
66}
67
68func TestPlan14AdoptBodyIsUnderRelease(t *testing.T) {
69 f, err := parser.ParseFile(token.NewFileSet(), "agent.go", nil, 0)
70 if err != nil {
71 t.Fatal(err)
72 }
73 var wrapper *ast.FuncDecl
74 for _, d := range f.Decls {
75 if fn, ok := d.(*ast.FuncDecl); ok && fn.Name.Name == "runAdoptWithEnsureSystemUnits" {
76 wrapper = fn
77 }
78 }
79 if wrapper == nil {
80 t.Fatal("missing adoption entrypoint")
81 }
82 calls, protected := 0, 0
83 ast.Inspect(wrapper, func(n ast.Node) bool {
84 call, ok := n.(*ast.CallExpr)
85 if !ok {
86 return true
87 }
88 id, ok := call.Fun.(*ast.Ident)
89 if !ok {
90 return true
91 }
92 if id.Name == "runAdoptUnderRootRelease" {
93 calls++
94 }
95 if id.Name == "withClaimRootRelease" && len(call.Args) == 5 {
96 fn, ok := call.Args[3].(*ast.FuncLit)
97 if !ok {
98 t.Fatal("release effect is not the adoption callback")
99 }
100 ast.Inspect(fn.Body, func(n ast.Node) bool {
101 if c, ok := n.(*ast.CallExpr); ok {
102 if id, ok := c.Fun.(*ast.Ident); ok && id.Name == "runAdoptUnderRootRelease" {
103 protected++
104 }
105 }
106 return true
107 })
108 }
109 return true
110 })
111 if calls != 1 || protected != 1 {
112 t.Fatalf("adoption calls=%d; under release=%d", calls, protected)
113 }
114}
115
116func TestPlan14AdoptNoCensusDoesNotOpenRelease(t *testing.T) {
117
118 effects := 0
119 for range 2 {
120 err := withClaimReleaseCensus(t.Context(), func() (rootparticipation.Ledger, bool, error) { return rootparticipation.Ledger{}, false, nil }, func() (rootstate.Store, error) {
121 t.Fatal("opened release without census")
122 return nil, os.ErrPermission
123 }, func() error { effects++; return nil })
124 if err != nil {
125 t.Fatal(err)
126 }
127 }
128 if effects != 2 {
129 t.Fatal(effects)
130 }
131}
132
133func TestPlan14ActivationNoCensusDoesNotExclude(t *testing.T) {
134 err := claimActivationReady(t.Context(), func() (rootparticipation.Ledger, bool, error) {
135 return rootparticipation.Ledger{ExcludeLaunches: func(context.Context, func(context.Context) error) error {
136 t.Fatal("excluded without census")
137 return nil
138 }}, false, nil
139 })
140 if !errors.Is(err, rootparticipation.ErrUnavailable) {
141 t.Fatal(err)
142 }
143}
144
145func noCensusAdoptSource(open func() (rootstate.Store, error)) adoptRootSource {
146 return adoptRootSource{OpenRelease: open, Census: func() (rootparticipation.Ledger, bool, error) { return rootparticipation.Ledger{}, false, nil }}
147}
148
149func TestPlan14AdoptRetainedCensusBothFlags(t *testing.T) {
150 for _, flag := range []bool{false, true} {
151 store := &plan14ReleaseStore{}
152 called := false
153 err := withClaimRootRelease(t.Context(), flag, func() (rootstate.Store, error) { return store, nil }, func() error {
154 called = true
155 if !store.held {
156 t.Fatal("retained census effect outside release")
157 }
158 return nil
159 }, func() (rootparticipation.Ledger, bool, error) { return rootparticipation.Ledger{}, true, nil })
160 if err != nil || !called {
161 t.Fatal(err, called)
162 }
163 }
164}
165