Skip to content

Commit 9da1158

Browse files
Feature/add test like verifications (#56)
1 parent 3759f08 commit 9da1158

File tree

9 files changed

+239
-40
lines changed

9 files changed

+239
-40
lines changed

infrastructure/afd-apim-pe/create.ipynb

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,16 @@
174174
"source": [
175175
"import utils\n",
176176
"from apimrequests import ApimRequests\n",
177+
"from apimtesting import ApimTesting\n",
177178
"\n",
178179
"reqs = ApimRequests(apim_gateway_url)\n",
180+
"tests = ApimTesting(\"AFD-APIM-PE Tests (Pre-Lockdown)\")\n",
179181
"\n",
180182
"utils.print_message('Calling Hello World (Root) API via API Management Gateway URL. Expect 200 (if run before disabling API Management public network access).')\n",
181183
"output = reqs.singleGet('/')\n",
184+
"tests.verify(output, 'Hello World from API Management!')\n",
185+
"\n",
186+
"tests.print_summary()\n",
182187
"\n",
183188
"utils.print_ok('API request via API Management completed')"
184189
]
@@ -234,25 +239,37 @@
234239
"outputs": [],
235240
"source": [
236241
"import utils\n",
242+
"import json\n",
237243
"from apimrequests import ApimRequests\n",
244+
"from apimtesting import ApimTesting\n",
238245
"\n",
239246
"reqsApim = ApimRequests(apim_gateway_url)\n",
240247
"reqsAfd = ApimRequests(afd_endpoint_url)\n",
248+
"tests = ApimTesting(\"AFD-APIM-PE Tests (Post-Lockdown)\")\n",
241249
"\n",
242250
"# 1) Unsuccessful call to APIM Gateway URL (should fail with 403 Forbidden)\n",
243-
"reqsApim.singleGet('/', msg = '1) Calling Hello World (Root) API via API Management Gateway URL. Expect 403 as APIM public access is disabled now.')\n",
251+
"output = reqsApim.singleGet('/', msg = '1) Calling Hello World (Root) API via API Management Gateway URL. Expect 403 as APIM public access is disabled now.')\n",
252+
"tests.verify(json.loads(output)['statusCode'], 403)\n",
244253
"\n",
245254
"# 2) Successful call to Front Door (200)\n",
246-
"reqsAfd.singleGet('/', msg = '2) Calling Hello World (Root) API via Azure Front Door. Expect 200.')\n",
255+
"output = reqsAfd.singleGet('/', msg = '2) Calling Hello World (Root) API via Azure Front Door. Expect 200.')\n",
256+
"tests.verify(output, 'Hello World from API Management!')\n",
247257
"\n",
248258
"# 3) Successful calls to Front Door -> APIM -> ACA (200)\n",
249259
"if use_ACA:\n",
250-
" reqsAfd.singleGet('/aca-1', msg = '3) Calling Hello World (ACA 1) API via Azure Front Door. Expect 200.')\n",
251-
" reqsAfd.singleGet('/aca-2', msg = '4) Calling Hello World (ACA 2) API via Azure Front Door. Expect 200.')\n",
252-
" reqsAfd.singleGet('/aca-pool', msg = '5) Calling Hello World (ACA Pool) API via Azure Front Door. Expect 200.')\n",
260+
" output = reqsAfd.singleGet('/aca-1', msg = '3) Calling Hello World (ACA 1) API via Azure Front Door. Expect 200.')\n",
261+
" tests.verify(output, 'Hello World!')\n",
262+
"\n",
263+
" output = reqsAfd.singleGet('/aca-2', msg = '4) Calling Hello World (ACA 2) API via Azure Front Door. Expect 200.')\n",
264+
" tests.verify(output, 'Hello World!')\n",
265+
"\n",
266+
" output = reqsAfd.singleGet('/aca-pool', msg = '5) Calling Hello World (ACA Pool) API via Azure Front Door. Expect 200.')\n",
267+
" tests.verify(output, 'Hello World!')\n",
253268
"else:\n",
254269
" utils.print_message('ACA APIs were not created. Skipping ACA API calls.', blank_above = True)\n",
255270
"\n",
271+
"tests.print_summary()\n",
272+
"\n",
256273
"utils.print_ok('All done!')"
257274
]
258275
},

