~veebers/juju-ci-tools/migration-check-all-model-names

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python
from argparse import ArgumentParser
from textwrap import dedent
import yaml

from deploy_stack import (
    BootstrapManager,
    )
from fakejuju import (
    FakeBackend,
    FakeControllerState,
    )
from jujuconfig import get_juju_home
from jujupy import (
    EnvJujuClient,
    get_client_class,
    WaitMachineNotPresent,
    )
from utility import (
    add_basic_testing_arguments,
    configure_logging,
    )


def client_from_args(args):
    """Return client from args, as generated by parse_args.

    If the path given is FAKE, fake_juju_client() is used.  Otherwise, the
    client is determined based on the path and version.
    """
    if args.juju_bin == 'FAKE':
        client_class = EnvJujuClient
        controller_state = FakeControllerState()
        version = '2.0.0'
        backend = FakeBackend(controller_state, full_path=args.juju_bin,
                              version=version)
    else:
        version = EnvJujuClient.get_version(args.juju_bin)
        client_class = get_client_class(version)
        backend = None
    juju_home = get_juju_home()
    with open(args.clouds_file) as f:
        clouds = yaml.safe_load(f)
    if args.config is None:
        config = {}
    else:
        with open(args.config) as f:
            config = yaml.safe_load(f)
    juju_data = client_class.config_class.from_cloud_region(
        args.cloud, args.region, config, clouds, juju_home)
    return client_class(juju_data, version, args.juju_bin, debug=args.debug,
                        soft_deadline=args.deadline, _backend=backend)


def assess_cloud_combined(bs_manager):
    """Assess several operations on a cloud.

    This tests bootstrap, deploy, remove-unit and destroy-controller.
    """
    client = bs_manager.client
    with bs_manager.booted_context(upload_tools=False):
        old_status = client.get_status()
        client.juju('deploy', ('ubuntu'))
        new_status = client.wait_for_started()
        new_machines = [k for k, v in new_status.iter_new_machines(old_status)]
        client.juju('remove-unit', 'ubuntu/0')
        new_status = client.wait_for([WaitMachineNotPresent(n)
                                      for n in new_machines])


def assess_cloud_provisioning(bs_manager):
    """Assess provisioning operations on a cloud.

    This was created for testing Azure streams.  It tests bootstrap,
    add-machine, remove-machine and destroy-controller.  It does not test
    charms.

    It tests "trusty" and "win2012r2" as representative series on Azure.  It
    does not test CentOS, because that is known-broken at the moment (bug
    #1571982).  It tests "trusty" rather than "xenial" because "xenial" will
    be used for bootstrap by default.
    """
    client = bs_manager.client
    with bs_manager.booted_context(upload_tools=False):
        old_status = client.get_status()
        client.juju('add-machine', ('--series', 'win2012r2'))
        client.juju('add-machine', ('--series', 'trusty'))
        new_status = client.wait_for_started()
        new_machines = [k for k, v in new_status.iter_new_machines(old_status)]
        for machine in new_machines:
            client.juju('remove-machine', (machine,))
        new_status = client.wait_for([WaitMachineNotPresent(n)
                                      for n in new_machines], timeout=600)


def assess_cloud_kill_controller(bs_manager):
    client = bs_manager.client
    with bs_manager.booted_context(upload_tools=False):
        controller_client = client.get_controller_client()
        controller_client.juju('run', (
            '--machine', '0', 'sudo service jujud-machine-0 stop'),
            check=False)
        bs_manager.has_controller = False


def parse_args(args):
    parser = ArgumentParser(description=dedent("""\
        Test a specified cloud.

        This tests basic provider operations and charm store support.

        The cloud.yaml file must be provided, followed by the name of the
        cloud to test.
        """))
    subparsers = parser.add_subparsers(dest='test')
    for test in ['combined', 'kill-controller', 'provisioning']:
        subparser = subparsers.add_parser(test)
        subparser.add_argument('clouds_file',
                               help='A clouds.yaml file to use for testing.')
        subparser.add_argument('cloud', help='Specific cloud to test.')
        add_basic_testing_arguments(subparser, env=False)
        subparser.add_argument('--config')
    return parser.parse_args(args)


def main():
    args = parse_args(None)
    configure_logging(args.verbose)
    client = client_from_args(args)
    bs_manager = BootstrapManager.from_client(args, client)
    if args.test == 'combined':
        assess_cloud_combined(bs_manager)
    if args.test == 'provisioning':
        assess_cloud_provisioning(bs_manager)
    else:
        assess_cloud_kill_controller(bs_manager)


if __name__ == '__main__':
    main()