~andrewjbeach/juju-ci-tools/make-local-patcher

« back to all changes in this revision

Viewing changes to assess_bootstrap.py

  • Committer: Martin Packman
  • Date: 2015-12-16 03:59:38 UTC
  • mto: This revision was merged to the branch mainline in revision 1200.
  • Revision ID: martin.packman@canonical.com-20151216035938-881wbry7ezh8he5m
Add missing template test file

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
from __future__ import print_function
3
3
 
4
4
from argparse import ArgumentParser
5
 
from contextlib import contextmanager
6
5
import logging
7
 
import sys
 
6
import os.path
8
7
 
9
8
from deploy_stack import (
10
9
    BootstrapManager,
11
10
    tear_down,
12
11
    )
 
12
from jujupy import (
 
13
    EnvJujuClient,
 
14
    SimpleEnvironment,
 
15
    )
13
16
from utility import (
14
 
    add_basic_testing_arguments,
15
17
    configure_logging,
16
 
    JujuAssertionError,
 
18
    scoped_environ,
17
19
    temp_dir,
18
 
    )
 
20
)
19
21
 
20
22
 
21
23
log = logging.getLogger("assess_bootstrap")
22
24
 
23
25
 
24
 
INVALID_URL = 'example.com/invalid'
25
 
 
26
 
 
27
 
def assess_base_bootstrap(bs_manager):
28
 
    client = bs_manager.client
29
 
    with bs_manager.top_context() as machines:
30
 
        with bs_manager.bootstrap_context(machines):
31
 
            tear_down(client, client.is_jes_enabled())
32
 
            client.bootstrap()
33
 
        with bs_manager.runtime_context(machines):
34
 
            client.get_status(1)
35
 
            log.info('Environment successfully bootstrapped.')
36
 
 
37
 
 
38
 
def prepare_metadata(client, local_dir):
39
 
    """Fill the given directory with metadata using sync_tools."""
40
 
    client.sync_tools(local_dir)
41
 
 
42
 
 
43
 
@contextmanager
44
 
def prepare_temp_metadata(client, source_dir=None):
45
 
    """Fill a temporary directory with metadata using sync_tools."""
46
 
    if source_dir is not None:
47
 
        yield source_dir
48
 
    else:
49
 
        with temp_dir() as md_dir:
50
 
            prepare_metadata(client, md_dir)
51
 
            yield md_dir
52
 
 
53
 
 
54
 
def assess_metadata(bs_manager, local_source):
55
 
    client = bs_manager.client
56
 
    # This disconnects from the metadata source, as INVALID_URL is different.
57
 
    # agent-metadata-url | tools-metadata-url
58
 
    client.env.config['agent-metadata-url'] = INVALID_URL
59
 
    with prepare_temp_metadata(client, local_source) as metadata_dir:
60
 
        log.info('Metadata written to: {}'.format(metadata_dir))
 
26
def assess_bootstrap(juju, env, debug, region, temp_env_name):
 
27
    with scoped_environ():
 
28
        juju_bin = os.path.dirname(os.path.abspath(juju))
 
29
        os.environ['PATH'] = '{}:{}'.format(juju_bin, os.environ['PATH'])
 
30
        client = EnvJujuClient.by_version(SimpleEnvironment.from_config(env),
 
31
                                          juju, debug)
 
32
    jes_enabled = client.is_jes_enabled()
 
33
    if temp_env_name is None:
 
34
        temp_env_name = client.env.environment
 
35
    with temp_dir() as log_dir:
 
36
        bs_manager = BootstrapManager(
 
37
            temp_env_name, client, client, region=region,
 
38
            permanent=jes_enabled, jes_enabled=jes_enabled, log_dir=log_dir,
 
39
            bootstrap_host=None, machines=[], series=None, agent_url=None,
 
40
            agent_stream=None, keep_env=False)
61
41
        with bs_manager.top_context() as machines:
62
42
            with bs_manager.bootstrap_context(machines):
63
 
                tear_down(client, client.is_jes_enabled())
64
 
                client.bootstrap(metadata_source=metadata_dir)
 
43
                tear_down(client, jes_enabled)
 
44
                client.bootstrap()
65
45
            with bs_manager.runtime_context(machines):
66
 
                log.info('Metadata bootstrap successful.')
67
 
                data = client.get_model_config()
68
 
                if INVALID_URL != data['agent-metadata-url']['value']:
69
 
                    raise JujuAssertionError('Error, possible web metadata.')
70
 
 
71
 
 
72
 
def assess_bootstrap(args):
73
 
    bs_manager = BootstrapManager.from_args(args)
74
 
    if 'base' == args.part:
75
 
        assess_base_bootstrap(bs_manager)
76
 
    elif 'metadata' == args.part:
77
 
        assess_metadata(bs_manager, args.local_metadata_source)
 
46
                client.get_status(1)
 
47
                log.info('Environment successfully bootstrapped.')
78
48
 
79
49
 
80
50
def parse_args(argv=None):
81
 
    """Parse all arguments.
82
 
 
83
 
    In addition to the basic testing arguments this script also accepts:
84
 
    part: The first argument, which is the name of test part to run.
85
 
    --local-metadata-source: If given it should be a directory that contains
86
 
    the agent to use in the test. This skips downloading them."""
87
 
    parser = ArgumentParser(description='Test the bootstrap command.')
88
 
    parser.add_argument('part', choices=['base', 'metadata'],
89
 
                        help='Which part of bootstrap to assess')
90
 
    add_basic_testing_arguments(parser)
91
 
    parser.add_argument('--local-metadata-source',
92
 
                        action='store', default=None,
93
 
                        help='Directory with pre-loaded metadata.')
 
51
    parser = ArgumentParser()
 
52
    parser.add_argument('juju', help="The Juju client to use.")
 
53
    parser.add_argument('env', help="The environment to test with.")
 
54
    parser.add_argument('temp_env_name', nargs='?',
 
55
                        help="Temporary environment name to use.")
 
56
    parser.add_argument('--debug', action="store_true", default=False,
 
57
                        help='Use --debug juju logging.')
 
58
    parser.add_argument('--region', help='Override environment region.')
94
59
    return parser.parse_args(argv)
95
60
 
96
61
 
97
 
def main(argv=None):
98
 
    args = parse_args(argv)
99
 
    configure_logging(args.verbose)
100
 
    assess_bootstrap(args)
101
 
    return 0
 
62
def main():
 
63
    args = parse_args()
 
64
    configure_logging(logging.INFO)
 
65
    assess_bootstrap(**args.__dict__)
102
66
 
103
67
 
104
68
if __name__ == '__main__':
105
 
    sys.exit(main())
 
69
    main()