Exemple #1
0
    def test_execute_exit_code(self, mock_popen, mock_poll):
        mock_proc = mock.Mock()
        mock_proc.stdout.readline.side_effect = [b'out1\n', b'out2\n', b'']
        mock_proc.stderr.readline.side_effect = [b'err1\n', b'']
        mock_popen.return_value = mock_proc
        mock_poll.return_value = 14

        return_code, stdout, stderr, duration = execute('foo', 10, 20, 30, 1, False)
        self.assertEqual(return_code, 14)
        self.assertEqual(stdout, b'out1\nout2\n')
        self.assertEqual(stderr, b'err1\n')

        mock_popen.assert_called_once()
        mock_poll.assert_called_once_with(mock_proc, 1, 10)
        mock_proc.terminate.assert_not_called()
        mock_proc.kill.assert_not_called()
Exemple #2
0
    def test_execute_oserror(self, mock_popen, mock_poll):
        mock_proc = mock.Mock()
        mock_proc.stdout.readline.side_effect = [b'out1\n', b'out2\n', '']
        mock_proc.stderr.readline.side_effect = [b'err1\n', b'']
        mock_popen.return_value = mock_proc
        mock_poll.side_effect = [Timeout, Timeout]
        mock_proc.kill.side_effect = OSError(3, 'No process')
        return_code, stdout, stderr, duration = execute(
            'foo', 10, 20, 30, 1, False)
        self.assertEqual(return_code, Timeout)
        self.assertEqual(stdout, b'out1\nout2\n')
        self.assertEqual(stderr, b'err1\n')

        mock_popen.assert_called_once()
        mock_poll.assert_has_calls(
            [mock.call(mock_proc, 1, 10),
             mock.call(mock_proc, 1, 20)])
        mock_proc.terminate.assert_called_once()
        mock_proc.kill.assert_called_once()
Exemple #3
0
    def test_execute_popen_fail(self, mock_popen):
        mock_popen.side_effect = ValueError('Bad things')

        with self.assertRaises(ValueError):
            execute('sleep 1', 10, 1, 1, 1, False)