~viswesn/juju-ci-tools/aws_boto3

1793.3.1 by Andrew Beach
Started the work in progress tests for model-defaults.
1
#!/usr/bin/env python
2
"""Assess the model-defaults command."""
3
4
from __future__ import print_function
5
6
import argparse
7
import logging
8
import sys
9
10
from deploy_stack import (
11
    BootstrapManager,
12
    )
13
from utility import (
14
    add_basic_testing_arguments,
15
    configure_logging,
16
    JujuAssertionError,
17
    )
18
19
20
__metaclass__ = type
21
22
23
log = logging.getLogger('assess_model_defaults')
24
25
1793.3.6 by Andrew Beach
Ready for the switch, ModelDefault => dict.
26
def assemble_model_default(model_key, default, controller=None, regions=None):
1793.3.12 by Andrew Beach
Maked fixes, added doc-strings and updated with upstream.
27
    """Create a dict that contrains the formatted model-defaults data."""
1793.3.6 by Andrew Beach
Ready for the switch, ModelDefault => dict.
28
    # Ordering in the regions argument is lost.
29
    defaults = {'default': default}
1793.3.12 by Andrew Beach
Maked fixes, added doc-strings and updated with upstream.
30
    if controller is not None:
1793.3.6 by Andrew Beach
Ready for the switch, ModelDefault => dict.
31
        defaults['controller'] = controller
1793.3.12 by Andrew Beach
Maked fixes, added doc-strings and updated with upstream.
32
    if regions is not None:
1793.3.6 by Andrew Beach
Ready for the switch, ModelDefault => dict.
33
        defaults['regions'] = [
1793.3.8 by Andrew Beach
Fixed the region test, so region is actually being tested.
34
            {'name': region, 'value': region_default}
35
            for (region, region_default) in regions.items()]
1793.3.7 by Andrew Beach
Removed the ModelDefault class.
36
    return {model_key: defaults}
1793.3.6 by Andrew Beach
Ready for the switch, ModelDefault => dict.
37
38
1793.3.4 by Andrew Beach
Renforced the test, shallow copying was causing some false passes.
39
def juju_assert_equal(lhs, rhs, msg):
1793.3.14 by Andrew Beach
EOD: I'm trying to add to the tests so they check that the model-defaults are actually used proberly.
40
    if lhs != rhs:
1793.3.3 by Andrew Beach
Started adding tests, resulted in some changes into the main file.
41
        raise JujuAssertionError(msg, lhs, rhs)
1793.3.1 by Andrew Beach
Started the work in progress tests for model-defaults.
42
43
1793.3.14 by Andrew Beach
EOD: I'm trying to add to the tests so they check that the model-defaults are actually used proberly.
44
def get_new_model_config(client, region=None, model_name=None):
45
    """Create a new model, get it's config and then destroy it.
46
47
    :param client: Client to use create the new model on.
48
    :param region: If given and not None, create the new model in that region
49
        otherwise create it in the client region.
50
    :param model_name: Name of the new model.
51
    """
1793.3.15 by Andrew Beach
Added a test for get_new_model_config.
52
    if model_name is None:
53
        model_name = 'temp-model'
54
    new_env = client.env.clone(model_name)
55
    if region is not None:
56
        new_env.set_region(region)
57
    new_model = client.add_model(new_env)
1793.3.14 by Andrew Beach
EOD: I'm trying to add to the tests so they check that the model-defaults are actually used proberly.
58
    config_data = new_model.get_model_config()
59
    new_model.destroy_model()
60
    return config_data
61
62
1793.3.17 by Andrew Beach
Improved support for the other-regions test.
63
def assess_model_defaults_case(client, model_key, value, expected_default,
1793.3.19 by Andrew Beach
A bit more clean-up.
64
                               cloud=None, region=None):
1793.3.17 by Andrew Beach
Improved support for the other-regions test.
65
    """Check the setting and unsetting of a region field."""
66
    base_line = client.get_model_defaults(model_key)
67
68
    client.set_model_defaults(model_key, value, cloud, region)
69
    juju_assert_equal(expected_default, client.get_model_defaults(model_key),
70
                      'Mismatch after setting model-default.')
1793.3.19 by Andrew Beach
A bit more clean-up.
71
    config = get_new_model_config(client, region)
1793.3.17 by Andrew Beach
Improved support for the other-regions test.
72
    juju_assert_equal(value, config[model_key]['value'],
73
                      'New model did not use the default.')
74
75
    client.unset_model_defaults(model_key, cloud, region)
