summaryrefslogtreecommitdiffstats
path: root/qa/tasks/ragweed.py
blob: e2b33527a0b738d61f2b2056337e531e9b81e595 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
"""
Run a set of s3 tests on rgw.
"""
from io import BytesIO
from configobj import ConfigObj
import base64
import contextlib
import logging
import os
import random
import string

from teuthology import misc as teuthology
from teuthology import contextutil
from teuthology.config import config as teuth_config
from teuthology.orchestra import run

log = logging.getLogger(__name__)


def get_ragweed_branches(config, client_conf):
    """
    figure out the ragweed branch according to the per-client settings

    use force-branch is specified, and fall back to the ones deduced using ceph
    branch under testing
    """
    force_branch = client_conf.get('force-branch', None)
    if force_branch:
        return [force_branch]
    else:
        S3_BRANCHES = ['master', 'nautilus', 'mimic',
                       'luminous', 'kraken', 'jewel']
        ceph_branch = config.get('branch')
        suite_branch = config.get('suite_branch', ceph_branch)
        if suite_branch in S3_BRANCHES:
            branch = client_conf.get('branch', 'ceph-' + suite_branch)
        else:
            branch = client_conf.get('branch', suite_branch)
        default_branch = client_conf.get('default-branch', None)
        if default_branch:
            return [branch, default_branch]
        else:
            return [branch]

def get_ragweed_dir(testdir, client):
    return '{}/ragweed.{}'.format(testdir, client)

@contextlib.contextmanager
def download(ctx, config):
    """
    Download the s3 tests from the git builder.
    Remove downloaded s3 file upon exit.

    The context passed in should be identical to the context
    passed in to the main task.
    """
    assert isinstance(config, dict)
    log.info('Downloading ragweed...')
    testdir = teuthology.get_testdir(ctx)
    for (client, cconf) in config.items():
        ragweed_dir = get_ragweed_dir(testdir, client)
        ragweed_repo = ctx.config.get('ragweed_repo',
                                      teuth_config.ceph_git_base_url + 'ragweed.git')
        for branch in get_ragweed_branches(ctx.config, cconf):
            log.info("Using branch '%s' for ragweed", branch)
            try:
                ctx.cluster.only(client).sh(
                    script=f'git clone -b {branch} {ragweed_repo} {ragweed_dir}')
                break
            except Exception as e:
                exc = e
        else:
            raise exc

        sha1 = cconf.get('sha1')
        if sha1 is not None:
            ctx.cluster.only(client).run(
                args=[
                    'cd', ragweed_dir,
                    run.Raw('&&'),
                    'git', 'reset', '--hard', sha1,
                    ],
                )
    try:
        yield
    finally:
        log.info('Removing ragweed...')
        for client in config:
            ragweed_dir = get_ragweed_dir(testdir, client)
            ctx.cluster.only(client).run(
                args=['rm', '-rf', ragweed_dir]
                )


def _config_user(ragweed_conf, section, user):
    """
    Configure users for this section by stashing away keys, ids, and
    email addresses.
    """
    ragweed_conf[section].setdefault('user_id', user)
    ragweed_conf[section].setdefault('email', '{user}+test@test.test'.format(user=user))
    ragweed_conf[section].setdefault('display_name', 'Mr. {user}'.format(user=user))
    ragweed_conf[section].setdefault('access_key', ''.join(random.choice(string.ascii_uppercase) for i in range(20)))
    ragweed_conf[section].setdefault('secret_key', base64.b64encode(os.urandom(40)).decode('ascii'))


