-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathtest_daemonset.py
215 lines (171 loc) · 7.02 KB
/
test_daemonset.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from unittest.mock import ANY, MagicMock, call, patch
import pytest
import urllib3
from chaoslib.exceptions import ActivityFailed
from kubernetes.client import V1DaemonSet, V1DaemonSetList
from kubernetes.client.models import V1ObjectMeta
from chaosk8s.daemonset.actions import (
create_daemon_set,
delete_daemon_set,
update_daemon_set,
)
from chaosk8s.daemonset.probes import (
daemon_set_available_and_healthy,
daemon_set_fully_available,
daemon_set_not_fully_available,
daemon_set_partially_available,
)
@patch("chaosk8s.has_local_config_file", autospec=True)
def test_cannot_process_other_than_yaml_and_json(has_conf):
has_conf.return_value = False
path = "./tests/fixtures/invalid-k8s.txt"
with pytest.raises(ActivityFailed) as excinfo:
create_daemon_set(spec_path=path)
assert f"cannot process {path}" in str(excinfo)
@patch("builtins.open", autospec=True)
@patch("chaosk8s.daemonset.actions.json", autospec=True)
@patch("chaosk8s.daemonset.actions.create_k8s_api_client", autospec=True)
@patch("chaosk8s.daemonset.actions.client", autospec=True)
def test_create_daemon_set(client, api, json, open):
v1 = MagicMock()
client.AppsV1Api.return_value = v1
json.loads.return_value = {"Kind": "Deployment"}
create_daemon_set(spec_path="depl.json")
v1.create_namespaced_daemon_set.assert_called_with(
ANY, body=json.loads.return_value
)
@patch("chaosk8s.daemonset.actions.create_k8s_api_client", autospec=True)
@patch("chaosk8s.daemonset.actions.client", autospec=True)
def test_delete_deployment(client, api):
depl1 = V1DaemonSet(metadata=V1ObjectMeta(name="depl1"))
depl2 = V1DaemonSet(metadata=V1ObjectMeta(name="depl2"))
v1 = MagicMock()
client.AppsV1Api.return_value = v1
v1.list_namespaced_daemon_set.return_value = V1DaemonSetList(items=(depl1, depl2))
delete_daemon_set("fake_name", "fake_ns")
v1.list_namespaced_daemon_set.assert_called_with(
"fake_ns", field_selector="metadata.name=fake_name"
)
v1.delete_namespaced_daemon_set.assert_has_calls(
calls=[
call(depl1.metadata.name, "fake_ns", body=ANY),
call(depl2.metadata.name, "fake_ns", body=ANY),
],
any_order=True,
)
@patch("chaosk8s.daemonset.actions.create_k8s_api_client", autospec=True)
@patch("chaosk8s.daemonset.actions.client", autospec=True)
def test_update_daemon_set(client, api):
v1 = MagicMock()
client.AppsV1Api.return_value = v1
spec = {
"spec": {
"containers": [
{
"image": "foobar:latest",
},
],
},
}
update_daemon_set("fake", spec, "fake_ns")
v1.patch_namespaced_daemon_set.assert_called_with(
name="fake", namespace="fake_ns", body=spec
)
@patch("chaosk8s.has_local_config_file", autospec=True)
@patch("chaosk8s.daemonset.probes.watch", autospec=True)
@patch("chaosk8s.daemonset.probes.client", autospec=True)
@patch("chaosk8s.client")
def test_daemon_set_is_fully_available_when_it_should_not(cl, client, watch, has_conf):
has_conf.return_value = False
daemonset = MagicMock()
daemonset.status.desired_number_scheduled = 2
daemonset.status.number_ready = 1
watcher = MagicMock()
watcher.stream = MagicMock()
watcher.stream.side_effect = urllib3.exceptions.ReadTimeoutError(None, None, None)
watch.Watch.return_value = watcher
assert daemon_set_not_fully_available("mysvc") is False
@patch("chaosk8s.has_local_config_file", autospec=True)
@patch("chaosk8s.daemonset.probes.client", autospec=True)
@patch("chaosk8s.client")
def test_expecting_a_healthy_daemon_set_should_be_reported_when_not(
cl, client, has_conf
):
has_conf.return_value = False
result = MagicMock()
result.items = []
v1 = MagicMock()
v1.list_namespaced_daemon_set.return_value = result
client.AppsV1Api.return_value = v1
assert daemon_set_available_and_healthy("mysvc") is False
daemonset = MagicMock()
daemonset.status.desired_number_scheduled = 2
daemonset.status.number_ready = 1
result.items.append(daemonset)
v1.list_namespaced_daemon_set.return_value = result
client.AppsV1Api.return_value = v1
assert daemon_set_available_and_healthy("mysvc") is False
@patch("chaosk8s.has_local_config_file", autospec=True)
@patch("chaosk8s.daemonset.probes.client", autospec=True)
@patch("chaosk8s.client")
def test_expecting_a_healthy_daemon_set(cl, client, has_conf):
has_conf.return_value = False
result = MagicMock()
result.items = []
v1 = MagicMock()
v1.list_namespaced_daemonset.return_value = result
client.AppsV1Api.return_value = v1
daemonset = MagicMock()
daemonset.status.desired_number_scheduled = 2
daemonset.status.number_ready = 2
result.items.append(daemonset)
assert daemon_set_available_and_healthy("mysvc") is True
@patch("chaosk8s.has_local_config_file", autospec=True)
@patch("chaosk8s.daemonset.probes.watch", autospec=True)
@patch("chaosk8s.daemonset.probes.client", autospec=True)
@patch("chaosk8s.client")
def test_daemon_set_is_fully_available(cl, client, watch, has_conf):
has_conf.return_value = False
daemonset = MagicMock()
daemonset.status.desired_number_scheduled = 2
daemonset.status.number_ready = 2
watcher = MagicMock()
watcher.stream = MagicMock()
watcher.stream.side_effect = [[{"object": daemonset, "type": "ADDED"}]]
watch.Watch.return_value = watcher
assert daemon_set_fully_available("mysvc") is True
@patch("chaosk8s.has_local_config_file", autospec=True)
@patch("chaosk8s.daemonset.probes.watch", autospec=True)
@patch("chaosk8s.daemonset.probes.client", autospec=True)
@patch("chaosk8s.client")
def test_daemon_set_is_not_fully_available_when_it_should(cl, client, watch, has_conf):
has_conf.return_value = False
daemonset = MagicMock()
daemonset.status.desired_number_scheduled = 2
daemonset.status.number_ready = 0
watcher = MagicMock()
watcher.stream = MagicMock()
watcher.stream.side_effect = [[{"object": daemonset, "type": "ADDED"}]]
watch.Watch.return_value = watcher
assert daemon_set_fully_available("mysvc") is False
@patch("chaosk8s.has_local_config_file", autospec=True)
@patch("chaosk8s.daemonset.probes.watch", autospec=True)
@patch("chaosk8s.daemonset.probes.client", autospec=True)
@patch("chaosk8s.client")
def test_daemon_set_is_not_fully_available(cl, client, watch, has_conf):
has_conf.return_value = False
result = MagicMock()
result.items = []
v1 = MagicMock()
v1.list_namespaced_daemon_set.return_value = result
client.AppsV1Api.return_value = v1
daemonset = MagicMock()
daemonset.status.desired_number_scheduled = 2
daemonset.status.number_ready = 1
result.items.append(daemonset)
watcher = MagicMock()
watcher.stream = MagicMock()
watcher.stream.side_effect = [[{"object": daemonset, "type": "ADDED"}]]
watch.Watch.return_value = watcher
assert daemon_set_partially_available("mysvc") is True
assert daemon_set_not_fully_available("mysvc") is True