Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / qa / tasks / util / rgw.py
1 from cStringIO import StringIO
2 import logging
3 import json
4 import requests
5
6 from requests.packages.urllib3 import PoolManager
7 from requests.packages.urllib3.util import Retry
8 from urlparse import urlparse
9
10 from teuthology.orchestra.connection import split_user
11 from teuthology import misc as teuthology
12
13 log = logging.getLogger(__name__)
14
15 def rgwadmin(ctx, client, cmd, stdin=StringIO(), check_status=False,
16              format='json', decode=True, log_level=logging.DEBUG):
17     log.info('rgwadmin: {client} : {cmd}'.format(client=client,cmd=cmd))
18     testdir = teuthology.get_testdir(ctx)
19     cluster_name, daemon_type, client_id = teuthology.split_role(client)
20     client_with_id = daemon_type + '.' + client_id
21     pre = [
22         'adjust-ulimits',
23         'ceph-coverage'.format(tdir=testdir),
24         '{tdir}/archive/coverage'.format(tdir=testdir),
25         'radosgw-admin'.format(tdir=testdir),
26         '--log-to-stderr',
27         '--format', format,
28         '-n',  client_with_id,
29         '--cluster', cluster_name,
30         ]
31     pre.extend(cmd)
32     log.log(log_level, 'rgwadmin: cmd=%s' % pre)
33     (remote,) = ctx.cluster.only(client).remotes.iterkeys()
34     proc = remote.run(
35         args=pre,
36         check_status=check_status,
37         stdout=StringIO(),
38         stderr=StringIO(),
39         stdin=stdin,
40         )
41     r = proc.exitstatus
42     out = proc.stdout.getvalue()
43     if not decode:
44         return (r, out)
45     j = None
46     if not r and out != '':
47         try:
48             j = json.loads(out)
49             log.log(log_level, ' json result: %s' % j)
50         except ValueError:
51             j = out
52             log.log(log_level, ' raw result: %s' % j)
53     return (r, j)
54
55 def get_user_summary(out, user):
56     """Extract the summary for a given user"""
57     user_summary = None
58     for summary in out['summary']:
59         if summary.get('user') == user:
60             user_summary = summary
61
62     if not user_summary:
63         raise AssertionError('No summary info found for user: %s' % user)
64
65     return user_summary
66
67 def get_user_successful_ops(out, user):
68     summary = out['summary']
69     if len(summary) == 0:
70         return 0
71     return get_user_summary(out, user)['total']['successful_ops']
72
73 def wait_for_radosgw(url):
74     """ poll the given url until it starts accepting connections
75
76     add_daemon() doesn't wait until radosgw finishes startup, so this is used
77     to avoid racing with later tasks that expect radosgw to be up and listening
78     """
79     # use a connection pool with retry/backoff to poll until it starts listening
80     http = PoolManager(retries=Retry(connect=8, backoff_factor=1))
81     http.request('GET', url)