infrastructure/apim-aca/create.ipynb

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,27 @@
116116
"source": [
117117
"import utils\n",
118118
"from apimrequests import ApimRequests\n",
119+
"from apimtesting import ApimTesting\n",
119120
"\n",
120121
"reqs = ApimRequests(apim_gateway_url)\n",
122+
"tests = ApimTesting(\"APIM-ACA Tests\")\n",
121123
"\n",
122-
"reqs.singleGet('/', msg = 'Calling Hello World (Root) API')\n",
123-
"reqs.singleGet('/aca-1/', msg = 'Calling Hello World (ACA Backend 1) API')\n",
124-
"reqs.singleGet('/aca-2/', msg = 'Calling Hello World (ACA Backend 2) API')\n",
125-
"reqs.multiGet('/aca-pool/', 3, msg = 'Calling Hello World (ACA Backend Pool) API')\n",
124+
"output = reqs.singleGet('/', msg = 'Calling Hello World (Root) API')\n",
125+
"tests.verify(output, 'Hello World from API Management!')\n",
126+
"\n",
127+
"output = reqs.singleGet('/aca-1/', msg = 'Calling Hello World (ACA Backend 1) API')\n",
128+
"tests.verify(output, 'Hello World!')\n",
129+
"\n",
130+
"output = reqs.singleGet('/aca-2/', msg = 'Calling Hello World (ACA Backend 2) API')\n",
131+
"tests.verify(output, 'Hello World!')\n",
132+
"\n",
133+
"output = reqs.multiGet('/aca-pool/', 3, msg = 'Calling Hello World (ACA Backend Pool) API')\n",
134+
"tests.verify(len(output), 3)\n",
135+
"tests.verify(output[0]['response'], 'Hello World!')\n",
136+
"tests.verify(output[1]['response'], 'Hello World!')\n",
137+
"tests.verify(output[2]['response'], 'Hello World!')\n",
138+
"\n",
139+
"tests.print_summary()\n",
126140
"\n",
127141
"utils.print_ok('All done!')"
128142
]

infrastructure/simple-apim/create.ipynb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,15 @@
9898
"source": [
9999
"import utils\n",
100100
"from apimrequests import ApimRequests\n",
101+
"from apimtesting import ApimTesting\n",
101102
"\n",
102103
"reqs = ApimRequests(apim_gateway_url)\n",
104+
"tests = ApimTesting(\"Simple APIM Tests\")\n",
103105
"\n",
104106
"output = reqs.singleGet('/', msg = 'Calling Hello World (Root) API')\n",
107+
"tests.verify(output, 'Hello World from API Management!')\n",
108+
"\n",
109+
"tests.print_summary()\n",
105110
"\n",
106111
"utils.print_ok('All done!')"
107112
]

