Skip to content

Commit a8fe490

Browse files
committed
fix(test): poll for multi-role replica count to avoid race condition
Signed-off-by: Oleksander Piskun <oleksandr2088@icloud.com>
1 parent 51b54fc commit a8fe490

1 file changed

Lines changed: 31 additions & 18 deletions

File tree

tests/test_occ_commands_k8s.py

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,29 @@ def test_k8s_multi_deploy():
632632
print("OK")
633633

634634

635+
def _wait_multi_role_replicas(expected, label="app.kubernetes.io/component=exapp", timeout_sec=30):
636+
"""Poll until all multi-role deployments reach the expected replica count."""
637+
for _ in range(timeout_sec // 2):
638+
deploy_json = kubectl_output(f"get deploy -l {label} -o json", check=False)
639+
data = json.loads(deploy_json)
640+
items = data.get("items", [])
641+
if len(items) >= 2 and all(
642+
item["spec"].get("replicas", -1) == expected for item in items
643+
):
644+
return items
645+
time.sleep(2)
646+
# Final check with assertion
647+
deploy_json = kubectl_output(f"get deploy -l {label} -o json", check=False)
648+
data = json.loads(deploy_json)
649+
items = data.get("items", [])
650+
assert len(items) >= 2, f"Expected 2+ deployments, got {len(items)}"
651+
for item in items:
652+
replicas = item["spec"].get("replicas", -1)
653+
name = item["metadata"]["name"]
654+
assert replicas == expected, f"Expected {expected} replicas for {name}, got {replicas}"
655+
return items
656+
657+
635658
def test_k8s_multi_enable_disable():
636659
"""Disable and re-enable a multi-role ExApp."""
637660
print(" test_k8s_multi_enable_disable...", end=" ", flush=True)
@@ -640,29 +663,19 @@ def test_k8s_multi_enable_disable():
640663
r = occ("app_api:app:disable app-skeleton-python", check=False, timeout=120)
641664
assert r.returncode == 0, f"Disable failed: {r.stdout.decode()}"
642665

643-
# Verify all deployments scaled to 0
644-
deploy_json = kubectl_output("get deploy -l app.kubernetes.io/component=exapp -o json", check=False)
645-
data = json.loads(deploy_json)
646-
items = data.get("items", [])
647-
assert len(items) >= 2, f"Expected 2+ deployments after disable, got {len(items)}"
648-
for item in items:
649-
replicas = item["spec"].get("replicas", -1)
650-
name = item["metadata"]["name"]
651-
assert replicas == 0, f"Expected 0 replicas for {name} after disable, got {replicas}"
666+
# Verify all deployments scaled to 0 (poll to avoid race)
667+
_wait_multi_role_replicas(0)
668+
669+
# Verify AppAPI shows disabled
670+
list_output = occ_output("app_api:app:list")
671+
assert "disabled" in list_output, f"Expected 'disabled' in app list after disable: {list_output}"
652672

653673
# Re-enable
654674
r = occ("app_api:app:enable app-skeleton-python", check=False, timeout=300)
655675
assert r.returncode == 0, f"Enable failed: {r.stdout.decode()}"
656676

657-
# Verify all deployments scaled to 1
658-
deploy_json = kubectl_output("get deploy -l app.kubernetes.io/component=exapp -o json", check=False)
659-
data = json.loads(deploy_json)
660-
items = data.get("items", [])
661-
assert len(items) >= 2, f"Expected 2+ deployments after enable, got {len(items)}"
662-
for item in items:
663-
replicas = item["spec"].get("replicas", -1)
664-
name = item["metadata"]["name"]
665-
assert replicas == 1, f"Expected 1 replica for {name} after enable, got {replicas}"
677+
# Verify all deployments scaled to 1 (poll to avoid race)
678+
_wait_multi_role_replicas(1)
666679
print("OK")
667680

668681

0 commit comments

Comments
 (0)