github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/acceptancetests/assess_multimodel.py (about) 1 #!/usr/bin/env python 2 """Assess multimodel support.""" 3 4 from argparse import ArgumentParser 5 from contextlib import contextmanager 6 import logging 7 import sys 8 import os 9 10 from deploy_stack import ( 11 boot_context, 12 check_token, 13 configure_logging, 14 deploy_dummy_stack, 15 dump_env_logs, 16 get_random_string, 17 safe_print_status, 18 ) 19 from jujupy import ( 20 client_from_config, 21 ) 22 from utility import ( 23 add_basic_testing_arguments, 24 ensure_dir, 25 print_now, 26 ) 27 28 29 def assess_multimodel_deploy(client, charm_series, log_dir, base_env): 30 """Deploy the dummy stack in two hosted environments.""" 31 # deploy into system env 32 deploy_dummy_stack(client, charm_series) 33 34 # deploy into hosted envs 35 with hosted_environment(client, log_dir, 'env1') as env1_client: 36 deploy_dummy_stack(env1_client, charm_series) 37 with hosted_environment(client, log_dir, 'env2') as env2_client: 38 deploy_dummy_stack(env2_client, charm_series) 39 # check all the services can talk 40 check_services(client) 41 check_services(env1_client) 42 check_services(env2_client) 43 44 45 def assess_destroy_current(client): 46 model_name = client.model_name 47 new_model = client.add_model('delete-me') 48 new_model.switch('delete-me') 49 new_model.destroy_model() 50 new_model.show_controller() 51 client.switch(model_name) 52 53 54 @contextmanager 55 def multimodel_setup(args): 56 """ 57 Sets up the juju client and its environment. 58 59 Returns return client, charm_prefix and base_env. 60 """ 61 base_env = args.env 62 configure_logging(args.verbose) 63 # TODO(gz): Logic from deploy_stack, and precise is a bad default series? 64 series = args.series 65 if series is None: 66 series = 'precise' 67 charm_series = series 68 client = client_from_config(base_env, args.juju_bin, args.debug, 69 soft_deadline=args.deadline) 70 with boot_context( 71 args.temp_env_name, 72 client, 73 args.bootstrap_host, 74 args.machine, 75 args.series, 76 args.agent_url, 77 args.agent_stream, 78 args.logs, args.keep_env, 79 upload_tools=False, 80 region=args.region, 81 ): 82 yield client, charm_series, base_env 83 84 85 def env_token(env_name): 86 return env_name + get_random_string() 87 88 89 @contextmanager 90 def hosted_environment(system_client, log_dir, suffix): 91 env_name = '{}-{}'.format(system_client.env.environment, suffix) 92 client = system_client.add_model(env_name) 93 try: 94 yield client 95 except: 96 logging.exception( 97 'Exception while environment "{}" active'.format( 98 client.env.environment)) 99 sys.exit(1) 100 finally: 101 safe_print_status(client) 102 hosted_log_dir = os.path.join(log_dir, suffix) 103 ensure_dir(hosted_log_dir) 104 dump_env_logs(client, None, hosted_log_dir) 105 client.destroy_model() 106 107 108 def check_services(client): 109 token = env_token(client.env.environment) 110 client.set_config('dummy-source', {'token': token}) 111 print_now("checking services in " + client.env.environment) 112 check_token(client, token) 113 114 115 def parse_args(argv=None): 116 """Parse all arguments.""" 117 parser = ArgumentParser() 118 add_basic_testing_arguments( 119 parser, using_jes=True, deadline=True, existing=False) 120 return parser.parse_args(argv) 121 122 123 def main(): 124 args = parse_args() 125 with multimodel_setup(args) as (client, charm_series, base_env): 126 assess_multimodel_deploy(client, charm_series, args.logs, base_env) 127 assess_destroy_current(client) 128 129 130 if __name__ == '__main__': 131 main()