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

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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
from argparse import Namespace
from contextlib import contextmanager
import logging
from mock import (
    call,
    patch,
)
from unittest import TestCase

from jujupy import (
    EnvJujuClient,
    SimpleEnvironment,
)
from scale_out import (
    deploy_charms,
    get_service_name,
    parse_args,
    scale_out,
    scaleout_setup,
)


def fake_SimpleEnvironment(name):
    return SimpleEnvironment(name, {})


def fake_EnvJujuClient(env, path=None, debug=None):
    return EnvJujuClient(env=env, version='1.2.3.4', full_path=path)


class TestScaleOut(TestCase):

    @contextmanager
    def fake_client_cxt(self):
        env = fake_SimpleEnvironment('foo')
        client = fake_EnvJujuClient(env)
        bv_cxt = patch('jujupy.EnvJujuClient.by_version',
                       return_value=client)
        fc_cxt = patch('jujupy.SimpleEnvironment.from_config',
                       return_value=env)
        with bv_cxt, fc_cxt:
            yield (client, env)

    def test_parse_args(self):
        args = parse_args(
            ['env', '/path/juju', 'logs', 'temp_name', 'foo', 'bar'])
        expected = Namespace(
            agent_stream=None,
            agent_url=None,
            bootstrap_host=None,
            charms=['foo', 'bar'],
            debug=False,
            env='env',
            juju_bin='/path/juju',
            keep_env=False,
            logs='logs',
            machine=[],
            region=None,
            series=None,
            temp_env_name='temp_name',
            upload_tools=False,
            verbose=logging.INFO
        )
        self.assertEqual(args, expected)

    @patch('scale_out.boot_context', autospec=True)
    @patch('scale_out.EnvJujuClient.add_ssh_machines', autospec=True)
    def test_scaleout_setup(
            self,
            add_ssh_machines_func,
            boot_context_func):

        args = Namespace(
            agent_stream=None,
            agent_url=None,
            bootstrap_host=None,
            charms='ubuntu',
            debug=False,
            env='test_env',
            juju_bin='/path/juju',
            keep_env=False,
            logs='/tmp/logs',
            machine=['0'],
            region=None,
            series=None,
            temp_env_name='temp_name',
            upload_tools=False,
            verbose=logging.INFO
        )

        with self.fake_client_cxt() as (fake_client, fake_env):
            with scaleout_setup(args) as client:
                # Get a reference to the by_version function that was patched
                # in fake_client_cxt()
                bv_mock = EnvJujuClient.by_version
                pass
        boot_context_func.assert_called_once_with(
            args.temp_env_name,
            client,
            args.bootstrap_host,
            args.machine,
            'trusty',
            args.agent_url,
            args.agent_stream,
            args.logs,
            args.keep_env,
            args.upload_tools,
            region=args.region)
        bv_mock.assert_called_once_with(fake_env, '/path/juju', False)
        add_ssh_machines_func.assert_called_once_with(client, ['0'])
        self.assertIs(client, fake_client)

    def test_scaleout_setup_sets_series(self):

        args = Namespace(
            agent_stream=None,
            agent_url=None,
            bootstrap_host=None,
            charms='ubuntu some_other_charm',
            debug=False,
            env='test_env',
            juju_bin='/path/juju',
            keep_env=False,
            logs='/tmp/logs',
            machine=['0'],
            region=None,
            series='my_series',
            temp_env_name='temp_name',
            upload_tools=False,
            verbose=logging.INFO
        )

        with self.fake_client_cxt():
            with patch('scale_out.boot_context', autospec=True) as bc_mock:
                with patch('scale_out.EnvJujuClient.add_ssh_machines',
                           autospec=True):
                    with scaleout_setup(args) as client:
                        pass
        bc_mock.assert_called_once_with(
            args.temp_env_name,
            client,
            args.bootstrap_host,
            args.machine,
            'my_series',
            args.agent_url,
            args.agent_stream,
            args.logs,
            args.keep_env,
            args.upload_tools,
            region=args.region)

    def test_deploy_charms(self):
        with self.fake_client_cxt() as (client, env):
            with patch.object(EnvJujuClient, 'deploy') as d_mock:
                with patch.object(EnvJujuClient,
                                  'wait_for_started') as wfs_mock:
                    deploy_charms(client, ['ubuntu', 'mysql'])
        expected = [call('ubuntu', service='ubuntu'),
                    call('mysql', service='mysql')]
        self.assertEqual(d_mock.mock_calls, expected)
        wfs_mock.assert_called_once_with()

    def test_deploy_charms_local(self):
        with self.fake_client_cxt() as (client, env):
            with patch.object(EnvJujuClient, 'deploy') as d_mock:
                with patch.object(EnvJujuClient,
                                  'wait_for_started') as wfs_mock:
                    deploy_charms(client, ['local:foo', 'local:bar'])
        expected = [call('local:foo', service='foo'),
                    call('local:bar', service='bar')]
        self.assertEqual(d_mock.mock_calls, expected)
        wfs_mock.assert_called_once_with()

    def test_scale_out(self):
        with self.fake_client_cxt() as (client, env):
            with patch.object(EnvJujuClient, 'juju') as j_mock:
                with patch.object(EnvJujuClient,
                                  'wait_for_started') as wfs_mock:
                    scale_out(client, 'ubuntu')
        j_mock.assert_called_once_with('add-unit', ('ubuntu', '-n', '5'))
        wfs_mock.assert_called_once_with()

    def test_get_service_name(self):
        charms = [('charm-name', 'charm-name'),
                  ('charm-name-21', 'charm-name-21'),
                  ('series/charm-name-13', 'charm-name-13'),
                  ('local:charm-name', 'charm-name'),
                  ('cs:~user/charm-name', 'charm-name'),
                  ('cs:charm-name-2-1', 'charm-name-2-1'),
                  ('lp:~user/some/path/to/charm-name', 'charm-name')]
        for charm, expected in charms:
            result = get_service_name(charm)
            self.assertEqual(result, expected)