def test_success(self, mock_log): # A command suceeds. jujushell.call('echo') self.assertEqual(2, mock_log.call_count) mock_log.assert_has_calls([ call("running the following: 'echo'"), call("command 'echo' succeeded: '\\n'"), ])
def test_multiple_arguments(self, mock_log): # A command with multiple arguments succeeds. jujushell.call('echo', 'we are the borg') self.assertEqual(2, mock_log.call_count) mock_log.assert_has_calls([ call('running the following: "echo \'we are the borg\'"'), call('command "echo \'we are the borg\'" succeeded: ' '\'we are the borg\\n\''), ])
def test_invalid_command(self, mock_log): # An OSError is raised if the subprocess fails to find the provided # command in the PATH. with self.assertRaises(OSError) as ctx: jujushell.call('no-such-command') expected_error = ("command 'no-such-command' not found: [Errno 2] " "No such file or directory: 'no-such-command'") self.assertTrue(str(ctx.exception).startswith(expected_error)) mock_log.assert_has_calls([ call("running the following: 'no-such-command'"), ])
def test_failure(self, mock_log): # An OSError is raise when the command fails. with self.assertRaises(OSError) as ctx: jujushell.call('ls', 'no-such-file') expected_error = 'command \'ls no-such-file\' failed with retcode 2:' obtained_error = str(ctx.exception) self.assertTrue(obtained_error.startswith(expected_error)) mock_log.assert_has_calls([ call("running the following: 'ls no-such-file'"), call(obtained_error), ])