Skip to content

Commit

Permalink
added pod and namespace delete queue
Browse files Browse the repository at this point in the history
lint + more queues

added some logic

linting

reduced complexity
  • Loading branch information
tsebastiani committed Dec 4, 2024
1 parent bdc2e17 commit ef29dce
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 21 deletions.
32 changes: 32 additions & 0 deletions src/krkn_lib/tests/base_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import cProfile
import logging
import os
import queue
import random
import string
import sys
Expand Down Expand Up @@ -32,6 +33,7 @@ class BaseTest(unittest.TestCase):
lib_telemetry_ocp: KrknTelemetryOpenshift
lib_elastic: KrknElastic
pr: cProfile.Profile
pod_delete_queue: queue.Queue

@classmethod
def setUpClass(cls):
Expand All @@ -57,6 +59,14 @@ def setUpClass(cls):
# cls.pr = cProfile.Profile()
# cls.pr.enable()
# print("\n<<<---")

# starting pod_delete_queue
cls.pod_delete_queue = queue.Queue()
worker = threading.Thread(target=cls.pod_delete_worker, args=[cls])

worker.daemon = True
worker.start()

try:
requests.get(host, timeout=2, verify=False)
except ConnectTimeout:
Expand All @@ -69,6 +79,27 @@ def setUpClass(cls):
)
sys.exit(1)

def pod_delete_worker(self):
while True:
pod_namespace = self.pod_delete_queue.get()
if pod_namespace[0] == "exit" and pod_namespace == "exit":
print("pod killing thread exiting....")
return
try:
self.lib_k8s.delete_pod(pod_namespace[0], pod_namespace[1])
self.lib_k8s.delete_namespace(pod_namespace[1])
print(
f"[POD DELETE WORKER] deleted pod: "
f"{pod_namespace[0]} namespace:{pod_namespace[1]}"
)
self.pod_delete_queue.task_done()
except Exception as e:
print(
f"[POD DELETE WORKER] "
f"exception raised but continuing: {e}"
)
continue

@classmethod
def tearDownClass(cls) -> None:
# PROFILER
Expand All @@ -79,6 +110,7 @@ def tearDownClass(cls) -> None:
# p.print_stats()
# print
# "\n--->>>"
cls.pod_delete_queue.put(["exit", "exit"])
pass

