Esempio n. 1
0
    def test_execute(self,
                     mock_avoid_blocking_call,
                     mock_create_process,
                     returncode=0,
                     check_exit_code=True,
                     return_stder=True,
                     log_fail_as_error=True,
                     extra_ok_codes=None,
                     exp_fail=False):
        fake_stdin = 'fake_stdin'
        fake_stdout = 'fake_stdout'
        fake_stderr = 'fake_stderr'

        mock_popen = mock.Mock()
        mock_popen.communicate.return_value = fake_stdout, fake_stderr
        mock_popen.returncode = returncode

        mock_create_process.return_value = mock_popen, mock.sentinel.cmd
        mock_avoid_blocking_call.side_effect = (
            lambda func, *args, **kwargs: func(*args, **kwargs))

        args = (mock.sentinel.cmd, fake_stdin, mock.sentinel.env,
                check_exit_code, return_stder, log_fail_as_error,
                extra_ok_codes)

        if exp_fail:
            self.assertRaises(exceptions.ProcessExecutionError, utils.execute,
                              *args)
        else:
            ret_val = utils.execute(*args)
            if return_stder:
                exp_ret_val = (fake_stdout, fake_stderr)
            else:
                exp_ret_val = fake_stdout

            self.assertEqual(exp_ret_val, ret_val)

        mock_create_process.assert_called_once_with(mock.sentinel.cmd,
                                                    addl_env=mock.sentinel.env,
                                                    tpool_proxy=False)
        mock_avoid_blocking_call.assert_called_once_with(
            mock_popen.communicate, six.b(fake_stdin))
        mock_popen.communicate.assert_called_once_with(six.b(fake_stdin))
        mock_popen.stdin.close.assert_called_once_with()
Esempio n. 2
0
    def test_execute(self, mock_avoid_blocking_call, mock_create_process,
                     returncode=0, check_exit_code=True, return_stder=True,
                     log_fail_as_error=True, extra_ok_codes=None,
                     exp_fail=False):
        fake_stdin = 'fake_stdin'
        fake_stdout = 'fake_stdout'
        fake_stderr = 'fake_stderr'

        mock_popen = mock.Mock()
        mock_popen.communicate.return_value = fake_stdout, fake_stderr
        mock_popen.returncode = returncode

        mock_create_process.return_value = mock_popen, mock.sentinel.cmd
        mock_avoid_blocking_call.side_effect = (
            lambda func, *args, **kwargs: func(*args, **kwargs))

        args = (mock.sentinel.cmd, fake_stdin, mock.sentinel.env,
                check_exit_code, return_stder, log_fail_as_error,
                extra_ok_codes)

        if exp_fail:
            self.assertRaises(exceptions.ProcessExecutionError,
                              utils.execute,
                              *args)
        else:
            ret_val = utils.execute(*args)
            if return_stder:
                exp_ret_val = (fake_stdout, fake_stderr)
            else:
                exp_ret_val = fake_stdout

            self.assertEqual(exp_ret_val, ret_val)

        mock_create_process.assert_called_once_with(
            mock.sentinel.cmd, addl_env=mock.sentinel.env,
            tpool_proxy=False)
        mock_avoid_blocking_call.assert_called_once_with(
            mock_popen.communicate, six.b(fake_stdin))
        mock_popen.communicate.assert_called_once_with(six.b(fake_stdin))
        mock_popen.stdin.close.assert_called_once_with()
Esempio n. 3
0
 def run(self, intf):
     cmd = [
         'sudo', 'simple_switch', '-i', intf, '--thrift-port', '10811',
         '--no-p4', '--', '--enable-swap'
     ]
     exc_utils.execute(cmd, run_as_root=True, log_fail_as_error=True)