Repository: chaostoolkit/chaostoolkit-lib

Issue:
Fix needed for activity-level controls to work as expected
Currently there is a bug when a `controls` block as applied at the activity level, i.e. not top level, and there are no top level controls applied either.

A fix such as the following needs to be applied to the chaos lib from line 201 onwards:

```
    for c in controls.copy():
        if "ref" in c:
            for top_level_control in top_level_controls:
                if c["ref"] == top_level_control["name"]:
                    controls.append(deepcopy(top_level_control))
                    break
        else:
            tc = None
            for tc in top_level_controls:
                if c.get("name") == tc.get("name"):
                    break
            else:
                if tc and tc.get("automatic", True):
                    controls.append(deepcopy(tc))
```

Discussion:
Lawouach: Right, so I took sometime to look into this one and I think there is a slightly better approach to avoid the whole block anyway. Do you want me to merge into your branch as I have also tests for this issue now? Or do you want me to close this one and open a different one associated with the issue?
russmiles: Merge into this one then let's squash and merge @Lawouach ?

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 88eefe3..835f999 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,14 @@
 
 [Unreleased]: https://github.com/chaostoolkit/chaostoolkit-lib/compare/1.1.0...HEAD
 
+### Changed
+
+- Fix a failure when no top-level controls were provided but an activity
+  control was declared [#88][88]
+
+[88]: https://github.com/chaostoolkit/chaostoolkit-lib/issues/88
+
+
 ## [1.1.0][] - 2019-03-11
 
 [1.1.0]: https://github.com/chaostoolkit/chaostoolkit-lib/compare/1.0.0...1.1.0
diff --git a/chaoslib/control/__init__.py b/chaoslib/control/__init__.py
index 4088e15..5439bac 100644
--- a/chaoslib/control/__init__.py
+++ b/chaoslib/control/__init__.py
@@ -179,17 +179,18 @@ def get_context_controls(level: str, experiment: Experiment,
     top-level ine.
     """
     top_level_controls = experiment.get("controls", [])
-
     controls = context.get("controls", [])
+
+    # do we even have something at the top level to be merged?
+    if not top_level_controls:
+        return controls
+
     if not controls:
-        if not top_level_controls:
-            return []
-        else:
-            return [
-                deepcopy(c)
-                for c in top_level_controls
-                if c.get("automatic", True)
-            ]
+        return [
+            deepcopy(c)
+            for c in top_level_controls
+            if c.get("automatic", True)
+        ]
 
     if level in ["method", "rollback"]:
         return [


Test patch:
diff --git a/tests/fixtures/experiments.py b/tests/fixtures/experiments.py
index 58df243..7667167 100644
--- a/tests/fixtures/experiments.py
+++ b/tests/fixtures/experiments.py
@@ -241,6 +241,31 @@ ExperimentWithControls = {
 }
 
 
+ExperimentWithControlsAtVariousLevels = deepcopy(ExperimentWithControls)
+ExperimentWithControlsAtVariousLevels["method"][0]["controls"] = [
+        {
+            "name": "dummy-two",
+            "provider": {
+                "type": "python",
+                "module": "fixtures.controls.dummy"
+            }
+        }
+    ]
+
+
+ExperimentWithControlNotAtTopLevel = deepcopy(ExperimentWithControls)
+ExperimentWithControlNotAtTopLevel.pop("controls")
+ExperimentWithControlNotAtTopLevel["method"][0]["controls"] = [
+        {
+            "name": "dummy",
+            "provider": {
+                "type": "python",
+                "module": "fixtures.controls.dummy"
+            }
+        }
+    ]
+
+
 ExperimentWithControlAccessingExperiment = deepcopy(ExperimentWithControls)
 ExperimentWithControlAccessingExperiment["controls"][0]["provider"]["module"] = "fixtures.controls.dummy_with_experiment"
 
diff --git a/tests/test_control.py b/tests/test_control.py
index dc6fd4b..2fe3430 100644
--- a/tests/test_control.py
+++ b/tests/test_control.py
@@ -241,4 +241,29 @@ def test_controls_can_access_experiment():
     activities = get_all_activities(exp)
     for activity in activities:
         assert activity["has_experiment_before"] is True
-        assert activity["has_experiment_after"] is True
\ No newline at end of file
+        assert activity["has_experiment_after"] is True
+
+
+def test_controls_are_applied_at_various_levels():
+    exp = deepcopy(experiments.ExperimentWithControlsAtVariousLevels)
+    exp["dry"] = True
+
+    run_experiment(exp)
+    activities = get_all_activities(exp)
+    for activity in activities:
+        print(activity)
+        if "controls" in activity:
+            assert activity["before_activity_control"] is True
+            assert activity["after_activity_control"] is True
+
+
+def test_controls_are_applied_when_they_are_not_top_level():
+    exp = deepcopy(experiments.ExperimentWithControlNotAtTopLevel)
+    exp["dry"] = True
+
+    run_experiment(exp)
+    activities = get_all_activities(exp)
+    for activity in activities:
+        if "controls" in activity:
+            assert activity["before_activity_control"] is True
+            assert activity["after_activity_control"] is True