Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / qa / tasks / autotest.py
1 """ 
2 Run an autotest test on the ceph cluster.
3 """
4 import json
5 import logging
6 import os
7
8 from teuthology import misc as teuthology
9 from teuthology.parallel import parallel
10 from teuthology.orchestra import run
11
12 log = logging.getLogger(__name__)
13
14 def task(ctx, config):
15     """
16     Run an autotest test on the ceph cluster.
17
18     Only autotest client tests are supported.
19
20     The config is a mapping from role name to list of tests to run on
21     that client.
22
23     For example::
24
25         tasks:
26         - ceph:
27         - ceph-fuse: [client.0, client.1]
28         - autotest:
29             client.0: [dbench]
30             client.1: [bonnie]
31
32     You can also specify a list of tests to run on all clients::
33
34         tasks:
35         - ceph:
36         - ceph-fuse:
37         - autotest:
38             all: [dbench]
39     """
40     assert isinstance(config, dict)
41     config = teuthology.replace_all_with_clients(ctx.cluster, config)
42     log.info('Setting up autotest...')
43     testdir = teuthology.get_testdir(ctx)
44     with parallel() as p:
45         for role in config.iterkeys():
46             (remote,) = ctx.cluster.only(role).remotes.keys()
47             p.spawn(_download, testdir, remote)
48
49     log.info('Making a separate scratch dir for every client...')
50     for role in config.iterkeys():
51         assert isinstance(role, basestring)
52         PREFIX = 'client.'
53         assert role.startswith(PREFIX)
54         id_ = role[len(PREFIX):]
55         (remote,) = ctx.cluster.only(role).remotes.iterkeys()
56         mnt = os.path.join(testdir, 'mnt.{id}'.format(id=id_))
57         scratch = os.path.join(mnt, 'client.{id}'.format(id=id_))
58         remote.run(
59             args=[
60                 'sudo',
61                 'install',
62                 '-d',
63                 '-m', '0755',
64                 '--owner={user}'.format(user='ubuntu'), #TODO
65                 '--',
66                 scratch,
67                 ],
68             )
69
70     with parallel() as p:
71         for role, tests in config.iteritems():
72             (remote,) = ctx.cluster.only(role).remotes.keys()
73             p.spawn(_run_tests, testdir, remote, role, tests)
74
75 def _download(testdir, remote):
76     """
77     Download.  Does not explicitly support muliple tasks in a single run.
78     """
79     remote.run(
80         args=[
81             # explicitly does not support multiple autotest tasks
82             # in a single run; the result archival would conflict
83             'mkdir', '{tdir}/archive/autotest'.format(tdir=testdir),
84             run.Raw('&&'),
85             'mkdir', '{tdir}/autotest'.format(tdir=testdir),
86             run.Raw('&&'),
87             'wget',
88             '-nv',
89             '--no-check-certificate',
90             'https://github.com/ceph/autotest/tarball/ceph',
91             '-O-',
92             run.Raw('|'),
93             'tar',
94             '-C', '{tdir}/autotest'.format(tdir=testdir),
95             '-x',
96             '-z',
97             '-f-',
98             '--strip-components=1',
99             ],
100         )
101
102 def _run_tests(testdir, remote, role, tests):
103     """
104     Spawned to run test on remote site
105     """
106     assert isinstance(role, basestring)
107     PREFIX = 'client.'
108     assert role.startswith(PREFIX)
109     id_ = role[len(PREFIX):]
110     mnt = os.path.join(testdir, 'mnt.{id}'.format(id=id_))
111     scratch = os.path.join(mnt, 'client.{id}'.format(id=id_))
112
113     assert isinstance(tests, list)
114     for idx, testname in enumerate(tests):
115         log.info('Running autotest client test #%d: %s...', idx, testname)
116
117         tag = 'client.{id}.num{idx}.{testname}'.format(
118             idx=idx,
119             testname=testname,
120             id=id_,
121             )
122         control = '{tdir}/control.{tag}'.format(tdir=testdir, tag=tag)
123         teuthology.write_file(
124             remote=remote,
125             path=control,
126             data='import json; data=json.loads({data!r}); job.run_test(**data)'.format(
127                 data=json.dumps(dict(
128                         url=testname,
129                         dir=scratch,
130                         # TODO perhaps tag
131                         # results will be in {testdir}/autotest/client/results/dbench
132                         # or {testdir}/autotest/client/results/dbench.{tag}
133                         )),
134                 ),
135             )
136         remote.run(
137             args=[
138                 '{tdir}/autotest/client/bin/autotest'.format(tdir=testdir),
139                 '--verbose',
140                 '--harness=simple',
141                 '--tag={tag}'.format(tag=tag),
142                 control,
143                 run.Raw('3>&1'),
144                 ],
145             )
146
147         remote.run(
148             args=[
149                 'rm', '-rf', '--', control,
150                 ],
151             )
152
153         remote.run(
154             args=[
155                 'mv',
156                 '--',
157                 '{tdir}/autotest/client/results/{tag}'.format(tdir=testdir, tag=tag),
158                 '{tdir}/archive/autotest/{tag}'.format(tdir=testdir, tag=tag),
159                 ],
160             )
161
162     remote.run(
163         args=[
164             'rm', '-rf', '--', '{tdir}/autotest'.format(tdir=testdir),
165             ],
166         )