1
1
# Copyright 2014 Canonical Ltd. This software is licensed under the
2
2
# GNU General Public License version 3 (see the file LICENSE).
4
5
from unittest import TestCase
7
from mojo.juju.debuglogs import DebugLogs, UnsupportedDebugLogType, DEFAULT_DEBUG_LOG_CONFIG
6
8
from mojo.juju.status import Status, JujuStatusError
7
9
from mojo.tests import utils
207
209
# First check with no environment set
208
210
no_env_status = Status()
209
211
no_env_status._get_status()
210
mock_check_output.assert_called_with(['juju', 'status'])
212
mock_check_output.assert_called_with(
213
['juju', 'status', '--format=yaml'])
211
214
# Second test with an environment set
212
215
env_status = Status(environment="test-env")
213
216
env_status._get_status()
214
mock_check_output.assert_called_with(['juju', 'status', '-e', 'test-env'])
217
mock_check_output.assert_called_with(
218
['juju', 'status', '--format=yaml', '-e', 'test-env'])
216
220
with mock.patch('subprocess.check_output') as mock_check_output:
217
221
# Check that if we're calling _get_status where _status_output is
222
226
# And now check if we pass the force_update option, that it is
224
228
self.status._get_status(force_update=True)
225
mock_check_output.assert_called_with(['juju', 'status'])
229
mock_check_output.assert_called_with(
230
['juju', 'status', '--format=yaml'])
228
232
def test_yaml_status(self):
229
233
"""Test the yaml_status function"""
292
296
def test_machine_instance_id(self):
293
297
self.assertEqual(self.status.machine_instance_id('1'),
294
298
'0a598fda-78b0-47dd-aa21-ef9ec9e6879b')
301
class JujuDebugLogsTestCase(TestCase, utils.MojoTestCaseMixin):
303
def test__load_config_production_config(self):
304
"""Test _load_config does nothing with production stage"""
306
debug_logs = DebugLogs(ws, "production", None, None)
307
self.assertFalse(debug_logs._load_config())
309
@mock.patch('subprocess.check_output')
310
def test__resolve_remote_paths(self, _check_output):
311
"""Test _resolve_remote_paths"""
312
_check_output.return_value = u"['/etc/init.d/apache2']"
314
debug_logs = DebugLogs(ws, "production", None, None)
315
self.assertEqual(debug_logs._resolve_remote_paths('apache2/0', '/etc/init.d/apache2'),
316
['/etc/init.d/apache2'])
317
_check_output.assert_called_with(
318
['juju', 'run', '--unit', 'apache2/0', 'python -c "import glob; print glob.glob(\'/etc/init.d/apache2\')"'],
319
stderr=subprocess.STDOUT)
321
@mock.patch('subprocess.check_output')
322
def test__run_remote_command(self, _check_output):
323
"""Test _run_remote_command"""
325
debug_logs = DebugLogs(ws, "production", None, None)
326
debug_logs._run_remote_command('apache2/0', 'cat', ['/etc/init.d/apache2'])
327
_check_output.assert_called_with(
328
['juju', 'run', '--unit', 'apache2/0', 'cat /etc/init.d/apache2'],
329
stderr=subprocess.STDOUT)
331
def test__check_config(self):
332
"""Test _check_config"""
334
debug_logs = DebugLogs(ws, "production", None, None)
335
with mock.patch('mojo.juju.debuglogs.DebugLogs._load_config') as _load_config:
336
_load_config.return_value = DEFAULT_DEBUG_LOG_CONFIG
337
# Assert this doesn't raise an error
338
debug_logs._check_config()
339
# Now set the return value to something that will raise an error.
340
# First of all, reset _config so that _load_config() will be triggered again.
341
debug_logs._config = None
342
_load_config.return_value = {
344
'bogus-debuglog-type': ['/etc/apache2']
347
with self.assertRaises(UnsupportedDebugLogType):
348
debug_logs._check_config()