diff --git a/tests/test-simple.py b/tests/test-simple.py index 757b9f1ef2..31fe3529ae 100644 --- a/tests/test-simple.py +++ b/tests/test-simple.py @@ -2,7 +2,6 @@ import time import requests import os.path - import utils @@ -136,15 +135,16 @@ def test_microk8s_services_running(self): assert running_node_services == set(node_services), "Not all node services are running" def test_microk8s_stop_start(self): - coredns_procs = utils._get_process("coredns") - assert len(coredns_procs) > 0, "Expected to find a coredns process running." + assert ( + utils.is_coredns_running() + ), "Expected CoreDNS pod to be running before microk8s stop." utils.run_until_success("/snap/bin/microk8s.stop", timeout_insec=180) - new_coredns_procs = utils._get_process("coredns") - assert len(new_coredns_procs) == 0, "coredns found still running after microk8s stop." + assert not utils.is_coredns_running(), "CoreDNS pod still running after microk8s stop." utils.run_until_success("/snap/bin/microk8s.start", timeout_insec=180) - new_coredns_procs = utils._get_process("coredns") - assert len(new_coredns_procs) > 0, "Expected to find a new coredns process running." + assert ( + utils.is_coredns_running() + ), "Expected CoreDNS pod to be running after microk8s start." diff --git a/tests/utils.py b/tests/utils.py index 3e2db97e60..4dd0edb0c6 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -4,6 +4,7 @@ import yaml import platform import psutil +import subprocess from subprocess import check_output, CalledProcessError, check_call @@ -90,6 +91,31 @@ def kubectl_get(target, timeout_insec=300): return yaml.safe_load(output) +def is_coredns_running(): + cmd = [ + "/snap/bin/microk8s", + "kubectl", + "get", + "pods", + "-n", + "kube-system", + "-l", + "k8s-app=kube-dns", + "--no-headers", + ] + + result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + + if result.returncode != 0: + return False + + for line in result.stdout.splitlines(): + if "Running" in line: + return True + + return False + + def wait_for_pod_state( pod, namespace, desired_state, desired_reason=None, label=None, timeout_insec=600 ):