Skip to content

Commit fb16a98

Browse files
authored
Add chaosaws.fis.probes.get_experiment (#118)
adds chaosaws.fis.probes.get_experiment so that people may retrieve the information of a given AWS FIS experiment run
1 parent b428f3b commit fb16a98

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
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
1515
- added `chaosaws.fis.actions.stop_experiment` to stop an AWS FIS experiment
16+
- added `chaosaws.fis.probes.get_experiment` to retrieve an AWS FIS experiments details
1617

1718
### Removed
1819
- Removed TravisCI related files

Diff for: chaosaws/fis/probes.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from chaoslib.exceptions import FailedActivity
2+
from chaoslib.types import Configuration, Secrets
3+
4+
from chaosaws import aws_client
5+
from chaosaws.types import AWSResponse
6+
7+
__all__ = ["get_experiment"]
8+
9+
10+
def get_experiment(
11+
experiment_id: str, configuration: Configuration = None, secrets: Secrets = None
12+
) -> AWSResponse:
13+
"""
14+
Gets information about the specified experiment.
15+
16+
:param experiment_id: str representing the id of the experiment to fetch information
17+
of
18+
:param configuration: Configuration object representing the CTK Configuration
19+
:param secrets: Secret object representing the CTK Secrets
20+
:returns: AWSResponse representing the response from FIS upon retrieving the
21+
experiment information
22+
23+
Examples
24+
--------
25+
>>> get_experiment(
26+
... experiment_id="EXPTUCK2dxepXgkR38"
27+
... )
28+
{'ResponseMetadata': {'RequestId': '0665fe39-2213-400b-b7ff-5f1ab9b7a5ea',
29+
'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Fri, 20 Aug 2021 11:08:27 GMT',
30+
...
31+
'experiment': {'id': 'EXPTUCK2dxepXgkR38',
32+
'experimentTemplateId': 'EXT6oWVA1WrLNy4XS',
33+
...
34+
}
35+
"""
36+
37+
if not experiment_id:
38+
raise FailedActivity(
39+
"You must pass a valid experiment id, id provided was empty"
40+
)
41+
42+
fis_client = aws_client(
43+
resource_name="fis", configuration=configuration, secrets=secrets
44+
)
45+
46+
try:
47+
return fis_client.get_experiment(id=experiment_id)
48+
except Exception as ex:
49+
raise FailedActivity(f"Get Experiment failed, reason was: {ex}")

Diff for: tests/fis/test_fis_actions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from chaosaws.fis.actions import start_experiment, stop_experiment
77

88

9-
def test_that_fis_modules___all___attribute_exposed_correctly():
9+
def test_that_fis_action_modules___all___attribute_exposed_correctly():
1010
import chaosaws.fis.actions as actions
1111

1212
all = actions.__all__

Diff for: tests/fis/test_fis_probes.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from unittest.mock import MagicMock, patch
2+
3+
import pytest
4+
from chaoslib.exceptions import FailedActivity
5+
6+
from chaosaws.fis.probes import get_experiment
7+
8+
9+
def test_that_fis_probe_modules___all___attribute_exposed_correctly():
10+
import chaosaws.fis.probes as probes
11+
12+
all = probes.__all__
13+
assert "get_experiment" in all
14+
15+
16+
@patch("chaosaws.fis.probes.aws_client", autospec=True)
17+
def test_that_get_experiment_invoked_correctly_if_only_given_experiment_id(aws_client):
18+
client = MagicMock()
19+
aws_client.return_value = client
20+
21+
get_experiment(experiment_id="an-id")
22+
client.get_experiment.assert_called_once_with(id="an-id")
23+
24+
25+
@patch("chaosaws.fis.probes.aws_client", autospec=True)
26+
def test_that_get_experiment_fails_if_experiment_id_empty_or_none(aws_client):
27+
client = MagicMock()
28+
aws_client.return_value = client
29+
30+
with pytest.raises(FailedActivity) as ex:
31+
get_experiment(experiment_id="")
32+
assert str(ex.value) == "You must pass a valid experiment id, id provided was empty"
33+
34+
with pytest.raises(FailedActivity) as ex:
35+
get_experiment(experiment_id=None)
36+
assert str(ex.value) == "You must pass a valid experiment id, id provided was empty"
37+
38+
39+
@patch("chaosaws.fis.probes.aws_client", autospec=True)
40+
def test_that_get_experiment_fails_if_exception_raised(aws_client):
41+
client = MagicMock()
42+
aws_client.return_value = client
43+
client.get_experiment.side_effect = Exception("Something went wrong")
44+
45+
with pytest.raises(FailedActivity) as ex:
46+
get_experiment(experiment_id="an-id")
47+
assert str(ex.value) == "Get Experiment failed, reason was: Something went wrong"
48+
49+
50+
@patch("chaosaws.fis.probes.aws_client", autospec=True)
51+
def test_that_get_experiment_returns_client_response(aws_client):
52+
client = MagicMock()
53+
aws_client.return_value = client
54+
resp = {"a-key", "a-value"}
55+
client.get_experiment.return_value = resp
56+
57+
actual_resp = get_experiment(experiment_id="an-id")
58+
assert actual_resp == resp

0 commit comments

Comments
 (0)