pubdeploy 0.8.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. pubdeploy/__init__.py +532 -0
  2. pubdeploy/channels.py +188 -0
  3. pubdeploy/cli.py +120 -0
  4. pubdeploy/config.py +140 -0
  5. pubdeploy/contract.py +87 -0
  6. pubdeploy/github.py +61 -0
  7. pubdeploy/hooks.py +82 -0
  8. pubdeploy/implied.py +179 -0
  9. pubdeploy/journal.json +207 -0
  10. pubdeploy/k8s.py +472 -0
  11. pubdeploy/machine.py +363 -0
  12. pubdeploy/model.py +229 -0
  13. pubdeploy/names.py +25 -0
  14. pubdeploy/order.py +101 -0
  15. pubdeploy/providers.py +75 -0
  16. pubdeploy/registry.py +96 -0
  17. pubdeploy/release-commit +1 -0
  18. pubdeploy/schema.json +127 -0
  19. pubdeploy/schema.py +238 -0
  20. pubdeploy/store.py +127 -0
  21. pubdeploy/testing/__init__.py +94 -0
  22. pubdeploy/testing/fixtures/deploy-app/.github/workflows/deploy.yml +11 -0
  23. pubdeploy/testing/fixtures/deploy-app/k8s/admin/network-policy.yaml +11 -0
  24. pubdeploy/testing/fixtures/deploy-app/k8s/app.yaml +63 -0
  25. pubdeploy/testing/fixtures/deploy-app/k8s/kustomization.yaml +6 -0
  26. pubdeploy/testing/fixtures/deploy-app/pubrules.json +27 -0
  27. pubdeploy/testing/fixtures/deploy-app/rendered.yaml +86 -0
  28. pubdeploy/testing/fixtures/deploy-app/zones.json +8 -0
  29. pubdeploy/testing/plugin.py +26 -0
  30. pubdeploy/testing/steps.py +1124 -0
  31. pubdeploy/testing/world.py +569 -0
  32. pubdeploy/types/__init__.py +181 -0
  33. pubdeploy/types/auth.py +199 -0
  34. pubdeploy/types/github.py +110 -0
  35. pubdeploy/types/k8s.py +609 -0
  36. pubdeploy/types/net.py +237 -0
  37. pubdeploy/types/rollout.py +186 -0
  38. pubdeploy/units.py +177 -0
  39. pubdeploy/walk.py +325 -0
  40. pubdeploy-0.8.0.dist-info/METADATA +520 -0
  41. pubdeploy-0.8.0.dist-info/RECORD +45 -0
  42. pubdeploy-0.8.0.dist-info/WHEEL +5 -0
  43. pubdeploy-0.8.0.dist-info/entry_points.txt +11 -0
  44. pubdeploy-0.8.0.dist-info/licenses/LICENSE +202 -0
  45. pubdeploy-0.8.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1124 @@
