oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package isolatedjob
2
3import (
4 "errors"
5 "net/netip"
6 "os"
7 "strings"
8 "testing"
9 "time"
10
11 "amadan.net/rastrillo/rastrillo/budget"
12)
13
14func TestMain(m *testing.M) { os.Exit(budget.Main(m)) }
15
16const goodDigest = ImageDigest("sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
17
18func goodSpec() LaunchSpec {
19 return LaunchSpec{
20 AttemptID: "attempt-1",
21 Image: goodDigest,
22 Size: SizeSmall,
23 MaxLifetime: time.Hour,
24 Bootstrap: []byte("one-time-code"),
25 }
26}
27
28func TestLaunchSpecValidate(t *testing.T) {
29 limits := Limits{MaxLifetime: HardMaxLifetime, MaxBootstrapBytes: 4096}
30 short := Limits{MaxLifetime: time.Hour, MaxBootstrapBytes: 4096}
31 loose := Limits{MaxLifetime: 10 * time.Hour, MaxBootstrapBytes: 4096}
32
33 cases := []struct {
34 name string
35 limits Limits
36 edit func(*LaunchSpec)
37 want error // nil means the spec passes
38 field string // a word the error must name
39 }{
40 {"good", limits, func(*LaunchSpec) {}, nil, ""},
41 {"attempt id empty", limits, func(s *LaunchSpec) { s.AttemptID = "" }, ErrInvalid, "attempt id"},
42 {"attempt id 65 chars", limits, func(s *LaunchSpec) { s.AttemptID = strings.Repeat("a", 65) }, ErrInvalid, "attempt id"},
43 {"attempt id underscore", limits, func(s *LaunchSpec) { s.AttemptID = "attempt_1" }, ErrInvalid, "attempt id"},
44 {"attempt id space", limits, func(s *LaunchSpec) { s.AttemptID = "attempt 1" }, ErrInvalid, "attempt id"},
45 {"attempt id non-ascii", limits, func(s *LaunchSpec) { s.AttemptID = "attempt-é" }, ErrInvalid, "attempt id"},
46 {"attempt id 64 chars at limit", limits, func(s *LaunchSpec) { s.AttemptID = strings.Repeat("A9-", 21) + "z" }, nil, ""},
47 {"attempt id one char", limits, func(s *LaunchSpec) { s.AttemptID = "a" }, nil, ""},
48 {"image empty", limits, func(s *LaunchSpec) { s.Image = "" }, ErrInvalid, "image"},
49 {"image no prefix", limits, func(s *LaunchSpec) { s.Image = goodDigest[len("sha256:"):] }, ErrInvalid, "image"},
50 {"image upper prefix", limits, func(s *LaunchSpec) { s.Image = ImageDigest(strings.ToUpper(string(goodDigest[:20]))) + goodDigest[20:] }, ErrInvalid, "image"},
51 {"image upper hex digit", limits, func(s *LaunchSpec) { s.Image = goodDigest[:len(goodDigest)-1] + "F" }, ErrInvalid, "image"},
52 {"image short", limits, func(s *LaunchSpec) { s.Image = goodDigest[:len(goodDigest)-1] }, ErrInvalid, "image"},
53 {"image long", limits, func(s *LaunchSpec) { s.Image = goodDigest + "0" }, ErrInvalid, "image"},
54 {"image other algorithm", limits, func(s *LaunchSpec) { s.Image = "sha512:" + goodDigest[len("sha256:"):] }, ErrInvalid, "image"},
55 {"size empty", limits, func(s *LaunchSpec) { s.Size = "" }, ErrInvalid, "size"},
56 {"size medium", limits, func(s *LaunchSpec) { s.Size = "medium" }, ErrInvalid, "size"},
57 {"size capitalised", limits, func(s *LaunchSpec) { s.Size = "Small" }, ErrInvalid, "size"},
58 {"size large", limits, func(s *LaunchSpec) { s.Size = SizeLarge }, nil, ""},
59 {"lifetime zero", limits, func(s *LaunchSpec) { s.MaxLifetime = 0 }, ErrInvalid, "lifetime"},
60 {"lifetime negative", limits, func(s *LaunchSpec) { s.MaxLifetime = -time.Second }, ErrInvalid, "lifetime"},
61 {"lifetime one nanosecond", limits, func(s *LaunchSpec) { s.MaxLifetime = 1 }, nil, ""},
62 {"lifetime over the provider limit", short, func(s *LaunchSpec) { s.MaxLifetime = time.Hour + time.Second }, ErrInvalid, "lifetime"},
63 {"lifetime at the provider limit", short, func(s *LaunchSpec) { s.MaxLifetime = time.Hour }, nil, ""},
64 {"lifetime 28801 s", limits, func(s *LaunchSpec) { s.MaxLifetime = 28801 * time.Second }, ErrInvalid, "lifetime"},
65 {"lifetime 28801 s over a looser provider", loose, func(s *LaunchSpec) { s.MaxLifetime = 28801 * time.Second }, ErrInvalid, "lifetime"},
66 {"lifetime 28800 s at the hard limit", limits, func(s *LaunchSpec) { s.MaxLifetime = 28800 * time.Second }, nil, ""},
67 {"lifetime 28800 s at the hard limit, looser provider", loose, func(s *LaunchSpec) { s.MaxLifetime = 28800 * time.Second }, nil, ""},
68 {"lifetime with no provider limit", Limits{MaxBootstrapBytes: 4096}, func(*LaunchSpec) {}, ErrInvalid, "lifetime"},
69 {"bootstrap empty", limits, func(s *LaunchSpec) { s.Bootstrap = nil }, ErrInvalid, "bootstrap"},
70 {"bootstrap zero length", limits, func(s *LaunchSpec) { s.Bootstrap = []byte{} }, ErrInvalid, "bootstrap"},
71 {"bootstrap one byte", limits, func(s *LaunchSpec) { s.Bootstrap = []byte("x") }, nil, ""},
72 {"bootstrap at the limit", limits, func(s *LaunchSpec) { s.Bootstrap = make([]byte, 4096) }, nil, ""},
73 {"bootstrap one byte over", limits, func(s *LaunchSpec) { s.Bootstrap = make([]byte, 4097) }, ErrBootstrapTooLarge, "bootstrap"},
74 {"bootstrap with no provider limit", Limits{MaxLifetime: HardMaxLifetime}, func(*LaunchSpec) {}, ErrBootstrapTooLarge, "bootstrap"},
75 }
76 for _, c := range cases {
77 t.Run(c.name, func(t *testing.T) {
78 s := goodSpec()
79 c.edit(&s)
80 err := s.Validate(c.limits)
81 if c.want == nil {
82 if err != nil {
83 t.Fatalf("Validate = %v, want nil", err)
84 }
85 return
86 }
87 if !errors.Is(err, c.want) {
88 t.Fatalf("Validate = %v, want %v", err, c.want)
89 }
90 if !strings.Contains(err.Error(), c.field) {
91 t.Fatalf("Validate = %q, want it to name %q", err, c.field)
92 }
93 })
94 }
95}
96
97func TestHardMaxLifetime(t *testing.T) {
98 if HardMaxLifetime != 8*time.Hour {
99 t.Fatalf("HardMaxLifetime = %v, want 8h", HardMaxLifetime)
100 }
101}
102
103func TestSizeMemory(t *testing.T) {
104 for _, c := range []struct {
105 size Size
106 mib int
107 }{{SizeSmall, 2048}, {SizeLarge, 4096}, {"", 0}, {"medium", 0}} {
108 if got := c.size.MemoryMiB(); got != c.mib {
109 t.Errorf("Size(%q).MemoryMiB() = %d, want %d", c.size, got, c.mib)
110 }
111 }
112 if SizeSmall != "small" || SizeLarge != "large" {
113 t.Fatalf("sizes = %q, %q", SizeSmall, SizeLarge)
114 }
115}
116
117func TestStateSets(t *testing.T) {
118 cases := []struct {
119 state State
120 live, terminal bool
121 }{
122 {StateStarting, true, false},
123 {StateRunning, true, false},
124 {StateSuspending, true, false},
125 {StateSuspended, true, false},
126 {StateStopping, true, false},
127 {StateStopped, false, true},
128 {StateFailed, false, true},
129 {StateUnknown, false, false},
130 // Values no adapter should produce are treated as unknown.
131 {"", false, false},
132 {"TERMINATED", false, false},
133 {"RUNNING", false, false},
134 {"Stopped", false, false},
135 }
136 for _, c := range cases {
137 if got := c.state.Live(); got != c.live {
138 t.Errorf("State(%q).Live() = %v, want %v", c.state, got, c.live)
139 }
140 if got := c.state.Terminal(); got != c.terminal {
141 t.Errorf("State(%q).Terminal() = %v, want %v", c.state, got, c.terminal)
142 }
143 }
144 want := map[State]string{
145 StateStarting: "starting", StateRunning: "running", StateSuspending: "suspending",
146 StateSuspended: "suspended", StateStopping: "stopping", StateStopped: "stopped",
147 StateFailed: "failed", StateUnknown: "unknown",
148 }
149 for s, v := range want {
150 if string(s) != v {
151 t.Errorf("state %q, want %q", s, v)
152 }
153 }
154}
155
156func TestNetworkAuditOK(t *testing.T) {
157 a := netip.MustParseAddrPort("10.0.0.1:443")
158 b := netip.MustParseAddrPort("10.0.0.2:443")
159 c := netip.MustParseAddrPort("10.0.0.1:80")
160 clean := func(egress ...netip.AddrPort) NetworkAudit {
161 return NetworkAudit{ResolverDisabled: true, IngressClosed: true, RouteOutAbsent: true, EgressOnly: egress}
162 }
163 all := []string{"resolver", "ingress", "route out", "egress"}
164
165 cases := []struct {
166 name string
167 audit NetworkAudit
168 want []netip.AddrPort
169 named []string // properties the error must name; nil means OK
170 }{
171 {"exact", clean(a, b), []netip.AddrPort{a, b}, nil},
172 {"other order", clean(b, a), []netip.AddrPort{a, b}, nil},
173 {"single", clean(a), []netip.AddrPort{a}, nil},
174 {"none wanted none seen", clean(), nil, nil},
175 {"resolver on", NetworkAudit{IngressClosed: true, RouteOutAbsent: true, EgressOnly: []netip.AddrPort{a}}, []netip.AddrPort{a}, []string{"resolver"}},
176 {"ingress open", NetworkAudit{ResolverDisabled: true, RouteOutAbsent: true, EgressOnly: []netip.AddrPort{a}}, []netip.AddrPort{a}, []string{"ingress"}},
177 {"route out", NetworkAudit{ResolverDisabled: true, IngressClosed: true, EgressOnly: []netip.AddrPort{a}}, []netip.AddrPort{a}, []string{"route out"}},
178 {"extra egress", clean(a, b), []netip.AddrPort{a}, []string{"egress"}},
179 {"missing egress", clean(a), []netip.AddrPort{a, b}, []string{"egress"}},
180 {"same address other port", clean(c), []netip.AddrPort{a}, []string{"egress"}},
181 {"egress seen none wanted", clean(a), nil, []string{"egress"}},
182 {"nothing seen", clean(), []netip.AddrPort{a}, []string{"egress"}},
183 {"everything wrong", NetworkAudit{EgressOnly: []netip.AddrPort{b}}, []netip.AddrPort{a}, all},
184 }
185 for _, c := range cases {
186 t.Run(c.name, func(t *testing.T) {
187 err := c.audit.OK(c.want)
188 if c.named == nil {
189 if err != nil {
190 t.Fatalf("OK = %v, want nil", err)
191 }
192 return
193 }
194 if err == nil {
195 t.Fatalf("OK = nil, want an error naming %v", c.named)
196 }
197 msg := err.Error()
198 for _, p := range all {
199 named := strings.Contains(msg, p)
200 want := false
201 for _, n := range c.named {
202 want = want || n == p
203 }
204 if named != want {
205 t.Errorf("OK = %q: names %q is %v, want %v", msg, p, named, want)
206 }
207 }
208 })
209 }
210}
211
212func TestSystemClock(t *testing.T) {
213 c := SystemClock()
214 last := c.Mono()
215 if last < 0 {
216 t.Fatalf("Mono = %v, want >= 0", last)
217 }
218 for i := 0; i < 1000; i++ {
219 m := c.Mono()
220 if m < last {
221 t.Fatalf("Mono went back: %v after %v", m, last)
222 }
223 last = m
224 }
225 w := c.Wall()
226 if strings.Contains(w.String(), "m=") {
227 t.Fatalf("Wall = %v carries a monotonic reading", w)
228 }
229 if d := time.Since(w); d < 0 || d > time.Minute {
230 t.Fatalf("Wall = %v, %v from now", w, d)
231 }
232 // Mono must come from Go's monotonic reading, so the start must keep it.
233 if start := c.(systemClock).start; !strings.Contains(start.String(), " m=") {
234 t.Fatalf("start = %v has no monotonic reading", start)
235 }
236}
237
238// TestSystemClockWallJump sets the wall clock back an hour while elapsed
239// time moves forward: Mono must follow elapsed time, Wall the wall clock.
240func TestSystemClockWallJump(t *testing.T) {
241 wall := time.Date(2026, 11, 22, 12, 0, 0, 0, time.UTC)
242 var elapsed time.Duration
243 c := newSystemClock(
244 func() time.Time { return wall },
245 func(time.Time) time.Duration { return elapsed },
246 )
247 steps := []struct {
248 advance, wallShift time.Duration
249 }{
250 {5 * time.Second, 5 * time.Second},
251 {5 * time.Second, -time.Hour},
252 {time.Second, 0},
253 {2 * time.Second, 90 * time.Minute},
254 }
255 var lastMono time.Duration
256 for i, s := range steps {
257 elapsed += s.advance
258 wall = wall.Add(s.wallShift)
259 if got := c.Mono(); got != elapsed || got < lastMono {
260 t.Fatalf("step %d: Mono = %v, want %v (last %v)", i, got, elapsed, lastMono)
261 }
262 lastMono = c.Mono()
263 if got := c.Wall(); !got.Equal(wall) {
264 t.Fatalf("step %d: Wall = %v, want %v", i, got, wall)
265 }
266 }
267}
268