-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
test.py
237 lines (218 loc) · 13.9 KB
/
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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import yaml
import unittest
import sys
import logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)
def load_template(yaml_file):
try:
with open(yaml_file, 'r') as file:
documents = yaml.safe_load_all(file)
list_of_documents = [doc for doc in documents]
return list_of_documents
except yaml.YAMLError as error:
print("Error in configuration file: ", error)
class ChartTemplateTests(unittest.TestCase):
def test_set_affinity(self):
resources_name = ['{0}selenium-chrome-node'.format(RELEASE_NAME),
'{0}selenium-distributor'.format(RELEASE_NAME),
'{0}selenium-edge-node'.format(RELEASE_NAME),
'{0}selenium-firefox-node'.format(RELEASE_NAME),
'{0}selenium-event-bus'.format(RELEASE_NAME),
'{0}selenium-router'.format(RELEASE_NAME),
'{0}selenium-session-map'.format(RELEASE_NAME),
'{0}selenium-session-queue'.format(RELEASE_NAME)]
count = 0
logger.info(f"Assert affinity is set in global and nodes")
for doc in LIST_OF_DOCUMENTS:
if doc['metadata']['name'] in resources_name and doc['kind'] == 'Deployment':
logger.info(f"Assert affinity is set in resource {doc['metadata']['name']}")
self.assertTrue(doc['spec']['template']['spec']['affinity']['podAffinity']['requiredDuringSchedulingIgnoredDuringExecution'][0]['labelSelector']['matchExpressions'] is not None)
count += 1
self.assertEqual(count, len(resources_name), "Not all resources have affinity set")
def test_ingress_nginx_annotations(self):
resources_name = ['{0}selenium-ingress'.format(RELEASE_NAME)]
count = 0
for doc in LIST_OF_DOCUMENTS:
if doc['metadata']['name'] in resources_name and doc['kind'] == 'Ingress':
logger.info(f"Assert ingress ingress annotations")
logger.info(f"Config `ingress.nginx.proxyTimeout` is able to be set a different value")
self.assertTrue(doc['metadata']['annotations']['nginx.ingress.kubernetes.io/proxy-read-timeout'] == '360')
logger.info(f"Duplicated in `ingress.annotations` take precedence to overwrite the default value")
self.assertTrue(doc['metadata']['annotations']['nginx.ingress.kubernetes.io/proxy-connect-timeout'] == '3600')
logger.info(f"Default annotation is able to be disabled by setting it to null")
self.assertTrue(doc['metadata']['annotations'].get('nginx.ingress.kubernetes.io/proxy-buffers-number') is None)
logger.info(f"Default annotation is added if no override value")
self.assertTrue(doc['metadata']['annotations']['nginx.ingress.kubernetes.io/client-body-buffer-size'] == '512M')
count += 1
self.assertEqual(count, len(resources_name), "No ingress resources found")
def test_sub_path_append_to_node_grid_url(self):
resources_name = ['{0}selenium-node-config'.format(RELEASE_NAME)]
count = 0
for doc in LIST_OF_DOCUMENTS:
if doc['metadata']['name'] in resources_name and doc['kind'] == 'ConfigMap':
logger.info(f"Assert subPath is appended to Node env SE_NODE_GRID_URL")
self.assertTrue(doc['data']['SE_NODE_GRID_URL'] == 'https://sysadmin:strongPassword@10.10.10.10:8443/selenium')
count += 1
self.assertEqual(count, len(resources_name), "No node config resources found")
def test_sub_path_set_to_grid_env_var(self):
resources_name = ['{0}selenium-router'.format(RELEASE_NAME)]
is_present = False
for doc in LIST_OF_DOCUMENTS:
if doc['metadata']['name'] in resources_name and doc['kind'] == 'Deployment':
logger.info(f"Assert subPath is set to Router env SE_SUB_PATH")
list_env = doc['spec']['template']['spec']['containers'][0]['env']
for env in list_env:
if env['name'] == 'SE_SUB_PATH' and env['value'] == '/selenium':
is_present = True
self.assertTrue(is_present, "ENV variable SE_SUB_PATH is not populated")
def test_distributor_new_session_thread_pool_size(self):
resources_name = ['{0}selenium-distributor'.format(RELEASE_NAME)]
is_present = False
for doc in LIST_OF_DOCUMENTS:
if doc['metadata']['name'] in resources_name and doc['kind'] == 'Deployment':
logger.info(f"Assert newSessionThreadPoolSize is set to Distributor env SE_NEW_SESSION_THREAD_POOL_SIZE")
list_env = doc['spec']['template']['spec']['containers'][0]['env']
for env in list_env:
if env['name'] == 'SE_NEW_SESSION_THREAD_POOL_SIZE' and env['value'] == '24':
is_present = True
self.assertTrue(is_present, "ENV variable SE_NEW_SESSION_THREAD_POOL_SIZE is not populated")
def test_disable_ui_set_to_grid_env_var(self):
resources_name = ['{0}selenium-router'.format(RELEASE_NAME)]
is_present = False
for doc in LIST_OF_DOCUMENTS:
if doc['metadata']['name'] in resources_name and doc['kind'] == 'Deployment':
logger.info(f"Assert option disable UI is set to Router env SE_DISABLE_UI")
list_env = doc['spec']['template']['spec']['containers'][0]['env']
for env in list_env:
if env['name'] == 'SE_DISABLE_UI' and env['value'] == 'true':
is_present = True
self.assertTrue(is_present, "ENV variable SE_DISABLE_UI is not populated")
def test_log_level_set_to_logging_config_map(self):
resources_name = ['{0}selenium-chrome-node'.format(RELEASE_NAME),
'{0}selenium-distributor'.format(RELEASE_NAME),
'{0}selenium-edge-node'.format(RELEASE_NAME),
'{0}selenium-firefox-node'.format(RELEASE_NAME),
'{0}selenium-event-bus'.format(RELEASE_NAME),
'{0}selenium-router'.format(RELEASE_NAME),
'{0}selenium-session-map'.format(RELEASE_NAME),
'{0}selenium-session-queue'.format(RELEASE_NAME)]
logger.info(f"Assert log level value is set to logging ConfigMap")
count_config = 0
for doc in LIST_OF_DOCUMENTS:
if doc['metadata']['name'] == '{0}selenium-logging-config'.format(RELEASE_NAME) and doc['kind'] == 'ConfigMap':
self.assertTrue(doc['data']['SE_LOG_LEVEL'] == 'FINE')
count_config += 1
self.assertEqual(count_config, 1, "No logging ConfigMap found")
count = 0
for doc in LIST_OF_DOCUMENTS:
if doc['metadata']['name'] in resources_name and doc['kind'] == 'Deployment':
is_present = False
logger.info(f"Assert logging ConfigMap is set to envFrom in resource {doc['metadata']['name']}")
list_env_from = doc['spec']['template']['spec']['containers'][0]['envFrom']
for env in list_env_from:
if env.get('configMapRef') is not None:
if env['configMapRef']['name'] == '{0}selenium-logging-config'.format(RELEASE_NAME):
is_present = True
self.assertTrue(is_present, "envFrom doesn't contain logging ConfigMap")
count += 1
self.assertEqual(count, len(resources_name), "Logging ConfigMap is not present in expected resources")
def test_node_port_set_when_service_type_is_node_port(self):
single_node_port = {'{0}selenium-distributor'.format(RELEASE_NAME): 30553,
'{0}selenium-router'.format(RELEASE_NAME): 30444,
'{0}selenium-session-queue'.format(RELEASE_NAME): 30559}
count = 0
logger.info(f"Assert NodePort is set to components service")
for doc in LIST_OF_DOCUMENTS:
if doc['metadata']['name'] in single_node_port.keys() and doc['kind'] == 'Service':
logger.info(f"Assert NodePort is set to service {doc['metadata']['name']}")
self.assertTrue(doc['spec']['ports'][0]['nodePort'] == single_node_port[doc['metadata']['name']], f"Service {doc['metadata']['name']} with expect NodePort {single_node_port[doc['metadata']['name']]} is not found")
count += 1
self.assertEqual(count, len(single_node_port.keys()), "Number of services with NodePort is not correct")
def test_all_metadata_name_is_prefixed_with_release_name(self):
logger.info(f"Assert all metadata name is prefixed with RELEASE NAME")
prefix = "selenium-" if RELEASE_NAME == "" else RELEASE_NAME
for doc in LIST_OF_DOCUMENTS:
logger.info(f"Assert metadata name: {doc['metadata']['name']}")
self.assertTrue(doc['metadata']['name'].startswith(RELEASE_NAME),
f"Metadata name {doc['metadata']['name']} is not prefixed with RELEASE NAME: {RELEASE_NAME}")
def test_extra_script_import_to_node_configmap(self):
resources_name = ['{0}selenium-node-config'.format(RELEASE_NAME)]
count = 0
for doc in LIST_OF_DOCUMENTS:
if doc['metadata']['name'] in resources_name and doc['kind'] == 'ConfigMap':
logger.info(f"Assert default file is imported to Node ConfigMap")
self.assertTrue(doc['data']['nodeProbe.sh'] != "")
self.assertTrue(doc['data']['nodePreStop.sh'] != "")
self.assertTrue(doc['data']['nodeCustomTask.sh'] != "")
self.assertTrue(doc['data']['setFromCommand.sh'] != "")
count += 1
self.assertEqual(count, len(resources_name), "No node config resources found")
def test_extra_script_import_to_uploader_configmap(self):
resources_name = ['{0}selenium-uploader-config'.format(RELEASE_NAME)]
count = 0
for doc in LIST_OF_DOCUMENTS:
if doc['metadata']['name'] in resources_name and doc['kind'] == 'ConfigMap':
logger.info(f"Assert extra script is imported to Uploader ConfigMap")
self.assertTrue(doc['data']['upload.sh'] is not None)
self.assertTrue(doc['data']['setFromCommand.sh'] is not None)
count += 1
self.assertEqual(count, len(resources_name), "No uploader config resources found")
def test_extra_script_import_to_recorder_configmap(self):
resources_name = ['{0}selenium-recorder-config'.format(RELEASE_NAME)]
count = 0
for doc in LIST_OF_DOCUMENTS:
if doc['metadata']['name'] in resources_name and doc['kind'] == 'ConfigMap':
logger.info(f"Assert extra script is imported to Recorder ConfigMap")
self.assertTrue(doc['data']['video.sh'] is not None)
self.assertTrue(doc['data']['setFromCommand.sh'] is not None)
count += 1
self.assertEqual(count, len(resources_name), "No recorder config resources found")
def test_terminationGracePeriodSeconds_in_deployment_autoscaling(self):
resources_name = ['{0}selenium-chrome-node'.format(RELEASE_NAME),]
count = 0
for doc in LIST_OF_DOCUMENTS:
if doc['metadata']['name'] in resources_name and doc['kind'] == 'Deployment':
logger.info(f"Assert terminationGracePeriodSeconds is set in resource {doc['metadata']['name']}")
self.assertTrue(doc['spec']['template']['spec']['terminationGracePeriodSeconds'] == 7200)
count += 1
self.assertEqual(count, len(resources_name), "node.terminationGracePeriodSeconds doesn't override a higher value than autoscaling.terminationGracePeriodSeconds")
resources_name = ['{0}selenium-edge-node'.format(RELEASE_NAME),
'{0}selenium-firefox-node'.format(RELEASE_NAME),]
count = 0
for doc in LIST_OF_DOCUMENTS:
if doc['metadata']['name'] in resources_name and doc['kind'] == 'Deployment':
logger.info(f"Assert terminationGracePeriodSeconds is set in resource {doc['metadata']['name']}")
self.assertTrue(doc['spec']['template']['spec']['terminationGracePeriodSeconds'] == 3600)
count += 1
self.assertEqual(count, len(resources_name), "node.terminationGracePeriodSeconds doesn't inherit the global value autoscaling.terminationGracePeriodSeconds")
def test_enable_leftovers_cleanup(self):
resources_name = ['{0}selenium-node-config'.format(RELEASE_NAME)]
count = 0
for doc in LIST_OF_DOCUMENTS:
if doc['metadata']['name'] in resources_name and doc['kind'] == 'ConfigMap':
logger.info(f"Assert ENV vars for function leftovers cleanup is set to Node ConfigMap")
self.assertEqual(doc['data']['SE_ENABLE_BROWSER_LEFTOVERS_CLEANUP'], 'true')
self.assertEqual(doc['data']['SE_BROWSER_LEFTOVERS_INTERVAL_SECS'], '3600')
self.assertEqual(doc['data']['SE_BROWSER_LEFTOVERS_PROCESSES_SECS'], '7200')
self.assertEqual(doc['data']['SE_BROWSER_LEFTOVERS_TEMPFILES_DAYS'], '1')
count += 1
self.assertEqual(count, len(resources_name), "No node config resources found")
if __name__ == '__main__':
failed = False
try:
FILE_NAME = sys.argv[1]
RELEASE_NAME = sys.argv[2]
if RELEASE_NAME == "selenium":
RELEASE_NAME = ""
else:
RELEASE_NAME = RELEASE_NAME + "-"
LIST_OF_DOCUMENTS = load_template(FILE_NAME)
suite = unittest.TestLoader().loadTestsFromTestCase(ChartTemplateTests)
test_runner = unittest.TextTestRunner(verbosity=3)
failed = not test_runner.run(suite).wasSuccessful()
except Exception as e:
logger.fatal(e)
failed = True
if failed:
exit(1)