forked from chaostoolkit-incubator/chaostoolkit-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_msk_actions.py
91 lines (69 loc) · 2.74 KB
/
test_msk_actions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from unittest.mock import MagicMock, patch
import pytest
from chaosaws.msk.actions import reboot_msk_broker, delete_cluster
from chaoslib.exceptions import FailedActivity
class NotFoundException(Exception):
def __init__(self, message="Cluster not found"):
super().__init__(f"{message}")
@patch("chaosaws.msk.actions.aws_client", autospec=True)
def test_reboot_msk_broker_success(aws_client):
client = MagicMock()
aws_client.return_value = client
cluster_arn = "arn_msk_cluster"
broker_ids = ["1"]
client.reboot_broker.return_value = {
"ResponseMetadata": {
"HTTPStatusCode": 200
}
}
response = reboot_msk_broker(cluster_arn=cluster_arn,
broker_ids=broker_ids)
client.reboot_broker.assert_called_with(ClusterArn=cluster_arn,
BrokerIds=broker_ids)
assert response["ResponseMetadata"]["HTTPStatusCode"] == 200
@patch("chaosaws.msk.actions.aws_client", autospec=True)
def test_reboot_msk_broker_not_found(aws_client):
client = MagicMock()
aws_client.return_value = client
cluster_arn = "arn_msk_cluster"
broker_ids = ["1"]
client.exceptions = MagicMock()
client.exceptions.NotFoundException = NotFoundException
client.reboot_broker.side_effect = NotFoundException(
"Cluster not found"
)
expected_error_message = "The specified cluster was not found"
with pytest.raises(FailedActivity) as exc_info:
reboot_msk_broker(cluster_arn=cluster_arn, broker_ids=broker_ids)
assert expected_error_message in str(
exc_info.value
)
@patch("chaosaws.msk.actions.aws_client", autospec=True)
def test_delete_cluster_success(aws_client):
client = MagicMock()
aws_client.return_value = client
cluster_arn = "arn_msk_cluster"
client.delete_cluster.return_value = {
"ResponseMetadata": {
"HTTPStatusCode": 200
}
}
response = delete_cluster(cluster_arn=cluster_arn)
client.delete_cluster.assert_called_with(ClusterArn=cluster_arn)
assert response["ResponseMetadata"]["HTTPStatusCode"] == 200
@patch("chaosaws.msk.actions.aws_client", autospec=True)
def test_delete_cluster_not_found(aws_client):
client = MagicMock()
aws_client.return_value = client
cluster_arn = "arn_msk_cluster"
client.exceptions = MagicMock()
client.exceptions.NotFoundException = NotFoundException
client.delete_cluster.side_effect = NotFoundException(
"Cluster not found"
)
expected_error_message = "The specified cluster was not found"
with pytest.raises(FailedActivity) as exc_info:
delete_cluster(cluster_arn=cluster_arn)
assert expected_error_message in str(
exc_info.value
)