|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +import tempfile |
| 6 | +import time |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +from test_base import TestBase |
| 10 | + |
| 11 | +from warnet.k8s import get_default_namespace |
| 12 | +from warnet.process import run_command |
| 13 | + |
| 14 | + |
| 15 | +class BinaryTest(TestBase): |
| 16 | + TEST_STRING = "Hello, World!" |
| 17 | + |
| 18 | + def __init__(self): |
| 19 | + super().__init__() |
| 20 | + self.network_dir = Path(os.path.dirname(__file__)) / "data" / "small_2_node" |
| 21 | + |
| 22 | + def run_test(self): |
| 23 | + try: |
| 24 | + self.setup_network() |
| 25 | + self.test_generic_binary() |
| 26 | + finally: |
| 27 | + self.cleanup() |
| 28 | + |
| 29 | + def setup_network(self): |
| 30 | + self.log.info("Setting up network") |
| 31 | + self.log.info(self.warnet(f"deploy {self.network_dir}")) |
| 32 | + self.wait_for_all_tanks_status(target="running") |
| 33 | + self.wait_for_all_edges() |
| 34 | + |
| 35 | + def test_generic_binary(self): |
| 36 | + self.log.info("Launching binary") |
| 37 | + temp_file_content = f"""#!/usr/bin/env sh |
| 38 | +echo {self.TEST_STRING}""" |
| 39 | + |
| 40 | + with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".sh") as temp_file: |
| 41 | + temp_file.write(temp_file_content) |
| 42 | + temp_file_path = temp_file.name |
| 43 | + |
| 44 | + os.chmod(temp_file_path, 0o755) |
| 45 | + self.warnet(f"run-binary {temp_file_path}") |
| 46 | + |
| 47 | + # Get the commander pod name |
| 48 | + pods = run_command(f"kubectl get pods -n {get_default_namespace()} -o json") |
| 49 | + pods = json.loads(pods) |
| 50 | + pod_list = [item["metadata"]["name"] for item in pods["items"]] |
| 51 | + binary_pod = next((pod for pod in pod_list if pod.startswith("binary")), None) |
| 52 | + if binary_pod is None: |
| 53 | + raise ValueError("No pod found starting with 'binary'") |
| 54 | + self.log.info(f"Got pod: {binary_pod}") |
| 55 | + |
| 56 | + def g_log(): |
| 57 | + logs = self.warnet(f"logs {binary_pod}") |
| 58 | + return self.TEST_STRING in logs |
| 59 | + |
| 60 | + time.sleep(5) |
| 61 | + self.wait_for_predicate(g_log, timeout=60, interval=5) |
| 62 | + |
| 63 | + os.unlink(temp_file_path) |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + test = BinaryTest() |
| 68 | + test.run_test() |
0 commit comments