forked from powerfulseal/powerfulseal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_pod_scenario.py
180 lines (162 loc) · 4.96 KB
/
test_pod_scenario.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
# Copyright 2017 Bloomberg Finance L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import random
import pytest
from mock import MagicMock
# noinspection PyUnresolvedReferences
from tests.fixtures import pod_scenario
from tests.fixtures import make_dummy_object
def test_matching_namespace(pod_scenario):
a, b = make_dummy_object(), make_dummy_object()
pod_scenario.schema = {
"match": [
{
"namespace": {
"name": "something",
}
},
]
}
pod_scenario.k8s_inventory.find_pods = MagicMock(return_value=[a, b])
matched = pod_scenario.match()
assert matched == list(set([a, b]))
assert pod_scenario.k8s_inventory.find_pods.call_args[1] == {"namespace": "something"}
def test_matching_deployment(pod_scenario):
a, b = make_dummy_object(), make_dummy_object()
pod_scenario.schema = {
"match": [
{
"deployment": {
"name": "lol",
"namespace": "something",
}
},
]
}
pod_scenario.k8s_inventory.find_pods = MagicMock(return_value=[a, b])
matched = pod_scenario.match()
assert matched == list(set([a, b]))
assert pod_scenario.k8s_inventory.find_pods.call_args[1] == {
"deployment_name": "lol",
"namespace": "something"
}
def test_matching_labels(pod_scenario):
a, b = make_dummy_object(), make_dummy_object()
pod_scenario.schema = {
"match": [
{
"labels": {
"selector": "yes=true",
"namespace": "something",
}
},
]
}
pod_scenario.k8s_inventory.find_pods = MagicMock(return_value=[a, b])
matched = pod_scenario.match()
assert matched == list(set([a, b]))
assert pod_scenario.k8s_inventory.find_pods.call_args[1] == {
"selector": "yes=true",
"namespace": "something"
}
@pytest.mark.parametrize("should_force", [
(True,),
(False,)
])
def test_kills_correctly_with_force(pod_scenario, should_force):
pod_scenario.schema = {
"actions": [
{
"kill": {
"force": should_force
}
},
]
}
mock = MagicMock(return_value={
"some ip": {
"ret_code": 1
},
})
pod_scenario.executor.execute = mock
mock_item1 = MagicMock()
mock_item1.container_ids = ["docker://container1"]
mock_item2 = MagicMock()
mock_item2.container_ids = ["docker://container1"]
items = [mock_item1, mock_item2]
pod_scenario.act(items)
assert mock.call_count == 2
template = "sudo docker kill -s {signal} {container_id}"
for i, call in enumerate(mock.call_args_list):
args, kwargs = call
cmd = template.format(
signal="SIGKILL" if should_force else "SIGTERM",
container_id=items[i].container_ids[0].replace("docker://","")
)
assert args[0] == cmd
@pytest.mark.parametrize("prob", [
0.3,
0.1,
0.5,
0.9
])
def test_kills_with_probability(pod_scenario, prob):
random.seed(12)
sample = 10000
pod_scenario.schema = {
"actions": [
{
"kill": {
"probability": prob,
}
},
]
}
mock = MagicMock(return_value={
"some ip": {
"ret_code": 0
},
})
pod_scenario.executor.execute = mock
mock_item1 = MagicMock()
mock_item1.container_ids = ["docker://container1"]
items = [mock_item1]
for _ in range(sample):
pod_scenario.act(items)
actual_prob = float(mock.call_count / len(items)) / sample
assert (actual_prob == pytest.approx(prob, 0.05))
def test_doesnt_kill_when_cant_find_node(pod_scenario):
pod_scenario.schema = {
"actions": [
{
"kill": {
"force": True
}
},
]
}
pod_scenario.inventory.get_node_by_ip = MagicMock(return_value=None)
pod_scenario.logger = MagicMock()
mock = MagicMock(return_value={
"some ip": {
"ret_code": 1
},
})
pod_scenario.executor.execute = mock
mock_item1 = MagicMock()
mock_item2 = MagicMock()
items = [mock_item1, mock_item2]
pod_scenario.act(items)
assert mock.call_count == 0
assert pod_scenario.logger.info.call_args[0] == ("Node not found for pod: %s", items[1])