oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package isolatedjob_test
2
3import (
4 "context"
5 "errors"
6 "fmt"
7 "net/netip"
8 "slices"
9 "strings"
10 "sync"
11 "testing"
12 "time"
13
14 "amadan.net/oficina/equipo/internal/isolatedjob"
15 "amadan.net/oficina/equipo/internal/isolatedjob/isolatedjobtest"
16)
17
18const fakeDigest = isolatedjob.ImageDigest("sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
19
20var fakeStart = time.Date(2026, 11, 22, 9, 0, 0, 0, time.UTC)
21
22func newFake(t *testing.T) (*isolatedjob.Fake, *isolatedjobtest.FakeClock) {
23 t.Helper()
24 c := isolatedjobtest.NewFakeClock(fakeStart)
25 return isolatedjob.NewFake(c, isolatedjob.SpikeLimits()), c
26}
27
28func fakeSpec(attempt string) isolatedjob.LaunchSpec {
29 return isolatedjob.LaunchSpec{
30 AttemptID: attempt,
31 Image: fakeDigest,
32 Size: isolatedjob.SizeSmall,
33 MaxLifetime: time.Hour,
34 Bootstrap: []byte("one-time-code"),
35 }
36}
37
38// settleFake ticks the fake until want holds, reading the VM before each tick.
39func settleFake(f *isolatedjob.Fake) func(context.Context, isolatedjob.Ref, func(isolatedjob.State) bool) (isolatedjob.Job, error) {
40 return func(ctx context.Context, ref isolatedjob.Ref, want func(isolatedjob.State) bool) (isolatedjob.Job, error) {
41 const maxTicks = 10
42 var job isolatedjob.Job
43 for range maxTicks + 1 {
44 var err error
45 job, err = f.Get(ctx, ref)
46 if err != nil {
47 return job, err
48 }
49 if want(job.State) {
50 return job, nil
51 }
52 f.Tick()
53 }
54 return job, fmt.Errorf("fake VM %s still %s after %d ticks", ref, job.State, maxTicks)
55 }
56}
57
58func TestFakeContract(t *testing.T) {
59 f, _ := newFake(t)
60 isolatedjobtest.Contract(t, isolatedjobtest.Harness{Runner: f, Image: fakeDigest, Settle: settleFake(f)})
61}
62
63// lateVisible is the Fake with reads that trail launches: Get answers
64// ErrNotFound once for each VM Launch returns, as AWS GetMicrovm can just
65// after the VM is created, its reads being eventually consistent. It also
66// counts Get and List calls whose context has no deadline.
67type lateVisible struct {
68 *isolatedjob.Fake
69 mu sync.Mutex
70 seen map[isolatedjob.Ref]bool
71 hidden map[isolatedjob.Ref]bool
72 misses int
73 unbounded int
74 boundedOps int
75}
76
77func (l *lateVisible) note(ctx context.Context) {
78 _, ok := ctx.Deadline()
79 l.mu.Lock()
80 defer l.mu.Unlock()
81 if ok {
82 l.boundedOps++
83 } else {
84 l.unbounded++
85 }
86}
87
88func (l *lateVisible) List(ctx context.Context) ([]isolatedjob.Job, error) {
89 l.note(ctx)
90 return l.Fake.List(ctx)
91}
92
93func (l *lateVisible) Launch(ctx context.Context, s isolatedjob.LaunchSpec) (isolatedjob.Ref, error) {
94 ref, err := l.Fake.Launch(ctx, s)
95 if err == nil {
96 l.mu.Lock()
97 if !l.seen[ref] {
98 l.seen[ref], l.hidden[ref] = true, true
99 }
100 l.mu.Unlock()
101 }
102 return ref, err
103}
104
105func (l *lateVisible) Get(ctx context.Context, ref isolatedjob.Ref) (isolatedjob.Job, error) {
106 l.note(ctx)
107 l.mu.Lock()
108 hide := l.hidden[ref]
109 if hide {
110 delete(l.hidden, ref)
111 l.misses++
112 }
113 l.mu.Unlock()
114 if hide {
115 return isolatedjob.Job{}, fmt.Errorf("%w: %s is not visible yet", isolatedjob.ErrNotFound, ref)
116 }
117 return l.Fake.Get(ctx, ref)
118}
119
120// TestFakeContractLateVisibility runs the contract against a runner whose
121// Get misses every new VM once (Task 17, final review A3): the contract
122// waits for the VM to appear, within Harness.Visibility, before it reads it.
123// Every Get and List the contract makes carries a deadline, so a stalled
124// provider read fails a live run rather than hanging it.
125func TestFakeContractLateVisibility(t *testing.T) {
126 f, _ := newFake(t)
127 lv := &lateVisible{Fake: f, seen: map[isolatedjob.Ref]bool{}, hidden: map[isolatedjob.Ref]bool{}}
128 var logMu sync.Mutex
129 var logged []string
130 isolatedjobtest.Contract(t, isolatedjobtest.Harness{Runner: lv, Image: fakeDigest, Settle: settleFake(f),
131 Visibility: 200 * time.Millisecond, Logf: func(format string, args ...any) {
132 logMu.Lock()
133 defer logMu.Unlock()
134 logged = append(logged, fmt.Sprintf(format, args...))
135 }})
136 // The live run reads these delays from the log (Task 17 review, round 1).
137 var visible, listed int
138 for _, l := range logged {
139 switch {
140 case strings.HasPrefix(l, "visibility: Get showed new VM ") && strings.HasSuffix(l, "(2 reads)"):
141 visible++
142 case strings.HasPrefix(l, "list lag: List showed running VM "):
143 listed++
144 }
145 }
146 if visible == 0 || listed == 0 {
147 t.Errorf("measured delays logged: %d visibility after a miss, %d list lag; want both; log %q", visible, listed, logged)
148 }
149 lv.mu.Lock()
150 defer lv.mu.Unlock()
151 if lv.misses == 0 {
152 t.Fatal("no Get answered ErrNotFound for a new VM; the test checked nothing")
153 }
154 if lv.unbounded != 0 || lv.boundedOps == 0 {
155 t.Fatalf("contract made %d Get or List calls without a deadline (%d with one), want none without", lv.unbounded, lv.boundedOps)
156 }
157}
158
159func mustLaunch(t *testing.T, f *isolatedjob.Fake, s isolatedjob.LaunchSpec) isolatedjob.Ref {
160 t.Helper()
161 ref, err := f.Launch(context.Background(), s)
162 if err != nil {
163 t.Fatalf("Launch(%s) = %v", s.AttemptID, err)
164 }
165 return ref
166}
167
168func mustGet(t *testing.T, f *isolatedjob.Fake, ref isolatedjob.Ref) isolatedjob.Job {
169 t.Helper()
170 job, err := f.Get(context.Background(), ref)
171 if err != nil {
172 t.Fatalf("Get(%s) = %v", ref, err)
173 }
174 return job
175}
176
177func wantState(t *testing.T, f *isolatedjob.Fake, ref isolatedjob.Ref, want isolatedjob.State) {
178 t.Helper()
179 if got := mustGet(t, f, ref).State; got != want {
180 t.Fatalf("Get(%s).State = %s, want %s", ref, got, want)
181 }
182}
183
184// running launches a VM and ticks it to running.
185func running(t *testing.T, f *isolatedjob.Fake, s isolatedjob.LaunchSpec) isolatedjob.Ref {
186 t.Helper()
187 ref := mustLaunch(t, f, s)
188 f.Tick()
189 wantState(t, f, ref, isolatedjob.StateRunning)
190 return ref
191}
192
193func isSentinel(err error) bool {
194 for _, s := range []error{isolatedjob.ErrNotFound, isolatedjob.ErrInvalid, isolatedjob.ErrConflict,
195 isolatedjob.ErrThrottled, isolatedjob.ErrIncomplete, isolatedjob.ErrBootstrapTooLarge} {
196 if errors.Is(err, s) {
197 return true
198 }
199 }
200 return false
201}
202
203// TestFakeFaults has one row per fault. Every row also checks that the fault
204// was used up by the one call it hit.
205func TestFakeFaults(t *testing.T) {
206 ctx := context.Background()
207 t.Run("throttle does nothing", func(t *testing.T) {
208 f, _ := newFake(t)
209 f.Inject(isolatedjob.OpLaunch, isolatedjob.FaultThrottle)
210 ref, err := f.Launch(ctx, fakeSpec("a1"))
211 if !errors.Is(err, isolatedjob.ErrThrottled) || ref != "" {
212 t.Fatalf("throttled Launch = (%q, %v), want (\"\", ErrThrottled)", ref, err)
213 }
214 if live := f.LiveByAttempt(); len(live) != 0 {
215 t.Fatalf("LiveByAttempt after a throttled launch = %v, want none", live)
216 }
217 mustLaunch(t, f, fakeSpec("a1"))
218 if live := f.LiveByAttempt(); live["a1"] != 1 {
219 t.Fatalf("LiveByAttempt after the next launch = %v, want a1:1", live)
220 }
221 })
222 t.Run("lost reply applies then errs", func(t *testing.T) {
223 f, _ := newFake(t)
224 f.Inject(isolatedjob.OpLaunch, isolatedjob.FaultLostReply)
225 ref, err := f.Launch(ctx, fakeSpec("a1"))
226 if !errors.Is(err, isolatedjob.ErrLostReply) || isSentinel(err) || ref != "" {
227 t.Fatalf("lost-reply Launch = (%q, %v), want (\"\", ErrLostReply) and no sentinel", ref, err)
228 }
229 if live := f.LiveByAttempt(); live["a1"] != 1 {
230 t.Fatalf("LiveByAttempt after a lost reply = %v, want a1:1 (the launch happened)", live)
231 }
232 again := mustLaunch(t, f, fakeSpec("a1"))
233 if live := f.LiveByAttempt(); live["a1"] != 1 {
234 t.Fatalf("LiveByAttempt after the retry = %v, want a1:1", live)
235 }
236 f.Inject(isolatedjob.OpStop, isolatedjob.FaultLostReply)
237 if err := f.Stop(ctx, again, "test"); !errors.Is(err, isolatedjob.ErrLostReply) {
238 t.Fatalf("lost-reply Stop = %v, want ErrLostReply", err)
239 }
240 wantState(t, f, again, isolatedjob.StateStopping)
241 if err := f.Stop(ctx, again, "test"); err != nil {
242 t.Fatalf("the next Stop = %v, want nil", err)
243 }
244 })
245 t.Run("lost reply on resume", func(t *testing.T) {
246 f, _ := newFake(t)
247 suspendedVM := func(attempt string) isolatedjob.Ref {
248 ref := running(t, f, fakeSpec(attempt))
249 if err := f.Suspend(ctx, ref); err != nil {
250 t.Fatal(err)
251 }
252 f.Tick()
253 wantState(t, f, ref, isolatedjob.StateSuspended)
254 return ref
255 }
256 lost := func(ref isolatedjob.Ref) {
257 t.Helper()
258 f.Inject(isolatedjob.OpResume, isolatedjob.FaultLostReply)
259 err := f.Resume(ctx, ref)
260 if !errors.Is(err, isolatedjob.ErrLostReply) || isSentinel(err) {
261 t.Fatalf("lost-reply Resume(%s) = %v, want ErrLostReply and no sentinel", ref, err)
262 }
263 wantState(t, f, ref, isolatedjob.StateSuspended)
264 }
265 // The resume took effect: one Tick runs the VM with no second Resume.
266 a := suspendedVM("a1")
267 lost(a)
268 f.Tick()
269 wantState(t, f, a, isolatedjob.StateRunning)
270 // A retry finds no fault left and changes nothing while the resume is pending.
271 b := suspendedVM("a2")
272 lost(b)
273 if err := f.Resume(ctx, b); err != nil {
274 t.Fatalf("retried Resume(%s) = %v, want nil", b, err)
275 }
276 wantState(t, f, b, isolatedjob.StateSuspended)
277 f.Tick()
278 wantState(t, f, b, isolatedjob.StateRunning)
279 if err := f.Resume(ctx, b); err != nil {
280 t.Fatalf("Resume(%s) once running = %v, want nil", b, err)
281 }
282 })
283 t.Run("incomplete list", func(t *testing.T) {
284 f, _ := newFake(t)
285 ref := mustLaunch(t, f, fakeSpec("a1"))
286 f.Inject(isolatedjob.OpList, isolatedjob.FaultIncompleteList)
287 jobs, err := f.List(ctx)
288 if !errors.Is(err, isolatedjob.ErrIncomplete) || jobs != nil {
289 t.Fatalf("faulted List = (%v, %v), want (nil, ErrIncomplete)", jobs, err)
290 }
291 jobs, err = f.List(ctx)
292 if err != nil || len(jobs) != 1 || jobs[0].Ref != ref {
293 t.Fatalf("the next List = (%v, %v), want just %s", jobs, err, ref)
294 }
295 })
296 t.Run("unknown state on get", func(t *testing.T) {
297 f, _ := newFake(t)
298 ref := running(t, f, fakeSpec("a1"))
299 f.Inject(isolatedjob.OpGet, isolatedjob.FaultUnknownState)
300 wantState(t, f, ref, isolatedjob.StateUnknown)
301 wantState(t, f, ref, isolatedjob.StateRunning)
302 })
303 t.Run("age unknown on get", func(t *testing.T) {
304 f, c := newFake(t)
305 ref := running(t, f, fakeSpec("a1"))
306 c.AdvanceMono(5 * time.Minute)
307 f.Inject(isolatedjob.OpGet, isolatedjob.FaultAgeUnknown)
308 if job := mustGet(t, f, ref); job.AgeKnown || job.State != isolatedjob.StateRunning {
309 t.Fatalf("faulted Get = %+v, want AgeKnown false and still running", job)
310 }
311 if job := mustGet(t, f, ref); !job.AgeKnown || job.ProviderAge != 5*time.Minute {
312 t.Fatalf("the next Get = %+v, want AgeKnown and ProviderAge 5m", job)
313 }
314 })
315 t.Run("faults queue in order", func(t *testing.T) {
316 f, _ := newFake(t)
317 ref := running(t, f, fakeSpec("a1"))
318 f.Inject(isolatedjob.OpSuspend, isolatedjob.FaultThrottle)
319 f.Inject(isolatedjob.OpSuspend, isolatedjob.FaultLostReply)
320 if err := f.Suspend(ctx, ref); !errors.Is(err, isolatedjob.ErrThrottled) {
321 t.Fatalf("first Suspend = %v, want ErrThrottled", err)
322 }
323 wantState(t, f, ref, isolatedjob.StateRunning)
324 if err := f.Suspend(ctx, ref); !errors.Is(err, isolatedjob.ErrLostReply) {
325 t.Fatalf("second Suspend = %v, want ErrLostReply", err)
326 }
327 wantState(t, f, ref, isolatedjob.StateSuspending)
328 f.Tick()
329 if err := f.Resume(ctx, ref); err != nil {
330 t.Fatalf("Resume with no fault queued = %v", err)
331 }
332 })
333 t.Run("tick counts suspended time", func(t *testing.T) {
334 f, c := newFake(t)
335 ref := running(t, f, fakeSpec("a1")) // one-hour lifetime
336 c.AdvanceMono(10 * time.Minute)
337 if err := f.Suspend(ctx, ref); err != nil {
338 t.Fatal(err)
339 }
340 f.Tick()
341 c.AdvanceMono(49 * time.Minute)
342 f.Tick()
343 wantState(t, f, ref, isolatedjob.StateSuspended)
344 c.AdvanceMono(time.Minute)
345 f.Tick()
346 wantState(t, f, ref, isolatedjob.StateStopping)
347 f.Tick()
348 job := mustGet(t, f, ref)
349 if job.State != isolatedjob.StateStopped || !strings.Contains(job.Reason, "lifetime") {
350 t.Fatalf("Get after the backstop = %+v, want stopped with a lifetime reason", job)
351 }
352 if live := f.LiveByAttempt(); len(live) != 0 {
353 t.Fatalf("LiveByAttempt = %v, want none", live)
354 }
355 })
356 t.Run("suspended time excluded when the provider says so", func(t *testing.T) {
357 c := isolatedjobtest.NewFakeClock(fakeStart)
358 l := isolatedjob.SpikeLimits()
359 l.SuspendUsesLifetime = false
360 f := isolatedjob.NewFake(c, l)
361 ref := running(t, f, fakeSpec("a1"))
362 c.AdvanceMono(10 * time.Minute)
363 if err := f.Suspend(ctx, ref); err != nil {
364 t.Fatal(err)
365 }
366 f.Tick()
367 c.AdvanceMono(2 * time.Hour)
368 f.Tick()
369 wantState(t, f, ref, isolatedjob.StateSuspended)
370 if err := f.Resume(ctx, ref); err != nil {
371 t.Fatal(err)
372 }
373 c.AdvanceMono(49 * time.Minute)
374 f.Tick()
375 wantState(t, f, ref, isolatedjob.StateRunning)
376 c.AdvanceMono(time.Minute)
377 f.Tick()
378 wantState(t, f, ref, isolatedjob.StateStopping)
379 })
380}
381
382func TestFakeLifecycle(t *testing.T) {
383 ctx := context.Background()
384 t.Run("states step on tick", func(t *testing.T) {
385 f, _ := newFake(t)
386 ref := mustLaunch(t, f, fakeSpec("a1"))
387 wantState(t, f, ref, isolatedjob.StateStarting)
388 if err := f.Suspend(ctx, ref); !errors.Is(err, isolatedjob.ErrConflict) {
389 t.Fatalf("Suspend while starting = %v, want ErrConflict", err)
390 }
391 if err := f.Resume(ctx, ref); !errors.Is(err, isolatedjob.ErrConflict) {
392 t.Fatalf("Resume while starting = %v, want ErrConflict", err)
393 }
394 f.Tick()
395 wantState(t, f, ref, isolatedjob.StateRunning)
396 if err := f.Suspend(ctx, ref); err != nil {
397 t.Fatal(err)
398 }
399 wantState(t, f, ref, isolatedjob.StateSuspending)
400 if err := f.Suspend(ctx, ref); !errors.Is(err, isolatedjob.ErrConflict) {
401 t.Fatalf("Suspend while suspending = %v, want ErrConflict", err)
402 }
403 f.Tick()
404 wantState(t, f, ref, isolatedjob.StateSuspended)
405 if err := f.Resume(ctx, ref); err != nil {
406 t.Fatal(err)
407 }
408 f.Tick()
409 wantState(t, f, ref, isolatedjob.StateRunning)
410 if err := f.Stop(ctx, ref, "done"); err != nil {
411 t.Fatal(err)
412 }
413 wantState(t, f, ref, isolatedjob.StateStopping)
414 if err := f.Resume(ctx, ref); !errors.Is(err, isolatedjob.ErrConflict) {
415 t.Fatalf("Resume while stopping = %v, want ErrConflict", err)
416 }
417 f.Tick()
418 if job := mustGet(t, f, ref); job.State != isolatedjob.StateStopped || job.Reason != "done" {
419 t.Fatalf("Get after stop = %+v, want stopped with reason done", job)
420 }
421 for _, call := range []func(context.Context, isolatedjob.Ref) error{f.Suspend, f.Resume} {
422 if err := call(ctx, "vm-missing"); !errors.Is(err, isolatedjob.ErrNotFound) {
423 t.Fatalf("Suspend or Resume of an unknown ref = %v, want ErrNotFound", err)
424 }
425 }
426 if _, err := f.Get(ctx, "vm-missing"); !errors.Is(err, isolatedjob.ErrNotFound) {
427 t.Fatalf("Get of an unknown ref = %v, want ErrNotFound", err)
428 }
429 })
430 t.Run("resume reports suspended until the next tick", func(t *testing.T) {
431 f, _ := newFake(t)
432 suspended := func(attempt string) isolatedjob.Ref {
433 ref := running(t, f, fakeSpec(attempt))
434 if err := f.Suspend(ctx, ref); err != nil {
435 t.Fatal(err)
436 }
437 f.Tick()
438 wantState(t, f, ref, isolatedjob.StateSuspended)
439 return ref
440 }
441 ref := suspended("a1")
442 for i := 1; i <= 2; i++ {
443 if err := f.Resume(ctx, ref); err != nil {
444 t.Fatalf("Resume %d while the resume is pending = %v, want nil", i, err)
445 }
446 wantState(t, f, ref, isolatedjob.StateSuspended)
447 }
448 f.Tick()
449 wantState(t, f, ref, isolatedjob.StateRunning)
450 f.Tick()
451 wantState(t, f, ref, isolatedjob.StateRunning)
452
453 // Stop while the resume is pending stops the VM; the resume is dropped.
454 other := suspended("a2")
455 if err := f.Resume(ctx, other); err != nil {
456 t.Fatal(err)
457 }
458 if err := f.Stop(ctx, other, "stop mid-resume"); err != nil {
459 t.Fatal(err)
460 }
461 wantState(t, f, other, isolatedjob.StateStopping)
462 f.Tick()
463 wantState(t, f, other, isolatedjob.StateStopped)
464 })
465 t.Run("launch replays after stop and refuses bad specs", func(t *testing.T) {
466 f, _ := newFake(t)
467 ref := mustLaunch(t, f, fakeSpec("a1"))
468 if err := f.Stop(ctx, ref, "x"); err != nil {
469 t.Fatal(err)
470 }
471 f.Tick()
472 if again := mustLaunch(t, f, fakeSpec("a1")); again != ref {
473 t.Fatalf("Launch after stop = %s, want the stopped VM %s (client token replay)", again, ref)
474 }
475 calls := f.Calls(isolatedjob.OpLaunch)
476 f.Inject(isolatedjob.OpLaunch, isolatedjob.FaultThrottle)
477 bad := fakeSpec("a2")
478 bad.Size = "medium"
479 if _, err := f.Launch(ctx, bad); !errors.Is(err, isolatedjob.ErrInvalid) {
480 t.Fatalf("Launch with a bad size = %v, want ErrInvalid", err)
481 }
482 big := fakeSpec("a3")
483 big.Bootstrap = make([]byte, f.Limits().MaxBootstrapBytes+1)
484 if _, err := f.Launch(ctx, big); !errors.Is(err, isolatedjob.ErrBootstrapTooLarge) {
485 t.Fatalf("Launch with an oversized bootstrap = %v, want ErrBootstrapTooLarge", err)
486 }
487 if got := f.Calls(isolatedjob.OpLaunch); got != calls {
488 t.Fatalf("Calls(OpLaunch) = %d after two refused specs, want %d: a bad spec must not reach the provider", got, calls)
489 }
490 if _, err := f.Launch(ctx, fakeSpec("a4")); !errors.Is(err, isolatedjob.ErrThrottled) {
491 t.Fatalf("next good Launch = %v, want the queued ErrThrottled (a refused spec takes no fault)", err)
492 }
493 if got := f.Calls(isolatedjob.OpLaunch); got != calls+1 {
494 t.Fatalf("Calls(OpLaunch) = %d after a throttled launch, want %d", got, calls+1)
495 }
496 if jobs, _ := f.List(ctx); len(jobs) != 1 {
497 t.Fatalf("List = %v, want only the first VM", jobs)
498 }
499 })
500 t.Run("vanish hides the VM but keeps its client token", func(t *testing.T) {
501 f, _ := newFake(t)
502 gone := running(t, f, fakeSpec("a1"))
503 kept := running(t, f, fakeSpec("a2"))
504 f.Vanish(gone)
505 f.Vanish("vm-missing") // a ref the fake never held changes nothing
506 if _, err := f.Get(ctx, gone); !errors.Is(err, isolatedjob.ErrNotFound) {
507 t.Fatalf("Get of a vanished VM = %v, want ErrNotFound", err)
508 }
509 jobs, err := f.List(ctx)
510 if err != nil || len(jobs) != 1 || jobs[0].Ref != kept {
511 t.Fatalf("List after vanish = (%v, %v), want just %s", jobs, err, kept)
512 }
513 if live := f.LiveByAttempt(); len(live) != 1 || live["a2"] != 1 {
514 t.Fatalf("LiveByAttempt after vanish = %v, want a2:1", live)
515 }
516 if stops := f.StopsIssued(); len(stops) != 0 {
517 t.Fatalf("StopsIssued after vanish = %v, want none: the controller stopped nothing", stops)
518 }
519 for name, call := range map[string]func(context.Context, isolatedjob.Ref) error{"Suspend": f.Suspend, "Resume": f.Resume} {
520 if err := call(ctx, gone); !errors.Is(err, isolatedjob.ErrNotFound) {
521 t.Fatalf("%s of a vanished VM = %v, want ErrNotFound", name, err)
522 }
523 }
524 if err := f.Stop(ctx, gone, "late"); err != nil {
525 t.Fatalf("Stop of a vanished VM = %v, want nil", err)
526 }
527 f.Tick()
528 if _, err := f.Get(ctx, gone); !errors.Is(err, isolatedjob.ErrNotFound) {
529 t.Fatalf("Get of a vanished VM after Stop and Tick = %v, want ErrNotFound", err)
530 }
531 if again := mustLaunch(t, f, fakeSpec("a1")); again != gone {
532 t.Fatalf("Launch replay after vanish = %s, want %s (the client token outlives the VM)", again, gone)
533 }
534 changed := fakeSpec("a1")
535 changed.Bootstrap = []byte("another-code")
536 if _, err := f.Launch(ctx, changed); !errors.Is(err, isolatedjob.ErrConflict) {
537 t.Fatalf("Launch of a changed spec after vanish = %v, want ErrConflict", err)
538 }
539 if live := f.LiveByAttempt(); len(live) != 1 || live["a2"] != 1 {
540 t.Fatalf("LiveByAttempt after the replay = %v, want a2:1", live)
541 }
542 })
543 t.Run("times come from the clock", func(t *testing.T) {
544 f, c := newFake(t)
545 ref := mustLaunch(t, f, fakeSpec("a1"))
546 c.SetWall(fakeStart.Add(-24 * time.Hour))
547 c.AdvanceMono(90 * time.Second)
548 job := mustGet(t, f, ref)
549 if !job.StartedAt.Equal(fakeStart) || job.ProviderAge != 90*time.Second || !job.AgeKnown {
550 t.Fatalf("Get = %+v, want StartedAt %v, ProviderAge 90s, AgeKnown", job, fakeStart)
551 }
552 })
553 t.Run("backstop off keeps the VM", func(t *testing.T) {
554 f, c := newFake(t)
555 f.SetBackstop(false)
556 ref := running(t, f, fakeSpec("a1"))
557 c.AdvanceMono(2 * time.Hour)
558 f.Tick()
559 wantState(t, f, ref, isolatedjob.StateRunning)
560 f.SetBackstop(true)
561 f.Tick()
562 wantState(t, f, ref, isolatedjob.StateStopping)
563 })
564 t.Run("plant, live count and stops issued", func(t *testing.T) {
565 f, _ := newFake(t)
566 orphan := f.Plant(fakeSpec("a1"))
567 wantState(t, f, orphan, isolatedjob.StateRunning)
568 other := mustLaunch(t, f, fakeSpec("a2"))
569 if live := f.LiveByAttempt(); len(live) != 2 || live["a1"] != 1 || live["a2"] != 1 {
570 t.Fatalf("LiveByAttempt = %v, want a1:1 a2:1", live)
571 }
572 for _, r := range []isolatedjob.Ref{orphan, "vm-missing", orphan} {
573 if err := f.Stop(ctx, r, "reap"); err != nil {
574 t.Fatalf("Stop(%s) = %v", r, err)
575 }
576 }
577 want := []isolatedjob.Ref{orphan, "vm-missing", orphan}
578 if got := f.StopsIssued(); !slices.Equal(got, want) {
579 t.Fatalf("StopsIssued = %v, want %v", got, want)
580 }
581 f.Tick()
582 if live := f.LiveByAttempt(); len(live) != 1 || live["a2"] != 1 {
583 t.Fatalf("LiveByAttempt after the reap = %v, want a2:1 (%s)", live, other)
584 }
585 })
586 t.Run("network audit", func(t *testing.T) {
587 f, _ := newFake(t)
588 a, err := f.AuditNetwork(ctx)
589 if err != nil || a.OK(nil) != nil {
590 t.Fatalf("default AuditNetwork = (%+v, %v), want a passing audit", a, err)
591 }
592 set := isolatedjob.NetworkAudit{EgressOnly: []netip.AddrPort{netip.MustParseAddrPort("10.0.0.1:3128")}}
593 boom := errors.New("describe failed")
594 f.SetAudit(set, boom)
595 a, err = f.AuditNetwork(ctx)
596 if !errors.Is(err, boom) || len(a.EgressOnly) != 1 {
597 t.Fatalf("AuditNetwork after SetAudit = (%+v, %v)", a, err)
598 }
599 })
600 t.Run("concurrent launches of one attempt", func(t *testing.T) {
601 f, _ := newFake(t)
602 refs := make([]isolatedjob.Ref, 20)
603 var wg sync.WaitGroup
604 for i := range refs {
605 wg.Go(func() {
606 ref, err := f.Launch(ctx, fakeSpec("a1"))
607 if err != nil {
608 t.Errorf("Launch = %v", err)
609 }
610 refs[i] = ref
611 })
612 }
613 wg.Wait()
614 if live := f.LiveByAttempt(); live["a1"] != 1 {
615 t.Fatalf("LiveByAttempt = %v, want a1:1", live)
616 }
617 for _, r := range refs {
618 if r != refs[0] {
619 t.Fatalf("refs = %v, want all the same", refs)
620 }
621 }
622 })
623 t.Run("inject refuses a fault the op cannot have", func(t *testing.T) {
624 f, _ := newFake(t)
625 defer func() {
626 if recover() == nil {
627 t.Fatal("Inject(OpGet, FaultIncompleteList) did not panic")
628 }
629 }()
630 f.Inject(isolatedjob.OpGet, isolatedjob.FaultIncompleteList)
631 })
632 t.Run("limits as the spike found", func(t *testing.T) {
633 want := isolatedjob.Limits{MaxLifetime: 28800 * time.Second, MaxBootstrapBytes: 4096, SuspendUsesLifetime: true}
634 if got := isolatedjob.SpikeLimits(); got != want {
635 t.Fatalf("SpikeLimits = %+v, want %+v", got, want)
636 }
637 f, _ := newFake(t)
638 if f.Limits() != want {
639 t.Fatalf("Limits = %+v, want %+v", f.Limits(), want)
640 }
641 })
642}
643