-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanup_test.py
186 lines (170 loc) · 6.46 KB
/
cleanup_test.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
#!/usr/bin/env python3
# Std libs
import unittest
# 1st party deps
from cleanup import should_pod_be_deleted, get_namespaces_to_check, get_pods_to_check
# 3rd party deps
from kubernetes import client
class TestCleanup(unittest.TestCase):
def test_should_pod_be_deleted_for_cannot_not(self):
pod = client.V1Pod()
pod.status = client.V1PodStatus()
pod.status.container_statuses = [
client.V1ContainerStatus(
image='test',
image_id='test',
name='test',
ready=False,
restart_count=0,
last_state=client.V1ContainerState(
terminated=client.V1ContainerStateTerminated(
reason='ContainerCannotRun',
exit_code=1,
)
)
)]
self.assertTrue(should_pod_be_deleted(pod))
def test_should_pod_be_deleted_for_other_reason(self):
pod = client.V1Pod()
pod.status = client.V1PodStatus()
pod.status.container_statuses = [
client.V1ContainerStatus(
image='test',
image_id='test',
name='test',
ready=False,
restart_count=0,
last_state=client.V1ContainerState(
terminated=client.V1ContainerStateTerminated(
reason='OtherReason',
exit_code=1,
)
)
)]
self.assertFalse(should_pod_be_deleted(pod))
def test_should_pod_be_deleted_for_running_pod(self):
pod = client.V1Pod()
pod.status = client.V1PodStatus()
pod.status.container_statuses = [
client.V1ContainerStatus(
image='test',
image_id='test',
name='test',
ready=True,
restart_count=0,
last_state=client.V1ContainerState(
running=client.V1ContainerStateRunning(
started_at='2021-09-01T00:00:00Z',
)
)
)]
self.assertFalse(should_pod_be_deleted(pod))
def test_get_namespaces_to_check(self):
# mock api.list_namespace
# assert that the correct namespaces are returned
api = client.CoreV1Api()
api.list_namespace = lambda: client.V1NamespaceList(
items=[
client.V1Namespace(
metadata=client.V1ObjectMeta(
name='test'
)
),
client.V1Namespace(
metadata=client.V1ObjectMeta(
name='kube-system'
)
),
client.V1Namespace(
metadata=client.V1ObjectMeta(
name='reboot-coordinator'
)
),
]
)
namespaces = list(get_namespaces_to_check(api))
self.assertEqual(len(namespaces), 1)
self.assertEqual(namespaces[0].metadata.name, 'test')
def test_get_pods_to_check(self):
# mock api.list_namespaced_pod
# assert that the correct pods are returned
api = client.CoreV1Api()
api.list_namespaced_pod = lambda namespace: client.V1PodList(
items=[
client.V1Pod(
metadata=client.V1ObjectMeta(
name='test'
),
status=client.V1PodStatus(
container_statuses=[
client.V1ContainerStatus(
image='test',
image_id='test',
name='test',
ready=False,
restart_count=0,
last_state=client.V1ContainerState(
terminated=client.V1ContainerStateTerminated(
reason='ContainerCannotRun',
exit_code=1,
)
)
)
]
)
),
client.V1Pod(
metadata=client.V1ObjectMeta(
name='test2'
),
status=client.V1PodStatus(
container_statuses=[
client.V1ContainerStatus(
image='test',
image_id='test',
name='test',
ready=False,
restart_count=0,
last_state=client.V1ContainerState(
terminated=client.V1ContainerStateTerminated(
reason='OtherReason',
exit_code=1,
)
)
)
]
)
),
client.V1Pod(
metadata=client.V1ObjectMeta(
name='test3'
),
status=client.V1PodStatus(
container_statuses=[
client.V1ContainerStatus(
image='test',
image_id='test',
name='test',
ready=True,
restart_count=0,
last_state=client.V1ContainerState(
running=client.V1ContainerStateRunning(
started_at='2021-09-01T00:00:00Z',
)
)
)
]
)
),
]
)
namespace = client.V1Namespace(
metadata=client.V1ObjectMeta(
name='test'
)
)
pods = list(get_pods_to_check(namespace, api))
self.assertEqual(len(pods), 1)
self.assertEqual(pods[0].metadata.name, 'test')
if __name__ == '__main__':
unittest.main()