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 ?
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 [

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))
```
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

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest pytest-cov pytest-sugar
3.7
requirements.txt requirements-dev.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
certifi @ file:///croot/certifi_1671487769961/work/certifi
-e git+https://github.com/chaostoolkit/chaostoolkit-lib.git@e18a9985454dbb91a278ea0c019a81db4f6da000#egg=chaostoolkit_lib
chardet==5.2.0
charset-normalizer==3.4.1
coverage==7.2.7
exceptiongroup==1.2.2
hvac==0.11.2
idna==3.10
importlib-metadata==6.7.0
iniconfig==2.0.0
jsonpath-ng==1.7.0
logzero==1.7.0
packaging==24.0
pluggy==1.2.0
ply==3.4
pycodestyle==2.10.0
pyhcl==0.2.3
pytest==7.4.4
pytest-cov==4.1.0
pytest-sugar==1.0.0
PyYAML==6.0.1
requests==2.31.0
requests-mock==1.12.1
six==1.17.0
termcolor==2.3.0
tomli==2.0.1
typing_extensions==4.7.1
urllib3==2.0.7
zipp==3.15.0

name: chaostoolkit-lib
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2022.12.7=py37h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=22.3.1=py37h06a4308_0
  - python=3.7.16=h7a1cb2a_0
  - readline=8.2=h5eee18b_0
  - setuptools=65.6.3=py37h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.38.4=py37h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - chardet==5.2.0
      - charset-normalizer==3.4.1
      - coverage==7.2.7
      - exceptiongroup==1.2.2
      - hvac==0.11.2
      - idna==3.10
      - importlib-metadata==6.7.0
      - iniconfig==2.0.0
      - jsonpath-ng==1.7.0
      - logzero==1.7.0
      - packaging==24.0
      - pluggy==1.2.0
      - ply==3.4
      - pycodestyle==2.10.0
      - pyhcl==0.2.3
      - pytest==7.4.4
      - pytest-cov==4.1.0
      - pytest-sugar==1.0.0
      - pyyaml==6.0.1
      - requests==2.31.0
      - requests-mock==1.12.1
      - six==1.17.0
      - termcolor==2.3.0
      - tomli==2.0.1
      - typing-extensions==4.7.1
      - urllib3==2.0.7
      - zipp==3.15.0
prefix: /opt/conda/envs/chaostoolkit-lib

tests/test_control.py::test_controls_are_applied_when_they_are_not_top_level
tests/test_control.py::test_initialize_controls_will_configure_a_control tests/test_control.py::test_initialize_controls_will_cleanup_a_control tests/test_control.py::test_controls_are_applied_before_and_after_experiment tests/test_control.py::test_controls_are_applied_before_and_but_not_after_experiment tests/test_control.py::test_controls_are_applied_not_before_and_but_after_experiment tests/test_control.py::test_controls_may_interrupt_experiment tests/test_control.py::test_controls_are_applied_before_and_after_hypothesis tests/test_control.py::test_controls_are_applied_before_and_after_method tests/test_control.py::test_controls_are_applied_before_and_after_rollbacks tests/test_control.py::test_controls_are_applied_before_and_after_activities tests/test_control.py::test_no_controls_get_applied_when_none_defined tests/test_control.py::test_automatic_goes_deep_down_the_tree tests/test_control.py::test_not_automatic_does_not_go_deep_down_the_tree tests/test_control.py::test_validate_python_control_must_be_loadable tests/test_control.py::test_validate_python_control_needs_a_module tests/test_control.py::test_controls_can_access_experiment tests/test_control.py::test_controls_are_applied_at_various_levels
CHANGELOG.md chaoslib/control/__init__.py
CHANGELOG.md chaoslib/control/__init__.py