2
2
from __future__ import print_function
4
4
from argparse import ArgumentParser
5
from contextlib import contextmanager
9
8
from deploy_stack import (
13
16
from utility import (
14
add_basic_testing_arguments,
21
23
log = logging.getLogger("assess_bootstrap")
24
INVALID_URL = 'example.com/invalid'
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())
33
with bs_manager.runtime_context(machines):
35
log.info('Environment successfully bootstrapped.')
38
def prepare_metadata(client, local_dir):
39
"""Fill the given directory with metadata using sync_tools."""
40
client.sync_tools(local_dir)
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:
49
with temp_dir() as md_dir:
50
prepare_metadata(client, md_dir)
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),
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)
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.')
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)
47
log.info('Environment successfully bootstrapped.')
80
50
def parse_args(argv=None):
81
"""Parse all arguments.
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)
98
args = parse_args(argv)
99
configure_logging(args.verbose)
100
assess_bootstrap(args)
64
configure_logging(logging.INFO)
65
assess_bootstrap(**args.__dict__)
104
68
if __name__ == '__main__':