4646PV_NAME = "test-k-pv"
4747POD_NAME = "test-k-pod"
4848
49+
4950def check_default_storageclass (k8s_client_storage ):
5051 api_response = k8s_client_storage .list_storage_class (_preload_content = False )
5152 storageclasses = api_response .read ().decode ("utf-8" )
@@ -145,13 +146,13 @@ def create_pvc_pod(k8s_api_instance, storage_class):
145146 time .sleep (1 )
146147 retries += 1
147148
148- # assert pod_status == "Running"
149149 if pod_status != "Running" :
150150 raise SCSTestException (
151- "pod is not Running not able to setup test Enviornment " ,
151+ "pod is not Running not able to setup test Environment " ,
152152 return_code = 13 ,
153153 )
154154
155+
155156def check_default_persistentvolumeclaim_readwriteonce (k8s_api_instance ):
156157 """
157158 3. Check if PV got succesfully created using ReadWriteOnce
@@ -183,7 +184,8 @@ def check_default_persistentvolumeclaim_readwriteonce(k8s_api_instance):
183184 )
184185 return 0
185186
186- #with TestEnvironment(k8s_core_api, return_code, return_message) as env:
187+
188+ # with TestEnvironment(k8s_core_api, return_code, return_message) as env:
187189class TestEnvironment :
188190 def __init__ (self , kubeconfig ):
189191 self .namespace = NAMESPACE
@@ -192,37 +194,40 @@ def __init__(self, kubeconfig):
192194 self .k8s_core_api = None
193195 self .return_code = 0
194196 self .return_message = "return_message: FAILED"
195- self .kubeconfig = kubeconfig
196-
197+ self .kubeconfig = kubeconfig
197198
198199 def prepare (self ):
199200 try :
200- logger .debug ("setup_k8s_client(kubeconfig)" )
201- self .k8s_core_api , self .k8s_storage_api = setup_k8s_client (self .kubeconfig )
201+ logger .debug ("setup_k8s_client(kubeconfig)" )
202+ self .k8s_core_api , self .k8s_storage_api = setup_k8s_client (self .kubeconfig )
202203 except Exception as exception_message :
203204 logger .info (f"L{ inspect .currentframe ().f_lineno } { exception_message } " )
204205 self .return_message = f"{ exception_message } "
205206 self .return_code = 1
206207
207-
208208 logger .debug ("Checking Environment for Leftovers" )
209209 try :
210- pod_list = self .k8s_core_api .list_namespaced_pod (namespace = self .namespace )
211- for pod in pod_list .items :
212- if pod .metadata .name == self .pod_name :
213- logger .debug (f"POD '{ self .pod_name } ' exists in namespace '{ self .namespace } '" )
214- return True
215- pvc_list = self .k8s_core_api .list_namespaced_persistent_volume_claim (namespace = self .namespace )
216- for pvc in pvc_list .items :
217- if pvc .metadata .name == self .pvc_name :
218- logger .debug (f"PVC '{ self .pvc_name } ' exists in namespace '{ self .namespace } '" )
219- return True
220- return False
210+ pod_list = self .k8s_core_api .list_namespaced_pod (namespace = self .namespace )
211+ for pod in pod_list .items :
212+ if pod .metadata .name == self .pod_name :
213+ logger .debug (
214+ f"POD '{ self .pod_name } ' exists in namespace '{ self .namespace } '"
215+ )
216+ return True
217+ pvc_list = self .k8s_core_api .list_namespaced_persistent_volume_claim (
218+ namespace = self .namespace
219+ )
220+ for pvc in pvc_list .items :
221+ if pvc .metadata .name == self .pvc_name :
222+ logger .debug (
223+ f"PVC '{ self .pvc_name } ' exists in namespace '{ self .namespace } '"
224+ )
225+ return True
226+ return False
221227 except ApiException as e :
222228 logger .debug (f"Error preparing Environment: { e } " )
223229 return False
224230
225-
226231 def clean (self ):
227232 api_response = None
228233 try :
@@ -234,10 +239,8 @@ def clean(self):
234239 logger .debug (f"The pod { self .pod_name } couldn't be deleted." , exc_info = True )
235240 try :
236241 logger .debug (f"delete pvc:{ self .pvc_name } " )
237- api_response = (
238- self .k8s_core_api .delete_namespaced_persistent_volume_claim (
239- self .pvc_name , self .namespace
240- )
242+ api_response = self .k8s_core_api .delete_namespaced_persistent_volume_claim (
243+ self .pvc_name , self .namespace
241244 )
242245 except :
243246 logger .debug (f"The PVC { self .pvc_name } couldn't be deleted." , exc_info = True )
@@ -246,31 +249,34 @@ def clean(self):
246249 def __enter__ (self ):
247250 retries = 0
248251 while retries <= 2 :
249- if self .prepare ():
250- self .clean ()
251- logger .debug (f"Deleting Leftovers in namespace { self .namespace } from previous test runs" )
252- time .sleep (2 )
253- else :
254- logger .debug (f"Entering the context { self .k8s_core_api } " )
255- return self
256- retries += 1
252+ if self .prepare ():
253+ self .clean ()
254+ logger .debug (
255+ f"Deleting Leftovers in namespace { self .namespace } from previous test runs"
256+ )
257+ time .sleep (2 )
258+ else :
259+ logger .debug (f"Entering the context { self .k8s_core_api } " )
260+ return self
261+ retries += 1
257262 return self
258263
259-
260264 def __exit__ (self , exc_type , exc_value , traceback ):
261265 self .clean ()
262266 if self .return_code == 0 :
263267 self .return_message = "all tests passed"
264268 if isinstance (exc_value , SCSTestException ):
265- # Get the return_code from the exception
269+ self . return_message = exc_value . args [ 0 ]
266270 self .return_code = exc_value .return_code
267271 print (f"SCSTestException occurred with return_code: { self .return_code } " )
268272 else :
269273 # No specific exception, handle normally
270274 print (f"Exiting the context with return_code: { self .return_code } " )
271275 logger .debug (f"return_code:{ self .return_code } { self .return_message } " )
272276
273- gen_sonobuoy_result_file (self .return_code , self .return_message , os .path .basename (__file__ ))
277+ gen_sonobuoy_result_file (
278+ self .return_code , self .return_message , os .path .basename (__file__ )
279+ )
274280 print (f"Exiting the context { self .k8s_core_api } " )
275281 if exc_type :
276282 logger .debug (f"An exception occurred: { exc_value } " )
@@ -330,12 +336,14 @@ def main(argv):
330336 env .return_code = 1
331337
332338 try :
333- env .return_code = create_pvc_pod (k8s_core_api , default_class_name )
339+ env .return_code = create_pvc_pod (k8s_core_api , default_class_name )
334340 except ApiException as api_exception :
335341 if api_exception .status == 409 :
336- logger .info ("(409) conflicting resources, "
337- "try to clean up left overs, then start again" )
338- #return_code = create_pvc_pod(k8s_core_api, default_class_name)
342+ logger .info (
343+ "(409) conflicting resources, "
344+ "try to clean up left overs, then start again"
345+ )
346+ # return_code = create_pvc_pod(k8s_core_api, default_class_name)
339347 env .return_code = 1
340348 env .return_message = "(409) conflicting resources"
341349 return
@@ -348,7 +356,8 @@ def main(argv):
348356 logger .info ("check_default_persistentvolume_readwriteonce()" )
349357
350358 env .return_code = check_default_persistentvolumeclaim_readwriteonce (
351- k8s_core_api )
359+ k8s_core_api
360+ )
352361 except SCSTestException as test_exception :
353362 logger .info (f"L{ inspect .currentframe ().f_lineno } { test_exception } " )
354363 env .return_message = f"{ test_exception } "
0 commit comments