samples/authX-pro/create.ipynb

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,13 @@
170170
"source": [
171171
"import utils\n",
172172
"from apimrequests import ApimRequests\n",
173+
"from apimtesting import ApimTesting\n",
173174
"from apimtypes import Role\n",
174175
"from users import UserHelper\n",
175176
"from authfactory import AuthFactory\n",
176177
"\n",
178+
"tests = ApimTesting(\"AuthX-Pro Sample Tests\")\n",
179+
"\n",
177180
"# Preflight: Check if the infrastructure architecture deployment uses Azure Front Door. If so, assume that APIM is not directly accessible and use the Front Door URL instead.\n",
178181
"endpoint_url = utils.test_url_preflight_check(deployment, rg_name, apim_gateway_url)\n",
179182
"\n",
@@ -187,10 +190,17 @@
187190
"reqsApimAdmin.headers['Authorization'] = f'Bearer {encoded_jwt_token_hr_admin}'\n",
188191
"\n",
189192
"# Call APIM\n",
190-
"reqsApimAdmin.singleGet(hremployees_api_path, msg = 'Calling GET Employees API via API Management Gateway URL. Expect 200.')\n",
191-
"reqsApimAdmin.singlePost(hremployees_api_path, msg = 'Calling POST Employees API via API Management Gateway URL. Expect 200.')\n",
192-
"reqsApimAdmin.singleGet(hrbenefits_api_path, msg = 'Calling GET Benefits API via API Management Gateway URL. Expect 200.')\n",
193-
"reqsApimAdmin.singlePost(hrbenefits_api_path, msg = 'Calling POST Benefits API via API Management Gateway URL. Expect 200.')\n",
193+
"output = reqsApimAdmin.singleGet(hremployees_api_path, msg = 'Calling GET Employees API via API Management Gateway URL. Expect 200.')\n",
194+
"tests.verify(output, 'Successful GET')\n",
195+
"\n",
196+
"output = reqsApimAdmin.singlePost(hremployees_api_path, msg = 'Calling POST Employees API via API Management Gateway URL. Expect 200.')\n",
197+
"tests.verify(output, 'Successful POST')\n",
198+
"\n",
199+
"output = reqsApimAdmin.singleGet(hrbenefits_api_path, msg = 'Calling GET Benefits API via API Management Gateway URL. Expect 200.')\n",
200+
"tests.verify(output, 'Successful GET')\n",
201+
"\n",
202+
"output = reqsApimAdmin.singlePost(hrbenefits_api_path, msg = 'Calling POST Benefits API via API Management Gateway URL. Expect 200.')\n",
203+
"tests.verify(output, 'Successful POST')\n",
194204
"\n",
195205
"# 2) HR Associate\n",
196206
"# Create a JSON Web Token with a payload and sign it with the symmetric key from above.\n",
@@ -202,10 +212,19 @@
202212
"reqsApimAssociate.headers['Authorization'] = f'Bearer {encoded_jwt_token_hr_associate}'\n",
203213
"\n",
204214
"# Call APIM\n",
205-
"reqsApimAssociate.singleGet(hremployees_api_path, msg = 'Calling GET Employees API via API Management Gateway URL. Expect 200.')\n",
206-
"reqsApimAssociate.singlePost(hremployees_api_path, msg = 'Calling POST Employees API via API Management Gateway URL. Expect 403.')\n",
207-
"reqsApimAssociate.singleGet(hrbenefits_api_path, msg = 'Calling GET Benefits API via API Management Gateway URL. Expect 200.')\n",
208-
"reqsApimAssociate.singlePost(hrbenefits_api_path, msg = 'Calling POST Benefits API via API Management Gateway URL. Expect 403.')\n",
215+
"output = reqsApimAssociate.singleGet(hremployees_api_path, msg = 'Calling GET Employees API via API Management Gateway URL. Expect 200.')\n",
216+
"tests.verify(output, 'Successful GET')\n",
217+
"\n",
218+
"output = reqsApimAssociate.singlePost(hremployees_api_path, msg = 'Calling POST Employees API via API Management Gateway URL. Expect 403.')\n",
219+
"tests.verify(output, 'Access denied - no matching roles found')\n",
220+
"\n",
221+
"output = reqsApimAssociate.singleGet(hrbenefits_api_path, msg = 'Calling GET Benefits API via API Management Gateway URL. Expect 200.')\n",
222+
"tests.verify(output, 'Successful GET')\n",
223+
"\n",
224+
"output = reqsApimAssociate.singlePost(hrbenefits_api_path, msg = 'Calling POST Benefits API via API Management Gateway URL. Expect 403.')\n",
225+
"tests.verify(output, 'Access denied - no matching roles found')\n",
226+
"\n",
227+
"tests.print_summary()\n",
209228
"\n",
210229
"utils.print_ok('All done!')"
211230
]

