Skip to content

Commit ceedc60

Browse files
committed
Rework actions tests
1 parent 2a1b66c commit ceedc60

File tree

4 files changed

+107
-35
lines changed

4 files changed

+107
-35
lines changed

tests/test_deployment.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# -*- coding: utf-8 -*-
2+
from unittest.mock import patch, MagicMock, ANY, call
3+
4+
import pytest
5+
from chaoslib.exceptions import ActivityFailed
6+
from kubernetes.client.models import V1DeploymentList, V1Deployment, V1ObjectMeta
7+
8+
from chaosk8s.deployment.actions import create_deployment, delete_deployment, scale_deployment
9+
10+
11+
@patch('chaosk8s.has_local_config_file', autospec=True)
12+
def test_cannot_process_other_than_yaml_and_json(has_conf):
13+
has_conf.return_value = False
14+
path = "./tests/fixtures/invalid-k8s.txt"
15+
with pytest.raises(ActivityFailed) as excinfo:
16+
create_deployment(spec_path=path)
17+
assert "cannot process {path}".format(path=path) in str(excinfo)
18+
19+
20+
@patch('builtins.open', autospec=True)
21+
@patch('chaosk8s.deployment.actions.json', autospec=True)
22+
@patch('chaosk8s.deployment.actions.create_k8s_api_client', autospec=True)
23+
@patch('chaosk8s.deployment.actions.client', autospec=True)
24+
def test_create_deployment(client, api, json, open):
25+
v1 = MagicMock()
26+
client.AppsV1beta1Api.return_value = v1
27+
json.loads.return_value = {"Kind": "Deployment"}
28+
29+
create_deployment(spec_path="depl.json")
30+
31+
v1.create_namespaced_deployment.assert_called_with(ANY, body=json.loads.return_value)
32+
33+
34+
@patch('chaosk8s.deployment.actions.create_k8s_api_client', autospec=True)
35+
@patch('chaosk8s.deployment.actions.client', autospec=True)
36+
def test_delete_deployment(client, api):
37+
depl1 = V1Deployment(metadata=V1ObjectMeta(name="depl1"))
38+
depl2 = V1Deployment(metadata=V1ObjectMeta(name="depl2"))
39+
v1 = MagicMock()
40+
client.AppsV1beta1Api.return_value = v1
41+
v1.list_namespaced_deployment.return_value = V1DeploymentList(items=(depl1, depl2))
42+
43+
delete_deployment("fake_name", "fake_ns")
44+
45+
v1.list_namespaced_deployment.assert_called_with("fake_ns", label_selector=ANY)
46+
v1.delete_namespaced_deployment.assert_has_calls(
47+
calls=[
48+
call(depl1.metadata.name, "fake_ns", ANY),
49+
call(depl2.metadata.name, "fake_ns", ANY)
50+
],
51+
any_order=True
52+
)
53+
54+
55+
@patch('chaosk8s.deployment.actions.create_k8s_api_client', autospec=True)
56+
@patch('chaosk8s.deployment.actions.client', autospec=True)
57+
def test_scale_deployment(client, api):
58+
v1 = MagicMock()
59+
client.ExtensionsV1beta1Api.return_value = v1
60+
61+
scale_deployment("fake", 3, "fake_ns")
62+
63+
body = {"spec": {"replicas": 3}}
64+
v1.patch_namespaced_deployment_scale.assert_called_with("fake", namespace="fake_ns", body=body)

tests/test_actions.py renamed to tests/test_node.py

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,10 @@
55
from chaoslib.exceptions import ActivityFailed
66
from kubernetes.client.rest import ApiException
77

8-
from chaosk8s.actions import start_microservice
98
from chaosk8s.node.actions import cordon_node, create_node, delete_nodes, \
109
uncordon_node, drain_nodes
1110

1211

13-
@patch('chaosk8s.has_local_config_file', autospec=True)
14-
def test_cannot_process_other_than_yaml_and_json(has_conf):
15-
has_conf.return_value = False
16-
path = "./tests/fixtures/invalid-k8s.txt"
17-
with pytest.raises(ActivityFailed) as excinfo:
18-
start_microservice(spec_path=path)
19-
assert "cannot process {path}".format(path=path) in str(excinfo)
20-
21-
2212
@patch('chaosk8s.has_local_config_file', autospec=True)
2313
@patch('chaosk8s.node.actions.client', autospec=True)
2414
@patch('chaosk8s.client')
@@ -92,31 +82,6 @@ def test_delete_nodes(cl, client, has_conf):
9282
v1.delete_node.assert_called_with("mynode", ANY, grace_period_seconds=None)
9383

9484

95-
@patch('chaosk8s.has_local_config_file', autospec=True)
96-
@patch('chaosk8s.node.actions.client', autospec=True)
97-
@patch('chaosk8s.client')
98-
def test_delete_nodes(cl, client, has_conf):
99-
has_conf.return_value = False
100-
101-
v1 = MagicMock()
102-
client.CoreV1Api.return_value = v1
103-
104-
node = MagicMock()
105-
node.metadata.name = "mynode"
106-
107-
result = MagicMock()
108-
result.items = [node]
109-
v1.list_node.return_value = result
110-
111-
res = MagicMock()
112-
res.status = "Success"
113-
v1.delete_node.return_value = res
114-
115-
delete_nodes(label_selector="k=mynode")
116-
117-
v1.delete_node.assert_called_with("mynode", ANY, grace_period_seconds=None)
118-
119-
12085
@patch('chaosk8s.has_local_config_file', autospec=True)
12186
@patch('chaosk8s.node.actions.client', autospec=True)
12287
@patch('chaosk8s.client')

tests/test_replicaset.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# -*- coding: utf-8 -*-
2+
from unittest.mock import patch, MagicMock, ANY, call
3+
4+
from kubernetes.client.models import V1ReplicaSetList, V1ReplicaSet, V1ObjectMeta
5+
6+
from chaosk8s.replicaset.actions import delete_replica_set
7+
8+
9+
@patch('chaosk8s.replicaset.actions.create_k8s_api_client', autospec=True)
10+
@patch('chaosk8s.replicaset.actions.client', autospec=True)
11+
def test_create_deployment(client, api):
12+
v1 = MagicMock()
13+
client.ExtensionsV1beta1Api.return_value = v1
14+
v1.list_namespaced_replica_set.return_value = V1ReplicaSetList(items=(
15+
V1ReplicaSet(metadata=V1ObjectMeta(name="repl1")),
16+
V1ReplicaSet(metadata=V1ObjectMeta(name="repl2"))
17+
))
18+
19+
delete_replica_set("fake", "fake_ns")
20+
21+
v1.list_namespaced_replica_set.assert_called_with("fake_ns", label_selector="name in (fake)")
22+
v1.delete_namespaced_replica_set.assert_has_calls(
23+
[
24+
call("repl1", "fake_ns", ANY),
25+
call("repl2", "fake_ns", ANY)
26+
],
27+
any_order=True
28+
)

tests/test_service.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*- coding: utf-8 -*-
2+
from unittest.mock import patch, MagicMock
3+
4+
from chaosk8s.service.actions import delete_service
5+
6+
7+
@patch('chaosk8s.service.actions.create_k8s_api_client', autospec=True)
8+
@patch('chaosk8s.service.actions.client', autospec=True)
9+
def test_delete_service(client, api):
10+
v1 = MagicMock()
11+
client.CoreV1Api.return_value = v1
12+
13+
delete_service("fake", "fake_ns")
14+
15+
v1.delete_namespaced_service.assert_called_with("fake", namespace="fake_ns")

0 commit comments

Comments
 (0)