|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import random |
| 4 | + |
| 5 | +import subprocess |
| 6 | +import re |
| 7 | + |
| 8 | + |
| 9 | +RAPIDAST_IMAGE = "quay.io/redhatproductsecurity/rapidast:2.5.0" |
| 10 | +def get_vpn_ip_address(): |
| 11 | + try: |
| 12 | + ip_output = subprocess.check_output(['ip', 'addr']).decode('utf-8') |
| 13 | + # Use regular expression to extract IP addresses |
| 14 | + ip_addresses = re.findall(r'10.64.\d+\.\d+', ip_output) |
| 15 | + |
| 16 | + # Currently return the first IP address |
| 17 | + # TODO: fix if there are multiple IP addresses and it causes an issue |
| 18 | + |
| 19 | + return ip_addresses[0] |
| 20 | + except subprocess.CalledProcessError as e: |
| 21 | + return f"Error: {e}" |
| 22 | + |
| 23 | +def test_oobt_basic(): |
| 24 | + # 1. place kubeconfig in the TEST_DATA_DIR directory |
| 25 | + |
| 26 | + TEST_DATA_DIR = "oobt_test_data" |
| 27 | + RAPIDAST_CFG_FILE = "v5-none-oobt-template.yaml" |
| 28 | + |
| 29 | + port = random.randint(10000, 30000) |
| 30 | + ipaddr = get_vpn_ip_address() |
| 31 | + |
| 32 | + # create a rapidast config |
| 33 | + sed_cmd = f"sed 's/-p <port> -i <ipaddr>/-p {port} -i {ipaddr}/' {TEST_DATA_DIR}/{RAPIDAST_CFG_FILE} > rapidast_runtime_cfg.yaml" |
| 34 | + os.system(sed_cmd) |
| 35 | + |
| 36 | + # prep for testing |
| 37 | + os.system(f"chmod 666 {TEST_DATA_DIR}/kubeconfig") |
| 38 | + if not os.path.exists("results"): |
| 39 | + os.makedirs("results") |
| 40 | + os.system("podman unshare chown 1000 results") |
| 41 | + |
| 42 | + # Run the command and capture stdout |
| 43 | + command = f"podman run -it --rm -v ./{TEST_DATA_DIR}/kubeconfig:/home/rapidast/.kube/config:Z -v ./results:/opt/rapidast/results:Z -v $PWD:/test:Z -p {port}:{port} {RAPIDAST_IMAGE} rapidast.py --config /test/rapidast_runtime_cfg.yaml" |
| 44 | + print(command) |
| 45 | + |
| 46 | + process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
| 47 | + stdout, stderr = process.communicate() |
| 48 | +# print(stdout) |
| 49 | + print("test completed. See the results directory") |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + test_oobt_basic() |
0 commit comments