Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

gdp / apps / rest-test / gdp-rest-testcases.py @ master

History | View | Annotate | Download (31.3 KB)

1 04ad7859 Rick Pratt
#!/usr/bin/env python
2
#
3
#        RESTful interface to GDP - Test Suite
4
#
5
#        ----- BEGIN LICENSE BLOCK -----
6
#        Applications for the Global Data Plane
7
#        From the Ubiquitous Swarm Lab, 490 Cory Hall, U.C. Berkeley.
8
#
9
#        Copyright (c) 2017, Regents of the University of California.
10
#        All rights reserved.
11
#
12
#        Permission is hereby granted, without written agreement and without
13
#        license or royalty fees, to use, copy, modify, and distribute this
14
#        software and its documentation for any purpose, provided that the above
15
#        copyright notice and the following two paragraphs appear in all copies
16
#        of this software.
17
#
18
#        IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
19
#        SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST
20
#        PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
21
#        EVEN IF REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
#
23
#        REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
24
#        LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25
#        FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION,
26
#        IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO
27
#        OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
28
#        OR MODIFICATIONS.
29
#        ----- END LICENSE BLOCK -----
30
31
import os
32
import sys
33
import requests
34
import json
35
import fcntl
36
import re
37
import socket
38
import subprocess
39
40
#
41 df9efbfe Rick Pratt
# sanity check invocations
42
#
43
testbed = socket.gethostname()
44
if testbed != "gdp-rest-01":
45
    print "host is {}".format(testbed)
46
    print "Error: this script is only safe to run on the RESTful server"
47
    sys.exit(1)
