Skip to content

Commit b8df85a

Browse files
committed
Let controls update conf/secrets
Closes #181 Signed-off-by: Sylvain Hellegouarch <sh@defuze.org>
1 parent 9ada8d6 commit b8df85a

File tree

7 files changed

+122
-3
lines changed

7 files changed

+122
-3
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
[Unreleased]: https://github.com/chaostoolkit/chaostoolkit-lib/compare/1.11.0...HEAD
66

7+
## Changed
8+
9+
- Controls can now update/change configuration and secrets on the fly as per
10+
the specification [#181][181]
11+
12+
[181]: https://github.com/chaostoolkit/chaostoolkit-lib/issues/181
13+
714
## [1.11.0][] - 2020-07-06
815

916
[1.11.0]: https://github.com/chaostoolkit/chaostoolkit-lib/compare/1.10.0...1.11.0

chaoslib/control/python.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ def apply_python_control(level: str, control: Control, # noqa: C901
127127
if "secrets" in provider and "secrets" in sig.parameters:
128128
arguments["secrets"] = {}
129129
for s in provider["secrets"]:
130-
arguments["secrets"].update(secrets.get(s, {}).copy())
130+
arguments["secrets"].update(secrets.get(s, {}))
131131

132132
if "configuration" in sig.parameters:
133-
arguments["configuration"] = configuration.copy()
133+
arguments["configuration"] = configuration
134134

135135
if "state" in sig.parameters:
136136
arguments["state"] = state
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from chaoslib.types import Activity, Configuration, Run, Secrets
2+
3+
4+
def after_activity_control(context: Activity, state: Run,
5+
configuration: Configuration = None,
6+
secrets: Secrets = None, **kwargs):
7+
if context["name"] == "generate-token":
8+
configuration["my_token"] = state["output"]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from chaoslib.types import Activity, Configuration, Run, Secrets
2+
3+
4+
def after_activity_control(context: Activity, state: Run,
5+
configuration: Configuration = None,
6+
secrets: Secrets = None, **kwargs):
7+
if context["name"] == "generate-token":
8+
secrets["mytokens"]["my_token"] = state["output"]

tests/fixtures/experiments.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
PythonModuleProbeWithProcessStatusTolerance, \
1414
PythonModuleProbeWithProcessFailedStatusTolerance, \
1515
PythonModuleProbeWithProcesStdoutTolerance, \
16-
PythonModuleProbeWithHTTPStatusToleranceDeviation, FailProbe
16+
PythonModuleProbeWithHTTPStatusToleranceDeviation, FailProbe, \
17+
GenerateSecretTokenProbe, ReadSecretTokenProbe, \
18+
ReadSecretTokenFromSecretsProbe
1719

1820
Secrets = {}
1921

@@ -308,6 +310,44 @@
308310
}
309311
]
310312

313+
ExperimentWithControlsThatUpdatedConfiguration = deepcopy(ExperimentNoControls)
314+
ExperimentWithControlsThatUpdatedConfiguration["configuration"] = {
315+
"my_token": "UNSET"
316+
}
317+
ExperimentWithControlsThatUpdatedConfiguration["method"] = [
318+
deepcopy(GenerateSecretTokenProbe),
319+
deepcopy(ReadSecretTokenProbe)
320+
]
321+
ExperimentWithControlsThatUpdatedConfiguration["controls"] = [
322+
{
323+
"name": "dummy",
324+
"provider": {
325+
"type": "python",
326+
"module": "fixtures.controls.dummy_changed_configuration"
327+
}
328+
}
329+
]
330+
331+
ExperimentWithControlsThatUpdatedSecrets = deepcopy(ExperimentNoControls)
332+
ExperimentWithControlsThatUpdatedSecrets["secrets"] = {
333+
"mytokens": {
334+
"my_token": "UNSET"
335+
}
336+
}
337+
ExperimentWithControlsThatUpdatedSecrets["method"] = [
338+
deepcopy(GenerateSecretTokenProbe),
339+
deepcopy(ReadSecretTokenFromSecretsProbe)
340+
]
341+
ExperimentWithControlsThatUpdatedSecrets["controls"] = [
342+
{
343+
"name": "dummy",
344+
"provider": {
345+
"type": "python",
346+
"module": "fixtures.controls.dummy_changed_secrets"
347+
}
348+
}
349+
]
350+
311351
ExperimentWithArgumentsControls = deepcopy(ExperimentNoControls)
312352
ExperimentWithArgumentsControls["controls"] = [
313353
{

tests/fixtures/probes.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,3 +384,43 @@ def must_be_in_range(a: int, b: int, value: Any = None) -> bool:
384384
"func": "force_failed_activity"
385385
}
386386
}
387+
388+
389+
GenerateSecretTokenProbe = {
390+
"type": "probe",
391+
"name": "generate-token",
392+
"provider": {
393+
"type": "python",
394+
"module": "secrets",
395+
"func": "token_hex"
396+
}
397+
}
398+
399+
400+
ReadSecretTokenProbe = {
401+
"type": "action",
402+
"name": "use-token",
403+
"provider": {
404+
"type": "python",
405+
"module": "pprint",
406+
"func": "pformat",
407+
"arguments": {
408+
"object": "${my_token}"
409+
}
410+
}
411+
}
412+
413+
414+
ReadSecretTokenFromSecretsProbe = {
415+
"type": "action",
416+
"name": "use-token",
417+
"provider": {
418+
"type": "python",
419+
"module": "pprint",
420+
"func": "pformat",
421+
"secrets": ["mytokens"],
422+
"arguments": {
423+
"object": "${my_token}"
424+
}
425+
}
426+
}

tests/test_control.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,3 +554,19 @@ def test_controls_on_loaded_experiment():
554554
assert experiment["title"] == "BOOM I changed it"
555555
finally:
556556
cleanup_global_controls()
557+
558+
559+
def test_control_can_update_configuration():
560+
exp = deepcopy(experiments.ExperimentWithControlsThatUpdatedConfiguration)
561+
with controls("experiment", exp, context=exp):
562+
state = run_experiment(exp)
563+
564+
assert state["run"][0]["output"] != "UNSET"
565+
566+
567+
def test_control_can_update_secrets():
568+
exp = deepcopy(experiments.ExperimentWithControlsThatUpdatedSecrets)
569+
with controls("experiment", exp, context=exp):
570+
state = run_experiment(exp)
571+
572+
assert state["run"][0]["output"] != "UNSET"

0 commit comments

Comments
 (0)