| Ievgeniia Zadorozhna | 86ab37a | 2025-09-11 12:58:49 +0200 | [diff] [blame] | 1 | import pytest |
| 2 | |
| 3 | from si_tests import settings |
| 4 | from si_tests.clients import k8s as k8s_client |
| 5 | from si_tests.fixtures.kubectl import kcm_manager |
| 6 | |
| 7 | |
| 8 | @pytest.fixture |
| 9 | def ready_child_cluster(request, kcm_manager): |
| 10 | namespace, name = request.param |
| 11 | return namespace, name |
| 12 | |
| 13 | |
| 14 | def pytest_generate_tests(metafunc): |
| 15 | if "ready_child_cluster" in metafunc.fixturenames: |
| 16 | clds = get_ready_cluster_deployments(kcm_manager()) |
| 17 | metafunc.parametrize("ready_child_cluster", |
| 18 | range(len(clds)), indirect=True) |
| 19 | |
| 20 | |
| 21 | def get_ready_cluster_deployments(kcm_manager): |
| 22 | ready_clds = [] |
| 23 | clds = kcm_manager.list_all_clusterdeployments() |
| 24 | |
| 25 | for cld in clds: |
| 26 | conditions = cld.data.get("status", {}).get("conditions", []) |
| 27 | for cond in conditions: |
| 28 | if cond["type"] == "Ready" and cond["status"] == "True": |
| 29 | ready_clds.append((cld.namespace, cld.name)) |
| 30 | break |
| 31 | return ready_clds |
| 32 | |
| 33 | |
| 34 | def check_cluster_deployment_exists(kcm_manager, namespace, name) -> bool: |
| 35 | clds = kcm_manager.list_all_clusterdeployments() |
| 36 | return any(cld.namespace == namespace and cld.name == name for cld in clds) |
| 37 | |
| 38 | |
| 39 | @pytest.mark.sanity |
| 40 | def test_k0rdent_mgmt_object_readiness(kcm_manager): |
| 41 | assert kcm_manager.mgmt.ready == True,\ |
| 42 | f"Management 'kcm' object is not ready" |
| 43 | |
| 44 | |
| 45 | @pytest.mark.sanity |
| 46 | def test_cluster_deployments_are_ready(kcm_manager): |
| 47 | not_ready_clds = [] |
| 48 | clds = kcm_manager.list_all_clusterdeployments() |
| 49 | for cld in clds: |
| 50 | conditions = cld.data.get("status", {}).get("conditions", []) |
| 51 | for cond in conditions: |
| 52 | if cond["type"] == "Ready" and cond["status"] != "True": |
| 53 | not_ready_clds.append((cld.namespace, cld.name)) |
| 54 | break |
| 55 | assert not_ready_clds == [],\ |
| 56 | f"There are some cluster deployments not ready: {not_ready_clds}" |
| 57 | |
| 58 | |
| 59 | @pytest.mark.sanity |
| 60 | def test_provider_templates_are_valid(kcm_manager): |
| 61 | k8s = k8s_client.K8sCluster(kubeconfig=settings.KUBECONFIG_PATH) |
| 62 | provider_templates = k8s.k0rdent_provider_templates.list_raw().items |
| 63 | invalid_res = [] |
| 64 | for pt in provider_templates: |
| 65 | if not pt.status['valid']: |
| 66 | invalid_res.append({"name": pt.metadata.name, |
| 67 | "valid": pt.status['valid']}) |
| 68 | assert not invalid_res, f"Invalid provider templates found: {invalid_res}" |
| 69 | |
| 70 | |
| 71 | @pytest.mark.sanity |
| 72 | def test_cluster_templates_are_valid(kcm_manager): |
| 73 | k8s = k8s_client.K8sCluster(kubeconfig=settings.KUBECONFIG_PATH) |
| 74 | cluster_templates = k8s.k0rdent_cluster_templates.list_raw().items |
| 75 | invalid_res = [] |
| 76 | for ct in cluster_templates: |
| 77 | if not ct.status['valid']: |
| 78 | invalid_res.append({"name": ct.metadata.name, |
| 79 | "valid": ct.status['valid']}) |
| 80 | assert not invalid_res, f"Invalid cluster templates found: {invalid_res}" |
| 81 | |
| 82 | |
| 83 | @pytest.mark.sanity |
| 84 | def test_service_templates_are_valid(kcm_manager): |
| 85 | k8s = k8s_client.K8sCluster(kubeconfig=settings.KUBECONFIG_PATH) |
| 86 | service_templates = k8s.k0rdent_service_templates.list_raw().items |
| 87 | invalid_res = [] |
| 88 | for st in service_templates: |
| 89 | if not st.status['valid']: |
| 90 | invalid_res.append({"name": st.metadata.name, |
| 91 | "valid": st.status['valid']}) |
| 92 | assert not invalid_res, f"Invalid service templates found: {invalid_res}" |
| 93 | |
| 94 | |
| 95 | @pytest.mark.sanity |
| 96 | def test_k0rdent_mgmt_pods_readiness( |
| 97 | kcm_manager, namespaces=None, allowed_phases=None): |
| 98 | """Check k0rdent mgmt cluster pods readiness""" |
| 99 | if namespaces is None: |
| 100 | namespaces = ["kcm-system", "projectsveltos"] |
| 101 | if allowed_phases is None: |
| 102 | allowed_phases = ("Running", "Succeeded") |
| 103 | |
| 104 | for ns in namespaces: |
| 105 | pods = kcm_manager.api.pods.list(namespace=ns) |
| 106 | assert pods, f"No pods found in namespace '{ns}'" |
| 107 | |
| 108 | for pod in pods: |
| 109 | phase = pod.data["status"]["phase"] |
| 110 | assert phase in allowed_phases, ( |
| 111 | f"Pod '{pod.name}' in namespace '{ns}' is in phase '{phase}', " |
| 112 | f"expected one of {allowed_phases}" |
| 113 | ) |
| 114 | |
| 115 | |
| 116 | @pytest.mark.sanity |
| 117 | def test_k0rdent_mgmt_nodes_readiness(kcm_manager): |
| 118 | """Check k0rdent mgmt cluster nodes readiness""" |
| 119 | nodes = kcm_manager.api.nodes.list() |
| 120 | assert nodes, "No nodes found in the cluster" |
| 121 | |
| 122 | for node in nodes: |
| 123 | conditions = node.data["status"]["conditions"] |
| 124 | ready_condition = next((c for c in conditions if c["type"] == "Ready"), |
| 125 | None) |
| 126 | assert ready_condition is not None,\ |
| 127 | f"Node '{node.name}' has no Ready condition" |
| 128 | assert ready_condition["status"] == "True",\ |
| 129 | f"Node '{node.name}' is not Ready" |
| 130 | |
| 131 | |
| 132 | @pytest.mark.sanity_targeted |
| 133 | def test_check_target_child_cluster_readiness(kcm_manager): |
| 134 | """Check the target child cluster readiness""" |
| 135 | ns = kcm_manager.get_namespace(settings.TARGET_NAMESPACE) |
| 136 | cld = ns.get_cluster_deployment(settings.TARGET_CLD) |
| 137 | if not check_cluster_deployment_exists( |
| 138 | kcm_manager, settings.TARGET_NAMESPACE, settings.TARGET_CLD): |
| 139 | pytest.skip(f"Target cluster deployment '{cld.name}' is not found in " |
| 140 | f"namespace '{settings.TARGET_NAMESPACE}'. Please check " |
| 141 | f"TARGET_NAMESPACE and TARGET_CLD env vars.") |
| 142 | cld.check.check_cluster_readiness(timeout=600) |
| 143 | cld.check.check_k8s_pods() |
| 144 | cld.check.check_k8s_nodes() |
| 145 | |
| 146 | |
| 147 | @pytest.mark.sanity |
| 148 | def test_check_all_child_clusters_readiness(kcm_manager, subtests): |
| 149 | ready_clds = get_ready_cluster_deployments(kcm_manager) |
| 150 | assert ready_clds, "No ready child clusters found" |
| 151 | |
| 152 | for namespace, cld_name in ready_clds: |
| 153 | label = f"{cld_name} ({namespace})" |
| 154 | with subtests.test(cluster=label): |
| 155 | ns = kcm_manager.get_namespace(namespace) |
| 156 | cld = ns.get_cluster_deployment(cld_name) |
| 157 | |
| 158 | cld.check.check_cluster_readiness(timeout=600) |
| 159 | cld.check.check_k8s_pods() |
| 160 | cld.check.check_k8s_nodes() |