samples/authX/create.ipynb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,13 @@
136136
"source": [
137137
"import utils\n",
138138
"from apimrequests import ApimRequests\n",
139+
"from apimtesting import ApimTesting\n",
139140
"from apimtypes import Role\n",
140141
"from users import UserHelper\n",
141142
"from authfactory import AuthFactory\n",
142143
"\n",
144+
"tests = ApimTesting(\"AuthX Sample Tests\")\n",
145+
"\n",
143146
"# Preflight: Check if the infrastructure architecture deployment uses Azure Front Door. If so, assume that APIM is not directly accessible and use the Front Door URL instead.\n",
144147
"endpoint_url = utils.test_url_preflight_check(deployment, rg_name, apim_gateway_url)\n",
145148
"\n",
@@ -153,8 +156,11 @@
153156
"reqsApimAdmin.headers['Authorization'] = f'Bearer {encoded_jwt_token_hr_admin}'\n",
154157
"\n",
155158
"# Call APIM\n",
156-
"reqsApimAdmin.singleGet('/employees', msg = 'Calling GET Employees API via API Management Gateway URL. Expect 200.')\n",
157-
"reqsApimAdmin.singlePost('/employees', msg = 'Calling POST Employees API via API Management Gateway URL. Expect 200.')\n",
159+
"output = reqsApimAdmin.singleGet('/employees', msg = 'Calling GET Employees API via API Management Gateway URL. Expect 200.')\n",
160+
"tests.verify(output, 'Returning a mock employee')\n",
161+
"\n",
162+
"output = reqsApimAdmin.singlePost('/employees', msg = 'Calling POST Employees API via API Management Gateway URL. Expect 200.')\n",
163+
"tests.verify(output, 'A mock employee has been created.')\n",
158164
"\n",
159165
"# 2) HR Associate\n",
160166
"# Create a JSON Web Token with a payload and sign it with the symmetric key from above.\n",
@@ -166,8 +172,14 @@
166172
"reqsApimAssociate.headers['Authorization'] = f'Bearer {encoded_jwt_token_hr_associate}'\n",
167173
"\n",
168174
"# Call APIM\n",
169-
"reqsApimAssociate.singleGet('/employees', msg = 'Calling GET Employees API via API Management Gateway URL. Expect 200.')\n",
170-
"reqsApimAssociate.singlePost('/employees', msg = 'Calling POST Employees API via API Management Gateway URL. Expect 403.')\n",
175+
"output = reqsApimAssociate.singleGet('/employees', msg = 'Calling GET Employees API via API Management Gateway URL. Expect 200.')\n",
176+
"tests.verify(output, 'Returning a mock employee')\n",
177+
"\n",
178+
"output = reqsApimAssociate.singlePost('/employees', msg = 'Calling POST Employees API via API Management Gateway URL. Expect 403.')\n",
179+
"# The return value is not good enough, but checking for an empty string is the best we can do for now.\n",
180+
"tests.verify(output, '')\n",
181+
"\n",
182+
"tests.print_summary()\n",
171183
"\n",
172184
"utils.print_ok('All done!')"
173185
]

samples/general/create.ipynb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,14 @@
111111
"source": [
112112
"import utils\n",
113113
"from apimrequests import ApimRequests\n",
114+
"from apimtesting import ApimTesting\n",
115+
"\n",
116+
"tests = ApimTesting(\"General Sample Tests\")\n",
114117
"\n",
115118
"# 1) Issue a direct request to API Management\n",
116119
"reqsApim = ApimRequests(apim_gateway_url)\n",
117-
"reqsApim.singleGet('/request-headers', msg = 'Calling Request Headers API via API Management Gateway URL. Response codes 200 and 403 are both valid depending on the infrastructure used.')\n",
120+
"output = reqsApim.singleGet('/request-headers', msg = 'Calling Request Headers API via API Management Gateway URL. Response codes 200 and 403 are both valid depending on the infrastructure used.')\n",
121+
"tests.verify('Host:' in output, True)\n",
118122
"\n",
119123
"# 2) Issue requests against Front Door.\n",
120124
"# Check if the infrastructure architecture deployment uses Azure Front Door.\n",
@@ -123,7 +127,10 @@
123127
"\n",
124128
"if afd_endpoint_url:\n",
125129
" reqsAfd = ApimRequests(afd_endpoint_url)\n",
126-
" reqsAfd.singleGet('/request-headers', msg = 'Calling Request Headers API via via Azure Front Door. Expect 200.')\n",
130+
" output = reqsAfd.singleGet('/request-headers', msg = 'Calling Request Headers API via via Azure Front Door. Expect 200.')\n",
131+
" tests.verify('Host:' in output, True)\n",
132+
"\n",
133+
"tests.print_summary()\n",
127134
"\n",
128135
"utils.print_ok('All done!')"
129136
]

