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

« back to all changes in this revision

Viewing changes to test_assess_log_rotation.py

  • Committer: Aaron Bentley
  • Date: 2015-09-02 17:46:47 UTC
  • mto: This revision was merged to the branch mainline in revision 1082.
  • Revision ID: aaron.bentley@canonical.com-20150902174647-06vmnsooo6yzd46t
Stop supplying env to subprocess calls.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from argparse import Namespace
2
 
from contextlib import contextmanager
3
 
 
4
 
from mock import (
5
 
    Mock,
6
 
    patch,
7
 
)
8
 
 
 
2
from unittest import TestCase
9
3
from assess_log_rotation import (
 
4
    check_for_extra_backup,
10
5
    check_expected_backup,
11
 
    check_for_extra_backup,
12
6
    check_log0,
13
7
    LogRotateError,
14
 
    make_client_from_args,
15
8
    parse_args,
16
 
    assess_debug_log,
17
 
    assess_machine_rotation,
18
9
)
19
 
from jujupy import (
20
 
    EnvJujuClient,
21
 
    JujuData,
22
 
    _temp_env as temp_env,
23
 
    yaml_loads,
24
 
    )
25
 
from tests import TestCase
26
 
from tests.test_jujupy import FakeJujuClient
 
10
from jujupy import yaml_loads
27
11
 
28
12
good_yaml = \
29
13
    """
155
139
                "/var/log/juju/unit-fill-logs-0.log", big_obj)
156
140
 
157
141
 
158
 
class TestTestDebugLog(TestCase):
159
 
 
160
 
    def test_happy_log(self):
161
 
        client = Mock()
162
 
        client.get_juju_output.return_value = '\n'*100
163
 
        # Ensure that no exception is raised
164
 
        assess_debug_log(client, timeout=120)
165
 
        client.get_juju_output.assert_called_once_with(
166
 
            "debug-log", "--lines=100", "--limit=100", timeout=120)
167
 
 
168
 
    def test_unhappy_log(self):
169
 
        client = Mock()
170
 
        client.get_juju_output.return_value = ''
171
 
        # Ensure that no exception is raised
172
 
        with self.assertRaises(LogRotateError):
173
 
            assess_debug_log(client)
174
 
        client.get_juju_output.assert_called_once_with(
175
 
            "debug-log", "--lines=100", "--limit=100", timeout=180)
176
 
 
177
 
 
178
 
class TestMachineRoation(TestCase):
179
 
 
180
 
    def test_respects_machine_id_0(self):
181
 
        client = FakeJujuClient(jes_enabled=True)
182
 
        client.bootstrap()
183
 
        client.deploy('fill-logs')
184
 
        with patch('assess_log_rotation.test_rotation') as tr_mock:
185
 
            assess_machine_rotation(client)
186
 
        tr_mock.assert_called_once_with(
187
 
            client, '/var/log/juju/machine-0.log', 'machine-0', 'fill-machine',
188
 
            'machine-size', 'megs=300', 'machine=0')
189
 
 
190
 
    def test_respects_machine_id_1(self):
191
 
        client = FakeJujuClient(jes_enabled=False)
192
 
        client.bootstrap()
193
 
        client.deploy('fill-logs')
194
 
        with patch('assess_log_rotation.test_rotation') as tr_mock:
195
 
            assess_machine_rotation(client)
196
 
        tr_mock.assert_called_once_with(
197
 
            client, '/var/log/juju/machine-1.log', 'machine-1',
198
 
            'fill-machine', 'machine-size', 'megs=300', 'machine=1')
199
 
 
200
 
 
201
142
class TestParseArgs(TestCase):
202
143
 
203
144
    def test_parse_args(self):
204
 
        args = parse_args(['b', 'c/juju', 'd', 'e', 'machine'])
205
 
        self.assertEqual(args, Namespace(
206
 
            agent='machine', env='b', juju_bin='c/juju', logs='d',
207
 
            temp_env_name='e', debug=False, agent_stream=None, agent_url=None,
208
 
            bootstrap_host=None, machine=[], keep_env=False,
209
 
            region=None, series=None, upload_tools=False, verbose=20))
210
 
 
211
 
    def test_parse_args_unit(self):
212
 
        args = parse_args(['b', 'c/juju', 'd', 'e', 'unit'])
213
 
        self.assertEqual('unit', args.agent)
214
 
 
215
 
 
216
 
class TestMakeClientFromArgs(TestCase):
217
 
 
218
 
    @contextmanager
219
 
    def make_client_cxt(self):
220
 
        with temp_env({'environments': {'foo': {}}}):
221
 
            with patch('subprocess.check_output', return_value=''):
222
 
                with patch('jujupy.EnvJujuClient.get_jes_command',
223
 
                           autospec=True, return_value='controller'):
224
 
                    with patch('jujupy.EnvJujuClient.juju',
225
 
                               autospec=True, return_value=''):
226
 
                        with patch('assess_log_rotation.tear_down',
227
 
                                   autospec=True, return_value='') as td_func:
228
 
                            with patch.object(JujuData, 'load_yaml'):
229
 
                                yield td_func
230
 
 
231
 
    def test_defaults(self):
232
 
        with self.make_client_cxt() as td_func:
233
 
            client = make_client_from_args(Namespace(
234
 
                juju_bin='', debug=False, env='foo', temp_env_name='bar',
235
 
                agent_url=None, agent_stream=None, series=None, region=None,
236
 
                bootstrap_host=None, machine=[]
237
 
                ))
238
 
        self.assertIsInstance(client, EnvJujuClient)
239
 
        self.assertIn('/jes-homes/bar', client.env.juju_home)
240
 
        td_func.assert_called_once_with(client, True)
 
145
        args = parse_args(['machine', 'b', 'c', 'd', 'e'])
 
146
        self.assertEqual(args, Namespace(
 
147
            agent='machine', juju_path='b', env_name='c', logs='d',
 
148
            temp_env_name='e', debug=False))
 
149
 
 
150
    def test_parse_args_debug(self):
 
151
        args = parse_args(['--debug', 'unit', 'b', 'c', 'd', 'e'])
 
152
        self.assertEqual(args, Namespace(
 
153
            agent='unit', juju_path='b', env_name='c', logs='d',
 
154
            temp_env_name='e', debug=True))