~caio1982/mojo/local_repo_keys

« back to all changes in this revision

Viewing changes to mojo/tests/test_juju.py

  • Committer: Tim Kuhlman
  • Date: 2016-01-13 16:01:22 UTC
  • mfrom: (260 mojo)
  • mto: This revision was merged to the branch mainline in revision 268.
  • Revision ID: timothy.kuhlman@canonical.com-20160113160122-7te92i45gc9iplmm
Merged latest changes from mojo trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright 2014 Canonical Ltd.  This software is licensed under the
2
2
# GNU General Public License version 3 (see the file LICENSE).
3
3
import mock
 
4
import subprocess
4
5
from unittest import TestCase
5
6
 
 
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
8
10
 
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'])
215
219
 
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
223
227
            # called
224
228
            self.status._get_status(force_update=True)
225
 
            mock_check_output.assert_called_with(['juju', 'status'])
226
 
            
 
229
            mock_check_output.assert_called_with(
 
230
                ['juju', 'status', '--format=yaml'])
227
231
 
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')
 
299
 
 
300
 
 
301
class JujuDebugLogsTestCase(TestCase, utils.MojoTestCaseMixin):
 
302
 
 
303
    def test__load_config_production_config(self):
 
304
        """Test _load_config does nothing with production stage"""
 
305
        ws = None
 
306
        debug_logs = DebugLogs(ws, "production", None, None)
 
307
        self.assertFalse(debug_logs._load_config())
 
308
 
 
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']"
 
313
        ws = None
 
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)
 
320
 
 
321
    @mock.patch('subprocess.check_output')
 
322
    def test__run_remote_command(self, _check_output):
 
323
        """Test _run_remote_command"""
 
324
        ws = None
 
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)
 
330
 
 
331
    def test__check_config(self):
 
332
        """Test _check_config"""
 
333
        ws = None
 
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 = {
 
343
                'apache2': {
 
344
                    'bogus-debuglog-type': ['/etc/apache2']
 
345
                    }
 
346
                }
 
347
            with self.assertRaises(UnsupportedDebugLogType):
 
348
                debug_logs._check_config()