|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | +from time import sleep |
| 6 | + |
| 7 | +import pexpect |
| 8 | +from kubernetes.stream import stream |
| 9 | +from test_base import TestBase |
| 10 | + |
| 11 | +from warnet.k8s import get_pods_with_label, get_static_client |
| 12 | +from warnet.process import run_command |
| 13 | + |
| 14 | + |
| 15 | +class LNTest(TestBase): |
| 16 | + def __init__(self): |
| 17 | + super().__init__() |
| 18 | + self.network_dir = Path(os.path.dirname(__file__)) / "data" / "ln" |
| 19 | + |
| 20 | + def run_test(self): |
| 21 | + try: |
| 22 | + os.chdir(self.tmpdir) |
| 23 | + self.setup_network() |
| 24 | + self.run_plugin() |
| 25 | + self.check_simln_logs() |
| 26 | + result = self.copy_results() |
| 27 | + assert result |
| 28 | + finally: |
| 29 | + self.cleanup() |
| 30 | + |
| 31 | + def setup_network(self): |
| 32 | + self.log.info("Setting up network") |
| 33 | + self.log.info(self.warnet(f"deploy {self.network_dir}")) |
| 34 | + self.wait_for_all_tanks_status(target="running") |
| 35 | + |
| 36 | + def run_plugin(self): |
| 37 | + self.sut = pexpect.spawn("warnet init") |
| 38 | + self.sut.expect("network", timeout=10) |
| 39 | + self.sut.sendline("n") |
| 40 | + |
| 41 | + self.warnet("plugin run simln run_simln") |
| 42 | + |
| 43 | + def check_simln_logs(self): |
| 44 | + pod = get_pods_with_label("mission=plugin") |
| 45 | + self.log.info(run_command(f"kubectl logs pod/{pod.metadata.name}")) |
| 46 | + |
| 47 | + def copy_results(self) -> bool: |
| 48 | + self.log.info("Copying results") |
| 49 | + sleep(20) |
| 50 | + pod = get_pods_with_label("mission=plugin")[0] |
| 51 | + v1 = get_static_client() |
| 52 | + |
| 53 | + source_path = "/config/results" |
| 54 | + destination_path = "results" |
| 55 | + os.makedirs(destination_path, exist_ok=True) |
| 56 | + command = ["tar", "cf", "-", source_path] |
| 57 | + |
| 58 | + # Create the stream |
| 59 | + resp = stream( |
| 60 | + v1.connect_get_namespaced_pod_exec, |
| 61 | + name=pod.metadata.name, |
| 62 | + namespace=pod.metadata.namespace, |
| 63 | + command=command, |
| 64 | + stderr=True, |
| 65 | + stdin=False, |
| 66 | + stdout=True, |
| 67 | + tty=False, |
| 68 | + _preload_content=False, |
| 69 | + ) |
| 70 | + |
| 71 | + # Write the tar output to a file |
| 72 | + tar_file = os.path.join(destination_path, "results.tar") |
| 73 | + with open(tar_file, "wb") as f: |
| 74 | + while resp.is_open(): |
| 75 | + resp.update(timeout=1) |
| 76 | + if resp.peek_stdout(): |
| 77 | + f.write(resp.read_stdout().encode("utf-8")) |
| 78 | + if resp.peek_stderr(): |
| 79 | + print(resp.read_stderr()) |
| 80 | + |
| 81 | + resp.close() |
| 82 | + |
| 83 | + import tarfile |
| 84 | + |
| 85 | + with tarfile.open(tar_file, "r") as tar: |
| 86 | + tar.extractall(path=destination_path) |
| 87 | + |
| 88 | + os.remove(tar_file) |
| 89 | + |
| 90 | + for root, _dirs, files in os.walk(destination_path): |
| 91 | + for file_name in files: |
| 92 | + file_path = os.path.join(root, file_name) |
| 93 | + |
| 94 | + with open(file_path) as file: |
| 95 | + content = file.read() |
| 96 | + if "Success" in content: |
| 97 | + return True |
| 98 | + return False |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + test = LNTest() |
| 103 | + test.run_test() |
0 commit comments