76
    juju_assert_equal(base_line, client.get_model_defaults(model_key),
77
                      'Mismatch after resetting model-default.')
78
79
1793.3.20 by Andrew Beach
Renamed a function and allowed the region test to be skipped when provider has no regions.
80
def assess_model_defaults_no_region(client, model_key, value):
1793.3.12 by Andrew Beach
Maked fixes, added doc-strings and updated with upstream.
81
    """Check the setting and unsetting of the controller field."""
1793.3.17 by Andrew Beach
Improved support for the other-regions test.
82
    default = client.get_model_defaults(model_key)[model_key]['default']
83
    assess_model_defaults_case(
84
        client, model_key, value,
85
        assemble_model_default(model_key, default, str(value)))
1793.3.1 by Andrew Beach
Started the work in progress tests for model-defaults.
86
87
88
def assess_model_defaults_region(client, model_key, value,
89
                                 cloud=None, region=None):
1793.3.12 by Andrew Beach
Maked fixes, added doc-strings and updated with upstream.
90
    """Check the setting and unsetting of a region field."""
1793.3.17 by Andrew Beach
Improved support for the other-regions test.
91
    default = client.get_model_defaults(model_key)[model_key]['default']
92
    assess_model_defaults_case(
93
        client, model_key, value,
1793.3.12 by Andrew Beach
Maked fixes, added doc-strings and updated with upstream.
94
        assemble_model_default(model_key, default, None, {region: str(value)}),
1793.3.17 by Andrew Beach
Improved support for the other-regions test.
95
        cloud, region)
1793.3.1 by Andrew Beach
Started the work in progress tests for model-defaults.
96
97
1793.3.14 by Andrew Beach
EOD: I'm trying to add to the tests so they check that the model-defaults are actually used proberly.
98
def assess_model_defaults(client, other_region):
1793.3.5 by Andrew Beach
Added the remaining tests. It should work now, but I still have to clean up.
99
    log.info('Checking controller model-defaults.')
1793.3.20 by Andrew Beach
Renamed a function and allowed the region test to be skipped when provider has no regions.
100
    assess_model_defaults_no_region(
1793.3.5 by Andrew Beach
Added the remaining tests. It should work now, but I still have to clean up.
101
        client, 'automatically-retry-hooks', False)
1793.3.20 by Andrew Beach
Renamed a function and allowed the region test to be skipped when provider has no regions.
102
    region = client.env.get_region()
103
    if region is not None:
104
        log.info('Checking region model-defaults.')
105
        assess_model_defaults_region(
106
            client, 'default-series', 'trusty', region=region)
1793.3.14 by Andrew Beach
EOD: I'm trying to add to the tests so they check that the model-defaults are actually used proberly.
107
    if other_region is not None:
1793.3.15 by Andrew Beach
Added a test for get_new_model_config.
108
        log.info('Checking other region model-defaults.')
109
        assess_model_defaults_region(
110
            client, 'default-series', 'trusty', region=other_region)
1793.3.1 by Andrew Beach
Started the work in progress tests for model-defaults.
111
112
113
def parse_args(argv):
114
    """Parse all arguments."""
115
    parser = argparse.ArgumentParser(
116
        description='Assess the model-defaults command.')
117
    add_basic_testing_arguments(parser)
1793.3.14 by Andrew Beach
EOD: I'm trying to add to the tests so they check that the model-defaults are actually used proberly.
118
    parser.add_argument('--other-region',
119
                        help='Set the model default for a different region.')
1793.3.1 by Andrew Beach
Started the work in progress tests for model-defaults.
120
    return parser.parse_args(argv)
121
122
123
def main(argv=None):
124
    args = parse_args(argv)
125
    configure_logging(args.verbose)
126
    bs_manager = BootstrapManager.from_args(args)
1793.3.18 by Andrew Beach
A bit of clean-up.
127
    if (args.other_region is not None and
128
            args.other_region == bs_manager.client.env.get_region()):
1793.3.20 by Andrew Beach
Renamed a function and allowed the region test to be skipped when provider has no regions.
129
        raise ValueError('Other region is a repeat of region.')
1793.3.1 by Andrew Beach
Started the work in progress tests for model-defaults.
130
    with bs_manager.booted_context(args.upload_tools):
1793.3.14 by Andrew Beach
EOD: I'm trying to add to the tests so they check that the model-defaults are actually used proberly.
131
        assess_model_defaults(bs_manager.client, args.other_region)
1793.3.1 by Andrew Beach
Started the work in progress tests for model-defaults.
132
    return 0
133
134
135
if __name__ == '__main__':
136
    sys.exit(main())