@@ -88,6 +88,35 @@ def delete_kube_yaml(name, yaml_file):
8888 else :
8989 ok (name , "%s" % str (cp .stdout , 'utf-8' ))
9090
91+ def json_is_included_expectedly (expected , given ):
92+ if type (expected ) != type (given ):
93+ return False
94+
95+ if type (expected ) == type ({}):
96+ for k , v in expected .items ():
97+ actual = given .get (k , None )
98+ if type (v ) != type (actual ):
99+ return False
100+ if type (v ) == type ([]):
101+ for n in v :
102+ if not n in actual :
103+ return False
104+ elif type (v ) == type ({}):
105+ included = json_is_included_expectedly (v , actual )
106+ if not included :
107+ return False
108+ else :
109+ if actual != v :
110+ return False
111+ elif type (expected ) == type ([]):
112+ for n in expected :
113+ if not n in given :
114+ return False
115+ else :
116+ if expected != given :
117+ return False
118+ return True
119+
91120def curl_verify (name , req , expected_resp ):
92121 method = req .get ('method' , 'GET' )
93122 queries = req .get ('queries' , {})
@@ -96,32 +125,44 @@ def curl_verify(name, req, expected_resp):
96125 body = req .get ('body' , {})
97126 expected_status = expected_resp .get ('status_code' , 200 )
98127 expected_headers = expected_resp .get ('headers' , {})
99- expected_body_json = expected_resp .get ('body' , {})
128+ expected_body = expected_resp .get ('body' , {})
100129 try :
101- resp = requests .api .request (method = method , url = "%s" % url , params = queries , headers = headers , json = body )
102- if resp .status_code != expected_status :
103- fail (name , "Status code unexpected: expected: %d, actually: %d" % (expected_status , resp .status_code ))
104- for k , v in expected_headers .items ():
105- if resp .headers [k ] != v :
106- fail (name , "Header unexpected: expected %s => %s, actually %s => %s" % (k , v , k , resp .headers [k ]))
130+ warn (name , "requesting: %s" % req )
131+ resp = requests .request (method = method , url = "%s" % url , params = queries , headers = headers , json = body , allow_redirects = False )
107132 try :
108- resp_json = resp .json ()
133+ resp_headers = dict (resp .headers )
134+ if type (expected_body ) == type ({}):
135+ resp_body = resp .json ()
136+ else :
137+ resp_body = resp .text
109138 except Exception as e :
110- fail (name , "Response format unexpected: not json-formated(%s)" % e )
111- if type (expected_body_json ) != type (resp_json ):
112- fail (name , "Response format unexpected: expected: %s, actually: %s" % (type (expected_body_json ), type (resp_json )))
113- if type (expected_body_json ) == type ([]):
114- for n in expected_body_json :
115- if not n in resp_json :
116- fail (name , "Response body unexpected, missing %s" % n )
117- elif type (expected_body_json ) == type ({}):
118- for k , v in expected_body_json .items ():
119- if v != resp_json [k ]:
120- fail (name , "Response body unexpected: expected %s => %s, actually %s => %s" % (k , v , k , resp_json [k ]))
139+ return False , "Response format unexpected: not json-formated(%s)" % e
140+
141+ if resp .status_code != expected_status :
142+ return False , "Status code unexpected: expected: %d, actually: %d" % (expected_status , resp .status_code )
143+ if not json_is_included_expectedly (expected_headers , resp_headers ):
144+ return False , "Header unexpected: expected %s, actually %s " % (expected_headers , resp_headers )
145+
146+ if not json_is_included_expectedly (expected_body , resp_body ):
147+ return False , "Response body unexpected: expected %s, actually %s" % (expected_body , resp_body )
148+
121149 except Exception as e :
122- fail ( name , "failed to %s to %s: %s" % (method , url , e ) )
150+ return False , "failed to %s to %s: %s" % (method , url , e )
123151 else :
124- ok (name , "Successfully verified %s via %s %s" % (name , method , url ))
152+ return True , "Successfully verified %s via %s %s" % (name , method , url )
153+
154+
155+ class test_context ():
156+ def __init__ (self , name , ctx_yamls ) -> None :
157+ self .ctxs = ctx_yamls
158+ self .name = name
159+ def __enter__ (self ):
160+ for ctx in self .ctxs :
161+ gen_kube_yaml (self .name , ctx )
162+ apply_kube_yaml (self .name , '%s/deps/%s.yaml' % (homedir , ctx ))
163+ def __exit__ (self , exc_type , exc_val , exc_tb ):
164+ for ctx in self .ctxs :
165+ delete_kube_yaml (self .name , '%s/deps/%s.yaml' % (homedir , ctx ))
125166
126167os .environ .setdefault ('KUBE_CONFIG_FILEPATH' , '~/.kube/config' )
127168load_config ()
@@ -130,21 +171,17 @@ def curl_verify(name, req, expected_resp):
130171for case in testcases :
131172 n = case ['name' ]
132173 note (n , "Testing %s" % n )
133- for ctx in case ['context' ]:
134- gen_kube_yaml (n , ctx )
135- apply_kube_yaml (n , '%s/deps/%s.yaml' % (homedir , ctx ))
136-
137- retries = 50
138- for t in range (retries ):
139- try :
140- curl_verify (n , case ['request' ], case ['response' ])
141- except Exception as e :
142- time .sleep (2 )
143- warn (n , "Another retry: %d" % (retries - t ))
144- if t == retries - 1 :
145- fail (n , "Timeout for testing... quit." )
146- else :
147- break
148-
149- for ctx in case ['context' ]:
150- delete_kube_yaml (n , '%s/deps/%s.yaml' % (homedir , ctx ))
174+
175+ with test_context (n , case ['context' ]):
176+ retries = 50
177+ for t in range (retries ):
178+ (passed , msg ) = curl_verify (n , case ['request' ], case ['response' ])
179+ if not passed :
180+ time .sleep (2 )
181+ warn (n , msg )
182+ warn (n , "Another retry: %d" % (retries - t ))
183+ if t == retries - 1 :
184+ fail (n , "Timeout for testing... quit." )
185+ else :
186+ ok (n , msg )
187+ break
0 commit comments