コード例 #1
0
    def _test_create(self, old_kernel, exists, add, execute, IPTables):
        add.side_effect = linux_utils.ProcessExecutionError(
            message="Cannot create namespace file ns: File exists",
            returncode=1)
        exists.return_value = True
        # There are up to six sysctl calls - two to enable forwarding,
        # two for arp_ignore and arp_announce, and two for ip_nonlocal_bind
        execute.side_effect = [
            None, None, None, None, RuntimeError if old_kernel else None, None
        ]

        self.fip_ns._iptables_manager = IPTables()
        self.fip_ns.create()

        ns_name = self.fip_ns.get_name()

        netns_cmd = ['ip', 'netns', 'exec', ns_name]
        bind_cmd = ['sysctl', '-w', 'net.ipv4.ip_nonlocal_bind=1']
        expected = [
            mock.call(netns_cmd + bind_cmd,
                      check_exit_code=True,
                      extra_ok_codes=None,
                      log_fail_as_error=False,
                      run_as_root=True)
        ]

        if old_kernel:
            expected.append(
                mock.call(bind_cmd,
                          check_exit_code=True,
                          extra_ok_codes=None,
                          log_fail_as_error=True,
                          run_as_root=True))

        execute.assert_has_calls(expected)
コード例 #2
0
    def _test_kill_process(self, pid, exception_message=None,
                           kill_signal=signal.SIGKILL):
        if exception_message:
            exc = utils.ProcessExecutionError(exception_message, returncode=0)
        else:
            exc = None
        with mock.patch.object(utils, 'execute',
                               side_effect=exc) as mock_execute:
            utils.kill_process(pid, kill_signal, run_as_root=True)

        mock_execute.assert_called_with(['kill', '-%d' % kill_signal, pid],
                                        run_as_root=True)
コード例 #3
0
    def _test_kill_process(self,
                           pid,
                           raise_exception=False,
                           kill_signal=signal.SIGKILL,
                           pid_killed=True):
        if raise_exception:
            exc = utils.ProcessExecutionError('', returncode=0)
        else:
            exc = None
        with mock.patch.object(utils, 'execute',
                               side_effect=exc) as mock_execute:
            with mock.patch.object(utils,
                                   'process_is_running',
                                   return_value=not pid_killed):
                utils.kill_process(pid, kill_signal, run_as_root=True)

        mock_execute.assert_called_with(
            ['kill', '-%d' % kill_signal, pid], run_as_root=True)
コード例 #4
0
 def test_returns_empty_list_for_exit_code_1(self):
     with mock.patch.object(utils,
                            'execute',
                            side_effect=utils.ProcessExecutionError(
                                '', returncode=1)):
         self.assertEqual([], utils.find_child_pids(-1))
コード例 #5
0
 def test_raises_exception_returncode_0(self):
     with testtools.ExpectedException(utils.ProcessExecutionError):
         self.m_execute.side_effect = \
             utils.ProcessExecutionError('', returncode=0)
         utils.find_parent_pid(-1)
コード例 #6
0
 def test_returns_none_for_no_valid_pid(self):
     self.m_execute.side_effect = utils.ProcessExecutionError('',
                                                              returncode=1)
     self.assertIsNone(utils.find_parent_pid(-1))