Skip to content

Commit 40053de

Browse files
committed
Fix #251
Signed-off-by: Sylvain Hellegouarch <sh@defuze.org>
1 parent 6c95795 commit 40053de

File tree

4 files changed

+80
-5
lines changed

4 files changed

+80
-5
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.27.1...HEAD
66

7+
### Changed
8+
9+
- Ensure experiment level controls are applied to activity level controls
10+
when activity declares a control too [#251][251]
11+
12+
[251]: https://github.com/chaostoolkit/chaostoolkit-lib/issues/251
13+
714
## [1.27.1][] - 2022-02-25
815

916
[1.27.1]: https://github.com/chaostoolkit/chaostoolkit-lib/compare/1.27.0...1.27.1

chaoslib/control/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,19 +359,18 @@ def get_context_controls(
359359
if level in ["method", "rollback"]:
360360
return [deepcopy(c) for c in top_level_controls if c.get("automatic", True)]
361361

362-
for c in controls:
362+
for c in controls[:]:
363363
if "ref" in c:
364364
for top_level_control in top_level_controls:
365365
if c["ref"] == top_level_control["name"]:
366366
controls.append(deepcopy(top_level_control))
367367
break
368368
else:
369-
for tc in top_level_controls:
369+
for tc in reversed(top_level_controls):
370370
if c.get("name") == tc.get("name"):
371-
break
372-
else:
371+
continue
373372
if tc.get("automatic", True):
374-
controls.append(deepcopy(tc))
373+
controls.insert(0, deepcopy(tc))
375374

376375
return controls
377376

tests/fixtures/experiments.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,3 +854,63 @@
854854
"provider": {"type": "python", "module": "fixtures.controls.dummy_validator"},
855855
}
856856
]
857+
858+
859+
ExperimentWithTopLevelControlsAndActivityControl = {
860+
"title": "Hello world!",
861+
"description": "Say hello world.",
862+
"controls": [
863+
{
864+
"name": "tc1",
865+
"provider": {
866+
"type": "python",
867+
"module": "builtins",
868+
"func": "sum",
869+
"arguments": {"iterable": [4, 5]},
870+
},
871+
},
872+
{
873+
"name": "tc2",
874+
"automatic": False,
875+
"provider": {
876+
"type": "python",
877+
"module": "builtins",
878+
"func": "sum",
879+
"arguments": {"iterable": [6, 7]},
880+
},
881+
},
882+
{
883+
"name": "tc3",
884+
"provider": {
885+
"type": "python",
886+
"module": "builtins",
887+
"func": "sum",
888+
"arguments": {"iterable": [6, 7]},
889+
},
890+
},
891+
],
892+
"method": [
893+
{
894+
"type": "action",
895+
"name": "pretend-we-do-stuff",
896+
"background": True,
897+
"provider": {
898+
"type": "python",
899+
"module": "builtins",
900+
"func": "sum",
901+
"arguments": {"iterable": [1, 2]},
902+
},
903+
"controls": [
904+
{
905+
"name": "lc1",
906+
"provider": {
907+
"type": "python",
908+
"module": "builtins",
909+
"func": "sum",
910+
"arguments": {"iterable": [2, 3]},
911+
},
912+
}
913+
],
914+
},
915+
],
916+
}

tests/test_control.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,3 +584,12 @@ def test_control_can_validate_itself():
584584

585585
with pytest.raises(InvalidActivity):
586586
ensure_experiment_is_valid(exp)
587+
588+
589+
def test_activity_level_controls_are_merged_to_top_level_controls():
590+
x = deepcopy(experiments.ExperimentWithTopLevelControlsAndActivityControl)
591+
a = x["method"][0]
592+
controls = get_context_controls("activity", x, a)
593+
assert controls[0]["name"] == "tc1"
594+
assert controls[1]["name"] == "tc3"
595+
assert controls[2]["name"] == "lc1"

0 commit comments

Comments
 (0)