samples/load-balancing/create.ipynb

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,42 +114,61 @@
114114
"metadata": {},
115115
"outputs": [],
116116
"source": [
117+
"import json\n",
117118
"import time\n",
118119
"import utils\n",
119120
"from apimrequests import ApimRequests\n",
121+
"from apimtesting import ApimTesting\n",
120122
"\n",
121123
"def zzzs():\n",
122124
" sleep_in_s = 5\n",
123125
" utils.print_message(f'Waiting for {sleep_in_s} seconds for the backend timeouts to reset before starting the next set of calls', blank_above = True)\n",
124126
" time.sleep(sleep_in_s) # Wait a bit before the next set of calls to allow for the backend timeouts to reset\n",
125127
"\n",
128+
"tests = ApimTesting(\"Load Balancing Sample Tests\")\n",
129+
"\n",
126130
"# Preflight: Check if the infrastructure architecture deployment uses Azure Front Door. If so, assume that APIM is not directly accessible and use the Front Door URL instead.\n",
127131
"endpoint_url = utils.test_url_preflight_check(deployment, rg_name, apim_gateway_url)\n",
128132
"reqs = ApimRequests(endpoint_url)\n",
129133
"\n",
134+
"# Quick test to verify load balancing API is accessible\n",
135+
"output = reqs.singleGet('/lb-prioritized', msg = 'Quick test of load balancing API')\n",
136+
"# We expect to see a priority 1 backend (at index 0) with a count of 1 as this is the first request.\n",
137+
"tests.verify(json.loads(output)['index'], 0)\n",
138+
"tests.verify(json.loads(output)['count'], 1)\n",
139+
"\n",
140+
"# The following test assertions are rather basic. The real verification comes in the charts in the subsequent cell.\n",
141+
"\n",
130142
"# 1) Prioritized API calls\n",
131-
"utils.print_message('1/4: Starting API calls for prioritized distribution (50/50)')\n",
132-
"api_results_prioritized = reqs.multiGet('/lb-prioritized', runs = 15, msg = 'Calling prioritized APIs')\n",
133-
"zzzs()\n",
143+
"utils.print_message('1/5: Starting API calls for prioritized distribution (50/50)')\n",
144+
"output = reqs.multiGet('/lb-prioritized', runs = 15, msg = 'Calling prioritized APIs')\n",
145+
"tests.verify(len(output), 15)\n",
134146
"\n",
135-
"# 2) Weighted API calls\n",
136-
"utils.print_message('2/4: Starting API calls for weighted distribution (50/50)', blank_above = True)\n",
137-
"api_results_weighted_equal = reqs.multiGet('/lb-weighted-equal', runs = 15, msg = 'Calling weighted (equal) APIs')\n",
147+
"# # 2) Weighted API calls\n",
138148
"zzzs()\n",
149+
"utils.print_message('2/5: Starting API calls for weighted distribution (50/50)', blank_above = True)\n",
150+
"output = reqs.multiGet('/lb-weighted-equal', runs = 15, msg = 'Calling weighted (equal) APIs')\n",
151+
"tests.verify(len(output), 15)\n",
139152
"\n",
140-
"# 3) Weighted API calls\n",
141-
"utils.print_message('3/4: Starting API calls for weighted distribution (80/20)', blank_above = True)\n",
142-
"api_results_weighted_unequal = reqs.multiGet('/lb-weighted-unequal', runs = 15, msg = 'Calling weighted (unequal) APIs')\n",
153+
"# # 3) Weighted API calls\n",
143154
"zzzs()\n",
155+
"utils.print_message('3/5: Starting API calls for weighted distribution (80/20)', blank_above = True)\n",
156+
"output = reqs.multiGet('/lb-weighted-unequal', runs = 15, msg = 'Calling weighted (unequal) APIs')\n",
157+
"tests.verify(len(output), 15)\n",
144158
"\n",
145159
"# 4) Prioritized & weighted API calls\n",
146-
"utils.print_message('4/4: Starting API calls for prioritized & weighted distribution', blank_above = True)\n",
147-
"api_results_prioritized_and_weighted = reqs.multiGet('/lb-prioritized-weighted', runs = 20, msg = 'Calling prioritized & weighted APIs')\n",
148160
"zzzs()\n",
161+
"utils.print_message('4/5: Starting API calls for prioritized & weighted distribution', blank_above = True)\n",
162+
"output = reqs.multiGet('/lb-prioritized-weighted', runs = 20, msg = 'Calling prioritized & weighted APIs')\n",
163+
"tests.verify(len(output), 20)\n",
149164
"\n",
150165
"# 5) Prioritized & weighted API calls (500ms sleep)\n",
151-
"utils.print_message('5/4: Starting API calls for prioritized & weighted distribution (500ms sleep)', blank_above = True)\n",
152-
"api_results_prioritized_and_weighted_sleep = reqs.multiGet('/lb-prioritized-weighted', runs = 20, msg = 'Calling prioritized & weighted APIs', sleepMs = 500)\n",
166+
"zzzs()\n",
167+
"utils.print_message('5/5: Starting API calls for prioritized & weighted distribution (500ms sleep)', blank_above = True)\n",
168+
"output = reqs.multiGet('/lb-prioritized-weighted', runs = 20, msg = 'Calling prioritized & weighted APIs', sleepMs = 500)\n",
169+
"tests.verify(len(output), 20)\n",
170+
"\n",
171+
"tests.print_summary()\n",
153172
"\n",
154173
"utils.print_ok('All done!')"
155174
]

0 commit comments

Comments
 (0)