~andrewjbeach/juju-ci-tools/make-local-patcher

« back to all changes in this revision

Viewing changes to assess_model_config_tree.py

  • Committer: Aaron Bentley
  • Date: 2015-01-19 15:32:33 UTC
  • mto: This revision was merged to the branch mainline in revision 804.
  • Revision ID: aaron.bentley@canonical.com-20150119153233-jjcvikwiw1dx2lak
Print error on missing environment.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
"""Test Model Tree Config functionality.
3
 
 
4
 
Tests that default values can be overwritten and then unset.
5
 
 
6
 
Ensures that a model config overwrites a controller config and a controller
7
 
config overwrites a default.
8
 
 
9
 
Also ensures that when reverting/unsetting the controller config it reverts to
10
 
the most up to date model config setting.
11
 
 
12
 
"""
13
 
 
14
 
from __future__ import print_function
15
 
 
16
 
import argparse
17
 
import logging
18
 
import sys
19
 
 
20
 
from deploy_stack import (
21
 
    BootstrapManager,
22
 
)
23
 
from utility import (
24
 
    JujuAssertionError,
25
 
    add_basic_testing_arguments,
26
 
    configure_logging,
27
 
)
28
 
 
29
 
 
30
 
__metaclass__ = type
31
 
 
32
 
 
33
 
log = logging.getLogger("assess_model_config_tree")
34
 
 
35
 
 
36
 
def assess_model_config_tree(bs_manager, upload_tools):
37
 
    """Assess model config tree functionality."""
38
 
    # Need to update the cloud config details here so we can set something that
39
 
    # we can use after boostrap.
40
 
    config = {'ftp-proxy': 'abc.com'}
41
 
    set_clouds_yaml_config(bs_manager.client, config)
42
 
 
43
 
    with bs_manager.booted_context(upload_tools):
44
 
        client = bs_manager.client
45
 
        assert_config_value(client, 'ftp-proxy', 'controller', 'abc.com')
46
 
        client.set_env_option('ftp-proxy', 'abc-model.com')
47
 
        assert_config_value(client, 'ftp-proxy', 'model', 'abc-model.com')
48
 
        client.unset_env_option('ftp-proxy')
49
 
        assert_config_value(client, 'ftp-proxy', 'controller', 'abc.com')
50
 
 
51
 
        assert_config_value(client, 'development', 'default', False)
52
 
        client.set_env_option('development', True)
53
 
        assert_config_value(client, 'development', 'model', True)
54
 
        client.unset_env_option('development')
55
 
        assert_config_value(client, 'development', 'default', False)
56
 
 
57
 
 
58
 
def assert_config_value(client, attribute, source, value):
59
 
    config_values = client.get_model_config()
60
 
    try:
61
 
        source_value = config_values[attribute]['source']
62
 
        attr_value = config_values[attribute]['value']
63
 
        if attr_value != value:
64
 
            raise JujuAssertionError(
65
 
                'Unexpected value for {}.\nGot {} instead of {}'.format(
66
 
                    attribute, attr_value, value))
67
 
 
68
 
        if source_value != source:
69
 
            raise JujuAssertionError(
70
 
                'Unexpected source for {}.\nGot {} instead of {}'.format(
71
 
                    attribute, source_value, source))
72
 
    except KeyError:
73
 
        raise ValueError('Attribute {} not found in config values.'.format(
74
 
            attribute))
75
 
 
76
 
 
77
 
def set_clouds_yaml_config(client, config_details):
78
 
    """Setup cloud details so it gets written to clouds.yaml at bootstrap."""
79
 
    cloud_name = client.env.get_cloud()
80
 
 
81
 
    extra_conf = {
82
 
        'type': client.env.config['type'],
83
 
        'regions': {client.env.get_region(): {}},
84
 
        'config': config_details}
85
 
    client.env.clouds['clouds'][cloud_name] = extra_conf
86
 
 
87
 
 
88
 
def parse_args(argv):
89
 
    """Parse all arguments."""
90
 
    parser = argparse.ArgumentParser(description="Test Model Tree Config")
91
 
    add_basic_testing_arguments(parser)
92
 
    return parser.parse_args(argv)
93
 
 
94
 
 
95
 
def main(argv=None):
96
 
    args = parse_args(argv)
97
 
    configure_logging(args.verbose)
98
 
    bs_manager = BootstrapManager.from_args(args)
99
 
    assess_model_config_tree(bs_manager, args.upload_tools)
100
 
    return 0
101
 
 
102
 
 
103
 
if __name__ == '__main__':
104
 
    sys.exit(main())