~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
from mock import (
    patch
)
import subprocess
from unittest import TestCase

from jujupy import (
    EnvJujuClient,
    SimpleEnvironment,
    )
from cs_staging_deploy import CSStagingTest
from utility import (
    setup_test_logging,
)


def fake_EnvJujuClient_by_version(env, path=None, debug=None):
    return env, path


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


class TestCSStagingDeploy(TestCase):

    def setUp(self):
        setup_test_logging(self)

    def test_from_args(self):
        with patch('jujupy.EnvJujuClient.by_version',
                   side_effect=fake_EnvJujuClient_by_version):
            with patch('jujupy.SimpleEnvironment.from_config',
                       side_effect=fake_SimpleEnvironment_from_config):
                csstaging = CSStagingTest.from_args(
                    'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
                    '0.0.0.0'
                )
        self.assertIs(type(csstaging), CSStagingTest)
        self.assertEqual(csstaging.client[0].environment, 'temp_env_name')
        self.assertIs(csstaging.client[1], '/foo/bin/juju')
        self.assertEqual(csstaging.log_dir, '/tmp/tmp')
        self.assertEqual(csstaging.charm_store_ip, '0.0.0.0')

    def test_from_args_agent_url(self):
        with patch('jujupy.EnvJujuClient.by_version',
                   side_effect=fake_EnvJujuClient_by_version):
            with patch('jujupy.SimpleEnvironment.from_config',
                       side_effect=fake_SimpleEnvironment_from_config):
                csstaging = CSStagingTest.from_args(
                    'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
                    '0.0.0.0', agent_url='http://agent_url.com'
                )
        self.assertEqual(csstaging.client[0].config['agent_url'],
                         'http://agent_url.com')

    def test_from_args_series(self):
        with patch('jujupy.EnvJujuClient.by_version',
                   side_effect=fake_EnvJujuClient_by_version):
            with patch('jujupy.SimpleEnvironment.from_config',
                       side_effect=fake_SimpleEnvironment_from_config):
                csstaging = CSStagingTest.from_args(
                    'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
                    '0.0.0.0', series='precise'
                )
        self.assertEqual(csstaging.client[0].config['series'],
                         'precise')

    def test_from_args_charm(self):
        with patch('jujupy.EnvJujuClient.by_version',
                   side_effect=fake_EnvJujuClient_by_version):
            with patch('jujupy.SimpleEnvironment.from_config',
                       side_effect=fake_SimpleEnvironment_from_config):
                csstaging = CSStagingTest.from_args(
                    'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
                    '0.0.0.0', charm='some_charm'
                )
        self.assertEqual(csstaging.charm, 'some_charm')

    def test_from_args_debug(self):
        with patch('jujupy.EnvJujuClient.get_version',
                   side_effect=lambda x, juju_path=None: ''):
            with patch('jujupy.SimpleEnvironment.from_config',
                       side_effect=fake_SimpleEnvironment_from_config):
                csstaging = CSStagingTest.from_args(
                    'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
                    '0.0.0.0', debug_flag=True
                )
        self.assertEqual(csstaging.client.debug, True)

    def test_run(self):
        client = EnvJujuClient(
            SimpleEnvironment('foo', {'type': 'local'}), '1.234-76', None)
        csstaging = CSStagingTest(client, '0.0.0.0', 'charm', '/tmp/logs')
        with patch.object(client, 'destroy_environment') as qs_mock:
            with patch('cs_staging_deploy.safe_print_status') as ps_mock:
                with patch.object(csstaging, 'bootstrap') as bs_mock:
                    with patch.object(csstaging, 'remote_run') as rr_mock:
                        with patch('jujupy.EnvJujuClient.deploy'):
                            with patch(
                                    'jujupy.EnvJujuClient.wait_for_started'):
                                csstaging.run()
        bs_mock.assert_called_once_with()
        rr_call = (
            '''sudo bash -c "echo '%s store.juju.ubuntu.com' >> /etc/hosts"'''
            % '0.0.0.0')
        rr_mock.assert_called_once_with('0', rr_call)
        qs_mock.assert_called_once_with(delete_jenv=True)
        ps_mock.assert_called_once_with(client)

    def test_run_exception(self):
        client = EnvJujuClient(
            SimpleEnvironment('foo', {'type': 'local'}), '1.234-76', None)
        csstaging = CSStagingTest(client, '0.0.0.0', 'charm', '/tmp/logs')
        with patch.object(client, 'destroy_environment') as qs_mock:
            with patch('cs_staging_deploy.safe_print_status') as ps_mock:
                with patch('cs_staging_deploy.dump_env_logs') as dl_mock:
                    with patch('cs_staging_deploy.bootstrap_from_env'):
                        with patch('cs_staging_deploy.get_machine_dns_name',
                                   return_value='foo'):
                            with patch.object(csstaging, 'remote_run',
                                              side_effect=Exception()):
                                csstaging.run()
        dl_mock.assert_called_once_with(client, 'foo', '/tmp/logs')
        qs_mock.assert_called_once_with(delete_jenv=True)
        ps_mock.assert_called_once_with(client)

    def test_remote_run(self):
        client = EnvJujuClient(
            SimpleEnvironment('foo', {'type': 'local'}), '1.234-76', None)
        csstaging = CSStagingTest(client, '0.0.0.0', 'charm', '/tmp/logs')
        with patch('jujupy.EnvJujuClient.get_juju_output') as out_mock:
            csstaging.remote_run('machine', 'some_cmd')
        out_mock.assert_called_once_with('ssh', 'machine', 'some_cmd')

    def test_remote_run_exception(self):
        client = EnvJujuClient(
            SimpleEnvironment('foo', {'type': 'local'}), '1.234-76', None)
        csstaging = CSStagingTest(client, '0.0.0.0', 'charm', '/tmp/logs')
        with self.assertRaises(subprocess.CalledProcessError):
            with patch('jujupy.EnvJujuClient.get_juju_output',
                       side_effect=subprocess.CalledProcessError(1, 'cmd')):
                csstaging.remote_run('machine', 'some_cmd')

    def test_bootstrap(self):
        client = EnvJujuClient(
            SimpleEnvironment('foo', {'type': 'local'}), '1.234-76', None)
        csstaging = CSStagingTest(client, '0.0.0.0', 'charm', '/tmp/logs')
        with patch('cs_staging_deploy.get_juju_home',
                   return_value='foo') as gjh_mock:
            with patch('cs_staging_deploy.bootstrap_from_env') as bfe_mock:
                with patch(
                        'cs_staging_deploy.get_machine_dns_name') as dns_mock:
                    csstaging.bootstrap()
        gjh_mock.assert_called_once_with()
        bfe_mock.assert_called_once_with('foo', client)
        dns_mock.assert_called_once_with(client, 0)