@contextlib.contextmanager
def create_users(ctx, config, run_stages):
    """
    Create a main and an alternate s3 user.
    """
    assert isinstance(config, dict)

    for client, properties in config['config'].items():
        run_stages[client] = properties.get('stages', 'prepare,check').split(',')

    log.info('Creating rgw users...')
    testdir = teuthology.get_testdir(ctx)
    users = {'user regular': 'ragweed', 'user system': 'sysuser'}
    for client in config['clients']:
        if not 'prepare' in run_stages[client]:
            # should have been prepared in a previous run
            continue

        ragweed_conf = config['ragweed_conf'][client]
        ragweed_conf.setdefault('fixtures', {})
        ragweed_conf['rgw'].setdefault('bucket_prefix', 'test-' + client)
        for section, user in users.items():
            _config_user(ragweed_conf, section, '{user}.{client}'.format(user=user, client=client))
            log.debug('Creating user {user} on {host}'.format(user=ragweed_conf[section]['user_id'], host=client))
            if user == 'sysuser':
                sys_str = 'true'
            else:
                sys_str = 'false'
            ctx.cluster.only(client).run(
                args=[
                    'adjust-ulimits',
                    'ceph-coverage',
                    '{tdir}/archive/coverage'.format(tdir=testdir),
                    'radosgw-admin',
                    '-n', client,
                    'user', 'create',
                    '--uid', ragweed_conf[section]['user_id'],
                    '--display-name', ragweed_conf[section]['display_name'],
                    '--access-key', ragweed_conf[section]['access_key'],
                    '--secret', ragweed_conf[section]['secret_key'],
                    '--email', ragweed_conf[section]['email'],
                    '--system', sys_str,
                ],
            )
    try:
        yield
    finally:
        for client in config['clients']:
            if not 'check' in run_stages[client]:
                # only remove user if went through the check stage
                continue
            for user in users.values():
                uid = '{user}.{client}'.format(user=user, client=client)
                ctx.cluster.only(client).run(
                    args=[
                        'adjust-ulimits',
                        'ceph-coverage',
                        '{tdir}/archive/coverage'.format(tdir=testdir),
                        'radosgw-admin',
                        '-n', client,
                        'user', 'rm',
                        '--uid', uid,
                        '--purge-data',
                        ],
                    )


@contextlib.contextmanager
def configure(ctx, config, run_stages):
    """
    Configure the local config files.
    """
    assert isinstance(config, dict)
    log.info('Configuring ragweed...')
    testdir = teuthology.get_testdir(ctx)
    for client, properties in config['clients'].items():
        (remote,) = ctx.cluster.only(client).remotes.keys()
        preparing = 'prepare' in run_stages[client]
        if not preparing:
            # should have been prepared in a previous run
            continue

        ragweed_conf = config['ragweed_conf'][client]
        if properties is not None and 'slow_backend' in properties:
            ragweed_conf['fixtures']['slow backend'] = properties['slow_backend']

        conf_fp = BytesIO()
        ragweed_conf.write(conf_fp)
        remote.write_file(
            path='{tdir}/archive/ragweed.{client}.conf'.format(tdir=testdir, client=client),
            data=conf_fp.getvalue(),
            )

    log.info('Configuring boto...')
    boto_src = os.path.join(os.path.dirname(__file__), 'boto.cfg.template')
    for client, properties in config['clients'].items():
        with open(boto_src, 'r') as f:
            (remote,) = ctx.cluster.only(client).remotes.keys()
            conf = f.read().format(
                idle_timeout=config.get('idle_timeout', 30)
                )
            remote.write_file('{tdir}/boto.cfg'.format(tdir=testdir), conf)

    try:
        yield

    finally:
        log.info('Cleaning up boto...')
        for client, properties in config['clients'].items():
            (remote,) = ctx.cluster.only(client).remotes.keys()
            remote.run(
                args=[
                    'rm', '-f',
                    '{tdir}/boto.cfg'.format(tdir=testdir),
                    ],
                )

def get_toxvenv_dir(ctx):
    return ctx.tox.venv_path

def toxvenv_sh(ctx, remote, args, **kwargs):
    activate = get_toxvenv_dir(ctx) + '/bin/activate'
    return remote.sh(['source', activate, run.Raw('&&')] + args, **kwargs)

