Skip to content

Commit 0e1a28d

Browse files
committed
preparing in short loops working
Signed-off-by: Katharina Trentau <katharina.trentau@cloudandheat.com>
1 parent 27ae281 commit 0e1a28d

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

Tests/kaas/k8s-default-storage-class/k8s-default-storage-class-check.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,19 +195,26 @@ def __init__(self, k8s_api_instance):
195195

196196

197197
def prepare(self, k8s_api_instance):
198-
print("prepare")
198+
logger.debug("Checking Environment for Leftovers")
199199
try:
200+
pod_list = k8s_api_instance.list_namespaced_pod(namespace=self.namespace)
201+
for pod in pod_list.items:
202+
if pod.metadata.name == self.pod_name:
203+
logger.debug(f"POD '{self.pod_name}' exists in namespace '{self.namespace}'")
204+
return True
200205
pvc_list = k8s_api_instance.list_namespaced_persistent_volume_claim(namespace=self.namespace)
201206
for pvc in pvc_list.items:
202207
if pvc.metadata.name == self.pvc_name:
208+
logger.debug(f"PVC '{self.pvc_name}' exists in namespace '{self.namespace}'")
203209
return True
204210
return False
205211
except ApiException as e:
206-
print(f"Error: {e}")
212+
logger.debug(f"Error preparing Environment: {e}")
207213
return False
208214

209215

210216
def clean(self):
217+
api_response = None
211218
try:
212219
logger.debug(f"delete pod:{self.pod_name}")
213220
api_response = self.k8s_api_instance.delete_namespaced_pod(
@@ -224,17 +231,22 @@ def clean(self):
224231
)
225232
except:
226233
logger.debug(f"The PVC {self.pvc_name} couldn't be deleted.", exc_info=True)
234+
return api_response
227235

228236
def __enter__(self):
229-
# Check if the PVC exists
230-
if self.prepare(self.k8s_api_instance):
231-
logger.debug(f"PVC '{self.pvc_name}' exists in namespace '{self.namespace}'")
232-
self.clean()
233-
logger.debug(f"Deleting Leftovers from previous test runs")
234-
else:
235-
logger.debug(f"Entering the context {self.k8s_api_instance}")
237+
retries = 0
238+
while retries <= 2:
239+
if self.prepare(self.k8s_api_instance):
240+
self.clean()
241+
logger.debug(f"Deleting Leftovers in namespace {self.namespace} from previous test runs")
242+
time.sleep(2)
243+
else:
244+
logger.debug(f"Entering the context {self.k8s_api_instance}")
245+
return self
246+
retries += 1
236247
return self
237248

249+
238250
def __exit__(self, exc_type, exc_value, traceback):
239251
self.clean()
240252
if self.return_code == 0:
@@ -306,11 +318,12 @@ def main(argv):
306318
"try to clean up left overs, then start again")
307319
#return_code = create_pvc_pod(k8s_core_api, default_class_name)
308320
env.return_code = 1
321+
env.return_message = "(409) conflicting resources"
322+
return
309323
else:
310324
logger.info(f"An API error occurred: {api_exception}")
311325
env.return_code = 1
312326

313-
314327
# Check if default_persistent volume has ReadWriteOnce defined (MANDATORY)
315328
try:
316329
logger.info("check_default_persistentvolume_readwriteonce()")

0 commit comments

Comments
 (0)