1
+ """The deploy-walk steps and fixtures, loaded as a pytest plugin (see
2
+ pubdeploy.testing): a plugin's steps reach every test module of the suite.
3
+ The world they act on is world.py's."""
4
+
5
+ from __future__ import annotations
6
+
7
+ import base64
8
+ import json
9
+ import shutil
10
+ import sys
11
+ import threading
12
+ import time
13
+ import types as modules
14
+ from datetime import timedelta
15
+ from pathlib import Path
16
+ from types import SimpleNamespace
17
+
18
+ import pytest
19
+ import yaml
20
+ from pytest_bdd import given, parsers, then, when
21
+
22
+ from pubdeploy import load
23
+ from pubdeploy.model import Result, Stop
24
+ from pubdeploy.schema import journal_schema, problems, validate
25
+ from pubdeploy.testing import fixture_path
26
+ from pubdeploy.testing.world import CANARY, NOW, World, run
27
+
28
+
29
+ def value_of(text: str):
30
+ return json.loads(text)
31
+
32
+
33
+ def entry(world: World, need_id: str) -> dict:
34
+ """The need the block lists with that id."""
35
+ from pubdeploy.registry import types # noqa: PLC0415
36
+ known = types()
37
+ for e in world.block["needs"]:
38
+ params = {k: v for k, v in e.items() if k not in ("type", "name", "after")}
39
+ name = e.get("name") or known[e["type"]].default_name(params) or world.block["namespace"]
40
+ if f"{e['type']}:{name}" == need_id:
41
+ return e
42
+ raise AssertionError(f"the block lists no {need_id}")
43
+
44
+
45
+ # --- the world -----------------------------------------------------------------
46
+
47
+
48
+ @pytest.fixture
49
+ def deploy_world_class() -> type[World]:
50
+ """The world `world` makes: a suite extends it by overriding this fixture."""
51
+ return World
52
+
53
+
54
+ @pytest.fixture
55
+ def world(deploy_world_class, tmp_path, monkeypatch, capsys):
56
+ """The world, and after the scenario the canary: no value reached an
57
+ output, the journal or an exception's text."""
58
+ root = tmp_path / "repo"
59
+ shutil.copytree(deploy_world_class.sample(), root)
60
+ w = deploy_world_class(root)
61
+ w.patch(monkeypatch)
62
+ yield w
63
+ w.outputs += capsys.readouterr()
64
+ assert not w.leaked(), f"a value was printed: {w.leaked()[0][:2000]}"
65
+
66
+
67
+ @given(parsers.parse('a file "{path}" copied from the sample "{source}"'))
68
+ def copied_from_sample(pubtest_ctx, path, source):
69
+ """pubtest's `copied from`, for the sample service, which is not in the
70
+ suite's repository: a file or a directory of it, to P."""
71
+ origin = fixture_path(source)
72
+ target = (pubtest_ctx.directory or Path.cwd()) / path
73
+ target.parent.mkdir(parents=True, exist_ok=True)
74
+ if origin.is_dir():
75
+ shutil.copytree(origin, target, dirs_exist_ok=True)
76
+ else:
77
+ shutil.copyfile(origin, target)
78
+
79
+
80
+ # --- the block ---------------------------------------------------------------
81
+
82
+
83
+ @given("the example service")
84
+ def example(world):
85
+ pass
86
+
87
+
88
+ @given(parsers.parse('the deploy block\'s "{key}" is {value}'))
89
+ def block_key(world, key, value):
90
+ if value == "absent":
91
+ world.block.pop(key, None)
92
+ else:
93
+ world.block[key] = value_of(value)
94
+
95
+
96
+ @given(parsers.parse("the deploy block is {value}"))
97
+ def block_is(world, value):
98
+ if value == "absent":
99
+ del world.config["deploy"]
100
+ else:
101
+ world.config["deploy"] = value_of(value)
102
+
103
+
104
+ @given(parsers.parse("the deploy block also needs {need}"))
105
+ def also_needs(world, need):
106
+ world.block["needs"].append(value_of(need))
107
+
108
+
109
+ @given(parsers.parse('the need "{need_id}" is dropped'))
110
+ def dropped(world, need_id):
111
+ world.block["needs"].remove(entry(world, need_id))
112
+
113
+
114
+ @given(parsers.parse('the need "{need_id}" is after "{other}"'))
115
+ def is_after(world, need_id, other):
116
+ entry(world, need_id).setdefault("after", []).append(other)
117
+
118
+
119
+ @given("the deploy block lists its needs the other way round")
120
+ def reversed_needs(world):
121
+ world.block["needs"].reverse()
122
+
123
+
124
+ @given(parsers.parse('pubrules.json\'s github_app block names the app "{app}"'))
125
+ def github_app(world, app):
126
+ upper = app.upper().replace("-", "_")
127
+ world.config["github_app"] = {slot: {"from": f"{app}-{slot.replace('_', '-')}", "to": f"{upper}_{slot.upper()}"}
128
+ for slot in ("id", "installation_id", "key")}
129
+
130
+
131
+ @given(parsers.parse('the secrets block sends only "{to}"'))
132
+ def only_secret(world, to):
133
+ world.config["secrets"] = [e for e in world.config["secrets"] if e["to"] == to]
134
+
135
+
136
+ @given("the example service's managed files are in step")
137
+ def in_step(world, capsys):
138
+ """pubrules init's files, then sync's. sync is given no --zones, so the
139
+ host's zone is in no hosting block it reads."""
140
+ from pubrules.cli import main # noqa: PLC0415
141
+ (world.root / "pubrules.json").unlink()
142
+ assert main(["init", "--kind=service", "--project=example"]) == 0
143
+ world.save()
144
+ assert main(["sync"]) == 0
145
+ world.config = json.loads((world.root / "pubrules.json").read_text()) # with the pubsec sync listed
146
+ world.outputs += capsys.readouterr()
147
+ world.zones = None
148
+
149
+
150
+ # --- packages offering need types ---------------------------------------------
151
+
152
+
153
+ class Thing:
154
+ type, grade, budget, offline = "example.thing", "none", 1.0, True
155
+ schema = {"type": "object", "additionalProperties": False, "properties": {"size": {"type": "integer"}}}
156
+
157
+ def default_name(self, params):
158
+ return None
159
+
160
+ def depends_on(self, need, needs):
161
+ return []
162
+
163
+ def check(self, need, ctx):
164
+ return Result("ready", "the thing is there")
165
+
166
+ def fulfil(self, need, ctx):
167
+ raise NotImplementedError("phase 2")
168
+
169
+
170
+ class Point:
171
+ """An entry point of the sustenaut.needs group, from a distribution."""
172
+
173
+ def __init__(self, name, value, dist):
174
+ self.name, self.value, self.dist = name, value, SimpleNamespace(name=dist)
175
+
176
+ def load(self):
177
+ import importlib # noqa: PLC0415
178
+ module, _, attr = self.value.partition(":")
179
+ return getattr(importlib.import_module(module), attr)
180
+
181
+
182
+ @pytest.fixture
183
+ def offered(request, monkeypatch):
184
+ """The entry points the walk loads (the world's providers, in a scenario with
185
+ a world), and more a scenario makes up, with their module."""
186
+ if "world" in request.fixturenames:
187
+ request.getfixturevalue("world") # its providers first, so these come on top
188
+ points: list[Point] = []
189
+ module = modules.ModuleType("example_needs")
190
+ monkeypatch.setitem(sys.modules, "example_needs", module)
191
+ import pubdeploy.registry # noqa: PLC0415
192
+ before = pubdeploy.registry.installed
193
+ monkeypatch.setattr(pubdeploy.registry, "installed",
194
+ lambda group: before(group) + (list(points) if group == "sustenaut.needs" else []))
195
+ return SimpleNamespace(points=points, module=module)
196
+
197
+
198
+ def offer(offered, dist, kind, attr):
199
+ setattr(offered.module, attr, kind)
200
+ offered.points.append(Point(kind.type if isinstance(getattr(kind, "type", None), str) else "thing",
201
+ f"example_needs:{attr}", dist))
202
+
203
+
204
+ @given(parsers.parse('the package "{dist}" offers the need type "{name}"'))
205
+ def offers(offered, dist, name):
206
+ kind = Thing()
207
+ kind.type = name
208
+ offer(offered, dist, kind, name.upper().replace(".", "_").replace("-", "_").removeprefix("EXAMPLE_"))
209
+
210
+
211
+ @given(parsers.parse('the package "{dist}" offers the need type "{name}", whose schema takes any key'))
212
+ def offers_open(offered, dist, name):
213
+ kind = Thing()
214
+ kind.type = name
215
+ kind.schema = {"type": "object"}
216
+ setattr(offered.module, "BAD", kind)
217
+ offered.points.append(Point("bad", "example_needs:BAD", dist))
218
+
219
+
220
+ @given(parsers.parse('the package "{dist}" offers a need type whose {part} is {value}'))
221
+ def offers_bad(offered, dist, part, value):
222
+ kind = Thing()
223
+ if part == "module":
224
+ offered.points.append(Point("thing", f"{value_of(value)}:THING", dist))
225
+ return
226
+ setattr(kind, part, value_of(value))
227
+ setattr(offered.module, "THING", kind)
228
+ offered.points.append(Point("thing", "example_needs:THING", dist))
229
+
230
+
231
+ # --- running -------------------------------------------------------------------
232
+
233
+
234
+ @when(parsers.re(r'I run pubdeploy (?P<args>(?:"[^"]*" ?)+)'), target_fixture="result")
235
+ def run_pubdeploy(world, pubtest_ctx, args):
236
+ run(world, pubtest_ctx, *args.strip().strip('"').split('" "'))
237
+ return pubtest_ctx.outcome
238
+
239
+
240
+ @when("I check the deploy block")
241
+ def check_block(world):
242
+ from pubdeploy.registry import types # noqa: PLC0415
243
+ world.problems = problems(world.config, world.root, types())
244
+
245
+
246
+ @when("I load the plan")
247
+ def load_plan(world):
248
+ world.plan = load(world.root, world.config, world.context())
249
+ assert not world.plan.problems, world.plan.problems
250
+
251
+
252
+ @when(parsers.parse('I load the plan for the namespace "{ns}"'))
253
+ def load_plan_in(world, ns):
254
+ world.plan = load(world.root, world.config, world.context(namespace=ns), namespace=ns)
255
+
256
+
257
+ @then(parsers.parse('the only problem is "{text}"'))
258
+ def only_problem(world, text):
259
+ assert world.problems == [text], world.problems
260
+
261
+
262
+ @then(parsers.parse('the only problem starts "{text}"'))
263
+ def only_problem_starts(world, text):
264
+ assert len(world.problems) == 1 and world.problems[0].startswith(text), world.problems
265
+
266
+
267
+ @then("the problems are")
268
+ def the_problems(world, datatable):
269
+ assert world.problems == [row[0] for row in datatable[1:]], world.problems
270
+
271
+
272
+ @then("there is no problem")
273
+ def no_problem(world):
274
+ assert world.problems == [], world.problems
275
+
276
+
277
+ def plan(world):
278
+ if world.plan is None:
279
+ load_plan(world)
280
+ return world.plan
281
+
282
+
283
+ @then(parsers.parse('the plan has {count:d} "{need_id}"'))
284
+ def plan_has(world, count, need_id):
285
+ assert list(plan(world).needs).count(need_id) == count, list(plan(world).needs)
286
+
287
+
288
+ @then(parsers.parse('the plan has no "{need_id}"'))
289
+ def plan_lacks(world, need_id):
290
+ assert need_id not in plan(world).needs, list(plan(world).needs)
291
+
292
+
293
+ @then("its needs are")
294
+ def needs_are(world, datatable):
295
+ got = [(i, n.implied_by or "") for i, n in plan(world).needs.items()]
296
+ assert got == [tuple(row) for row in datatable[1:]], got
297
+
298
+
299
+ @then("its needs include")
300
+ def needs_include(world, datatable):
301
+ got = {(i, n.implied_by or "") for i, n in plan(world).needs.items()}
302
+ for row in datatable[1:]:
303
+ assert tuple(row) in got, (row, sorted(got))
304
+
305
+
306
+ @then("the walk order is")
307
+ def walk_order(world, datatable):
308
+ assert plan(world).order == [row[0] for row in datatable[1:]], plan(world).order
309
+
310
+
311
+ @then("the walk order is the example's")
312
+ def walk_order_example(world, tmp_path):
313
+ fresh = type(world)(world.root)
314
+ assert plan(world).order == load(fresh.root, fresh.config, fresh.context()).order
315
+
316
+
317
+ @then(parsers.parse('"{later}" comes after "{earlier}"'))
318
+ def comes_after(world, later, earlier):
319
+ order = plan(world).order
320
+ assert order.index(earlier) < order.index(later), order
321
+
322
+
323
+ @then(parsers.parse('"{need_id}" has the keys "{keys}" and the shape "{shape}" for "{key}"'))
324
+ def has_keys(world, need_id, keys, shape, key):
325
+ params = plan(world).needs[need_id].params
326
+ assert params["keys"] == keys.split(", ") and params["shapes"] == {key: shape}, params
327
+
328
+
329
+ @then(parsers.parse('the need "{need_id}" was implied by "{by}"'))
330
+ def implied_by(world, need_id, by):
331
+ assert plan(world).needs[need_id].implied_by == by
332
+
333
+
334
+ @then(parsers.parse('the listing shows "{kind}" graded "{grade}", from "{dist}", taking "{keys}"'))
335
+ def listing(world, kind, grade, dist, keys):
336
+ import re # noqa: PLC0415
337
+ rows = [re.split(r"\s{2,}", line.strip()) for line in world.stdout.splitlines()]
338
+ row = next(r for r in rows if r[0] == kind)
339
+ assert (row[1], row[4], row[6]) == (grade, dist, keys), row
340
+
341
+
342
+ # --- the walk's results ---------------------------------------------------------
343
+
344
+
345
+ def need_lines(world, kind: str) -> list[str]:
346
+ """The walk's line of each need of a type."""
347
+ return [line for line in world.stdout.splitlines()
348
+ if not line.startswith(" ") and line.split()[1:2] and line.split()[1].startswith(kind + ":")]
349
+
350
+
351
+ def line_of(world, need_id: str) -> str:
352
+ for line in world.stdout.splitlines():
353
+ parts = line.split()
354
+ if len(parts) > 1 and parts[1] == need_id:
355
+ return line
356
+ raise AssertionError(f"{need_id} is not in:\n{world.stdout}")
357
+
358
+
359
+ @then(parsers.parse('"{need_id}" is {state}, "{text}"'))
360
+ def state_with(world, need_id, state, text):
361
+ line = line_of(world, need_id)
362
+ assert line.split()[0] == state and text in line, line
363
+
364
+
365
+ @then(parsers.parse('"{need_id}" is {state}'))
366
+ def state_is(world, need_id, state):
367
+ line = line_of(world, need_id)
368
+ assert line.split()[0] == state, line
369
+
370
+
371
+ @then(parsers.parse('every "{kind}" need is {state}, "{text}"'))
372
+ def every_with(world, kind, state, text):
373
+ lines = need_lines(world, kind)
374
+ assert lines and all(line.split()[0] == state and text in line for line in lines), lines
375
+
376
+
377
+ @then(parsers.parse('every "{kind}" need is {state}'))
378
+ def every_is(world, kind, state):
379
+ lines = need_lines(world, kind)
380
+ assert lines and all(line.split()[0] == state for line in lines), lines
381
+
382
+
383
+ @then("nothing reached kubectl, GitHub or the network")
384
+ def nothing_reached(world):
385
+ assert {k: world.calls[k] for k in ("kubectl", "gh", "http")} == {"kubectl": [], "gh": [], "http": []}, world.calls
386
+
387
+
388
+ @then("no output holds a value")
389
+ def no_value(world):
390
+ assert not world.leaked()
391
+
392
+
393
+ # --- the journal ----------------------------------------------------------------
394
+
395
+
396
+ @then("the journal is a deploy-journal/2 document")
397
+ def journal_valid(world):
398
+ assert validate(world.journal, journal_schema()) == [], validate(world.journal, journal_schema())
399
+
400
+
401
+ @then(parsers.parse('the journal\'s walk is offline, from "{origin}"'))
402
+ def journal_walk(world, origin):
403
+ assert world.journal["walk"]["offline"] is True and world.journal["walk"]["from"] == origin
404
+
405
+
406
+ @then(parsers.parse("the journal lists the {count:d} needs in walk order, none with params"))
407
+ def journal_needs(world, count):
408
+ fresh = type(world)(world.root)
409
+ order = load(fresh.root, fresh.config, fresh.context()).order
410
+ assert [n["id"] for n in world.journal["needs"]] == order and len(order) == count
411
+ assert all("params" not in n for n in world.journal["needs"])
412
+
413
+
414
+ @then(parsers.parse("the journal counts {ready:d} ready and {unknown:d} unknown"))
415
+ def journal_counts(world, ready, unknown):
416
+ assert world.journal["summary"] == {"ready": ready, "missing": 0, "drift": 0, "blocked": 0, "unknown": unknown}
417
+
418
+
419
+ # --- the cluster ------------------------------------------------------------------
420
+
421
+
422
+ def namespace(world, ns="example"):
423
+ return world.cluster["namespaces"][ns]
424
+
425
+
426
+ @given(parsers.parse('the namespace "{ns}" is not in the cluster'))
427
+ def no_namespace(world, ns):
428
+ del world.cluster["namespaces"][ns]
429
+
430
+
431
+ @given(parsers.parse('the namespace "{ns}" has no "{kind}" "{name}"'))
432
+ def no_object(world, ns, kind, name):
433
+ namespace(world, ns)[kind].discard(name)
434
+
435
+
436
+ @given(parsers.parse('the Secret "{secret}" holds "{key}" as 20 bytes'))
437
+ def holds_20(world, secret, key):
438
+ namespace(world)["secrets"][secret][key] = (CANARY + "k" * 9).encode()
439
+
440
+
441
+ @given(parsers.parse('the Secret "{secret}" has no key "{key}"'))
442
+ def lacks_key(world, secret, key):
443
+ data = namespace(world)["secrets"][secret]
444
+ data.pop(key)
445
+ data["other"] = f"{CANARY}-other".encode()
446
+
447
+
448
+ @given(parsers.parse('the cluster still holds the Secret "{secret}", which nothing reads or declares'))
449
+ def leftover_secret(world, secret):
450
+ namespace(world)["secrets"][secret] = {"key.json": f"{CANARY}-key".encode()}
451
+
452
+
453
+ @given(parsers.parse('the Secret "{secret}" is not in the cluster'))
454
+ def no_secret(world, secret):
455
+ del namespace(world)["secrets"][secret]
456
+
457
+
458
+ @given("kubectl may not read anything")
459
+ def kubectl_forbidden(world):
460
+ world.cluster["forbidden"] = True
461
+
462
+
463
+ @given("kubectl is not installed")
464
+ def kubectl_missing(world):
465
+ world.cluster["missing"] = True
466
+
467
+
468
+ @given(parsers.parse("kubectl takes {seconds:g} second to show a namespace"))
469
+ def kubectl_slow(world, seconds):
470
+ world.slow["namespace"] = seconds
471
+
472
+
473
+ @given(parsers.parse('the Deployment "{name}" has {ready:d} of {want:d} replicas ready'))
474
+ def deployment_late(world, name, ready, want):
475
+ d = next(d for d in namespace(world)["deployment"] if d["metadata"]["name"] == name)
476
+ d["spec"]["replicas"], d["status"]["readyReplicas"] = want, ready
477
+
478
+
479
+ @given(parsers.parse('the CronJob "{name}" last succeeded {hours:d} hours ago'))
480
+ def cronjob_old(world, name, hours):
481
+ namespace(world)["cronjob"][name]["status"]["lastSuccessfulTime"] = (NOW - timedelta(hours=hours)).strftime(
482
+ "%Y-%m-%dT%H:%M:%SZ")
483
+
484
+
485
+ @given(parsers.parse('the CronJob "{name}" never succeeded'))
486
+ def cronjob_never(world, name):
487
+ namespace(world)["cronjob"][name]["status"] = {}
488
+
489
+
490
+ # --- the manifests -----------------------------------------------------------------
491
+
492
+
493
+ @given(parsers.parse('every reference to "{secret}" in the manifests is optional'))
494
+ def optional(world, secret):
495
+ docs = list(yaml.safe_load_all(world.rendered))
496
+
497
+ def mark(node):
498
+ if isinstance(node, dict):
499
+ for key in ("secretRef", "secretKeyRef"):
500
+ if isinstance(node.get(key), dict) and node[key].get("name") == secret:
501
+ node[key]["optional"] = True
502
+ for v in node.values():
503
+ mark(v)
504
+ elif isinstance(node, list):
505
+ for v in node:
506
+ mark(v)
507
+ mark(docs)
508
+ world.rendered = yaml.safe_dump_all(docs)
509
+
510
+
511
+ @given(parsers.parse('the rendered manifests hold the Secret "{secret}"'))
512
+ def held(world, secret):
513
+ world.rendered += (f"---\napiVersion: v1\nkind: Secret\nmetadata: {{name: {secret}, namespace: example}}\n"
514
+ f"stringData: {{LOG_LEVEL: {CANARY}-info}}\n")
515
+
516
+
517
+ @given(parsers.parse('kustomize fails with "{message}"'))
518
+ def kustomize_fails(world, message):
519
+ world.render_error = Stop(f"kustomize build k8s failed: {message}")
520
+
521
+
522
+ @given("neither kustomize nor kubectl is installed")
523
+ def no_kustomize(world):
524
+ world.render_error = Stop("neither kustomize nor kubectl is installed, to render k8s", missing_tool=True)
525
+
526
+
527
+ @given("the overlay does not render")
528
+ def no_render(world):
529
+ world.render_error = Stop("kustomize build k8s failed: no")
530
+
531
+
532
+ @given(parsers.parse('the file "{path}" pins the keys {keys}'))
533
+ def pins_keys(world, path, keys):
534
+ """Keys as kid=n, comma-separated, as k8s-jwks.json pins them."""
535
+ pinned = [dict(zip(("kid", "n"), k.strip().split("="), strict=True)) for k in keys.split(",")]
536
+ target = world.root / path
537
+ target.parent.mkdir(parents=True, exist_ok=True)
538
+ target.write_text(json.dumps({"issuer": "https://kubernetes.default.svc.cluster.local",
539
+ "jwks": {"keys": [{**k, "kty": "RSA", "e": "AQAB"} for k in pinned]}}))
540
+
541
+
542
+ @given("the cluster's key set is not one")
543
+ def cluster_garbled(world):
544
+ world.cluster["jwks"] = "garbled"
545
+
546
+
547
+ @given(parsers.parse("the cluster signs with the keys {keys}"))
548
+ def cluster_signs(world, keys):
549
+ world.cluster["jwks"] = dict(k.strip().split("=") for k in keys.split(","))
550
+
551
+
552
+ @given(parsers.parse('the file "{path}" was committed {days:d} days ago'))
553
+ def committed_long_ago(world, path, days):
554
+ import os # noqa: PLC0415
555
+ import subprocess # noqa: PLC0415
556
+ import time # noqa: PLC0415
557
+ when = f"{int(time.time()) - days * 86400} +0000"
558
+ env = {**os.environ, "GIT_AUTHOR_DATE": when, "GIT_COMMITTER_DATE": when}
559
+ run = lambda *a: subprocess.run(["git", "-C", str(world.root), *a], check=True, capture_output=True, # noqa: E731
560
+ env=env)
561
+ if not (world.root / ".git").exists():
562
+ run("init", "-q")
563
+ run("add", path)
564
+ run("-c", "user.name=t", "-c", "user.email=t@example.org", "-c", "commit.gpgsign=false",
565
+ "commit", "-q", "-m", "pin", "--", path)
566
+
567
+
568
+ @given(parsers.parse("the clone was last fetched {hours:d} hours ago"))
569
+ def fetched_ago(world, hours):
570
+ import os # noqa: PLC0415
571
+ import time # noqa: PLC0415
572
+ fetch_head = world.root / ".git" / "FETCH_HEAD"
573
+ fetch_head.write_text("")
574
+ then = time.time() - hours * 3600
575
+ os.utime(fetch_head, (then, then))
576
+
577
+
578
+ @given(parsers.parse('the need "{need_id}" fills the key "{key}"'))
579
+ def fills_key(world, need_id, key):
580
+ entry(world, need_id)["secret"]["key"] = key
581
+
582
+
583
+ @given(parsers.parse('the need "{need_id}" fills no key'))
584
+ def fills_any(world, need_id):
585
+ entry(world, need_id)["secret"].pop("key")
586
+
587
+
588
+ @given("no hosting block keeps the zone")
589
+ def no_zones(world):
590
+ world.zones = None
591
+
592
+
593
+ @given("the host has no address")
594
+ def no_address(world):
595
+ world.addresses = []
596
+
597
+
598
+ # --- GitHub ---------------------------------------------------------------------------
599
+
600
+
601
+ @given("GitHub refuses every request")
602
+ def github_forbidden(world):
603
+ world.github["forbidden"] = True
604
+
605
+
606
+ @given("origin is not a GitHub repository")
607
+ def no_origin(world):
608
+ world.slug = ""
609
+
610
+
611
+ @given(parsers.parse('the variable "{name}" is "{value}"'))
612
+ def variable_is(world, name, value):
613
+ world.github["variables"][(None, name)] = value
614
+
615
+
616
+ @given(parsers.parse('the variable "{name}" is ""'))
617
+ def variable_empty(world, name):
618
+ world.github["variables"][(None, name)] = ""
619
+
620
+
621
+ @given(parsers.parse('the variable "{name}" is not set'))
622
+ def variable_unset(world, name):
623
+ del world.github["variables"][(None, name)]
624
+
625
+
626
+ @given(parsers.parse('GitHub holds no secret "{name}"'))
627
+ def no_github_secret(world, name):
628
+ world.github["secrets"] = {s for s in world.github["secrets"] if s[1] != name}
629
+
630
+
631
+ @given(parsers.parse('GitHub holds no variable "{name}"'))
632
+ def no_github_variable(world, name):
633
+ world.github["variables"] = {k: v for k, v in world.github["variables"].items() if k[1] != name}
634
+
635
+
636
+ @given(parsers.parse('main is at "{sha}"'))
637
+ def main_at(world, sha):
638
+ world.github["head"] = sha + "0" * 33
639
+
640
+
641
+ @given(parsers.parse('the last deploy run ended "{conclusion}"'))
642
+ def run_ended(world, conclusion):
643
+ world.github["runs"]["workflow_run"][0]["conclusion"] = conclusion
644
+
645
+
646
+ @given("the deploy workflow never ran")
647
+ def never_ran(world):
648
+ world.github["runs"] = {"push": [], "workflow_run": []}
649
+
650
+
651
+ # --- HTTP ---------------------------------------------------------------------------------
652
+
653
+
654
+ @given(parsers.parse('"{url}" answers {status:d}'))
655
+ def answers(world, url, status):
656
+ world.http[url] = status
657
+
658
+
659
+ @given(parsers.parse('"{url}" does not answer in time'))
660
+ def times_out(world, url):
661
+ world.http[url] = "timeout"
662
+
663
+
664
+ @given(parsers.parse('"{url}" refuses the connection'))
665
+ def refuses(world, url):
666
+ world.http.pop(url)
667
+
668
+
669
+ @given("nothing answers on the network")
670
+ def no_network(world):
671
+ world.http.clear()
672
+
673
+
674
+ # --- Bitwarden, through ./with-secrets ----------------------------------------------------------
675
+
676
+
677
+ def env_name(name: str) -> str:
678
+ return name.upper().replace("-", "_")
679
+
680
+
681
+ VALUES = {"20 bytes": (CANARY + "k" * 9),
682
+ "another value": f"{CANARY}-other",
683
+ "64 hex digits": "5e" * 32,
684
+ "base64 of 32 bytes": base64.b64encode(b"\x5e" * 32).decode(),
685
+ "base64 of 16 bytes": base64.b64encode(b"\x5e" * 16).decode()}
686
+
687
+
688
+ @given(parsers.parse('Bitwarden\'s "{name}" is {what}'))
689
+ def bitwarden_is(world, name, what):
690
+ world.env[env_name(name)] = VALUES[what]
691
+ if CANARY not in VALUES[what]:
692
+ world.values.append(VALUES[what])
693
+
694
+
695
+ @given(parsers.parse('Bitwarden has no "{name}"'))
696
+ def bitwarden_lacks(world, name):
697
+ del world.env[env_name(name)]
698
+
699
+
700
+ @given("the environment is not ./with-secrets'")
701
+ def outside(world):
702
+ del world.env[world.secrets_project]
703
+
704
+
705
+ @given(parsers.parse('the need "{need_id}" has the shape "{shape}"'))
706
+ def has_shape(world, need_id, shape):
707
+ entry(world, need_id)["shape"] = shape
708
+
709
+
710
+ # --- budgets ----------------------------------------------------------------------------------
711
+
712
+
713
+ def builtin(kind: str):
714
+ """The type the walk loads: pubrules's own, or pubsec's."""
715
+ from pubdeploy.registry import types # noqa: PLC0415
716
+ return types()[kind]
717
+
718
+
719
+ @given(parsers.parse('the "{kind}" check\'s budget is {seconds:g} seconds'))
720
+ def budget(monkeypatch, kind, seconds):
721
+ monkeypatch.setattr(builtin(kind), "budget", seconds)
722
+
723
+
724
+ @given(parsers.parse('the "{kind}" check takes {seconds:g} seconds, and the walk waits for it'))
725
+ def slow_check(monkeypatch, kind, seconds):
726
+ t = builtin(kind)
727
+ check = t.check
728
+
729
+ def slow(need, ctx):
730
+ time.sleep(seconds)
731
+ return check(need, ctx)
732
+ monkeypatch.setattr(t, "check", slow)
733
+ join = threading.Thread.join
734
+ monkeypatch.setattr(threading.Thread, "join", lambda self, timeout=None: join(self))
735
+
736
+
737
+ # --- fulfilling ---------------------------------------------------------------------------------
738
+
739
+
740
+ @given(parsers.parse('"{answer}" is typed at the prompt'))
741
+ def typed(world, answer):
742
+ world.answers.append(answer)
743
+
744
+
745
+ @given("the walk has no terminal")
746
+ def no_terminal(world):
747
+ world.terminal = False
748
+
749
+
750
+ @given("the walk runs in GitHub Actions")
751
+ def in_actions(world):
752
+ world.env["GITHUB_ACTIONS"] = "true"
753
+
754
+
755
+ @given("kubectl may write nothing")
756
+ def no_writes(world):
757
+ world.cluster["refuse_writes"] = True
758
+
759
+
760
+ @given("the walk reads the cluster over the deploy channel")
761
+ def deploy_channel(world):
762
+ world.channel = "deploy"
763
+
764
+
765
+ @given("the server has no secret-keys verb")
766
+ def no_secret_keys(world):
767
+ world.cluster["secret_keys_verb"] = False
768
+
769
+
770
+ @given("no command runs on the server")
771
+ def no_admin(world):
772
+ world.admin = False
773
+
774
+
775
+ @given(parsers.parse('a deploy run by hand ends "{conclusion}"'))
776
+ def dispatch_ends(world, conclusion):
777
+ world.github["dispatch_ends"] = conclusion
778
+
779
+
780
+ @given("Bitwarden refuses every write")
781
+ def bitwarden_refuses(world):
782
+ world.bitwarden_refuses = True
783
+
784
+
785
+ WRITES = ("apply", "rollout", "create", "wait")
786
+
787
+
788
+ @then("kubectl was given nothing to write")
789
+ def nothing_written(world):
790
+ assert not world.cluster.get("stdin"), "kubectl was given a manifest"
791
+ assert not [c for c in world.calls["kubectl"] if any(w in c for w in WRITES)], world.calls["kubectl"]
792
+
793
+
794
+ @then(parsers.parse('the cluster\'s Secret "{secret}" holds Bitwarden\'s "{name}" as "{key}"'))
795
+ def holds_bitwarden(world, secret, name, key):
796
+ assert namespace(world)["secrets"][secret] == {key: world.env[env_name(name)].encode()}
797
+ value = base64.b64encode(world.env[env_name(name)].encode())
798
+ assert any(value in given for given in world.cluster["stdin"]), "the value did not come on standard input"
799
+
800
+
801
+ @then(parsers.parse('the cluster\'s Secret "{secret}" holds what Bitwarden was given for "{name}" as "{key}"'))
802
+ def holds_written(world, secret, name, key):
803
+ assert namespace(world)["secrets"][secret][key] == world.written[name].encode()
804
+
805
+
806
+ @then("the write token was asked for once")
807
+ def token_once(world):
808
+ assert sum("Bitwarden access token" in o for o in world.outputs) == 1, world.outputs
809
+
810
+
811
+ @then("the write token was never asked for")
812
+ def token_never(world):
813
+ assert not any("Bitwarden access token" in o for o in world.outputs)
814
+
815
+
816
+ @then(parsers.parse('Bitwarden was given "{name}", {size:d} bytes'))
817
+ def bitwarden_given(world, name, size):
818
+ assert len(world.written[name]) == size, world.written.keys()
819
+
820
+
821
+ @then(parsers.parse('Bitwarden was given nothing'))
822
+ def bitwarden_nothing(world):
823
+ assert world.written == {}
824
+
825
+
826
+ @then(parsers.parse('the server ran "{command}"'))
827
+ def server_ran(world, command):
828
+ assert command.split() in [argv for argv, _ in world.calls.get("server", [])], world.calls.get("server")
829
+
830
+
831
+ @then(parsers.parse('deploy.yml was run on "{branch}" once'))
832
+ def dispatched_once(world, branch):
833
+ assert world.github.get("dispatches") == [("deploy.yml", branch)], world.github.get("dispatches")
834
+
835
+
836
+ @then(parsers.parse('the variable "{name}" is still "{value}"'))
837
+ def variable_still(world, name, value):
838
+ assert world.github["variables"][(None, name)] == value
839
+
840
+
841
+ @then(parsers.parse('gh set "{kind}" "{name}"'))
842
+ def gh_set(world, kind, name):
843
+ assert any(s[0] == kind and s[1] == name for s in world.calls["sent"]), world.calls["sent"]
844
+
845
+
846
+ @then(parsers.parse('a Job was made from the CronJob "{name}"'))
847
+ def job_made(world, name):
848
+ assert any(j.startswith(f"{name}-pubrules-") for j in world.cluster.get("jobs", [])), world.cluster.get("jobs")
849
+
850
+
851
+ # --- the phase 2 types ------------------------------------------------------------------------------
852
+
853
+
854
+ @given("the service signs in with a client of example.eu")
855
+ def signs_in(world):
856
+ (world.root.parent / "auth").mkdir(exist_ok=True)
857
+ (world.root.parent / "auth" / "register-app.py").write_text("print('the registration')\n")
858
+ (world.root.parent / "infra").mkdir(exist_ok=True)
859
+ (world.root.parent / "infra" / "approvals.json").write_text("{}\n")
860
+ world.block["needs"].append({"type": "auth.sign-in-client", "realm": "team", "role": "example",
861
+ "auth": "https://auth.example.eu/realms/team", "domain": "example.eu",
862
+ "state": "/root/.config/example-auth/team",
863
+ "approvals": "../infra/approvals.json", "secret": {"name": "example-oidc"}})
864
+
865
+
866
+ @given(parsers.re(r'the client is "(?P<status>[a-z]+)"$'))
867
+ def client_is(world, status):
868
+ world.auth = {"status": status}
869
+
870
+
871
+ @given(parsers.re(r'the client lacks this site.s (?P<what>redirect|role)$'))
872
+ def client_lacks(world, what):
873
+ world.site = {"redirect", "logout", "role"} - {what} - ({"logout"} if what == "redirect" else set())
874
+
875
+
876
+ @given("the client cannot be read for this site")
877
+ def client_unreadable(world):
878
+ world.site = None
879
+
880
+
881
+ @given(parsers.parse('the client is "waiting", with the record "{name}" "{value}"'))
882
+ def client_waits(world, name, value):
883
+ world.auth = {"status": "waiting", "record": {"name": name, "value": value}}
884
+
885
+
886
+ @given(parsers.parse('the Secret "{secret}" holds the client'))
887
+ def holds_client(world, secret):
888
+ namespace(world)["secrets"][secret] = {"client-id": f"{CANARY}-id".encode(),
889
+ "client-secret": f"{CANARY}-secret".encode()}
890
+
891
+
892
+ @then(parsers.parse('register-app.py came on the server\'s standard input, with "{flag}"'))
893
+ def script_on_stdin(world, flag):
894
+ runs = [(argv, given) for argv, given in world.calls["server"] if argv[:3] == ["python3", "-I", "-"]
895
+ and argv[3] == "--state-dir"]
896
+ assert runs and all(given == b"print('the registration')\n" for _, given in runs), runs
897
+ assert any(flag in argv for argv, _ in runs), runs
898
+
899
+
900
+ @then(parsers.parse('the approvals file holds "{value}"'))
901
+ def approvals_hold(world, value):
902
+ assert json.loads((world.root.parent / "infra" / "approvals.json").read_text()) == {"@": value}
903
+
904
+
905
+ TENANTS_PATH = "repos/example-org/storage/contents/tenants.json"
906
+
907
+
908
+ def tenants(world, given):
909
+ world.github.setdefault("others", {})[TENANTS_PATH] = {
910
+ "content": base64.b64encode(json.dumps(given).encode()).decode(), "encoding": "base64"}
911
+
912
+
913
+ @given("the service is a private tenant of the storage")
914
+ def tenant(world):
915
+ world.block["needs"].append({"type": "storage.tenant", "name": "example", "storage": "example-org/storage",
916
+ "url": "https://files.example.eu", "read": ["k8s:example/app"], "private": True})
917
+ world.tenants = {"example": {"private": True, "read": ["k8s:example/app"], "write": ["k8s:example/app"]}}
918
+ tenants(world, world.tenants)
919
+ world.http["https://files.example.eu/example/list?prefix=f/"] = 401
920
+
921
+
922
+ @given(parsers.parse('the storage\'s tenants.json has no "{name}"'))
923
+ def no_tenant(world, name):
924
+ del world.tenants[name]
925
+ tenants(world, world.tenants)
926
+
927
+
928
+ @given(parsers.parse('the storage\'s tenant "{name}" lets nothing read'))
929
+ def no_readers(world, name):
930
+ world.tenants[name]["read"] = []
931
+ tenants(world, world.tenants)
932
+
933
+
934
+ ENTRY_PATH = "repos/example-org/releases/actions/variables/RELEASE_STATE"
935
+
936
+
937
+ def release_state(world, state):
938
+ world.github.setdefault("others", {})[ENTRY_PATH] = {"name": "RELEASE_STATE", "value": json.dumps(state)}
939
+
940
+
941
+ @given("another repository's variable holds the entry")
942
+ def entry_held(world):
943
+ world.block["needs"].append({"type": "github.variable-entry", "repository": "example-org/releases",
944
+ "variable": "RELEASE_STATE", "path": "/evaluator", "value": {"version": "1.2.0"}})
945
+ world.state = {"evaluator": {"version": "1.2.0"}, "other": {"token": CANARY}}
946
+ release_state(world, world.state)
947
+
948
+
949
+ @given("the variable's entry is not there")
950
+ def entry_gone(world):
951
+ del world.state["evaluator"]
952
+ release_state(world, world.state)
953
+
954
+
955
+ @given("the variable's entry is another value")
956
+ def entry_other(world):
957
+ world.state["evaluator"] = {"version": CANARY}
958
+ release_state(world, world.state)
959
+
960
+
961
+ @given(parsers.parse('the CronJob "{name}" is suspended'))
962
+ def cronjob_suspended(world, name):
963
+ namespace(world)["cronjob"][name]["spec"]["suspend"] = True
964
+
965
+
966
+ @given(parsers.parse('the CronJob "{name}" runs on "{schedule}"'))
967
+ def cronjob_schedule(world, name, schedule):
968
+ namespace(world)["cronjob"][name]["spec"]["schedule"] = schedule
969
+
970
+
971
+ # --- units ---------------------------------------------------------------------------------------
972
+
973
+
974
+ @given(parsers.parse('the deploy block has the unit "{unit}", in "{ns}", from "{overlay}", gated by "{gate}"'))
975
+ def has_unit(world, unit, ns, overlay, gate):
976
+ world.block.setdefault("units", {})[unit] = {"namespace": ns, "overlay": overlay, "gate": gate}
977
+ (world.root / overlay).mkdir(exist_ok=True)
978
+ (world.root / overlay / "kustomization.yaml").write_text("resources: []\n")
979
+
980
+
981
+ @when(parsers.parse('I load the plan for the unit "{unit}"'))
982
+ def load_unit(world, unit):
983
+ world.plan = load(world.root, world.config, world.context(unit=unit), unit=unit)
984
+ assert not world.plan.problems, world.plan.problems
985
+
986
+
987
+ @then(parsers.parse('the journals are of the units "{units}"'))
988
+ def journals_of(world, units):
989
+ documents = world.journal if isinstance(world.journal, list) else [world.journal]
990
+ assert [d["service"]["unit"] for d in documents] == units.split(", ")
991
+ for d in documents:
992
+ assert validate(d, journal_schema()) == []
993
+
994
+
995
+ @then(parsers.parse('the journal of "{unit}" says "{need_id}" is {state}'))
996
+ def journal_of_says(world, unit, need_id, state):
997
+ document = next(d for d in world.journal if d["service"]["unit"] == unit)
998
+ assert next(n for n in document["needs"] if n["id"] == need_id)["state"] == state
999
+
1000
+
1001
+ def caller(version: str, deploys: str, push: bool, dispatch: bool = True) -> str:
1002
+ lines = "".join(f" {line.strip()}\n" for line in deploys.split(";"))
1003
+ on = "on:\n push: { branches: [main] }\n" if push else (
1004
+ "on:\n workflow_run: { workflows: [main], types: [completed], branches: [main] }\n"
1005
+ + (" workflow_dispatch: {}\n" if dispatch else ""))
1006
+ permissions = "permissions: { contents: read, packages: write }\n" if push else (
1007
+ "permissions: { actions: read, contents: read, packages: write }\n")
1008
+ return (f"name: deploy\n{on}{permissions}jobs:\n deploy:\n"
1009
+ f" uses: example-org/pubdeploy/.github/workflows/k8s-deploy.yml@{version}\n"
1010
+ f" with:\n image: ghcr.io/example-org/example\n deploys: |\n{lines} secrets: inherit\n")
1011
+
1012
+
1013
+ @given(parsers.re(r'the deploy workflow calls k8s-deploy.yml "(?P<version>[^"]+)" deploying "(?P<deploys>[^"]+)"'
1014
+ r'(?P<push>, on push)?(?P<only>, on workflow_run alone)?$'))
1015
+ def calls_k8s_deploy(world, version, deploys, push, only):
1016
+ (world.root / ".github" / "workflows" / "deploy.yml").write_text(caller(version, deploys, bool(push), not only))
1017
+
1018
+
1019
+ @when("I check the workflow against the units")
1020
+ def check_units(world):
1021
+ from pubdeploy.units import problems as unit_problems # noqa: PLC0415
1022
+ world.problems = unit_problems(world.root, world.block)
1023
+
1024
+
1025
+ @then(parsers.parse('the problem names "{first}", "{second}" and "{third}"'))
1026
+ def problem_names(world, first, second, third):
1027
+ assert all(line in world.problems[0] for line in (first, second, third)), world.problems
1028
+
1029
+
1030
+ # --- the journal in storage -------------------------------------------------------------------------
1031
+
1032
+
1033
+ @given(parsers.parse('storage holds the head "{path}" at "{etag}"'))
1034
+ def head_at(world, path, etag):
1035
+ world.storage["heads"][path] = etag
1036
+
1037
+
1038
+ @given("another walk moves the head first, once")
1039
+ def race(world):
1040
+ world.storage["race"] = True
1041
+
1042
+
1043
+ @given("storage refuses every write")
1044
+ def storage_refuses(world):
1045
+ world.storage["refuse"] = True
1046
+
1047
+
1048
+ @given("there is no storage token")
1049
+ def no_storage_token(world):
1050
+ del world.env["PUBRULES_STORAGE_TOKEN"]
1051
+
1052
+
1053
+ @then(parsers.parse('storage was given a deploy-journal/2 document as a file of "{tenant}"'))
1054
+ def storage_given(world, tenant):
1055
+ (given_tenant, document), = world.storage["files"]
1056
+ assert given_tenant == tenant and validate(document, journal_schema()) == []
1057
+
1058
+
1059
+ @then(parsers.parse('the head "{path}" was moved If-Match "{etag}"'))
1060
+ def moved(world, path, etag):
1061
+ assert world.storage["moves"] == [(path, "file-1", etag)], world.storage["moves"]
1062
+
1063
+
1064
+ @then(parsers.parse('the head was moved twice, the second time If-Match "{etag}"'))
1065
+ def moved_twice(world, etag):
1066
+ assert len(world.storage["moves"]) == 2 and world.storage["moves"][1][2] == etag, world.storage["moves"]
1067
+
1068
+
1069
+ @then(parsers.parse('the journal\'s service is the unit "{unit}"'))
1070
+ def journal_unit(world, unit):
1071
+ assert world.journal["service"]["unit"] == unit
1072
+
1073
+
1074
+ @then(parsers.parse('the journal\'s walk is a "{mode}"'))
1075
+ def journal_mode(world, mode):
1076
+ assert world.journal["walk"]["mode"] == mode
1077
+
1078
+
1079
+ @then(parsers.parse('the journal grades "{first}" "{grade}" and "{second}" "{other}"'))
1080
+ def journal_grades(world, first, grade, second, other):
1081
+ grades = {n["id"]: n["grade"] for n in world.journal["needs"]}
1082
+ assert (grades[first], grades[second]) == (grade, other), grades
1083
+
1084
+
1085
+ @then("the journal fulfilled nothing")
1086
+ def fulfilled_nothing(world):
1087
+ assert not any(n["fulfilled"] for n in world.journal["needs"])
1088
+
1089
+
1090
+ @then(parsers.parse('the journal fulfilled "{need_id}" only'))
1091
+ def fulfilled_only(world, need_id):
1092
+ assert [n["id"] for n in world.journal["needs"] if n["fulfilled"]] == [need_id]
1093
+
1094
+
1095
+ @given(parsers.parse('the Job "{job}", made from it by hand, completed {hours:d} hours ago'))
1096
+ def job_completed(world, job, hours):
1097
+ namespace(world).setdefault("job", []).append({"metadata": {"name": job}, "status": {
1098
+ "succeeded": 1, "completionTime": (NOW - timedelta(hours=hours)).strftime("%Y-%m-%dT%H:%M:%SZ")}})
1099
+
1100
+
1101
+ @given("the cluster's objects differ from the manifests")
1102
+ def objects_differ(world):
1103
+ world.cluster["diff_code"] = 1
1104
+ # kubectl diff names its live and merged copies under its own temporary
1105
+ # directory; the paths are only text here, and nothing reads them.
1106
+ world.cluster["diff_out"] = ("diff -u -N LIVE-1/networking.k8s.io.v1.NetworkPolicy.example.page-only "
1107
+ "MERGED-1/networking.k8s.io.v1.NetworkPolicy.example.page-only\n- a\n+ b\n")
1108
+
1109
+
1110
+ @given("the cluster refuses a server-side dry run")
1111
+ def dry_run_refused(world):
1112
+ world.cluster["diff_code"] = 2
1113
+ world.cluster["diff_err"] = 'Error from server (Forbidden): cannot patch resource "networkpolicies"'
1114
+
1115
+
1116
+ @given("the cluster's objects were applied client-side")
1117
+ def client_side(world):
1118
+ world.cluster["client_side_owned"] = True
1119
+
1120
+
1121
+ @given(parsers.parse('the "{kind}" fulfil is blocked, "{summary}"'))
1122
+ def fulfil_blocked(monkeypatch, kind, summary):
1123
+ from pubdeploy.model import Result # noqa: PLC0415
1124
+ monkeypatch.setattr(builtin(kind), "fulfil", lambda need, ctx: Result("blocked", summary, fix="grant the role"))