11
11
import yaml
12
12
13
13
TEST_POD_NAME = "e2e-test"
14
+ TEST_CLUSTER_ROLE_NAME = "e2e-test"
15
+ TEST_CLUSTER_ROLE_BINDING_NAME = "e2e-test"
16
+ TEST_SERVICE_ACCOUNT_NAME = "e2e-test"
14
17
15
18
16
19
def load_yaml_from_file (path : str ) -> Dict :
@@ -32,27 +35,29 @@ def _load_test_role_binding() -> Dict:
32
35
33
36
def _prepare_test_environment (config_file : str ) -> None :
34
37
"""
35
- _prepare_testrunner_environment ensures the ServiceAccount,
36
- Role and ClusterRole and bindings are created for the test runner.
38
+ _prepare_test_environment ensures that the old test pod is deleted
39
+ and that namespace, cluster role, cluster role binding and service account
40
+ are created for the test pod.
37
41
"""
38
42
rbacv1 = client .RbacAuthorizationV1Api ()
39
43
corev1 = client .CoreV1Api ()
40
44
dev_config = load_config (config_file )
41
45
46
+ _delete_test_pod (config_file )
47
+
42
48
print ("Creating Namespace" )
43
49
k8s_conditions .ignore_if_already_exists (
44
50
lambda : corev1 .create_namespace (
45
51
client .V1Namespace (metadata = dict (name = dev_config .namespace ))
46
52
)
47
53
)
48
- _delete_test_pod (config_file )
49
54
50
- print ("Creating Role" )
55
+ print ("Creating Cluster Role" )
51
56
k8s_conditions .ignore_if_already_exists (
52
57
lambda : rbacv1 .create_cluster_role (_load_test_role ())
53
58
)
54
59
55
- print ("Creating Role Binding" )
60
+ print ("Creating Cluster Role Binding" )
56
61
role_binding = _load_test_role_binding ()
57
62
# set namespace specified in config.json
58
63
role_binding ["subjects" ][0 ]["namespace" ] = dev_config .namespace
@@ -61,26 +66,14 @@ def _prepare_test_environment(config_file: str) -> None:
61
66
lambda : rbacv1 .create_cluster_role_binding (role_binding )
62
67
)
63
68
64
- print ("Creating ServiceAccount " )
69
+ print ("Creating Service Account " )
65
70
k8s_conditions .ignore_if_already_exists (
66
71
lambda : corev1 .create_namespaced_service_account (
67
72
dev_config .namespace , _load_test_service_account ()
68
73
)
69
74
)
70
75
71
76
72
- def _delete_test_pod (config_file : str ) -> None :
73
- """
74
- _delete_testrunner_pod deletes the test runner pod
75
- if it already exists.
76
- """
77
- dev_config = load_config (config_file )
78
- corev1 = client .CoreV1Api ()
79
- k8s_conditions .ignore_if_doesnt_exist (
80
- lambda : corev1 .delete_namespaced_pod (TEST_POD_NAME , dev_config .namespace )
81
- )
82
-
83
-
84
77
def create_test_pod (args : argparse .Namespace , dev_config : DevConfig ) -> None :
85
78
corev1 = client .CoreV1Api ()
86
79
test_pod = {
@@ -158,7 +151,7 @@ def create_test_pod(args: argparse.Namespace, dev_config: DevConfig) -> None:
158
151
timeout = 60 ,
159
152
exceptions_to_ignore = ApiException ,
160
153
):
161
- raise Exception ("Could not create test_runner pod!" )
154
+ raise Exception ("Could not create test pod!" )
162
155
163
156
164
157
def wait_for_pod_to_be_running (
@@ -178,6 +171,41 @@ def wait_for_pod_to_be_running(
178
171
print ("Pod is running" )
179
172
180
173
174
+ def _delete_test_environment (config_file : str ) -> None :
175
+ """
176
+ _delete_test_environment ensures that the cluster role, cluster role binding and service account
177
+ for the test pod are deleted.
178
+ """
179
+ rbacv1 = client .RbacAuthorizationV1Api ()
180
+ corev1 = client .CoreV1Api ()
181
+ dev_config = load_config (config_file )
182
+
183
+ k8s_conditions .ignore_if_doesnt_exist (
184
+ lambda : rbacv1 .delete_cluster_role (TEST_CLUSTER_ROLE_NAME )
185
+ )
186
+
187
+ k8s_conditions .ignore_if_doesnt_exist (
188
+ lambda : rbacv1 .delete_cluster_role_binding (TEST_CLUSTER_ROLE_BINDING_NAME )
189
+ )
190
+
191
+ k8s_conditions .ignore_if_doesnt_exist (
192
+ lambda : corev1 .delete_namespaced_service_account (
193
+ TEST_SERVICE_ACCOUNT_NAME , dev_config .namespace
194
+ )
195
+ )
196
+
197
+
198
+ def _delete_test_pod (config_file : str ) -> None :
199
+ """
200
+ _delete_test_pod deletes the test pod.
201
+ """
202
+ dev_config = load_config (config_file )
203
+ corev1 = client .CoreV1Api ()
204
+ k8s_conditions .ignore_if_doesnt_exist (
205
+ lambda : corev1 .delete_namespaced_pod (TEST_POD_NAME , dev_config .namespace )
206
+ )
207
+
208
+
181
209
def parse_args () -> argparse .Namespace :
182
210
parser = argparse .ArgumentParser ()
183
211
parser .add_argument ("--test" , help = "Name of the test to run" )
@@ -246,6 +274,7 @@ def main() -> int:
246
274
exceptions_to_ignore = ApiException ,
247
275
):
248
276
return 1
277
+ _delete_test_environment (args .config_file )
249
278
return 0
250
279
251
280
0 commit comments