Example #1
0
    def test_stop_jobs_helper(self, jobs_ended, mock_get_pending_jobs):
        mock_job1 = mock.Mock(Cancellable=True)
        mock_job2 = mock.Mock(Cancellable=True)
        mock_job3 = mock.Mock(Cancellable=False)

        pending_jobs = [mock_job1, mock_job2, mock_job3]
        mock_get_pending_jobs.side_effect = (pending_jobs, pending_jobs
                                             if not jobs_ended else [])

        mock_job1.RequestStateChange.side_effect = (test_base.FakeWMIExc(
            hresult=jobutils._utils._WBEM_E_NOT_FOUND))
        mock_job2.RequestStateChange.side_effect = (test_base.FakeWMIExc(
            hresult=mock.sentinel.hresult))

        if jobs_ended:
            self.jobutils._stop_jobs(mock.sentinel.vm)
        else:
            self.assertRaises(exceptions.JobTerminateFailed,
                              self.jobutils._stop_jobs, mock.sentinel.vm)

        mock_get_pending_jobs.assert_has_calls([mock.call(mock.sentinel.vm)] *
                                               2)

        mock_job1.RequestStateChange.assert_called_once_with(
            self.jobutils._KILL_JOB_STATE_CHANGE_REQUEST)
        mock_job2.RequestStateChange.assert_called_once_with(
            self.jobutils._KILL_JOB_STATE_CHANGE_REQUEST)
        self.assertFalse(mock_job3.RequestStateqqChange.called)
    def test_wmi_retry_decorator(self, expect_hres, mock_time):
        expected_hres = 0x8007beef
        expected_err_code = expected_hres if expect_hres else 0xbeef
        other_hres = 0x80070001
        max_retry_count = 5
        # The second exception will contain an unexpected error code,
        # in which case we expect the function to propagate the error.
        expected_try_count = 2

        side_effect = [
            test_base.FakeWMIExc(hresult=expected_hres),
            test_base.FakeWMIExc(hresult=other_hres)
        ]

        decorator = (_utils.wmi_retry_decorator_hresult
                     if expect_hres else _utils.wmi_retry_decorator)
        (fake_func,
         fake_func_side_effect) = self._get_fake_func_with_retry_decorator(
             error_codes=expected_err_code,
             max_retry_count=max_retry_count,
             decorator=decorator,
             side_effect=side_effect)

        self.assertRaises(test_base.FakeWMIExc,
                          fake_func,
                          mock.sentinel.arg,
                          kwarg=mock.sentinel.kwarg)

        fake_func_side_effect.assert_has_calls(
            [mock.call(mock.sentinel.arg, kwarg=mock.sentinel.kwarg)] *
            expected_try_count)
    def test_is_not_found_exc(self, hresult):
        exc = test_base.FakeWMIExc(hresult=hresult)

        result = _utils._is_not_found_exc(exc)

        expected = hresult == _utils._WBEM_E_NOT_FOUND
        self.assertEqual(expected, result)
Example #4
0
    def _test_create_iscsi_target_exception(self, target_exists=False,
                                            fail_if_exists=False):
        fake_file_exists_hres = -0x7ff8ffb0
        fake_hres = fake_file_exists_hres if target_exists else 1
        mock_wt_host_cls = self._tgutils._conn_wmi.WT_Host
        mock_wt_host_cls.NewHost.side_effect = test_base.FakeWMIExc(
            hresult=fake_hres)

        if target_exists and not fail_if_exists:
            self._tgutils.create_iscsi_target(mock.sentinel.target_name,
                                              fail_if_exists=fail_if_exists)
        else:
            self.assertRaises(exceptions.ISCSITargetException,
                              self._tgutils.create_iscsi_target,
                              mock.sentinel.target_name,
                              fail_if_exists=fail_if_exists)

        mock_wt_host_cls.NewHost.assert_called_once_with(
            HostName=mock.sentinel.target_name)