~veebers/juju-ci-tools/model_migration_check_all_units_of_charm

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
141
142
143
144
145
146
147
148
"""Tests for assess_mixed_images module."""

from argparse import Namespace
import logging
from mock import (
    call,
    patch
    )
import StringIO

from assess_mixed_images import (
    assess_mixed_images,
    parse_args,
    main,
    )
from fakejuju import fake_juju_client
from tests import (
    parse_error,
    TestCase,
    )


class TestParseArgs(TestCase):

    def test_defaults(self):
        args = parse_args(["an-env", "/bin/juju", "/tmp/logs", "an-env-mod"])
        self.assertEqual(Namespace(
            env='an-env',
            juju_bin='/bin/juju',
            logs='/tmp/logs',
            temp_env_name='an-env-mod',
            debug=False,
            series='trusty',
            agent_stream=None,
            agent_url=None,
            bootstrap_host=None,
            upload_tools=False,
            verbose=logging.INFO,
            image_metadata_url=None,
            keep_env=False,
            machine=[],
            region=None,
            deadline=None,
            ), args)

    def test_image_metadata_url(self):
        args = parse_args([
            'an-env', '/bin/juju', '/tmp/logs', 'an-env-mod',
            '--image-metadata-url', 'http://example.com/images'])
        self.assertEqual(args.image_metadata_url, 'http://example.com/images')

    def test_help(self):
        fake_stdout = StringIO.StringIO()
        with parse_error(self) as fake_stderr:
            with patch("sys.stdout", fake_stdout):
                parse_args(["--help"])
        self.assertEqual("", fake_stderr.getvalue())


class TestMain(TestCase):

    def test_main(self):
        argv = ["an-env", "/bin/juju", "/tmp/logs", "an-env-mod", "--verbose"]
        client = fake_juju_client()
        with patch("assess_mixed_images.configure_logging",
                   autospec=True) as mock_cl:
            with patch("assess_mixed_images.BootstrapManager.booted_context",
                       autospec=True) as mock_bc:
                with patch("deploy_stack.client_from_config",
                           return_value=client) as mock_c:
                    with patch("assess_mixed_images.assess_mixed_images",
                               autospec=True) as mock_assess:
                        main(argv)
        mock_cl.assert_called_once_with(logging.DEBUG)
        mock_c.assert_called_once_with('an-env', "/bin/juju", debug=False,
                                       soft_deadline=None)
        self.assertEqual(mock_bc.call_count, 1)
        mock_assess.assert_called_once_with(client)


class TestAssess(TestCase):

    def test_mixed_images(self):
        mock_client = fake_juju_client()
        mock_client.bootstrap()
        assess_mixed_images(mock_client)
        expected = {
            'model': {'name': 'name'},
            'machines': {
                '0': {
                    'dns-name': '0.example.com',
                    'instance-id': '0',
                    'juju-status': {'current': 'idle'},
                    },
                '1': {
                    'dns-name': '1.example.com',
                    'instance-id': '1',
                    'juju-status': {'current': 'idle'},
                    },
                },
            'applications': {
                'dummy-sink': {
                    'exposed': False,
                    'relations': {'source': ['dummy-source']},
                    'units': {
                        'dummy-sink/0': {
                            'machine': '0',
                            'juju-status': {'current': 'idle'},
                            },
                        }
                    },
                'dummy-source': {
                    'exposed': False,
                    'relations': {},
                    'units': {
                        'dummy-source/0': {
                            'machine': '1',
                            'juju-status': {'current': 'idle'},
                            }
                        }
                    }
                }
            }
        actual = mock_client.get_status().status
        self.assertEqual(expected, actual)

    def test_mixed_images_charm_2x(self):
        mock_client = fake_juju_client()
        mock_client.bootstrap()
        with patch.object(mock_client, 'deploy') as mock_d:
            with patch('assess_mixed_images.assess_juju_relations',
                       autospec=True) as mock_ajr:
                assess_mixed_images(mock_client)
        calls = [call('dummy-sink'), call('dummy-source')]
        self.assertEqual(mock_d.mock_calls, calls)
        mock_ajr.assert_called_once_with(mock_client)

    def test_mixed_images_charm_1x(self):
        mock_client = fake_juju_client(version='1.25.0')
        mock_client.bootstrap()
        with patch.object(mock_client, 'deploy') as mock_d:
            with patch('assess_mixed_images.assess_juju_relations',
                       autospec=True) as mock_ajr:
                assess_mixed_images(mock_client)
        calls = [call('local:centos7/dummy-sink'),
                 call('local:trusty/dummy-source')]
        self.assertEqual(mock_d.mock_calls, calls)
        mock_ajr.assert_called_once_with(mock_client)