OLD | NEW |
1 # This file is part of the Juju GUI, which lets users view and manage Juju | 1 # This file is part of the Juju GUI, which lets users view and manage Juju |
2 # environments within a graphical interface (https://launchpad.net/juju-gui). | 2 # environments within a graphical interface (https://launchpad.net/juju-gui). |
3 # Copyright (C) 2013 Canonical Ltd. | 3 # Copyright (C) 2013 Canonical Ltd. |
4 # | 4 # |
5 # This program is free software: you can redistribute it and/or modify it under | 5 # This program is free software: you can redistribute it and/or modify it under |
6 # the terms of the GNU Affero General Public License version 3, as published by | 6 # the terms of the GNU Affero General Public License version 3, as published by |
7 # the Free Software Foundation. | 7 # the Free Software Foundation. |
8 # | 8 # |
9 # This program is distributed in the hope that it will be useful, but WITHOUT | 9 # This program is distributed in the hope that it will be useful, but WITHOUT |
10 # ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, | 10 # ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, |
11 # SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 11 # SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 # Affero General Public License for more details. | 12 # Affero General Public License for more details. |
13 # | 13 # |
14 # You should have received a copy of the GNU Affero General Public License | 14 # You should have received a copy of the GNU Affero General Public License |
15 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 15 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | 16 |
17 """Tests for the Juju Quickstart management infrastructure.""" | 17 """Tests for the Juju Quickstart management infrastructure.""" |
18 | 18 |
19 import argparse | 19 import argparse |
| 20 import logging |
20 import os | 21 import os |
21 import unittest | 22 import unittest |
22 | 23 |
23 import mock | 24 import mock |
24 import yaml | 25 import yaml |
25 | 26 |
26 import quickstart | 27 import quickstart |
27 from quickstart import manage | 28 from quickstart import manage |
28 from quickstart.tests import helpers | 29 from quickstart.tests import helpers |
29 | 30 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 def patch_get_default_env_name(self, env_name=None): | 116 def patch_get_default_env_name(self, env_name=None): |
116 """Patch the function used by setup() to retrieve the default env name. | 117 """Patch the function used by setup() to retrieve the default env name. |
117 | 118 |
118 This way the test does not rely on the user's Juju environment set up, | 119 This way the test does not rely on the user's Juju environment set up, |
119 and it is also possible to simulate an arbitrary environment name. | 120 and it is also possible to simulate an arbitrary environment name. |
120 """ | 121 """ |
121 mock_get_default_env_name = mock.Mock(return_value=env_name) | 122 mock_get_default_env_name = mock.Mock(return_value=env_name) |
122 path = 'quickstart.manage.utils.get_default_env_name' | 123 path = 'quickstart.manage.utils.get_default_env_name' |
123 return mock.patch(path, mock_get_default_env_name) | 124 return mock.patch(path, mock_get_default_env_name) |
124 | 125 |
125 def call_setup(self, args, env_name=None): | 126 def call_setup(self, args, env_name=None, exit_called=True): |
126 """Call the setup function simulating the given args and env name. | 127 """Call the setup function simulating the given args and env name. |
127 | 128 |
128 Also ensure the program exits without errors. | 129 Also ensure the program exits without errors if exit_called is True. |
129 """ | 130 """ |
130 with mock.patch('sys.argv', ['juju-quickstart'] + args): | 131 with mock.patch('sys.argv', ['juju-quickstart'] + args): |
131 with mock.patch('sys.exit') as mock_exit: | 132 with mock.patch('sys.exit') as mock_exit: |
132 with self.patch_get_default_env_name(env_name): | 133 with self.patch_get_default_env_name(env_name): |
133 manage.setup() | 134 manage.setup() |
134 mock_exit.assert_called_once_with(0) | 135 if exit_called: |
| 136 mock_exit.assert_called_once_with(0) |
135 | 137 |
136 def test_help(self): | 138 def test_help(self): |
137 # The program help message is properly formatted. | 139 # The program help message is properly formatted. |
138 with mock.patch('sys.stdout') as mock_stdout: | 140 with mock.patch('sys.stdout') as mock_stdout: |
139 self.call_setup(['--help']) | 141 self.call_setup(['--help']) |
140 stdout_write = mock_stdout.write | 142 stdout_write = mock_stdout.write |
141 self.assertTrue(stdout_write.called) | 143 self.assertTrue(stdout_write.called) |
142 # Retrieve the output from the mock call. | 144 # Retrieve the output from the mock call. |
143 output = stdout_write.call_args[0][0] | 145 output = stdout_write.call_args[0][0] |
144 self.assertIn('usage: juju-quickstart', output) | 146 self.assertIn('usage: juju-quickstart', output) |
(...skipping 18 matching lines...) Expand all Loading... |
163 with mock.patch('__builtin__.print') as mock_print: | 165 with mock.patch('__builtin__.print') as mock_print: |
164 self.call_setup(['--description']) | 166 self.call_setup(['--description']) |
165 mock_print.assert_called_once_with(manage.description) | 167 mock_print.assert_called_once_with(manage.description) |
166 | 168 |
167 def test_version(self): | 169 def test_version(self): |
168 # The program version is properly printed to stderr. | 170 # The program version is properly printed to stderr. |
169 with mock.patch('sys.stderr') as mock_stderr: | 171 with mock.patch('sys.stderr') as mock_stderr: |
170 self.call_setup(['--version']) | 172 self.call_setup(['--version']) |
171 expected = 'juju-quickstart {}\n'.format(quickstart.get_version()) | 173 expected = 'juju-quickstart {}\n'.format(quickstart.get_version()) |
172 mock_stderr.write.assert_called_once_with(expected) | 174 mock_stderr.write.assert_called_once_with(expected) |
| 175 |
| 176 def test_configure_logging(self): |
| 177 # Logging is properly set up at the info level. |
| 178 logger = logging.getLogger() |
| 179 self.call_setup([], 'ec2', exit_called=False) |
| 180 self.assertEqual(logging.INFO, logger.level) |
| 181 |
| 182 def test_configure_logging_debug(self): |
| 183 # Logging is properly set up at the debug level. |
| 184 logger = logging.getLogger() |
| 185 self.call_setup(['--debug'], 'ec2', exit_called=False) |
| 186 self.assertEqual(logging.DEBUG, logger.level) |
OLD | NEW |