Repository navigation
Expand file tree
/
Copy pathinit_test.go
More file actions
304 lines (270 loc) · 10.9 KB
/
Copy pathinit_test.go
File metadata and controls
304 lines (270 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package main
import (
"context"
"os"
"path/filepath"
"sync"
"testing"
"time"
basev0 "github.com/codefly-dev/core/generated/go/codefly/base/v0"
runtimev0 "github.com/codefly-dev/core/generated/go/codefly/services/runtime/v0"
"github.com/codefly-dev/core/resources"
runners "github.com/codefly-dev/core/runners/base"
"github.com/codefly-dev/core/standards"
"github.com/stretchr/testify/require"
)
const probeProject = `[project]
name = "init-probe"
version = "0.0.0"
`
// isolateWorkingDir moves the test off the checkout. builders.Dependencies
// resolves its hash file relative to the cache location, so a regression that
// empties that location writes uv.hash into the working directory.
func isolateWorkingDir(t *testing.T) {
t.Helper()
t.Chdir(t.TempDir())
}
// initTestRuntime stands a Runtime up at the point Init is called from: a
// native runtime, a source tree holding the files Init hashes, and the REST
// endpoint the proposed mappings resolve.
func initTestRuntime(t *testing.T) (*Runtime, *runtimev0.InitRequest) {
t.Helper()
runtime := NewRuntime(NewService())
runtime.Location = t.TempDir()
runtime.Logger = runtime.Wool
// Init resolves a real endpoint address, but the processes these tests
// start are stand-ins that serve nothing: count them as answering.
runtime.answers = func(context.Context, string) error { return nil }
// Load is what normally supplies these; these tests call Init directly.
runtime.Identity = resources.ServiceIdentityFromProto(&basev0.ServiceIdentity{
Name: "probe",
Module: "mod",
Workspace: "workspace",
WorkspacePath: runtime.Location,
})
runtime.Base.Runtime.RuntimeContext = resources.NewRuntimeContextNative()
source := t.TempDir()
require.NoError(t, os.MkdirAll(filepath.Join(source, "src"), 0o700))
require.NoError(t, os.WriteFile(filepath.Join(source, "pyproject.toml"), []byte(probeProject), 0o600))
require.NoError(t, os.WriteFile(filepath.Join(source, "src", "main.py"), []byte("app = None\n"), 0o600))
require.NoError(t, os.WriteFile(filepath.Join(source, openAPIScript), []byte("# the scaffold's generator\n"), 0o600))
runtime.Service.SourceLocation = source
endpoint := &basev0.Endpoint{Module: "mod", Service: "probe", Name: "rest", Api: standards.REST}
runtime.FastAPI.HTTPEndpoint = endpoint
t.Cleanup(runtime.Base.StopWatcher)
return runtime, &runtimev0.InitRequest{
RuntimeContext: resources.NewRuntimeContextNative(),
ProposedNetworkMappings: []*basev0.NetworkMapping{{
Endpoint: endpoint,
Instances: []*basev0.NetworkInstance{{
Access: resources.NewNativeNetworkAccess(),
Hostname: "localhost",
Port: 9999,
Address: "http://localhost:9999",
}},
}},
}
}
// releasingEnv releases the runtime's runner state the moment Init hands it
// control, standing in for a Stop or Destroy landing in the middle of Init's
// runner section.
type releasingEnv struct {
*fakeRunnerEnvironment
runtime *Runtime
}
func (e *releasingEnv) Init(context.Context) error {
e.runtime.runnerMu.Lock()
defer e.runtime.runnerMu.Unlock()
e.runtime.runnerEnvironment = nil
e.runtime.cacheLocation = ""
return nil
}
// TestInitWorksAgainstTheEnvironmentItCaptured is the contract: Init reads the
// environment and its cache location once and then works against those, so a
// teardown replacing the fields underneath it cannot leave Init dereferencing
// a nil environment or caching against nothing.
func TestInitWorksAgainstTheEnvironmentItCaptured(t *testing.T) {
isolateWorkingDir(t)
ctx := context.Background()
runtime, req := initTestRuntime(t)
env := &releasingEnv{fakeRunnerEnvironment: &fakeRunnerEnvironment{}, runtime: runtime}
cache := t.TempDir()
runtime.runnerEnvironment = env
runtime.cacheLocation = cache
resp, err := runtime.Init(ctx, req)
require.NoError(t, err)
require.Equal(t, runtimev0.InitStatus_READY, resp.GetStatus().GetState(), resp.GetStatus().GetMessage())
var commands [][]string
for _, proc := range env.started() {
commands = append(commands, append([]string{proc.bin}, proc.args...))
}
require.Equal(t, [][]string{
{"uv", "sync"},
{"uv", "run", "python", "src/openapi.py"},
}, commands, "both must run on the captured environment")
entries, err := os.ReadDir(cache)
require.NoError(t, err)
require.NotEmpty(t, entries, "the dependency hashes must land in the captured cache location")
}
// TestInitWithoutAnOpenAPIGeneratorRunsNone is a service whose API is on its
// gRPC and Connect faces alone: its HTTP endpoint serves the probes, and it
// ships no src/openapi.py. Init must make it ready without running one, or a
// fresh checkout of such a service cannot start.
func TestInitWithoutAnOpenAPIGeneratorRunsNone(t *testing.T) {
isolateWorkingDir(t)
ctx := context.Background()
runtime, req := initTestRuntime(t)
require.NoError(t, os.Remove(filepath.Join(runtime.Service.SourceLocation, openAPIScript)))
connect := &basev0.Endpoint{Module: "mod", Service: "probe", Name: "connect", Api: standards.CONNECT}
runtime.FastAPI.Settings.GRPCServer.Enabled = true
runtime.FastAPI.Settings.ConnectEndpoint = true
runtime.FastAPI.ConnectEndpoint = connect
req.ProposedNetworkMappings = append(req.ProposedNetworkMappings, &basev0.NetworkMapping{
Endpoint: connect,
Instances: []*basev0.NetworkInstance{{
Access: resources.NewNativeNetworkAccess(), Hostname: "localhost", Port: 9998, Address: "localhost:9998",
}},
})
env := &fakeRunnerEnvironment{}
runtime.runnerEnvironment = env
runtime.cacheLocation = t.TempDir()
resp, err := runtime.Init(ctx, req)
require.NoError(t, err)
require.Equal(t, runtimev0.InitStatus_READY, resp.GetStatus().GetState(), resp.GetStatus().GetMessage())
var commands [][]string
for _, proc := range env.started() {
commands = append(commands, append([]string{proc.bin}, proc.args...))
}
require.Equal(t, [][]string{{"uv", "sync"}}, commands, "no OpenAPI generator, so none runs")
}
// TestInitIsSafeWithConcurrentTeardown overlaps Init with the calls that own
// the same fields. Under `-race` this is what catches Init publishing or
// reading the runner state outside runnerMu; without it, it still pins that
// neither side panics however they interleave.
func TestInitIsSafeWithConcurrentTeardown(t *testing.T) {
for _, tc := range []struct {
name string
// teardown reports whether the call returned a response: a panic is
// swallowed by the deferred Wool.Catch and surfaces as a nil one.
teardown func(*Runtime) bool
}{
{"destroy", func(r *Runtime) bool {
resp, err := r.Destroy(context.Background(), &runtimev0.DestroyRequest{})
return err == nil && resp != nil
}},
{"stop", func(r *Runtime) bool {
resp, err := r.Stop(context.Background(), &runtimev0.StopRequest{})
return err == nil && resp != nil
}},
} {
t.Run(tc.name, func(t *testing.T) {
isolateWorkingDir(t)
// One interleaving proves little: the race detector reports only
// accesses that actually execute, and a teardown landing before
// Init publishes touches none of the runner state. Repeat so both
// orders occur.
for range 50 {
runInitAgainstTeardown(t, tc.teardown)
}
})
}
}
func runInitAgainstTeardown(t *testing.T, teardown func(*Runtime) bool) {
t.Helper()
runtime, req := initTestRuntime(t)
// Init creates and publishes the runner environment — the section under
// test — and then stops on the missing project file, well before it would
// reach for uv.
require.NoError(t, os.Remove(filepath.Join(runtime.Service.SourceLocation, "pyproject.toml")))
var initResp *runtimev0.InitResponse
var initErr error
var teardownOK bool
gate := make(chan struct{})
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
<-gate
initResp, initErr = runtime.Init(context.Background(), req)
}()
go func() {
defer wg.Done()
<-gate
// Land in the window Init is actually vulnerable in. Racing the two
// from a bare start gate is not enough: the teardown wins, finds no
// environment, and touches none of the runner state — so the accesses
// that would race never execute and the detector sees nothing.
waitForRunnerEnvironment(runtime)
teardownOK = teardown(runtime)
}()
close(gate)
wg.Wait()
require.NoError(t, initErr)
require.NotNil(t, initResp, "Init must not panic when a teardown lands mid-flight")
require.Equal(t, runtimev0.InitStatus_ERROR, initResp.GetStatus().GetState())
require.Contains(t, initResp.GetStatus().GetMessage(), "no pyproject.toml",
"Init must run past its runner section, or this tests nothing")
require.True(t, teardownOK, "the teardown must not panic against a concurrent Init")
require.DirExists(t, filepath.Join(runtime.Location, ".cache/local"),
"Init must have created and published the runner environment")
}
// waitForRunnerEnvironment blocks until Init has published the environment.
// The read is taken under runnerMu, so waiting introduces no edge of its own
// between Init and the teardown.
func waitForRunnerEnvironment(runtime *Runtime) {
for range 2000 {
runtime.runnerMu.Lock()
published := runtime.runnerEnvironment != nil
runtime.runnerMu.Unlock()
if published {
return
}
time.Sleep(time.Millisecond)
}
}
// TestRunnerEnvCreatesOneEnvironment pins the single-flight: concurrent Inits
// on a cold runtime must share one environment. Without it each caller builds
// its own, one wins the field, and the losers are left initialized — holding a
// docker client and a log stream — with nothing left to shut them down.
func TestRunnerEnvCreatesOneEnvironment(t *testing.T) {
isolateWorkingDir(t)
runtime, _ := initTestRuntime(t)
const callers = 8
envs := make([]runners.RunnerEnvironment, callers)
errs := make([]error, callers)
gate := make(chan struct{})
var wg sync.WaitGroup
for i := range callers {
wg.Add(1)
go func() {
defer wg.Done()
<-gate
envs[i], _, errs[i] = runtime.runnerEnv(context.Background())
}()
}
close(gate)
wg.Wait()
for i := range callers {
require.NoError(t, errs[i])
require.NotNil(t, envs[i])
require.True(t, envs[i] == envs[0], "every caller must get the one environment that was published")
}
}
// TestInitAfterStopBuildsAFreshEnvironment pins what Stop owes the next Init:
// Shutdown closes the docker client, so an environment left in the field after
// it cannot serve another process.
func TestInitAfterStopBuildsAFreshEnvironment(t *testing.T) {
isolateWorkingDir(t)
ctx := context.Background()
runtime, _ := initTestRuntime(t)
first, _, err := runtime.runnerEnv(ctx)
require.NoError(t, err)
require.NotNil(t, first)
resp, err := runtime.Stop(ctx, &runtimev0.StopRequest{})
require.NoError(t, err)
require.Equal(t, runtimev0.StopStatus_SUCCESS, resp.GetStatus().GetState(), resp.GetStatus().GetMessage())
require.Nil(t, runtime.FastAPI.Service.ActiveEnvironment(), "Code and the REPL must not be left holding a dead environment")
second, _, err := runtime.runnerEnv(ctx)
require.NoError(t, err)
require.False(t, second == first, "Stop shut the environment down; Init must not be handed the corpse")
}