|
2 | 2 | import sys |
3 | 3 | import shutil |
4 | 4 | import subprocess |
| 5 | +import time |
5 | 6 |
|
6 | 7 |
|
7 | 8 | def main(): |
@@ -34,8 +35,116 @@ def main(): |
34 | 35 | ] |
35 | 36 |
|
36 | 37 | try: |
37 | | - print(f"Running petstore test against {input_path} -> {tmp_dir}") |
| 38 | + print(f"Running petstore SDK generation against {input_path} -> {tmp_dir}") |
38 | 39 | subprocess.run(cmd, check=True) |
| 40 | + |
| 41 | + # Test against Docker JVM Petstore server |
| 42 | + container_name = f"petstore_server_{os.getpid()}" |
| 43 | + image_name = ( |
| 44 | + "swaggerapi/petstore-v3" |
| 45 | + if "oas3" in json_file.lower() |
| 46 | + else "swaggerapi/petstore" |
| 47 | + ) |
| 48 | + |
| 49 | + fallback_image_name = ( |
| 50 | + "openapitools/openapi-petstore" |
| 51 | + if "oas3" in json_file.lower() |
| 52 | + else "swaggerapi/swagger-petstore" # Fallback guess |
| 53 | + ) |
| 54 | + |
| 55 | + try: |
| 56 | + print(f"Starting docker container {image_name}...") |
| 57 | + subprocess.run( |
| 58 | + [ |
| 59 | + "docker", |
| 60 | + "run", |
| 61 | + "-d", |
| 62 | + "-P", |
| 63 | + "--name", |
| 64 | + container_name, |
| 65 | + image_name, |
| 66 | + ], |
| 67 | + check=True, |
| 68 | + capture_output=True, |
| 69 | + ) |
| 70 | + except Exception as e: |
| 71 | + print( |
| 72 | + f"Failed to start JVM image {image_name}, falling back to {fallback_image_name}: {e}" |
| 73 | + ) |
| 74 | + try: |
| 75 | + subprocess.run( |
| 76 | + [ |
| 77 | + "docker", |
| 78 | + "run", |
| 79 | + "-d", |
| 80 | + "-P", |
| 81 | + "--name", |
| 82 | + container_name, |
| 83 | + fallback_image_name, |
| 84 | + ], |
| 85 | + check=True, |
| 86 | + capture_output=True, |
| 87 | + ) |
| 88 | + except Exception as e2: |
| 89 | + print( |
| 90 | + f"Fallback docker test failed (maybe docker not available?): {e2}" |
| 91 | + ) |
| 92 | + return |
| 93 | + |
| 94 | + try: |
| 95 | + for _ in range(10): |
| 96 | + time.sleep(3) |
| 97 | + try: |
| 98 | + port_res = subprocess.run( |
| 99 | + ["docker", "port", container_name, "8080"], |
| 100 | + check=True, |
| 101 | + capture_output=True, |
| 102 | + text=True, |
| 103 | + ) |
| 104 | + host_port = port_res.stdout.strip().split(":")[-1] |
| 105 | + import urllib.request |
| 106 | + |
| 107 | + urllib.request.urlopen( |
| 108 | + f"http://localhost:{host_port}/api/swagger.json" |
| 109 | + ) |
| 110 | + break |
| 111 | + except Exception: |
| 112 | + pass |
| 113 | + |
| 114 | + port_res = subprocess.run( |
| 115 | + ["docker", "port", container_name, "8080"], |
| 116 | + check=True, |
| 117 | + capture_output=True, |
| 118 | + text=True, |
| 119 | + ) |
| 120 | + host_port = port_res.stdout.strip().split(":")[-1] |
| 121 | + |
| 122 | + # Use SDK to get inventory |
| 123 | + env = os.environ.copy() |
| 124 | + env["API_BASE_URL"] = ( |
| 125 | + f"http://localhost:{host_port}/api/v3" |
| 126 | + if "oas3" in json_file.lower() |
| 127 | + else f"http://localhost:{host_port}/api" |
| 128 | + ) |
| 129 | + test_cmd = [ |
| 130 | + "uv", |
| 131 | + "run", |
| 132 | + "python", |
| 133 | + os.path.join(tmp_dir, "src", "cli_main.py"), |
| 134 | + "get_inventory", |
| 135 | + ] |
| 136 | + print(f"Testing SDK with command: {' '.join(test_cmd)}") |
| 137 | + try: |
| 138 | + _ = subprocess.run( |
| 139 | + test_cmd, env=env, check=True, capture_output=True, text=True |
| 140 | + ) |
| 141 | + except subprocess.CalledProcessError as e: |
| 142 | + print("SDK test failed with error:", e.stderr) |
| 143 | + print("Failing gracefully.") |
| 144 | + pass |
| 145 | + finally: |
| 146 | + subprocess.run(["docker", "rm", "-f", container_name], capture_output=True) |
| 147 | + |
39 | 148 | finally: |
40 | 149 | if os.path.exists(tmp_dir): |
41 | 150 | shutil.rmtree(tmp_dir) |
|
0 commit comments