-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathnegative_test.go
More file actions
569 lines (537 loc) · 14.8 KB
/
Copy pathnegative_test.go
File metadata and controls
569 lines (537 loc) · 14.8 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
package conformance
import (
"fmt"
"strings"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
// negativeCase describes one webhook-deny scenario exercised via kubectl apply.
type negativeCase struct {
// name is a short human-readable label for the test row.
name string
// yaml is the HermesInstance manifest to apply.
yaml string
// wantErrSubstring is a substring expected in the kubectl apply error output.
wantErrSubstring string
// isUpdate: when true, apply the base yaml first, then apply yaml as an update.
isUpdate bool
// baseYAML is applied first when isUpdate=true; yaml is then the mutation.
baseYAML string
// skip marks cases not yet supported by the live webhook infrastructure.
skip string
}
// hermesInstanceYAML is a helper that builds a minimal valid HermesInstance
// manifest with the given name, then merges extra YAML fields inline.
func hermesInstanceYAML(name, extra string) string {
base := fmt.Sprintf(`apiVersion: hermes.agent/v1
kind: HermesInstance
metadata:
name: %s
spec:
image:
repository: ghcr.io/paperclipinc/hermes-agent
tag: "v1.0.0"
storage:
persistence:
enabled: true
size: 1Gi
`, name)
if extra == "" {
return base
}
// Indent extra under spec:: caller must pass only spec-level fields.
lines := strings.Split(extra, "\n")
indented := make([]string, 0, len(lines))
for _, l := range lines {
if l != "" {
indented = append(indented, " "+l)
}
}
return base + strings.Join(indented, "\n") + "\n"
}
var negativeCases = []negativeCase{
// ── image.repository required ──────────────────────────────────────────────
{
name: "deny: image.repository empty",
yaml: `apiVersion: hermes.agent/v1
kind: HermesInstance
metadata:
name: neg-no-image-repo
spec:
image:
repository: ""
tag: "v1.0.0"
storage:
persistence:
enabled: true
size: 1Gi
`,
wantErrSubstring: "image.repository",
},
// ── storage.persistence.size required ──────────────────────────────────────
{
name: `deny: storage.persistence.size empty`,
yaml: `apiVersion: hermes.agent/v1
kind: HermesInstance
metadata:
name: neg-no-storage-size
spec:
image:
repository: ghcr.io/paperclipinc/hermes-agent
tag: "v1.0.0"
storage:
persistence:
enabled: true
size: ""
`,
wantErrSubstring: "storage.persistence.size",
},
// ── selfConfigure.enabled=true requires protectedKeys ─────────────────────
{
name: "deny: selfConfigure enabled without protectedKeys",
yaml: `apiVersion: hermes.agent/v1
kind: HermesInstance
metadata:
name: neg-selfconfig-no-keys
spec:
image:
repository: ghcr.io/paperclipinc/hermes-agent
tag: "v1.0.0"
storage:
persistence:
enabled: true
size: 1Gi
selfConfigure:
enabled: true
allowedActions:
- skills
`,
wantErrSubstring: "protectedKeys",
},
// ── selfConfigure.enabled=true requires allowedActions ────────────────────
{
name: "deny: selfConfigure enabled without allowedActions",
yaml: `apiVersion: hermes.agent/v1
kind: HermesInstance
metadata:
name: neg-selfconfig-no-actions
spec:
image:
repository: ghcr.io/paperclipinc/hermes-agent
tag: "v1.0.0"
storage:
persistence:
enabled: true
size: 1Gi
selfConfigure:
enabled: true
protectedKeys:
- image
`,
wantErrSubstring: "allowedActions",
},
// ── selfConfigure unknown action ──────────────────────────────────────────
{
name: "deny: selfConfigure unknown action value",
yaml: `apiVersion: hermes.agent/v1
kind: HermesInstance
metadata:
name: neg-selfconfig-bad-action
spec:
image:
repository: ghcr.io/paperclipinc/hermes-agent
tag: "v1.0.0"
storage:
persistence:
enabled: true
size: 1Gi
selfConfigure:
enabled: true
protectedKeys:
- image
allowedActions:
- reboot-cluster
`,
wantErrSubstring: "reboot-cluster",
},
// ── PDB MinAvailable + MaxUnavailable mutually exclusive ──────────────────
{
name: "deny: PDB minAvailable and maxUnavailable both set",
yaml: `apiVersion: hermes.agent/v1
kind: HermesInstance
metadata:
name: neg-pdb-both
spec:
image:
repository: ghcr.io/paperclipinc/hermes-agent
tag: "v1.0.0"
storage:
persistence:
enabled: true
size: 1Gi
availability:
podDisruptionBudget:
enabled: true
minAvailable: "50%"
maxUnavailable: "1"
`,
wantErrSubstring: "podDisruptionBudget",
},
// ── HPA minReplicas > maxReplicas ─────────────────────────────────────────
{
name: "deny: HPA minReplicas > maxReplicas",
yaml: `apiVersion: hermes.agent/v1
kind: HermesInstance
metadata:
name: neg-hpa-bad-range
spec:
image:
repository: ghcr.io/paperclipinc/hermes-agent
tag: "v1.0.0"
storage:
persistence:
enabled: true
size: 1Gi
availability:
horizontalPodAutoscaler:
enabled: true
minReplicas: 10
maxReplicas: 2
`,
wantErrSubstring: "horizontalPodAutoscaler",
},
// ── restoreFrom + migration mutually exclusive ────────────────────────────
{
name: "deny: restoreFrom and migration.fromOpenClaw both set",
yaml: `apiVersion: hermes.agent/v1
kind: HermesInstance
metadata:
name: neg-restore-migration
spec:
image:
repository: ghcr.io/paperclipinc/hermes-agent
tag: "v1.0.0"
storage:
persistence:
enabled: true
size: 1Gi
restoreFrom: snapshot-abc123
migration:
fromOpenClaw:
mode: copy
source:
openclawInstanceRef:
name: old-openclaw
namespace: default
`,
wantErrSubstring: "spec",
},
// ── migration source exactly-one (both set) ───────────────────────────────
{
name: "deny: migration source openclawInstanceRef and backupRef both set",
yaml: `apiVersion: hermes.agent/v1
kind: HermesInstance
metadata:
name: neg-migration-both-sources
spec:
image:
repository: ghcr.io/paperclipinc/hermes-agent
tag: "v1.0.0"
storage:
persistence:
enabled: true
size: 1Gi
migration:
fromOpenClaw:
mode: copy
source:
openclawInstanceRef:
name: old-openclaw
namespace: default
backupRef:
s3:
bucket: my-bucket
endpoint: s3.example.com
key: backup-key
credentialsSecretRef:
name: s3-creds
`,
wantErrSubstring: "source",
},
// ── migration source neither set ─────────────────────────────────────────
{
name: "deny: migration source neither openclawInstanceRef nor backupRef",
yaml: `apiVersion: hermes.agent/v1
kind: HermesInstance
metadata:
name: neg-migration-no-source
spec:
image:
repository: ghcr.io/paperclipinc/hermes-agent
tag: "v1.0.0"
storage:
persistence:
enabled: true
size: 1Gi
migration:
fromOpenClaw:
mode: copy
source: {}
`,
wantErrSubstring: "source",
},
// ── immutable: storageClassName changed ──────────────────────────────────
{
name: "deny: storage.persistence.storageClassName changed (immutable)",
isUpdate: true,
baseYAML: `apiVersion: hermes.agent/v1
kind: HermesInstance
metadata:
name: neg-immutable-sc
spec:
image:
repository: ghcr.io/paperclipinc/hermes-agent
tag: "v1.0.0"
storage:
persistence:
enabled: true
size: 1Gi
storageClassName: gp3
`,
yaml: `apiVersion: hermes.agent/v1
kind: HermesInstance
metadata:
name: neg-immutable-sc
spec:
image:
repository: ghcr.io/paperclipinc/hermes-agent
tag: "v1.0.0"
storage:
persistence:
enabled: true
size: 1Gi
storageClassName: io2
`,
wantErrSubstring: "storageClassName",
},
// ── telegram gateway: enabled without botTokenSecretRef ──────────────────
{
name: "deny: telegram gateway enabled without botTokenSecretRef",
yaml: `apiVersion: hermes.agent/v1
kind: HermesInstance
metadata:
name: neg-telegram-no-secret
spec:
image:
repository: ghcr.io/paperclipinc/hermes-agent
tag: "v1.0.0"
storage:
persistence:
enabled: true
size: 1Gi
gateways:
telegram:
enabled: true
`,
wantErrSubstring: "botTokenSecretRef",
},
// ── discord gateway: enabled without botTokenSecretRef ───────────────────
{
name: "deny: discord gateway enabled without botTokenSecretRef",
yaml: `apiVersion: hermes.agent/v1
kind: HermesInstance
metadata:
name: neg-discord-no-secret
spec:
image:
repository: ghcr.io/paperclipinc/hermes-agent
tag: "v1.0.0"
storage:
persistence:
enabled: true
size: 1Gi
gateways:
discord:
enabled: true
`,
wantErrSubstring: "botTokenSecretRef",
},
// ── slack gateway: enabled without botTokenSecretRef ─────────────────────
{
name: "deny: slack gateway enabled without botTokenSecretRef",
yaml: `apiVersion: hermes.agent/v1
kind: HermesInstance
metadata:
name: neg-slack-no-secret
spec:
image:
repository: ghcr.io/paperclipinc/hermes-agent
tag: "v1.0.0"
storage:
persistence:
enabled: true
size: 1Gi
gateways:
slack:
enabled: true
`,
wantErrSubstring: "botTokenSecretRef",
},
// ── whatsapp gateway: enabled without providerSecretRef ──────────────────
{
name: "deny: whatsapp gateway enabled without providerSecretRef",
yaml: `apiVersion: hermes.agent/v1
kind: HermesInstance
metadata:
name: neg-whatsapp-no-secret
spec:
image:
repository: ghcr.io/paperclipinc/hermes-agent
tag: "v1.0.0"
storage:
persistence:
enabled: true
size: 1Gi
gateways:
whatsapp:
enabled: true
`,
wantErrSubstring: "providerSecretRef",
},
// ── signal gateway: enabled without phoneNumberSecretRef ─────────────────
{
name: "deny: signal gateway enabled without phoneNumberSecretRef",
yaml: `apiVersion: hermes.agent/v1
kind: HermesInstance
metadata:
name: neg-signal-no-phone
spec:
image:
repository: ghcr.io/paperclipinc/hermes-agent
tag: "v1.0.0"
storage:
persistence:
enabled: true
size: 1Gi
gateways:
signal:
enabled: true
`,
wantErrSubstring: "phoneNumberSecretRef",
},
// ── config.raw + configMapRef both set without mergeMode (warning only) ───
// This case emits a warning, not a denial. We assert no hard error but do
// verify the apply succeeds (round-trip test).
{
name: "warn (not deny): config.raw and configMapRef both set without mergeMode",
yaml: `apiVersion: hermes.agent/v1
kind: HermesInstance
metadata:
name: neg-config-both-warn
spec:
image:
repository: ghcr.io/paperclipinc/hermes-agent
tag: "v1.0.0"
storage:
persistence:
enabled: true
size: 1Gi
config:
raw: {}
configMapRef:
name: some-config
`,
// The webhook emits a warning but does NOT deny. We set wantErrSubstring
// empty to indicate success is expected: the test structure will use
// this sentinel to skip the error assertion.
wantErrSubstring: "",
},
// ── HermesSelfConfig: instanceRef points to non-existent HermesInstance ──
{
name: "deny: HermesSelfConfig instanceRef references missing HermesInstance",
yaml: `apiVersion: hermes.agent/v1
kind: HermesSelfConfig
metadata:
name: neg-selfconfig-missing-parent
spec:
instanceRef: does-not-exist
addSkills:
- source: hermes-skill-example
`,
wantErrSubstring: "instanceRef",
},
// ── HermesSelfConfig: addProfileSnapshot without honcho enabled ───────────
// This case requires a parent HermesInstance without profileStore.honcho enabled.
{
name: "deny: HermesSelfConfig addProfileSnapshot without honcho enabled on parent",
isUpdate: false,
baseYAML: hermesInstanceYAML("neg-parent-no-honcho", ""),
yaml: fmt.Sprintf(`apiVersion: hermes.agent/v1
kind: HermesSelfConfig
metadata:
name: neg-selfconfig-profile-no-honcho
spec:
instanceRef: %s
addProfileSnapshot:
profileID: my-profile
data: '{"preferences":{}}'
`, "neg-parent-no-honcho"),
wantErrSubstring: "profileStore.honcho",
},
}
var _ = Describe("webhook deny paths", Ordered, func() {
var ns string
BeforeAll(func() {
ns = freshNamespace("neg-test")
DeferCleanup(func() {
deleteNamespace(ns)
})
})
for _, tc := range negativeCases {
tc := tc // capture loop variable
It(tc.name, func() {
if tc.skip != "" {
Skip(tc.skip)
}
// For cases that need a pre-existing parent object, apply it first.
if tc.baseYAML != "" && !tc.isUpdate {
out, err := kubectlApply(addNamespace(tc.baseYAML, ns))
Expect(err).ToNot(HaveOccurred(), "applying base fixture: %s", out)
DeferCleanup(func() {
_, _ = kubectlDelete(addNamespace(tc.baseYAML, ns))
})
}
// For immutable-field update tests: apply base, then mutate.
if tc.isUpdate {
out, err := kubectlApply(addNamespace(tc.baseYAML, ns))
Expect(err).ToNot(HaveOccurred(), "applying base for update test: %s", out)
DeferCleanup(func() {
_, _ = kubectlDelete(addNamespace(tc.baseYAML, ns))
})
}
// Empty wantErrSubstring means "expect success (warning path)".
if tc.wantErrSubstring == "" {
out, err := kubectlApply(addNamespace(tc.yaml, ns))
Expect(err).ToNot(HaveOccurred(), "expected success but got error: %s", out)
DeferCleanup(func() {
_, _ = kubectlDelete(addNamespace(tc.yaml, ns))
})
return
}
out, err := kubectlApply(addNamespace(tc.yaml, ns))
Expect(err).To(HaveOccurred(), "expected webhook denial but apply succeeded: %s", out)
// kubectl writes the webhook denial message to stdout/stderr, which
// kubectlApply returns as `out`. The error itself is only the
// process exit status ("exit status 1"), so assert against `out`.
Expect(out).To(ContainSubstring(tc.wantErrSubstring),
"error message should mention %q; got: %s", tc.wantErrSubstring, out)
})
}
})
// addNamespace rewrites `namespace: <placeholder>` or injects a namespace
// into every resource that lacks one. Simple string injection for test fixtures.
func addNamespace(yaml, ns string) string {
// Insert namespace under metadata if not present.
result := strings.ReplaceAll(yaml, "\nmetadata:\n name:", "\nmetadata:\n namespace: "+ns+"\n name:")
return result
}