Skip to content

Commit b428f3b

Browse files
authored
Add chaosaws.fis.actions.stop_experiment (#117)
adds the chaosaws.fis.actions.stop_experiment function to stop AWS FIS experiments Signed-off-by: Ciaran Evans <[email protected]>
1 parent 829c446 commit b428f3b

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- added GitHub Actions Workflows for Build and Test, Build and Discover, and Releasing
1313
- added `Makefile` to abstract away common commands: `install`, `install-dev`, `lint`, `format`, `tests`
1414
- added `chaosaws.fis.actions.start_experiment` to start an AWS FIS experiment
15+
- added `chaosaws.fis.actions.stop_experiment` to stop an AWS FIS experiment
1516

1617
### Removed
1718
- Removed TravisCI related files

chaosaws/fis/actions.py

+38-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from chaosaws import aws_client
77
from chaosaws.types import AWSResponse
88

9-
__all__ = ["start_experiment"]
9+
__all__ = ["start_experiment", "stop_experiment"]
1010

1111

1212
def start_experiment(
@@ -70,3 +70,40 @@ def start_experiment(
7070
return fis_client.start_experiment(**params)
7171
except Exception as ex:
7272
raise FailedActivity(f"Start Experiment failed, reason was: {ex}")
73+
74+
75+
def stop_experiment(
76+
experiment_id: str,
77+
configuration: Configuration = None,
78+
secrets: Secrets = None,
79+
) -> AWSResponse:
80+
"""
81+
Stops the specified experiment.
82+
83+
:param experiment_id: str representing the running experiment to stop
84+
:param configuration: Configuration object representing the CTK Configuration
85+
:param secrets: Secret object representing the CTK Secrets
86+
:returns: AWSResponse representing the response from FIS upon stopping the
87+
experiment
88+
89+
Examples
90+
--------
91+
>>> stop_experiment(experiment_id="EXPTUCK2dxepXgkR38")
92+
{'ResponseMetadata': {'RequestId': 'e5e9f9a9-f4d0-4d72-8704-1f26cc8b6ad6',
93+
'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Fri, 13 Aug 2021 09:12:17 GMT',
94+
...'experiment': {'id': 'EXPTUCK2dxepXgkR38',
95+
'experimentTemplateId': 'EXT6oWVA1WrLNy4XS', ... }
96+
"""
97+
if not experiment_id:
98+
raise FailedActivity(
99+
"You must pass a valid experiment id, id provided was empty"
100+
)
101+
102+
fis_client = aws_client(
103+
resource_name="fis", configuration=configuration, secrets=secrets
104+
)
105+
106+
try:
107+
return fis_client.stop_experiment(id=experiment_id)
108+
except Exception as ex:
109+
raise FailedActivity(f"Stop Experiment failed, reason was: {ex}")

tests/fis/test_fis_actions.py

+51-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
import pytest
44
from chaoslib.exceptions import FailedActivity
55

6-
from chaosaws.fis.actions import start_experiment
6+
from chaosaws.fis.actions import start_experiment, stop_experiment
77

88

99
def test_that_fis_modules___all___attribute_exposed_correctly():
1010
import chaosaws.fis.actions as actions
1111

1212
all = actions.__all__
1313
assert "start_experiment" in all
14+
assert "stop_experiment" in all
1415

1516

1617
@patch("chaosaws.fis.actions.aws_client", autospec=True)
@@ -77,3 +78,52 @@ def test_that_start_experiment_returns_client_response(aws_client):
7778

7879
actual_resp = start_experiment(experiment_template_id="an-id")
7980
assert actual_resp == resp
81+
82+
83+
@patch("chaosaws.fis.actions.aws_client", autospec=True)
84+
def test_that_stop_experiment_invoked_correctly_when_given_experiment_id(aws_client):
85+
client = MagicMock()
86+
aws_client.return_value = client
87+
88+
stop_experiment(experiment_id="an-id")
89+
client.stop_experiment.assert_called_once_with(id="an-id")
90+
91+
92+
@patch("chaosaws.fis.actions.aws_client", autospec=True)
93+
def test_that_stop_experiment_fails_if_experiment_id_empty_or_none(aws_client):
94+
client = MagicMock()
95+
aws_client.return_value = client
96+
97+
with pytest.raises(FailedActivity) as ex:
98+
stop_experiment(experiment_id="")
99+
assert str(ex.value) == (
100+
"You must pass a valid experiment id, id provided was empty"
101+
)
102+
103+
with pytest.raises(FailedActivity) as ex:
104+
stop_experiment(experiment_id=None)
105+
assert str(ex.value) == (
106+
"You must pass a valid experiment id, id provided was empty"
107+
)
108+
109+
110+
@patch("chaosaws.fis.actions.aws_client", autospec=True)
111+
def test_that_stop_experiment_fails_if_exception_raised(aws_client):
112+
client = MagicMock()
113+
aws_client.return_value = client
114+
client.stop_experiment.side_effect = Exception("Something went wrong")
115+
116+
with pytest.raises(FailedActivity) as ex:
117+
stop_experiment(experiment_id="an-id")
118+
assert str(ex.value) == "Stop Experiment failed, reason was: Something went wrong"
119+
120+
121+
@patch("chaosaws.fis.actions.aws_client", autospec=True)
122+
def test_that_stop_experiment_returns_client_response(aws_client):
123+
client = MagicMock()
124+
aws_client.return_value = client
125+
resp = {"a-key": "a-value"}
126+
client.stop_experiment.return_value = resp
127+
128+
actual_resp = stop_experiment(experiment_id="an-id")
129+
assert actual_resp == resp

0 commit comments

Comments
 (0)