]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
qa/workunits: Python 3 compat fixes for rest/test.py
authorAnirudha Bose <ani07nov@gmail.com>
Fri, 19 Aug 2016 05:47:32 +0000 (11:17 +0530)
committerAnirudha Bose <ani07nov@gmail.com>
Fri, 19 Aug 2016 05:52:27 +0000 (11:22 +0530)
Signed-off-by: Anirudha Bose <ani07nov@gmail.com>
qa/workunits/rest/test.py

index c93da1366e9a12c97604b44b016b311c155bf649..649e39005fa4d474d92b48a2f31a9cfc7fc702e1 100755 (executable)
@@ -1,6 +1,7 @@
 #!/usr/bin/python
 
-import exceptions
+from __future__ import print_function
+
 import json
 import os
 import requests
@@ -12,13 +13,15 @@ import xml.etree.ElementTree
 
 BASEURL = os.environ.get('BASEURL', 'http://localhost:5000/api/v0.1')
 
+
 def fail(r, msg):
-    print >> sys.stderr, 'FAILURE: url ', r.url
-    print >> sys.stderr, msg
-    print >> sys.stderr, 'Response content: ', r.content
-    print >> sys.stderr, 'Headers: ', r.headers
+    print('FAILURE: url ', r.url, file=sys.stderr)
+    print(msg, file=sys.stderr)
+    print('Response content: ', r.text, file=sys.stderr)
+    print('Headers: ', r.headers, file=sys.stderr)
     sys.exit(1)
 
+
 def expect(url, method, respcode, contenttype, extra_hdrs=None, data=None):
     failmsg, r = expect_nofail(url, method, respcode, contenttype, extra_hdrs,
                                data)
@@ -33,7 +36,7 @@ def expect_nofail(url, method, respcode, contenttype, extra_hdrs=None,
     f = fdict[method.lower()]
     r = f(BASEURL + '/' + url, headers=extra_hdrs, data=data)
 
-    print '{0} {1}: {2} {3}'.format(method, url, contenttype, r.status_code)
+    print('{0} {1}: {2} {3}'.format(method, url, contenttype, r.status_code))
 
     if r.status_code != respcode:
         return 'expected {0}, got {1}'.format(respcode, r.status_code), r
@@ -52,15 +55,15 @@ def expect_nofail(url, method, respcode, contenttype, extra_hdrs=None,
         if r_contenttype == 'application/json':
             try:
                 # older requests.py doesn't create r.myjson; create it myself
-                r.myjson = json.loads(r.content)
-                assert(r.myjson != None)
+                r.myjson = json.loads(r.text)
+                assert(r.myjson is not None)
             except Exception as e:
                 return 'Invalid JSON returned: "{0}"'.format(str(e)), r
 
         if r_contenttype == 'application/xml':
             try:
                 # if it's there, squirrel it away for use in the caller
-                r.tree = xml.etree.ElementTree.fromstring(r.content)
+                r.tree = xml.etree.ElementTree.fromstring(r.text)
             except Exception as e:
                 return 'Invalid XML returned: "{0}"'.format(str(e)), r
 
@@ -84,10 +87,10 @@ if __name__ == '__main__':
     r = expect('auth/export?entity=client.xx', 'GET', 200, 'plain')
     # must use text/plain; default is application/x-www-form-urlencoded
     expect('auth/add?entity=client.xx', 'PUT', 200, 'plain',
-           {'Content-Type':'text/plain'}, data=r.content)
+           {'Content-Type':'text/plain'}, data=r.text)
 
     r = expect('auth/list', 'GET', 200, 'plain')
-    assert('client.xx' in r.content)
+    assert('client.xx' in r.text)
 
     r = expect('auth/list.json', 'GET', 200, 'json')
     dictlist = r.myjson['output']['auth_dump']
@@ -114,21 +117,21 @@ if __name__ == '__main__':
 
     # export/import/export, compare
     r = expect('auth/export', 'GET', 200, 'plain')
-    exp1 = r.content
+    exp1 = r.text
     assert('client.xx' in exp1)
     r = expect('auth/import', 'PUT', 200, 'plain',
-               {'Content-Type':'text/plain'}, data=r.content)
+               {'Content-Type':'text/plain'}, data=r.text)
     r2 = expect('auth/export', 'GET', 200, 'plain')
