summaryrefslogtreecommitdiff
path: root/tests/cloud_tests/__main__.py
blob: ef7d1878deef48f454c1a2ba54ae2b6f5e095e2e (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
# This file is part of cloud-init. See LICENSE file for license information.

import argparse
import logging
import shutil
import sys
import tempfile

from tests.cloud_tests import (args, collect, manage, verify)
from tests.cloud_tests import LOG


def configure_log(args):
    """
    configure logging
    """
    level = logging.INFO
    if args.verbose:
        level = logging.DEBUG
    elif args.quiet:
        level = logging.WARN
    LOG.setLevel(level)


def run(args):
    """
    run full test suite
    """
    failed = 0
    args.data_dir = tempfile.mkdtemp(prefix='cloud_test_data_')
    LOG.debug('using tmpdir %s', args.data_dir)
    try:
        failed += collect.collect(args)
        failed += verify.verify(args)
    except Exception:
        failed += 1
        raise
    finally:
        # TODO: make this configurable via environ or cmdline
        if failed:
            LOG.warn('some tests failed, leaving data in %s', args.data_dir)
        else:
            shutil.rmtree(args.data_dir)
    return failed


def main():
    """
    entry point for cloud test suite
    """
    # configure parser
    parser = argparse.ArgumentParser(prog='cloud_tests')
    subparsers = parser.add_subparsers(dest="subcmd")
    subparsers.required = True

    def add_subparser(name, description, arg_sets):
        """
        add arguments to subparser
        """
        subparser = subparsers.add_parser(name, help=description)
        for (_args, _kwargs) in (a for arg_set in arg_sets for a in arg_set):
            subparser.add_argument(*_args, **_kwargs)

    # configure subparsers
    for (name, (description, arg_sets)) in args.SUBCMDS.items():
        add_subparser(name, description,
                      [args.ARG_SETS[arg_set] for arg_set in arg_sets])

    # parse arguments
    parsed = parser.parse_args()

    # process arguments
    configure_log(parsed)
    (_, arg_sets) = args.SUBCMDS[parsed.subcmd]
    for normalizer in [args.NORMALIZERS[arg_set] for arg_set in arg_sets]:
        parsed = normalizer(parsed)
        if not parsed:
            return -1

    # run handler
    LOG.debug('running with args: %s\n', parsed)
    return {
        'collect': collect.collect,
        'create': manage.create,
        'run': run,
        'verify': verify.verify,
    }[parsed.subcmd](parsed)


if __name__ == "__main__":
    sys.exit(main())

# vi: ts=4 expandtab