1111import yaml
1212
1313TEST_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"
1417
1518
1619def load_yaml_from_file (path : str ) -> Dict :
@@ -32,27 +35,29 @@ def _load_test_role_binding() -> Dict:
3235
3336def _prepare_test_environment (config_file : str ) -> None :
3437 """
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.
3741 """
3842 rbacv1 = client .RbacAuthorizationV1Api ()
3943 corev1 = client .CoreV1Api ()
4044 dev_config = load_config (config_file )
4145
46+ _delete_test_pod (config_file )
47+
4248 print ("Creating Namespace" )
4349 k8s_conditions .ignore_if_already_exists (
4450 lambda : corev1 .create_namespace (
4551 client .V1Namespace (metadata = dict (name = dev_config .namespace ))
4652 )
4753 )
48- _delete_test_pod (config_file )
4954
50- print ("Creating Role" )
55+ print ("Creating Cluster Role" )
5156 k8s_conditions .ignore_if_already_exists (
5257 lambda : rbacv1 .create_cluster_role (_load_test_role ())
5358 )
5459
55- print ("Creating Role Binding" )
60+ print ("Creating Cluster Role Binding" )
5661 role_binding = _load_test_role_binding ()
5762 # set namespace specified in config.json
5863 role_binding ["subjects" ][0 ]["namespace" ] = dev_config .namespace
@@ -61,26 +66,14 @@ def _prepare_test_environment(config_file: str) -> None:
6166 lambda : rbacv1 .create_cluster_role_binding (role_binding )
6267 )
6368
64- print ("Creating ServiceAccount " )
69+ print ("Creating Service Account " )
6570 k8s_conditions .ignore_if_already_exists (
6671 lambda : corev1 .create_namespaced_service_account (
6772 dev_config .namespace , _load_test_service_account ()
6873 )
6974 )
7075
7176
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-
8477def create_test_pod (args : argparse .Namespace , dev_config : DevConfig ) -> None :
8578 corev1 = client .CoreV1Api ()
8679 test_pod = {
@@ -158,7 +151,7 @@ def create_test_pod(args: argparse.Namespace, dev_config: DevConfig) -> None:
158151 timeout = 60 ,
159152 exceptions_to_ignore = ApiException ,
160153 ):
161- raise Exception ("Could not create test_runner pod!" )
154+ raise Exception ("Could not create test pod!" )
162155
163156
164157def wait_for_pod_to_be_running (
@@ -178,6 +171,41 @@ def wait_for_pod_to_be_running(
178171 print ("Pod is running" )
179172
180173
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+
181209def parse_args () -> argparse .Namespace :
182210 parser = argparse .ArgumentParser ()
183211 parser .add_argument ("--test" , help = "Name of the test to run" )
@@ -246,6 +274,7 @@ def main() -> int:
246274 exceptions_to_ignore = ApiException ,
247275 ):
248276 return 1
277+ _delete_test_environment (args .config_file )
249278 return 0
250279
251280
0 commit comments