-    assert(exp1 == r2.content)
+    assert(exp1 == r2.text)
     expect('auth/del?entity=client.xx', 'PUT', 200, 'json', JSONHDR)
 
     r = expect('osd/dump', 'GET', 200, 'json', JSONHDR)
     assert('epoch' in r.myjson['output'])
 
-    assert('GLOBAL' in expect('df', 'GET', 200, 'plain').content)
-    assert('CATEGORY' in expect('df?detail=detail', 'GET', 200, 'plain').content)
+    assert('GLOBAL' in expect('df', 'GET', 200, 'plain').text)
+    assert('CATEGORY' in expect('df?detail=detail', 'GET', 200, 'plain').text)
     # test param with no value (treated as param=param)
-    assert('CATEGORY' in expect('df?detail', 'GET', 200, 'plain').content)
+    assert('CATEGORY' in expect('df?detail', 'GET', 200, 'plain').text)
 
     r = expect('df', 'GET', 200, 'json', JSONHDR)
     assert('total_used_bytes' in r.myjson['output']['stats'])
@@ -207,7 +210,7 @@ if __name__ == '__main__':
     expect('mon/dump.xml', 'GET', 200, 'xml')
 
     r = expect('mon/getmap', 'GET', 200, '')
-    assert(len(r.content) != 0)
+    assert(len(r.text) != 0)
     r = expect('mon_status.json', 'GET', 200, 'json')
     assert('name' in r.myjson['output'])
     r = expect('mon_status.xml', 'GET', 200, 'xml')
@@ -243,7 +246,7 @@ if __name__ == '__main__':
         if r.myjson['output']['osds'][0]['up'] == 1:
             break
         else:
-            print >> sys.stderr, "waiting for osd.0 to come back up"
+            print("waiting for osd.0 to come back up", file=sys.stderr)
             time.sleep(10)
 
     r = expect('osd/dump', 'GET', 200, 'json', JSONHDR)
@@ -340,7 +343,7 @@ if __name__ == '__main__':
     expect('pg/dump_stuck?stuckops=stale', 'GET', 200, '')
 
     r = expect('pg/getmap', 'GET', 200, '')
-    assert(len(r.content) != 0)
+    assert(len(r.text) != 0)
 
     r = expect('pg/map?pgid=0.0', 'GET', 200, 'json', JSONHDR)
     assert('acting' in r.myjson['output'])
@@ -384,12 +387,12 @@ if __name__ == '__main__':
     assert(r.tree.find('output/status/osdmap') is not None)
 
     r = expect('tell/osd.0/version', 'GET', 200, '')
-    assert('ceph version' in r.content)
+    assert('ceph version' in r.text)
     expect('tell/osd.999/version', 'GET', 400, '')
     expect('tell/osd.foo/version', 'GET', 400, '')
 
     r = expect('tell/osd.0/dump_pg_recovery_stats', 'GET', 200, '')
-    assert('Started' in r.content)
+    assert('Started' in r.text)
 
     expect('osd/reweight?id=0&weight=0.9', 'PUT', 200, '')
     expect('osd/reweight?id=0&weight=-1', 'PUT', 400, '')
@@ -414,4 +417,4 @@ if __name__ == '__main__':
     r = expect('osd/pool/get.json?pool=rbd&var=crush_ruleset', 'GET', 200, 'json')
     assert(r.myjson['output']['crush_ruleset'] == 0)
 
-    print 'OK'
+    print('OK')