48
49
#
50 04ad7859 Rick Pratt
# Monitor the gdp-rest-v2.log for diagnostic detail
51
#
52
log_path = "/var/log/gdp/gdp-rest-v2.log"
53
log = open(log_path)
54
log.seek(0, os.SEEK_END)
55
log_fd = log.fileno()
56
flags = fcntl.fcntl(log_fd, fcntl.F_GETFL)
57
fcntl.fcntl(log_fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
58
59
# dev null to silence subprocess output
60
dn = open("/dev/null", "w")
61
62
test_auth = None
63
#
64
# Create a temporary test account on gdp-rest-01 (do not use sample strings):
65
#
66
# $cat /etc/lighttpd/.plainauth
67
# your_unique_testuser:your_unique_testpassword
68
# [other_permanent_user_accounts_left_untouched]
69
# $
70
#
71 1a2e6f40 Rick Pratt
# ...fill in test_auth to match the above, and uncomment it
72 04ad7859 Rick Pratt
#
73
# test_auth = ("your_unique_testuser", "your_unique_testpassword")
74
75
if test_auth == None:
76
    print "Error: modify gdp-rest-testcases.py to use a temporary test account"
77
    sys.exit(1)
78
79
json_header = { 'Content-type': 'application/json' }
80
81 a555e771 Rick Pratt
# clean up test logs (defined to be gdp-rest-01 local logs), while
82
# being careful to only remove keys which go with test logs, as all
83
# other keys are for real logs located in the real GDP.
84
def clean_glogs_and_keys():
85 1a2e6f40 Rick Pratt
    output = subprocess.check_output([ "sudo", "-g", "gdp", "-u", "gdp",
86
                                       "/usr/bin/find",
87 a555e771 Rick Pratt
                                       "/var/swarm/gdp/glogs/", 
88
                                       "-type", "f", "-name", "*.glog" ])
89 1a2e6f40 Rick Pratt
90
    lines = output.splitlines()
91
    for line in lines:
92 a555e771 Rick Pratt
        # find <glog_id> in /var/swarm/gdp/glogs/<glog_id>.glog
93
        glog_id = line[25:-5]
94 1a2e6f40 Rick Pratt
        # check expectations, where file removal is involved
95 a555e771 Rick Pratt
        if len(glog_id) == 43:
96
            print "Info: remove local GCL {}...".format(glog_id),
97 1a2e6f40 Rick Pratt
            subprocess.call([ "sudo", "-g", "gdp", "-u", "gdp",
98 a555e771 Rick Pratt
                              "/usr/bin/find", "/var/swarm/gdp/glogs/", 
99
                              "-type", "f", "-name", glog_id + "*",
100 1a2e6f40 Rick Pratt
                              "-exec", "/bin/rm", "-f", "{}", ";"])
101
            print "done"
102 a555e771 Rick Pratt
            print "Info: remove local key {}...".format(glog_id),
103 1a2e6f40 Rick Pratt
            subprocess.call([ "sudo", "-g", "gdp", "-u", "gdp",
104
                              "/usr/bin/find", "/etc/gdp/keys/", 
105 a555e771 Rick Pratt
                              "-type", "f", "-name", glog_id + ".pem",
106 1a2e6f40 Rick Pratt
                              "-exec", "/bin/rm", "-f", "{}", ";"])
107
            print "done"
108
        else:
109 a555e771 Rick Pratt
            print "Error: cleanup has unexpected glog_id: {}".format(glog_id)
110 df9efbfe Rick Pratt
111
def page_display(p):
112
    if p != None:
113 04ad7859 Rick Pratt
        print "STATUS:"
114 df9efbfe Rick Pratt
        print p.status_code
115 04ad7859 Rick Pratt
        print "HEADERS:"
116 df9efbfe Rick Pratt
        print p.headers
117 04ad7859 Rick Pratt
        print "CONTENT:"
118 df9efbfe Rick Pratt
        print p.content
119
    else:
120
        print "Error: no page"
121
#
122
123
def failed_page_display(p):
124
    print "FAILED"
125
    print "DIAGNOSTICS:"
126
    page_display(p)
127
    print "END DIAGNOSTICS"
128
#
129
130
def failed_log_and_page_display(p, l):
131
    print "FAILURE DIAGNOSTICS:"
132
    print "==== gdp-rest-v2.log:"
133
    print l.read(-1)
134
    print "==== end"
135
136
    print "==== response page:"
137
    page_display(p)
138
    print "==== end"
139
    print "END DIAGNOSTICS"
140
#
141
    
142
def test_write(tc, tid, gid, rn):
143
    json_body = {
144
        tid : rn,
145
        "gcl_id" : gid
146
    }
147
    page = requests.post("https://gdp-rest-01.eecs.berkeley.edu/gdp/v2/gcl/" +
148
                         gid,
149
                         auth = test_auth,
150
                         timeout = 60,
151
                         headers = json_header,
152
                         data = json.dumps(json_body))
153
    print "{} WRITE {}".format(tc, rn),
154
    if page.status_code == 200:
155
        print ""
156
        return True
157
    else:
158
        failed_page_display(page)
159
        return False
160
#
161
162
def test_read(tc, tid, gid, rn):
163
    page = requests.get("https://gdp-rest-01.eecs.berkeley.edu/gdp/v2/gcl/" +
164
                         gid + "?recno=" + rn,
165
                         auth = test_auth,
166
                         timeout = 60)
167
    pj = page.json()
168
    print "{} READ {}".format(tc, rn),
169
    passed = False
170
    if page.status_code == 200 and pj[tid] == rn:
171
        print ""
172
        return True
173 04ad7859 Rick Pratt
    else:
174 df9efbfe Rick Pratt
        failed_page_display(page)
175
        return False
176 04ad7859 Rick Pratt
#
177
178 df9efbfe Rick Pratt
def test_log(tc, tid, gcl_id):
179
    print "{} GCL {} write and read".format(tc, gcl_id)
180
181
    if (test_write(tc, tid, gcl_id, "1") and
182
        test_write(tc, tid, gcl_id, "2") and
183
        test_write(tc, tid, gcl_id, "3") and
184
        test_read(tc, tid, gcl_id, "3") and
185
        test_read(tc, tid, gcl_id, "2") and
186
        test_read(tc, tid, gcl_id, "1")):
187
        print "{} PASSED".format(tc)
188
    else:
189
        print "{} FAILED GCL write and read".format(tc)
190 04ad7859 Rick Pratt
#
191 df9efbfe Rick Pratt
    
192
#
193
# HTTP PUT TESTS
194 04ad7859 Rick Pratt
#
195
196
def test_put_01():
197
    log.seek(0, os.SEEK_END)
198
    test_case = "TEST PUT 01:"
199 df9efbfe Rick Pratt
    test_id = "test_put_01_log"
200 04ad7859 Rick Pratt
    print "{} HTTP PUT new log".format(test_case)
201
202
203
    json_body = {
204 0cb06b6b Rick Pratt
        "external-name" : "edu.berkeley.eecs.gdp-rest.test_put_01"
205 04ad7859 Rick Pratt
    }
206
207
    page = requests.put("https://gdp-rest-01.eecs.berkeley.edu/gdp/v2/gcl",
208
                        auth = test_auth,
209
                        timeout = 60,
210
                        headers = json_header,
211
                        data = json.dumps(json_body))
212 df9efbfe Rick Pratt
    if page.status_code == 201:
213
        pj = page.json()
214
        gcl_name = pj["gcl_name"]
215 0cb06b6b Rick Pratt
        # DEPRECATED gdplogd_name retrieval code block
216
        # gdplogd_name = pj["gdplogd_name"]
217
        # if gcl_name != None and gdplogd_name != None:
218
        #     print "{} PASSED \"{}\" \"{}\"".format(test_case,
219
        #     gcl_name, gdplogd_name)
220
        #     test_log(test_case, test_id, gcl_name)
221
        # else:
222
        #     print "{} FAILED page: \"{}\" \"{}\"".format(test_case,
223
        #                                                  gcl_name, gdplogd_name)
224
        #     failed_log_and_page_display(page, log)
225
        # END DEPRECATED gdplogd_name retrieval code block
226
        if gcl_name != None:
227
            print "{} PASSED \"{}\"".format(test_case, gcl_name)
228 df9efbfe Rick Pratt
            test_log(test_case, test_id, gcl_name)
229
        else:
230 0cb06b6b Rick Pratt
            print "{} FAILED page: \"{}\"".format(test_case, gcl_name)
231 df9efbfe Rick Pratt
            failed_log_and_page_display(page, log)
232 04ad7859 Rick Pratt
    else:
233 df9efbfe Rick Pratt
        print "{} FAILED status code: {}".format(test_case, page.status_code)
234
        failed_log_and_page_display(page, log)
235 04ad7859 Rick Pratt
#
236
237
def test_put_02():
238
    log.seek(0, os.SEEK_END)
239
    test_case = "TEST PUT 02:"
240 df9efbfe Rick Pratt
    print "{} HTTP PUT existing log (from TEST PUT 01)".format(test_case)
241 04ad7859 Rick Pratt
    
242
    json_body = {
243 0cb06b6b Rick Pratt
        "external-name" : "edu.berkeley.eecs.gdp-rest.test_put_01"
244 04ad7859 Rick Pratt
    }
245
246
    page = requests.put("https://gdp-rest-01.eecs.berkeley.edu/gdp/v2/gcl",
247
                        auth = test_auth,
248
                        timeout = 60,
249
                        headers = json_header,
250
                        data = json.dumps(json_body))
251 df9efbfe Rick Pratt
    if page.status_code == 409:
252
        pj = page.json()
253
        detail = pj["detail"]
254
        if detail == "external-name already exists on gdplogd server":
255
            print "{} PASSED \"{}\"".format(test_case, detail)
256
        else:
257
            print "{} FAILED page: \"{}\"".format(test_case, detail)
258
            failed_log_and_page_display(page, log)
259
            
260 04ad7859 Rick Pratt
    else:
261 df9efbfe Rick Pratt
        print "{} FAILED status code: {}".format(test_case, page.status_code)
262
        failed_log_and_page_display(page, log)
263 04ad7859 Rick Pratt
#
264 df9efbfe Rick Pratt
        
265 04ad7859 Rick Pratt
def test_put_03():
266
    log.seek(0, os.SEEK_END)
267
    test_case = "TEST PUT 03:"
268
    print "{} HTTP PUT with no external-name".format(test_case)
269
    
270
    json_body = {
271
        "NO-external-name" : "edu.berkeley.eecs.gdp-rest.test_put_03"
272
    }
273
274
    page = requests.put("https://gdp-rest-01.eecs.berkeley.edu/gdp/v2/gcl",
275
                        auth = test_auth,
276
                        timeout = 60,
277
                        headers = json_header,
278
                        data = json.dumps(json_body))
279 df9efbfe Rick Pratt
    if page.status_code == 400:
280
        pj = page.json()
281
        detail = pj["detail"]
282
        if detail == "mandatory external-name not found":
283
            print "{} PASSED \"{}\"".format(test_case, detail)
284
        else:
285
            print "{} FAILED page: \"{}\"".format(test_case, detail)
286
            failed_log_and_page_display(page, log)
287
            
288 04ad7859 Rick Pratt
    else:
289 df9efbfe Rick Pratt
        print "{} FAILED status code: {}".format(test_case, page.status_code)
290
        failed_log_and_page_display(page, log)
291 04ad7859 Rick Pratt
#
292
293
def test_put_04():
294
    log.seek(0, os.SEEK_END)
295
    test_case = "TEST PUT 04:"
296
    print "{} HTTP PUT with no request body".format(test_case)
297
    
298
    page = requests.put("https://gdp-rest-01.eecs.berkeley.edu/gdp/v2/gcl",
299
                        auth = test_auth,
300
                        timeout = 60,
301
                        headers = json_header)
302 df9efbfe Rick Pratt
    if page.status_code == 400:
303
        pj = page.json()
304
        detail = pj["detail"]
305
        if detail == "request body not recognized json format":
306
            print "{} PASSED \"{}\"".format(test_case, detail)
307
        else:
308
            print "{} FAILED page: \"{}\"".format(test_case, detail)
309
            failed_log_and_page_display(page, log)
310
            
311 04ad7859 Rick Pratt
    else:
312 df9efbfe Rick Pratt
        print "{} FAILED status code: {}".format(test_case, page.status_code)
313
        failed_log_and_page_display(page, log)
314 04ad7859 Rick Pratt
#
315
316
def test_put_05():
317
    log.seek(0, os.SEEK_END)
318
    test_case = "TEST PUT 05:"
319
    print "{} HTTP PUT with no request header or body".format(test_case)
320
    
321
    page = requests.put("https://gdp-rest-01.eecs.berkeley.edu/gdp/v2/gcl",
322
                        auth = test_auth,
323
                        timeout = 60)
324 df9efbfe Rick Pratt
    if page.status_code == 400:
325
        pj = page.json()
326
        detail = pj["detail"]
327
        if detail == "request body not recognized json format":
328
            print "{} PASSED \"{}\"".format(test_case, detail)
329
        else:
330
            print "{} FAILED page: \"{}\"".format(test_case, detail)
331
            failed_log_and_page_display(page, log)
332
            
333 04ad7859 Rick Pratt
    else:
334 df9efbfe Rick Pratt
        print "{} FAILED status code: {}".format(test_case, page.status_code)
335
        failed_log_and_page_display(page, log)
336 04ad7859 Rick Pratt
#
337
338
def test_put_06():
339
    log.seek(0, os.SEEK_END)
340
    test_case = "TEST PUT 06:"
341 df9efbfe Rick Pratt
    test_id = "test_put_06_log"
342 04ad7859 Rick Pratt
    print "{} HTTP PUT new log, verify all valid options".format(test_case)
343
344
    json_body = {
345
        "external-name" : "edu.berkeley.eecs.gdp-rest.test_put_06",
346
        "-C" : "swarmlab@berkeley.edu",
347
        "-h" : "sha224",
348
        "-k" : "dsa",
349
        "-b" : "1024",
350
        "-c" : "ignored_for_dsa",
351
        "META" : [ "meta1=foo", "meta2=bar" ],
352
    }
353
354
    page = requests.put("https://gdp-rest-01.eecs.berkeley.edu/gdp/v2/gcl",
355
                        auth = test_auth,
356
                        timeout = 60,
357
                        headers = json_header,
358
                        data = json.dumps(json_body))
359 df9efbfe Rick Pratt
    if page.status_code == 201:
360
        pj = page.json()
361
        gcl_name = pj["gcl_name"]
362 0cb06b6b Rick Pratt
        # DEPRECATED gdplogd_name retrieval code block
363
        # gdplogd_name = pj["gdplogd_name"]
364
        # if gcl_name != None and gdplogd_name != None:
365
        #     print "{} PASSED \"{}\" \"{}\"".format(test_case,
366
        #     gcl_name, gdplogd_name)
367
        #     test_log(test_case, test_id, gcl_name)
368
        # else:
369
        #     print "{} FAILED page: \"{}\" \"{}\"".format(test_case,
370
        #                                                  gcl_name, gdplogd_name)
371
        #     failed_log_and_page_display(page, log)
372
        # END DEPRECATED gdplogd_name retrieval code block
373
        if gcl_name != None:
374
            print "{} PASSED \"{}\"".format(test_case, gcl_name)
375 df9efbfe Rick Pratt
            test_log(test_case, test_id, gcl_name)
376
        else:
377 0cb06b6b Rick Pratt
            print "{} FAILED page: \"{}\"".format(test_case, gcl_name)
378 df9efbfe Rick Pratt
            failed_log_and_page_display(page, log)
379 04ad7859 Rick Pratt
    else:
380 df9efbfe Rick Pratt
        print "{} FAILED status code: {}".format(test_case, page.status_code)
381
        failed_log_and_page_display(page, log)
382 04ad7859 Rick Pratt
#
383
384 0cb06b6b Rick Pratt
#
385
# server controlled options -e, -S, -K should not be accessible to clients
386
#
387
388 04ad7859 Rick Pratt
def test_put_07():
389
    log.seek(0, os.SEEK_END)
390
    test_case = "TEST PUT 07:"
391
    print "{} HTTP PUT create log, -e option is not permitted".format(test_case)
392
    
393
    json_body = {
394
        "external-name" : "edu.berkeley.eecs.gdp-rest.test_put_07",
395
        "-e" : "none"
396
    }
397
398
    page = requests.put("https://gdp-rest-01.eecs.berkeley.edu/gdp/v2/gcl",
399
                        auth = test_auth,
400
                        timeout = 60,
401
                        headers = json_header,
402
                        data = json.dumps(json_body))
403 df9efbfe Rick Pratt
    if page.status_code == 400:
404
        pj = page.json()
405
        detail = pj["detail"]
406
        if detail == "request contains unrecognized json objects":
407
            print "{} PASSED \"{}\"".format(test_case, detail)
408
        else:
409
            print "{} FAILED page: \"{}\"".format(test_case, detail)
410
            failed_log_and_page_display(page, log)
411
            
412 04ad7859 Rick Pratt
    else:
413 df9efbfe Rick Pratt
        print "{} FAILED status code: {}".format(test_case, page.status_code)
414
        failed_log_and_page_display(page, log)
415 04ad7859 Rick Pratt
#
416
417
def test_put_08():
418
    log.seek(0, os.SEEK_END)
419 0cb06b6b Rick Pratt
    test_case = "TEST PUT 09:"
420
    print "{} HTTP PUT create log, -S option is not permitted".format(test_case)
421 04ad7859 Rick Pratt
    
422
    json_body = {
423
        "external-name" : "edu.berkeley.eecs.gdp-rest.test_put_08",
424 0cb06b6b Rick Pratt
        "-S" : None
425 04ad7859 Rick Pratt
    }
426
427
    page = requests.put("https://gdp-rest-01.eecs.berkeley.edu/gdp/v2/gcl",
428
                        auth = test_auth,
429
                        timeout = 60,
430
                        headers = json_header,
431
                        data = json.dumps(json_body))
432 df9efbfe Rick Pratt
    if page.status_code == 400:
433
        pj = page.json()
434
        detail = pj["detail"]
435
        if detail == "request contains unrecognized json objects":
436
            print "{} PASSED \"{}\"".format(test_case, detail)
437
        else:
438
            print "{} FAILED page: \"{}\"".format(test_case, detail)
439
            failed_log_and_page_display(page, log)
440
            
441 04ad7859 Rick Pratt
    else:
442 df9efbfe Rick Pratt
        print "{} FAILED status code: {}".format(test_case, page.status_code)
443
        failed_log_and_page_display(page, log)
444 04ad7859 Rick Pratt
#
445
446
def test_put_09():
447
    log.seek(0, os.SEEK_END)
448
    test_case = "TEST PUT 09:"
449 0cb06b6b Rick Pratt
    print "{} HTTP PUT create log, -K option is not permitted".format(test_case)
450 04ad7859 Rick Pratt
    
451
    json_body = {
452 0cb06b6b Rick Pratt
        "external-name" : "edu.berkeley.eecs.gdp-rest.test_put_09",
453
        "-K" : "/etc/gdp/keys"
454 04ad7859 Rick Pratt
    }
455
456
    page = requests.put("https://gdp-rest-01.eecs.berkeley.edu/gdp/v2/gcl",
457
                        auth = test_auth,
458
                        timeout = 60,
459
                        headers = json_header,
460
                        data = json.dumps(json_body))
461 df9efbfe Rick Pratt
    if page.status_code == 400:
462
        pj = page.json()
463
        detail = pj["detail"]
464
        if detail == "request contains unrecognized json objects":
465
            print "{} PASSED \"{}\"".format(test_case, detail)
466
        else:
467
            print "{} FAILED page: \"{}\"".format(test_case, detail)
468
            failed_log_and_page_display(page, log)
469
            
470 04ad7859 Rick Pratt
    else:
471 df9efbfe Rick Pratt
        print "{} FAILED status code: {}".format(test_case, page.status_code)
472
        failed_log_and_page_display(page, log)
473 04ad7859 Rick Pratt
#
474
475 0cb06b6b Rick Pratt
#
476
# unsupported options -G, -q, -s, -w, -W should not be accessible to clients
477
#
478
479 04ad7859 Rick Pratt
def test_put_10():
480
    log.seek(0, os.SEEK_END)
481
    test_case = "TEST PUT 10:"
482 0cb06b6b Rick Pratt
    print "{} HTTP PUT create log, -G option is not permitted".format(test_case)
483 04ad7859 Rick Pratt
    
484
    json_body = {
485 0cb06b6b Rick Pratt
        "external-name" : "edu.berkeley.eecs.gdp-rest.test_put_10",
486
        "-G" : "127.0.0.1"
487 04ad7859 Rick Pratt
    }
488
489
    page = requests.put("https://gdp-rest-01.eecs.berkeley.edu/gdp/v2/gcl",
490
                        auth = test_auth,
491
                        timeout = 60,
492
                        headers = json_header,
493
                        data = json.dumps(json_body))
494 df9efbfe Rick Pratt
    if page.status_code == 400:
495
        pj = page.json()
496
        detail = pj["detail"]
497
        if detail == "request contains unrecognized json objects":
498
            print "{} PASSED \"{}\"".format(test_case, detail)
499
        else:
500
            print "{} FAILED page: \"{}\"".format(test_case, detail)
501
            failed_log_and_page_display(page, log)
502
            
503 04ad7859 Rick Pratt
    else:
504 df9efbfe Rick Pratt
        print "{} FAILED status code: {}".format(test_case, page.status_code)
505
        failed_log_and_page_display(page, log)
506 04ad7859 Rick Pratt
#
507
508
def test_put_11():
509
    log.seek(0, os.SEEK_END)
510
    test_case = "TEST PUT 11:"
511 0cb06b6b Rick Pratt
    print "{} HTTP PUT create log, -q option is not permitted".format(test_case)
512 04ad7859 Rick Pratt
    
513
    json_body = {
514 0cb06b6b Rick Pratt
        "external-name" : "edu.berkeley.eecs.gdp-rest.test_put_11",
515
        "-q" : None
516 04ad7859 Rick Pratt
    }
517
518
    page = requests.put("https://gdp-rest-01.eecs.berkeley.edu/gdp/v2/gcl",
519
                        auth = test_auth,
520
                        timeout = 60,
521
                        headers = json_header,
522
                        data = json.dumps(json_body))
523 df9efbfe Rick Pratt
    if page.status_code == 400:
524
        pj = page.json()
525
        detail = pj["detail"]
526
        if detail == "request contains unrecognized json objects":
527
            print "{} PASSED \"{}\"".format(test_case, detail)
528
        else:
529
            print "{} FAILED page: \"{}\"".format(test_case, detail)
530
            failed_log_and_page_display(page, log)
531
            
532 04ad7859 Rick Pratt
    else:
533 df9efbfe Rick Pratt
        print "{} FAILED status code: {}".format(test_case, page.status_code)
534
        failed_log_and_page_display(page, log)
535 04ad7859 Rick Pratt
#
536
537
def test_put_12():
538
    log.seek(0, os.SEEK_END)
539
    test_case = "TEST PUT 12:"
540 0cb06b6b Rick Pratt
    print "{} HTTP PUT create log, -s option is not permitted".format(test_case)
541 04ad7859 Rick Pratt
    
542
    json_body = {
543 0cb06b6b Rick Pratt
        "external-name" : "edu.berkeley.eecs.gdp-rest.test_put_12",
544
        "-s" : "edu.berkeley.eecs.service.creation"
545 04ad7859 Rick Pratt
    }
546
547
    page = requests.put("https://gdp-rest-01.eecs.berkeley.edu/gdp/v2/gcl",
548
                        auth = test_auth,
549
                        timeout = 60,
550
                        headers = json_header,
551
                        data = json.dumps(json_body))
552 df9efbfe Rick Pratt
    if page.status_code == 400:
553
        pj = page.json()
554
        detail = pj["detail"]
555 0cb06b6b Rick Pratt
        if detail == "request contains unrecognized json objects":
556 df9efbfe Rick Pratt
            print "{} PASSED \"{}\"".format(test_case, detail)
557
        else:
558
            print "{} FAILED page: \"{}\"".format(test_case, detail)
559
            failed_log_and_page_display(page, log)
560
            
561 04ad7859 Rick Pratt
    else:
562 df9efbfe Rick Pratt
        print "{} FAILED status code: {}".format(test_case, page.status_code)
563
        failed_log_and_page_display(page, log)
564 04ad7859 Rick Pratt
#
565
566 0cb06b6b Rick Pratt
def test_put_13():
567 04ad7859 Rick Pratt
    log.seek(0, os.SEEK_END)
568 0cb06b6b Rick Pratt
    test_case = "TEST PUT 13:"
569
    print "{} HTTP PUT create log, -w option is not permitted".format(test_case)
570
    
571 04ad7859 Rick Pratt
    json_body = {
572 0cb06b6b Rick Pratt
        "external-name" : "edu.berkeley.eecs.gdp-rest.test_put_13",
573
        "-w" : None
574 04ad7859 Rick Pratt
    }
575
576 0cb06b6b Rick Pratt
    page = requests.put("https://gdp-rest-01.eecs.berkeley.edu/gdp/v2/gcl",
577 04ad7859 Rick Pratt
                        auth = test_auth,
578
                        timeout = 60,
579
                        headers = json_header,
580
                        data = json.dumps(json_body))
581 0cb06b6b Rick Pratt
    if page.status_code == 400:
582 df9efbfe Rick Pratt
        pj = page.json()
583 0cb06b6b Rick Pratt
        detail = pj["detail"]
584
        if detail == "request contains unrecognized json objects":
585
            print "{} PASSED \"{}\"".format(test_case, detail)
586 df9efbfe Rick Pratt
        else:
587 0cb06b6b Rick Pratt
            print "{} FAILED page: \"{}\"".format(test_case, detail)
588 df9efbfe Rick Pratt
            failed_log_and_page_display(page, log)
589 0cb06b6b Rick Pratt
            
590 04ad7859 Rick Pratt
    else:
591 df9efbfe Rick Pratt
        print "{} FAILED status code: {}".format(test_case, page.status_code)
592
        failed_log_and_page_display(page, log)
593 04ad7859 Rick Pratt
#
594
595 0cb06b6b Rick Pratt
def test_put_14():
596 04ad7859 Rick Pratt
    log.seek(0, os.SEEK_END)
597 0cb06b6b Rick Pratt
    test_case = "TEST PUT 14:"
598
    print "{} HTTP PUT create log, -W option is not permitted".format(test_case)
599
    
600 04ad7859 Rick Pratt
    json_body = {
601 0cb06b6b Rick Pratt
        "external-name" : "edu.berkeley.eecs.gdp-rest.test_put_14",
602
        "-W" : "/etc/gdp/keys"
603 04ad7859 Rick Pratt
    }
604
605 0cb06b6b Rick Pratt
    page = requests.put("https://gdp-rest-01.eecs.berkeley.edu/gdp/v2/gcl",
606 04ad7859 Rick Pratt
                        auth = test_auth,
607
                        timeout = 60,
608
                        headers = json_header,
609
                        data = json.dumps(json_body))
610 0cb06b6b Rick Pratt
    if page.status_code == 400:
611 df9efbfe Rick Pratt
        pj = page.json()
612 0cb06b6b Rick Pratt
        detail = pj["detail"]
613
        if detail == "request contains unrecognized json objects":
614
            print "{} PASSED \"{}\"".format(test_case, detail)
615 df9efbfe Rick Pratt
        else:
616 0cb06b6b Rick Pratt
            print "{} FAILED page: \"{}\"".format(test_case, detail)
617 df9efbfe Rick Pratt
            failed_log_and_page_display(page, log)
618 0cb06b6b Rick Pratt
            
619 04ad7859 Rick Pratt
    else:
620 df9efbfe Rick Pratt
        print "{} FAILED status code: {}".format(test_case, page.status_code)
621
        failed_log_and_page_display(page, log)
622 04ad7859 Rick Pratt
#
623
624 0cb06b6b Rick Pratt
def test_put_15():
625 04ad7859 Rick Pratt
    log.seek(0, os.SEEK_END)
626 0cb06b6b Rick Pratt
    test_case = "TEST PUT 15:"
627
    print "{} HTTP PUT create log, META list is None(s)".format(test_case)
628 04ad7859 Rick Pratt
    
629
    json_body = {
630 0cb06b6b Rick Pratt
        "external-name" : "edu.berkeley.eecs.gdp-rest.test_put_15",
631
        "META" : None
632 04ad7859 Rick Pratt
    }
633
634 0cb06b6b Rick Pratt
    page = requests.put("https://gdp-rest-01.eecs.berkeley.edu/gdp/v2/gcl",
635 04ad7859 Rick Pratt
                        auth = test_auth,
636
                        timeout = 60,
637
                        headers = json_header,
638
                        data = json.dumps(json_body))
639 df9efbfe Rick Pratt
    if page.status_code == 400:
640
        pj = page.json()
641
        detail = pj["detail"]
642 0cb06b6b Rick Pratt
        if detail == "request contains unrecognized json objects":
643 df9efbfe Rick Pratt
            print "{} PASSED \"{}\"".format(test_case, detail)
644
        else:
645
            print "{} FAILED page: \"{}\"".format(test_case, detail)
646
            failed_log_and_page_display(page, log)
647
            
648 04ad7859 Rick Pratt
    else:
649 df9efbfe Rick Pratt
        print "{} FAILED status code: {}".format(test_case, page.status_code)
650
        failed_log_and_page_display(page, log)
651 04ad7859 Rick Pratt
#
652
653 0cb06b6b Rick Pratt
def test_put_16():
654 04ad7859 Rick Pratt
    log.seek(0, os.SEEK_END)
655 0cb06b6b Rick Pratt
    test_case = "TEST PUT 16:"
656
    print "{} HTTP PUT create log, META list element bad(s)".format(test_case)
657 04ad7859 Rick Pratt
    
658 0cb06b6b Rick Pratt
    json_body = {
659
        "external-name" : "edu.berkeley.eecs.gdp-rest.test_put_16",
660
        "META" : [ "foo=1", "missing_equal", "bar=2" ],
661
    }
662
663
    page = requests.put("https://gdp-rest-01.eecs.berkeley.edu/gdp/v2/gcl",
664 04ad7859 Rick Pratt
                        auth = test_auth,
665
                        timeout = 60,
666 0cb06b6b Rick Pratt
                        headers = json_header,
667
                        data = json.dumps(json_body))
668 df9efbfe Rick Pratt
    if page.status_code == 400:
669
        pj = page.json()
670
        detail = pj["detail"]
671 0cb06b6b Rick Pratt
        if detail == "request contains unrecognized json objects":
672 df9efbfe Rick Pratt
            print "{} PASSED \"{}\"".format(test_case, detail)
673
        else:
674
            print "{} FAILED page: \"{}\"".format(test_case, detail)
675
            failed_log_and_page_display(page, log)
676
            
677 04ad7859 Rick Pratt
    else:
678 df9efbfe Rick Pratt
        print "{} FAILED status code: {}".format(test_case, page.status_code)
679
        failed_log_and_page_display(page, log)
680 04ad7859 Rick Pratt
#
681
682 0cb06b6b Rick Pratt
#
683
# HTTP POST TESTS
684 04ad7859 Rick Pratt
#
685
686 0cb06b6b Rick Pratt
def test_post_01():
687 04ad7859 Rick Pratt
    log.seek(0, os.SEEK_END)
688 0cb06b6b Rick Pratt
    test_case = "TEST POST 01:"
689
    test_id = "test_post_01_log"
690
    print "{} HTTP POST new log".format(test_case)
691 04ad7859 Rick Pratt
692
693
    json_body = {
694 0cb06b6b Rick Pratt
        "external-name" : None
695 04ad7859 Rick Pratt
    }
696
697
    page = requests.post("https://gdp-rest-01.eecs.berkeley.edu/gdp/v2/gcl",
698
                        auth = test_auth,
699
                        timeout = 60,
700
                        headers = json_header,
701
                        data = json.dumps(json_body))
702 df9efbfe Rick Pratt
    if page.status_code == 201:
703
        pj = page.json()
704
        gcl_name = pj["gcl_name"]
705 0cb06b6b Rick Pratt
        # DEPRECATED gdplogd_name retrieval code block
706
        # gdplogd_name = pj["gdplogd_name"]
707
        # if gcl_name != None and gdplogd_name != None:
708
        #     print "{} PASSED \"{}\" \"{}\"".format(test_case,
709
        #     gcl_name, gdplogd_name)
710
        #     test_log(test_case, test_id, gcl_name)
711
        # else:
712
        #     print "{} FAILED page: \"{}\" \"{}\"".format(test_case,
713
        #                                                  gcl_name, gdplogd_name)
714
        #     failed_log_and_page_display(page, log)
715
        # END DEPRECATED gdplogd_name retrieval code block
716
        if gcl_name != None:
717
            print "{} PASSED \"{}\"".format(test_case, gcl_name)
718 df9efbfe Rick Pratt
            test_log(test_case, test_id, gcl_name)
719
        else:
720 0cb06b6b Rick Pratt
            print "{} FAILED page: \"{}\"".format(test_case, gcl_name)
721 df9efbfe Rick Pratt
            failed_log_and_page_display(page, log)
722 04ad7859 Rick Pratt
    else:
723 df9efbfe Rick Pratt
        print "{} FAILED status code: {}".format(test_case, page.status_code)
724
        failed_log_and_page_display(page, log)
725 04ad7859 Rick Pratt
#
726
727 0cb06b6b Rick Pratt
def test_post_02():
728 04ad7859 Rick Pratt
    log.seek(0, os.SEEK_END)
729 0cb06b6b Rick Pratt
    test_case = "TEST POST 02:"
730
    print "{} HTTP POST with an external-name".format(test_case)
731 04ad7859 Rick Pratt
    
732
    json_body = {
733 0cb06b6b Rick Pratt
        "external-name" : "edu.berkeley.eecs.gdp-rest.test_post_02"
734 04ad7859 Rick Pratt
    }
735
736
    page = requests.post("https://gdp-rest-01.eecs.berkeley.edu/gdp/v2/gcl",
737
                        auth = test_auth,
738
                        timeout = 60,
739
                        headers = json_header,
740
                        data = json.dumps(json_body))
741 df9efbfe Rick Pratt
    if page.status_code == 400:
742
        pj = page.json()
743
        detail = pj["detail"]
744 0cb06b6b Rick Pratt
        if detail == "POST external-name must have null value":
745 df9efbfe Rick Pratt
            print "{} PASSED \"{}\"".format(test_case, detail)
746
        else:
747
            print "{} FAILED page: \"{}\"".format(test_case, detail)
748
            failed_log_and_page_display(page, log)
749
            
750 04ad7859 Rick Pratt
    else:
751 df9efbfe Rick Pratt
        print "{} FAILED status code: {}".format(test_case, page.status_code)
752
        failed_log_and_page_display(page, log)
753 04ad7859 Rick Pratt
#
754
755 0cb06b6b Rick Pratt
def test_post_03():
756 04ad7859 Rick Pratt
    log.seek(0, os.SEEK_END)
757 0cb06b6b Rick Pratt
    test_case = "TEST POST 03:"
758
    print "{} HTTP POST with no request body".format(test_case)
759 04ad7859 Rick Pratt
    
760
    page = requests.post("https://gdp-rest-01.eecs.berkeley.edu/gdp/v2/gcl",
761
                        auth = test_auth,
762
                        timeout = 60,
763 0cb06b6b Rick Pratt
                        headers = json_header)
764 df9efbfe Rick Pratt
    if page.status_code == 400:
765
        pj = page.json()
766
        detail = pj["detail"]
767 0cb06b6b Rick Pratt
        if detail == "request body not recognized json format":
768 df9efbfe Rick Pratt
            print "{} PASSED \"{}\"".format(test_case, detail)
769
        else:
770
            print "{} FAILED page: \"{}\"".format(test_case, detail)
771
            failed_log_and_page_display(page, log)
772
            
773 04ad7859 Rick Pratt
    else:
774 df9efbfe Rick Pratt
        print "{} FAILED status code: {}".format(test_case, page.status_code)
775
        failed_log_and_page_display(page, log)
776 04ad7859 Rick Pratt
#
777
778 0cb06b6b Rick Pratt
def test_post_04():
779 04ad7859 Rick Pratt
    log.seek(0, os.SEEK_END)
780 0cb06b6b Rick Pratt
    test_case = "TEST POST 04:"
781
    print "{} HTTP POST with no request header or body".format(test_case)
782 04ad7859 Rick Pratt
    
783
    page = requests.post("https://gdp-rest-01.eecs.berkeley.edu/gdp/v2/gcl",
784
                        auth = test_auth,
785 0cb06b6b Rick Pratt
                        timeout = 60)
786 04ad7859 Rick Pratt
787 df9efbfe Rick Pratt
    if page.status_code == 400:
788
        pj = page.json()
789
        detail = pj["detail"]
790 0cb06b6b Rick Pratt
        if detail == "request body not recognized json format":
791 df9efbfe Rick Pratt
            print "{} PASSED \"{}\"".format(test_case, detail)
792
        else:
793
            print "{} FAILED page: \"{}\"".format(test_case, detail)
794
            failed_log_and_page_display(page, log)
795
            
796 04ad7859 Rick Pratt
    else:
797 df9efbfe Rick Pratt
        print "{} FAILED status code: {}".format(test_case, page.status_code)
798
        failed_log_and_page_display(page, log)
799 04ad7859 Rick Pratt
#
800
801 0cb06b6b Rick Pratt
def test_post_05():
802 04ad7859 Rick Pratt
    log.seek(0, os.SEEK_END)
803 0cb06b6b Rick Pratt
    test_case = "TEST POST 05:"
804
    test_id = "test_post_05_log"
805
    print "{} HTTP POST new log, verify all valid options".format(test_case)
806 04ad7859 Rick Pratt
807
    json_body = {
808
        "external-name" : None,
809 0cb06b6b Rick Pratt
        "-C" : "swarmlab@berkeley.edu",
810
        "-h" : "sha224",
811
        "-k" : "dsa",
812
        "-b" : "1024",
813
        "-c" : "ignored_for_dsa",
814
        "META" : [ "meta1=foo", "meta2=bar" ],
815 04ad7859 Rick Pratt
    }
816
817
    page = requests.post("https://gdp-rest-01.eecs.berkeley.edu/gdp/v2/gcl",
818
                        auth = test_auth,
819
                        timeout = 60,
820
                        headers = json_header,
821
                        data = json.dumps(json_body))
822 0cb06b6b Rick Pratt
    if page.status_code == 201:
823 df9efbfe Rick Pratt
        pj = page.json()
824 0cb06b6b Rick Pratt
        gcl_name = pj["gcl_name"]
825
        # DEPRECATED gdplogd_name retrieval code block
826
        # gdplogd_name = pj["gdplogd_name"]
827
        # if gcl_name != None and gdplogd_name != None:
828
        #     print "{} PASSED \"{}\" \"{}\"".format(test_case,
829
        #     gcl_name, gdplogd_name)
830
        #     test_log(test_case, test_id, gcl_name)
831
        # else:
832
        #     print "{} FAILED page: \"{}\" \"{}\"".format(test_case,
833
        #                                                  gcl_name, gdplogd_name)
834
        #     failed_log_and_page_display(page, log)
835
        # END DEPRECATED gdplogd_name retrieval code block
836
        if gcl_name != None:
837
            print "{} PASSED \"{}\"".format(test_case, gcl_name)
838
            test_log(test_case, test_id, gcl_name)
839 df9efbfe Rick Pratt
        else:
840 0cb06b6b Rick Pratt
            print "{} FAILED page: \"{}\"".format(test_case, gcl_name)
841 df9efbfe Rick Pratt
            failed_log_and_page_display(page, log)
842 04ad7859 Rick Pratt
    else:
843 df9efbfe Rick Pratt
        print "{} FAILED status code: {}".format(test_case, page.status_code)
844
        failed_log_and_page_display(page, log)
845 04ad7859 Rick Pratt
#
846
847 df9efbfe Rick Pratt
848 04ad7859 Rick Pratt
#
849 df9efbfe Rick Pratt
# RUN TESTS
850 04ad7859 Rick Pratt
#
851
852 0cb06b6b Rick Pratt
# #
853
# # preclean gdp-rest-01 v2 testbed (aborted test runs)
854
# #
855
# print "Info: stopping gdplogd2.service (purge GCL cache from prior test runs)"
856
# subprocess.call("sudo systemctl stop gdplogd2.service", shell=True)
857
# print "Info: stop gdp-rest-v2.service (purge GCL cache from prior test runs)"
858
# subprocess.call("sudo systemctl stop gdp-rest-v2.service", shell=True)
859
# print "Info: clean local logs (and their keys, but not production keys)"
860
# clean_glogs_and_keys()
861
862
# #
863
# # start v2 testbed
864
# #
865
# print "Info: start gdplogd2.service (sudo systemctl)"
866
# if subprocess.call("sudo systemctl start gdplogd2.service", shell=True) != 0:
867
#     print "Error: sudo systemctl start gdplogd2.service failed"
868
#     sys.exit(1)
869
# #
870
# log.seek(0, os.SEEK_END)
871
# print "Info: start gdp-rest-v2.service (sudo systemctl)"
872
# if subprocess.call("sudo systemctl start gdp-rest-v2.service", shell=True) != 0:
873
#     print "Error: sudo systemctl start gdp-rest-v2.service failed"
874
#     sys.exit(1)
875
# #
876
# m = re.search(".*could not initialize SCGI port.*", log.read(-1))
877
# if m != None:
878
#     print "Warning: gdp-rest has not released the SCGI port yet, wait and retry"
879
#     sys.exit(1)
880
# #
881 df9efbfe Rick Pratt
882 04ad7859 Rick Pratt
#
883
# TEST run
884
#
885
886 1a2e6f40 Rick Pratt
# HTTP PUT test suite
887 04ad7859 Rick Pratt
test_put_01()
888
test_put_02()
889
test_put_03()
890
test_put_04()
891
test_put_05()
892
test_put_06()
893
test_put_07()
894
test_put_08()
895
test_put_09()
896
test_put_10()
897
test_put_11()
898
test_put_12()
899 0cb06b6b Rick Pratt
test_put_13()
900
test_put_14()
901
test_put_15()
902
test_put_16()
903 04ad7859 Rick Pratt
904
# HTTP POST test suite
905
test_post_01()
906
test_post_02()
907
test_post_03()
908
test_post_04()
909
test_post_05()
910 0cb06b6b Rick Pratt
911
# print "Info: stopping gdplogd2.service (sudo systemctl) ...",
912
# if subprocess.call("sudo systemctl stop gdplogd2.service", shell=True) != 0:
913
#     print "\nError: systemctl stop gdplogd2.service failed"
914
# else:
915
#     print "stopped"
916
917
# print "Info: stopping gdp-rest-v2.service (sudo systemctl) ...",
918
# if subprocess.call("sudo systemctl stop gdp-rest-v2.service", shell=True) != 0:
919
#     print "\nError: systemctl stop gdp-rest-v2.service failed"
920
# else:
921
#     print "stopped"
922
923
# clean_glogs_and_keys()
924 1a2e6f40 Rick Pratt
925 04ad7859 Rick Pratt
dn.close()
926
log.close()
927
928
sys.exit(0)
929
#