Index: quickstart/tests/test_utils.py |
=== modified file 'quickstart/tests/test_utils.py' |
--- quickstart/tests/test_utils.py 2013-10-15 18:25:57 +0000 |
+++ quickstart/tests/test_utils.py 2013-10-16 12:12:31 +0000 |
@@ -61,13 +61,17 @@ |
'no-such-command: [Errno 2] No such file or directory', |
error) |
- |
-class TestGetDefaultEnvName(unittest.TestCase): |
- |
- def patch_call(self, retcode, output='', error=''): |
- """Patch the quickstart.utils.call function.""" |
- mock_call = mock.Mock(return_value=(retcode, output, error)) |
- return mock.patch('quickstart.utils.call', mock_call) |
+ def test_logging(self): |
+ # The command line call and the results are properly logged. |
+ expected_messages = ( |
+ "running the following: echo 'we are the borg!'", |
+ r"retcode: 0 | output: 'we are the borg!\n' | error: ''", |
+ ) |
+ with helpers.assert_logs(expected_messages): |
+ utils.call('echo', 'we are the borg!') |
+ |
+ |
+class TestGetDefaultEnvName(helpers.CallTestsMixin, unittest.TestCase): |
def test_environment_variable(self): |
# The environment name is successfully returned if JUJU_ENV is set. |
@@ -176,3 +180,39 @@ |
env_type, admin_secret = utils.parse_env_file(env_file, 'aws') |
self.assertEqual('ec2', env_type) |
self.assertEqual('Secret!', admin_secret) |
+ |
+ |
+class TestParseStatusOutput(helpers.ValueErrorTestsMixin, unittest.TestCase): |
+ |
+ def test_invalid_yaml(self): |
+ # A ValueError is raised if the output is not a valid YAML. |
+ with self.assertRaises(ValueError) as context_manager: |
+ utils.parse_status_output(':') |
+ expected = 'unable to parse the output' |
+ self.assertIn(expected, str(context_manager.exception)) |
+ |
+ def test_invalid_yaml_contents(self): |
+ # A ValueError is raised if the output is not well formed. |
+ with self.assert_value_error('invalid YAML contents: a-string'): |
+ utils.parse_status_output('a-string') |
+ |
+ def test_no_env(self): |
+ # A ValueError is raised if the agent-state is not found in the YAML. |
+ data = { |
+ 'machines': { |
+ '0': {'agent-version': '1.17.0.1'}, |
+ }, |
+ } |
+ expected = 'agent state not found in {}'.format(str(data)) |
+ with self.assert_value_error(expected): |
+ utils.parse_status_output(yaml.safe_dump(data)) |
+ |
+ def test_success(self): |
+ # The agent state is correctly returned. |
+ output = yaml.safe_dump({ |
+ 'machines': { |
+ '0': {'agent-version': '1.17.0.1', 'agent-state': 'started'}, |
+ }, |
+ }) |
+ agent_state = utils.parse_status_output(output) |
+ self.assertEqual('started', agent_state) |