@contextlib.contextmanager
def run_tests(ctx, config, run_stages):
    """
    Run the ragweed after everything is set up.

    :param ctx: Context passed to task
    :param config: specific configuration information
    """
    assert isinstance(config, dict)
    testdir = teuthology.get_testdir(ctx)
    attrs = ["not fails_on_rgw"]
    for client, client_config in config.items():
        ragweed_dir = get_ragweed_dir(testdir, client)
        stages = ','.join(run_stages[client])
        args = [
            'cd', ragweed_dir, run.Raw('&&'),
            'RAGWEED_CONF={tdir}/archive/ragweed.{client}.conf'.format(tdir=testdir, client=client),
            'RAGWEED_STAGES={stages}'.format(stages=stages),
            'BOTO_CONFIG={tdir}/boto.cfg'.format(tdir=testdir),
            'tox',
            '--sitepackages',
            '--',
            '-v',
            '-m', ' and '.join(attrs),
            ]
        if client_config is not None and 'extra_args' in client_config:
            args.extend(client_config['extra_args'])

        (remote,) = ctx.cluster.only(client).remotes.keys()
        toxvenv_sh(ctx, remote, args, label="ragweed tests against rgw")
    yield

@contextlib.contextmanager
def task(ctx, config):
    """
    Run the ragweed suite against rgw.

    To run all tests on all clients::

        tasks:
        - ceph:
        - rgw:
        - ragweed:

    To restrict testing to particular clients::

        tasks:
        - ceph:
        - rgw: [client.0]
        - ragweed: [client.0]

    To run against a server on client.1 and increase the boto timeout to 10m::

        tasks:
        - ceph:
        - rgw: [client.1]
        - ragweed:
            client.0:
              rgw_server: client.1
              idle_timeout: 600
              stages: prepare,check

    To pass extra arguments to nose (e.g. to run a certain test)::

        tasks:
        - ceph:
        - rgw: [client.0]
        - ragweed:
            client.0:
              extra_args: ['test_s3:test_object_acl_grand_public_read']
            client.1:
              extra_args: ['--exclude', 'test_100_continue']
    """
    assert hasattr(ctx, 'rgw'), 'ragweed must run after the rgw task'
    assert hasattr(ctx, 'tox'), 'ragweed must run after the tox task'
    assert config is None or isinstance(config, list) \
        or isinstance(config, dict), \
        "task ragweed only supports a list or dictionary for configuration"
    all_clients = ['client.{id}'.format(id=id_)
                   for id_ in teuthology.all_roles_of_type(ctx.cluster, 'client')]
    if config is None:
        config = all_clients
    if isinstance(config, list):
        config = dict.fromkeys(config)
    clients = config.keys()

    overrides = ctx.config.get('overrides', {})
    # merge each client section, not the top level.
    for client in config.keys():
        if not config[client]:
            config[client] = {}
        teuthology.deep_merge(config[client], overrides.get('ragweed', {}))

    log.debug('ragweed config is %s', config)

    ragweed_conf = {}
    for client in clients:
        # use rgw_server endpoint if given, or default to same client
        target = config[client].get('rgw_server', client)

        endpoint = ctx.rgw.role_endpoints.get(target)
        assert endpoint, 'ragweed: no rgw endpoint for {}'.format(target)

        ragweed_conf[client] = ConfigObj(
            indent_type='',
            infile={
                'rgw':
                    {
                    'host'      : endpoint.dns_name,
                    'port'      : endpoint.port,
                    'is_secure' : endpoint.cert is not None,
                    },
                'fixtures' : {},
                'user system'  : {},
                'user regular'   : {},
                'rados':
                    {
                    'ceph_conf'  : '/etc/ceph/ceph.conf',
                    },
                }
            )

    run_stages = {}

    with contextutil.nested(
        lambda: download(ctx=ctx, config=config),
        lambda: create_users(ctx=ctx, config=dict(
                clients=clients,
                ragweed_conf=ragweed_conf,
                config=config,
                ),
                run_stages=run_stages),
        lambda: configure(ctx=ctx, config=dict(
                clients=config,
                ragweed_conf=ragweed_conf,
                ),
                run_stages=run_stages),
        lambda: run_tests(ctx=ctx, config=config, run_stages=run_stages),
        ):
        pass
    yield