def wait_delete_namespace(
Expand Down
43 changes: 22 additions & 21 deletions src/krkn_lib/tests/test_krkn_kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@


class KrknKubernetesTests(BaseTest):
def test_zzzexec_command(self):

def test_exec_command(self):
namespace = "test-ec-" + self.get_random_string(10)
alpine_name = "alpine-" + self.get_random_string(10)
self.deploy_namespace(namespace, [])
Expand Down Expand Up @@ -81,11 +82,12 @@ def test_zzzexec_command(self):

try:
self.lib_k8s.exec_cmd_in_pod(["ls", "-al"], alpine_name, namespace)
self.lib_k8s.delete_namespace(namespace)
except Exception:
self.lib_k8s.delete_namespace(namespace)
self.fail()

self.pod_delete_queue.put(["fedtools", namespace])
self.pod_delete_queue.put([alpine_name, namespace])

def test_pod_shell(self):
namespace = "test-ps-" + self.get_random_string(10)
alpine_name = "alpine-" + self.get_random_string(10)
Expand Down Expand Up @@ -118,7 +120,7 @@ def test_pod_shell(self):
continue
shell = self.lib_k8s.get_pod_shell("fedtools", namespace)
self.assertEqual(shell, "bash")
self.lib_k8s.delete_namespace(namespace)
self.pod_delete_queue.put([alpine_name, namespace])

def test_command_on_node(self):
try:
Expand Down Expand Up @@ -297,8 +299,7 @@ def test_list_pods(self):
pods = self.lib_k8s.list_pods(namespace=namespace)
self.assertTrue(len(pods) == 1)
self.assertIn("kraken-deployment", pods)
self.lib_k8s.delete_namespace(namespace)
self.delete_fake_kraken(namespace=namespace)
self.pod_delete_queue.put(["kraken-deployment", namespace])

def test_get_all_pods(self):
namespace = "test-ap" + self.get_random_string(10)
Expand All @@ -317,9 +318,7 @@ def test_get_all_pods(self):
self.assertTrue(len(results) == 1)
self.assertEqual(results[0][0], "kraken-deployment")
self.assertEqual(results[0][1], namespace)

self.delete_fake_kraken(namespace=namespace)
self.lib_k8s.delete_namespace(namespace)
self.pod_delete_queue.put(["kraken-deployment", namespace])

def test_delete_pod(self):
namespace = "test-dp-" + self.get_random_string(10)
Expand All @@ -329,7 +328,7 @@ def test_delete_pod(self):
self.lib_k8s.delete_pod("fedtools", namespace=namespace)
with self.assertRaises(ApiException):
self.lib_k8s.read_pod("fedtools", namespace=namespace)
self.lib_k8s.delete_namespace(namespace)
self.pod_delete_queue.put(["fedtools", namespace])

def test_create_pod(self):
namespace = "test-cp-" + self.get_random_string(10)
Expand All @@ -339,11 +338,11 @@ def test_create_pod(self):
self.lib_k8s.create_pod(body, namespace)
try:
self.wait_pod("fedtools", namespace=namespace)
self.lib_k8s.delete_namespace(namespace)
except Exception:
logging.error("failed to create pod")
self.assertTrue(False)
self.lib_k8s.delete_namespace(namespace)
finally:
self.pod_delete_queue.put(["fedtools", namespace])

def test_read_pod(self):
namespace = "test-rp" + self.get_random_string(10)
Expand All @@ -361,7 +360,8 @@ def test_read_pod(self):
)
)
self.assertTrue(False)
self.lib_k8s.delete_namespace(namespace)
finally:
self.pod_delete_queue.put([name, namespace])

def test_get_pod_log(self):
namespace = "test-pl-" + self.get_random_string(10)
Expand All @@ -378,7 +378,8 @@ def test_get_pod_log(self):
"failed to get logs due to an exception: %s" % str(e)
)
self.assertTrue(False)
self.lib_k8s.delete_namespace(namespace)
finally:
self.pod_delete_queue.put([name, namespace])

def test_get_containers_in_pod(self):
namespace = "test-cip-" + self.get_random_string(10)
Expand All @@ -397,7 +398,8 @@ def test_get_containers_in_pod(self):
)
)
self.assertTrue(False)
self.lib_k8s.delete_namespace(namespace)
finally:
self.pod_delete_queue.put([name, namespace])

def test_net_policy(self):
namespace = "test-np" + self.get_random_string(10)
Expand Down Expand Up @@ -850,14 +852,13 @@ def test_download_folder_from_pod_as_archive(self):
self.assertTrue(os.stat(file[1]).st_size > 0)

def test_exists_path_in_pod(self):
namespace = "test-" + self.get_random_string(10)
pod_name = "alpine-"+self.get_random_string(10)
self.deploy_namespace(namespace, [])
namespace = "default"
pod_name = "alpine-" + self.get_random_string(10)
# to ensure that the namespace is fully deployed
time.sleep(5)
self.depoy_alpine(pod_name, namespace)
count = 0
MAX_RETRIES = 20
MAX_RETRIES = 100
while not self.lib_k8s.is_pod_running(pod_name, namespace):
if count > MAX_RETRIES:
self.assertFalse(True, "container failed to become ready")
Expand All @@ -867,13 +868,13 @@ def test_exists_path_in_pod(self):

self.assertTrue(
self.lib_k8s.path_exists_in_pod(
"fedtools", "fedtools", namespace, "/home"
"alpine", "alpine", namespace, "/home"
)
)

self.assertFalse(
self.lib_k8s.path_exists_in_pod(
"fedtools", "fedtools", namespace, "/does_not_exist"
"alpine", "alpine", namespace, "/does_not_exist"
)
)
self.lib_k8s.delete_namespace(namespace)
Expand Down

0 comments on commit ef29dce

Please sign in to comment.