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 "errors"
8 "os"
9 "path/filepath"
10 "testing"
11 "time"
12
13 "github.com/carlosframework/platform/internal/instancectl"
14)
15
16func TestServingParkMaintenanceExpiryAndPressure(t *testing.T) {
17 for _, mode := range []string{"healthy", "expiry", "pressure", "unknown", "incomplete", "replacement"} {
18 t.Run(mode, func(t *testing.T) {
19 f := newServingParkDiskFixture(t)
20 a, receipts := f.evidence(1, true, "")
21 f.put("", "retained database")
22 if _, err := f.park(a, receipts); err != nil {
23 t.Fatal(err)
24 }
25 if mode != "incomplete" {
26 f.complete(a)
27 }
28 if mode == "expiry" {
29 f.now = f.now.Add(7 * 24 * time.Hour)
30 }
31 if mode == "replacement" {
32 path := f.path(a, "")
33 if err := os.Rename(path, path+"-original"); err != nil {
34 t.Fatal(err)
35 }
36 if err := os.WriteFile(path, []byte("foreign"), 0600); err != nil {
37 t.Fatal(err)
38 }
39 }
40 calls := 0
41 room := func() error {
42 calls++
43 if mode == "unknown" {
44 return os.ErrPermission
45 }
46 if mode == "pressure" || mode == "incomplete" || mode == "replacement" {
47 return instancectl.ErrDiskFull
48 }
49 return nil
50 }
51 err := f.ledger.maintainParks(t.Context(), room)
52 if mode == "unknown" || mode == "replacement" {
53 if err == nil {
54 t.Fatal("expected refusal")
55 }
56 } else if err != nil {
57 t.Fatal(err)
58 }
59 r, err := f.ledger.read(a.ID)
60 if err != nil {
61 t.Fatal(err)
62 }
63 want := mode == "expiry" || mode == "pressure"
64 if r.Closed != want {
65 t.Fatalf("closed=%v want=%v", r.Closed, want)
66 }
67 if mode == "pressure" && (r.Cause != "pressure" || calls < 2) {
68 t.Fatal("pressure not remeasured", r.Cause, calls)
69 }
70 if !want {
71 if _, err := os.Stat(f.path(a, "")); err != nil {
72 t.Fatal("retained bytes lost", err)
73 }
74 }
75 })
76 }
77}
78
79func TestServingParkMaintenanceCancellation(t *testing.T) {
80 f := newServingParkDiskFixture(t)
81 ctx, cancel := context.WithCancel(t.Context())
82 cancel()
83 if err := f.ledger.maintainParks(ctx, func() error { t.Fatal("cancelled pass measured disk"); return nil }); !errors.Is(err, context.Canceled) {
84 t.Fatal(err)
85 }
86}
87
88type servingMaintenanceWait struct {
89 servingParkAuthority
90 entered chan struct{}
91}
92
93func (s servingMaintenanceWait) maintainServingParks(ctx context.Context, _ string) (string, error) {
94 close(s.entered)
95 <-ctx.Done()
96 return "", ctx.Err()
97}
98func TestServingParkMaintenanceShutdownJoins(t *testing.T) {
99 ctx, cancel := context.WithCancel(context.Background())
100 defer cancel()
101 s := &servingCloseStore{}
102 entered := make(chan struct{})
103 p := &servingParkRuntime{authority: servingMaintenanceWait{entered: entered}, state: s, ctx: ctx, cancel: cancel, idle: make(chan struct{})}
104 p.Start(t.Context())
105 select {
106 case <-entered:
107 case <-time.After(time.Second):
108 t.Fatal("maintenance did not start")
109 }
110 closeCtx, done := context.WithTimeout(t.Context(), time.Second)
111 defer done()
112 if err := p.Close(closeCtx); err != nil || !s.closed.Load() {
113 t.Fatal("maintenance did not join", err)
114 }
115}
116
117func TestServingCapacityFromAgentIdentity(t *testing.T) {
118 data := t.TempDir()
119 if err := os.Mkdir(filepath.Join(data, "instances"), 0700); err != nil {
120 t.Fatal(err)
121 }
122 if err := claimServingRoom(data); err != nil {
123 t.Fatal(err)
124 }
125 other := t.TempDir()
126 if err := os.Symlink(other, filepath.Join(data, "alias")); err != nil {
127 t.Fatal(err)
128 }
129 if err := claimServingRoom(filepath.Join(data, "alias")); err == nil {
130 t.Fatal("symlink data root accepted")
131 }
132}
133