github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/acceptancetests/assess_bootstrap.py (about) 1 #!/usr/bin/env python 2 from __future__ import print_function 3 4 from argparse import ArgumentParser 5 from contextlib import contextmanager 6 import logging 7 import sys 8 9 from deploy_stack import ( 10 BootstrapManager, 11 ) 12 from utility import ( 13 add_basic_testing_arguments, 14 configure_logging, 15 JujuAssertionError, 16 temp_dir, 17 ) 18 19 20 log = logging.getLogger("assess_bootstrap") 21 22 23 INVALID_URL = 'example.com/invalid' 24 25 26 @contextmanager 27 def thin_booted_context(bs_manager, **kwargs): 28 client = bs_manager.client 29 with bs_manager.top_context() as machines: 30 with bs_manager.bootstrap_context(machines): 31 client.kill_controller() 32 client.bootstrap(**kwargs) 33 with bs_manager.runtime_context(machines): 34 yield client 35 36 37 def assess_base_bootstrap(bs_manager): 38 with thin_booted_context(bs_manager) as client: 39 client.get_status(1) 40 log.info('Environment successfully bootstrapped.') 41 42 43 def prepare_metadata(client, local_dir, agent_stream=None, source=None): 44 """Fill the given directory with metadata using sync_tools.""" 45 client.sync_tools(local_dir, agent_stream, source) 46 47 48 @contextmanager 49 def prepare_temp_metadata(client, source_dir=None, agent_stream=None, 50 source=None): 51 """Fill a temporary directory with metadata using sync_tools.""" 52 if source_dir is not None: 53 yield source_dir 54 else: 55 with temp_dir() as md_dir: 56 prepare_metadata(client, md_dir, agent_stream, source) 57 yield md_dir 58 59 60 def assess_metadata(bs_manager, local_source): 61 client = bs_manager.client 62 # This disconnects from the metadata source, as INVALID_URL is different. 63 # agent-metadata-url | tools-metadata-url 64 client.env.update_config({'agent-metadata-url': INVALID_URL}) 65 with prepare_temp_metadata(client, local_source) as metadata_dir: 66 log.info('Metadata written to: {}'.format(metadata_dir)) 67 with thin_booted_context(bs_manager, 68 metadata_source=metadata_dir): 69 log.info('Metadata bootstrap successful.') 70 data = client.get_model_config() 71 if INVALID_URL != data['agent-metadata-url']['value']: 72 raise JujuAssertionError('Error, possible web metadata.') 73 74 75 def get_controller_hostname(client): 76 """Get the hostname of the controller for this model.""" 77 controller_client = client.get_controller_client() 78 name = controller_client.run(['hostname'], machines=['0'], use_json=False) 79 return name.strip() 80 81 82 def assess_to(bs_manager, to): 83 """Assess bootstraping with the --to option.""" 84 if to is None: 85 raise ValueError('--to not given when testing to') 86 with thin_booted_context(bs_manager) as client: 87 log.info('To {} bootstrap successful.'.format(to)) 88 addr = get_controller_hostname(client) 89 if addr != to: 90 raise JujuAssertionError('Not bootstrapped to the correct address.') 91 92 93 def assess_bootstrap(args): 94 bs_manager = BootstrapManager.from_args(args) 95 if 'base' == args.part: 96 assess_base_bootstrap(bs_manager) 97 elif 'metadata' == args.part: 98 assess_metadata(bs_manager, args.local_metadata_source) 99 elif 'to' == args.part: 100 assess_to(bs_manager, args.to) 101 102 103 def parse_args(argv=None): 104 """Parse all arguments. 105 106 In addition to the basic testing arguments this script also accepts: 107 part: The first argument, which is the name of test part to run. 108 --local-metadata-source: If given it should be a directory that contains 109 the agent to use in the test. This skips downloading them.""" 110 parser = ArgumentParser(description='Test the bootstrap command.') 111 parser.add_argument('part', choices=['base', 'metadata', 'to'], 112 help='Which part of bootstrap to assess') 113 add_basic_testing_arguments(parser) 114 parser.add_argument('--local-metadata-source', 115 action='store', default=None, 116 help='Directory with pre-loaded metadata.') 117 return parser.parse_args(argv) 118 119 120 def main(argv=None): 121 args = parse_args(argv) 122 configure_logging(args.verbose) 123 assess_bootstrap(args) 124 return 0 125 126 127 if __name__ == '__main__': 128 sys.exit(main())