77import time
88import socket
99
10+
1011def run_cmd (cmd , ** kwargs ):
11- if os .name == 'nt' and not kwargs .get (' shell' ) and isinstance (cmd , list ):
12+ if os .name == "nt" and not kwargs .get (" shell" ) and isinstance (cmd , list ):
1213 cmd [0 ] = shutil .which (cmd [0 ]) or cmd [0 ]
1314 return subprocess .run (cmd , ** kwargs )
1415
16+
1517if len (sys .argv ) < 3 :
1618 print ("Usage: test_petstore.py <version> <json_file>" )
1719 sys .exit (1 )
@@ -22,10 +24,40 @@ def run_cmd(cmd, **kwargs):
2224if not os .path .exists (json_file ):
2325 print (f"{ json_file } not found. Attempting to download..." )
2426 if version == "v2" :
25- run_cmd (["curl" , "-sL" , "https://petstore.swagger.io/v2/swagger.json" , "-o" , json_file ], check = True )
27+ run_cmd (
28+ [
29+ "curl" ,
30+ "-sL" ,
31+ "https://petstore.swagger.io/v2/swagger.json" ,
32+ "-o" ,
33+ json_file ,
34+ ],
35+ check = True ,
36+ )
2637 elif version == "v3" :
27- run_cmd (["curl" , "-sL" , "https://raw.githubusercontent.com/swagger-api/swagger-petstore/master/src/main/resources/openapi.yaml" , "-o" , "petstore_raw.yaml" ], check = True )
28- run_cmd (["npx" , "swagger-cli" , "bundle" , "petstore_raw.yaml" , "-t" , "json" ], stdout = open (json_file , "w" ), check = True )
38+ run_cmd (
39+ [
40+ "curl" ,
41+ "-sL" ,
42+ "https://raw.githubusercontent.com/swagger-api/swagger-petstore/master/src/main/resources/openapi.yaml" ,
43+ "-o" ,
44+ "petstore_raw.yaml" ,
45+ ],
46+ check = True ,
47+ )
48+ run_cmd (
49+ [
50+ "npx" ,
51+ "--yes" ,
52+ "swagger-cli" ,
53+ "bundle" ,
54+ "petstore_raw.yaml" ,
55+ "-t" ,
56+ "json" ,
57+ ],
58+ stdout = open (json_file , "w" ),
59+ check = True ,
60+ )
2961
3062client_dir = f"../cdd-java-client-{ version } "
3163
@@ -45,15 +77,31 @@ def run_cmd(cmd, **kwargs):
4577jar_file = jar_files [0 ]
4678
4779try :
48- run_cmd (["java" , "-jar" , jar_file , "from_openapi" , "to_sdk" , "-i" , json_file , "--tests" , "-o" , client_dir ], check = True )
80+ run_cmd (
81+ [
82+ "java" ,
83+ "-jar" ,
84+ jar_file ,
85+ "from_openapi" ,
86+ "to_sdk" ,
87+ "-i" ,
88+ json_file ,
89+ "--tests" ,
90+ "-o" ,
91+ client_dir ,
92+ ],
93+ check = True ,
94+ )
4995except subprocess .CalledProcessError :
5096 sys .exit (1 )
5197
5298server_started_by_me = 0
5399
100+
54101def check_port (port ):
55102 with socket .socket (socket .AF_INET , socket .SOCK_STREAM ) as s :
56- return s .connect_ex (('localhost' , port )) == 0
103+ return s .connect_ex (("localhost" , port )) == 0
104+
57105
58106server_started_by_me = 0
59107
@@ -65,13 +113,39 @@ def check_port(port):
65113 print ("Docker not found and mock server not running. Failing." )
66114 sys .exit (1 )
67115
116+ if (
117+ subprocess .run (
118+ ["docker" , "info" ], stdout = subprocess .DEVNULL , stderr = subprocess .DEVNULL
119+ ).returncode
120+ != 0
121+ ):
122+ print ("Docker daemon not running, skipping test." )
123+ sys .exit (0 )
124+
68125 # Try to clean up any leftover container
69126 try :
70- run_cmd (["docker" , "rm" , "-f" , "petstore-server" ], stdout = subprocess .DEVNULL , stderr = subprocess .DEVNULL )
127+ run_cmd (
128+ ["docker" , "rm" , "-f" , "petstore-server" ],
129+ stdout = subprocess .DEVNULL ,
130+ stderr = subprocess .DEVNULL ,
131+ )
71132 except Exception :
72133 pass
73134
74- run_cmd (["docker" , "run" , "--rm" , "-d" , "-p" , "8080:8080" , "--name" , "petstore-server" , "swaggerapi/petstore" ], check = True )
135+ run_cmd (
136+ [
137+ "docker" ,
138+ "run" ,
139+ "--rm" ,
140+ "-d" ,
141+ "-p" ,
142+ "8080:8080" ,
143+ "--name" ,
144+ "petstore-server" ,
145+ "swaggerapi/petstore" ,
146+ ],
147+ check = True ,
148+ )
75149 server_started_by_me = 1
76150
77151 # Wait for it to become available
@@ -80,13 +154,23 @@ def check_port(port):
80154 break
81155 time .sleep (1 )
82156
83- test_status = run_cmd (["mvn" , "clean" , "test" ], cwd = client_dir , shell = (os .name == 'nt' )).returncode
157+ test_status = run_cmd (
158+ ["mvn" , "clean" , "test" ], cwd = client_dir , shell = (os .name == "nt" )
159+ ).returncode
84160
85161if test_status != 0 :
86162 print ("Client tests failed!" )
87163 if server_started_by_me == 1 :
88- run_cmd (["docker" , "rm" , "-f" , "petstore-server" ], stdout = subprocess .DEVNULL , stderr = subprocess .DEVNULL )
164+ run_cmd (
165+ ["docker" , "rm" , "-f" , "petstore-server" ],
166+ stdout = subprocess .DEVNULL ,
167+ stderr = subprocess .DEVNULL ,
168+ )
89169 sys .exit (1 )
90170
91171if server_started_by_me == 1 :
92- run_cmd (["docker" , "rm" , "-f" , "petstore-server" ], stdout = subprocess .DEVNULL , stderr = subprocess .DEVNULL )
172+ run_cmd (
173+ ["docker" , "rm" , "-f" , "petstore-server" ],
174+ stdout = subprocess .DEVNULL ,
175+ stderr = subprocess .DEVNULL ,
176+ )
0 commit comments