~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-08-19 15:07:08 UTC
  • mto: This revision was merged to the branch mainline in revision 1069.
  • Revision ID: aaron.bentley@canonical.com-20150819150708-88xesx4iardg12b4
Wait for proc to exit after signalling.

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