Esempio n. 1
0
    def test_inject_file(self):
        instance = _get_fake_instance()
        agent = self._create_agent(instance)
        self.mox.StubOutWithMock(agent, "_call_agent")

        b64_path = base64.b64encode("path")
        b64_contents = base64.b64encode("contents")
        agent._call_agent("inject_file", {"b64_contents": b64_contents, "b64_path": b64_path})

        self.mox.ReplayAll()

        agent.inject_file("path", "contents")
Esempio n. 2
0
    def test_call_agent_swallows_error(self, mock_call_agent,
                                       mock_add_instance_fault):
        fake_error = exception.AgentError(method="bob")
        mock_call_agent.side_effect = fake_error

        instance = _get_fake_instance()
        agent = self._create_agent(instance)

        agent._call_agent("bob")

        mock_call_agent.assert_called_once_with(agent.session, agent.instance,
                agent.vm_ref, "bob", None, None, None)
        mock_add_instance_fault.assert_called_once_with(fake_error, mock.ANY)
Esempio n. 3
0
    def test_call_agent_swallows_error(self, mock_call_agent,
                                       mock_add_instance_fault):
        fake_error = exception.AgentError(method="bob")
        mock_call_agent.side_effect = fake_error

        instance = _get_fake_instance()
        agent = self._create_agent(instance)

        agent._call_agent("bob")

        mock_call_agent.assert_called_once_with(agent.session, agent.instance,
                agent.vm_ref, "bob", None, None, None)
        mock_add_instance_fault.assert_called_once_with(fake_error, mock.ANY)
Esempio n. 4
0
    def test_inject_file(self):
        instance = _get_fake_instance()
        agent = self._create_agent(instance)
        self.mox.StubOutWithMock(agent, "_call_agent")

        b64_path = base64.b64encode('path')
        b64_contents = base64.b64encode('contents')
        agent._call_agent('inject_file',
                          {'b64_contents': b64_contents,
                           'b64_path': b64_path})

        self.mox.ReplayAll()

        agent.inject_file("path", "contents")
Esempio n. 5
0
    def test_inject_file(self):
        instance = _get_fake_instance()
        agent = self._create_agent(instance)
        self.mox.StubOutWithMock(agent, "_call_agent")

        b64_path = base64.b64encode('path')
        b64_contents = base64.b64encode('contents')
        agent._call_agent('inject_file',
                          {'b64_contents': b64_contents,
                           'b64_path': b64_path})

        self.mox.ReplayAll()

        agent.inject_file("path", "contents")
Esempio n. 6
0
    def test_call_agent_success(self, mock_uuid):
        session = mock.Mock()
        instance = {"uuid": "fake"}
        addl_args = {"foo": "bar"}

        session.VM.get_domid.return_value = '42'
        mock_uuid.return_value = 1
        session.call_plugin.return_value = {
            'returncode': '4',
            'message': "asdf\\r\\n"
        }

        self.assertEqual(
            "asdf",
            agent._call_agent(session,
                              instance,
                              "vm_ref",
                              "method",
                              addl_args,
                              timeout=300,
                              success_codes=['0', '4']))

        expected_args = {
            'id': '1',
            'dom_id': '42',
            'timeout': '300',
        }
        expected_args.update(addl_args)
        session.call_plugin.assert_called_once_with("agent.py", "method",
                                                    expected_args)
        session.VM.get_domid.assert_called_once_with("vm_ref")
Esempio n. 7
0
    def test_call_agent_success(self, mock_uuid):
        session = mock.Mock()
        instance = {"uuid": "fake"}
        addl_args = {"foo": "bar"}

        session.VM.get_domid.return_value = '42'
        mock_uuid.return_value = 1
        mock_method = mock.Mock().method()
        mock_method.return_value = {'returncode': '4', 'message': "asdf\\r\\n"}
        mock_method.__name__ = "mock_method"

        self.assertEqual(
            "asdf",
            agent._call_agent(session,
                              instance,
                              "vm_ref",
                              mock_method,
                              addl_args,
                              timeout=300,
                              success_codes=['0', '4']))

        expected_args = {}
        expected_args.update(addl_args)
        mock_method.assert_called_once_with(session, 1, '42', 300,
                                            **expected_args)
        session.VM.get_domid.assert_called_once_with("vm_ref")
Esempio n. 8
0
    def _call_agent_setup(self,
                          session,
                          mock_uuid,
                          mock_method,
                          returncode='0',
                          success_codes=None,
                          exception=None):
        session.XenAPI.Failure = XenAPI.Failure
        instance = {"uuid": "fake"}
        addl_args = {"foo": "bar"}

        session.VM.get_domid.return_value = "42"
        mock_uuid.return_value = 1
        if exception:
            mock_method.side_effect = exception
        else:
            mock_method.return_value = {
                'returncode': returncode,
                'message': "asdf\\r\\n"
            }

        return agent._call_agent(session,
                                 instance,
                                 "vm_ref",
                                 mock_method,
                                 addl_args,
                                 success_codes=success_codes)
Esempio n. 9
0
 def test_retry_on_reboot(self, mock_wait):
     mock_session = mock.Mock()
     mock_session.VM.get_domid.return_value = "fake_dom_id"
     agent = self._create_agent(None, mock_session)
     mock_method = mock.Mock().method()
     mock_method.side_effect = [XenAPI.Failure(["REBOOT: fake"]),
                                {"returncode": '0', "message": "done"}]
     result = agent._call_agent(mock_method)
     self.assertEqual("done", result)
     self.assertTrue(mock_session.VM.get_domid.called)
     self.assertEqual(2, mock_method.call_count)
     mock_wait.assert_called_once_with(mock_session, self.vm_ref,
                                       "fake_dom_id", mock_method)
Esempio n. 10
0
 def test_retry_on_reboot(self, mock_wait):
     mock_session = mock.Mock()
     mock_session.VM.get_domid.return_value = "fake_dom_id"
     agent = self._create_agent(None, mock_session)
     mock_method = mock.Mock().method()
     mock_method.side_effect = [XenAPI.Failure(["REBOOT: fake"]),
                                {"returncode": '0', "message": "done"}]
     result = agent._call_agent(mock_method)
     self.assertEqual("done", result)
     self.assertTrue(mock_session.VM.get_domid.called)
     self.assertEqual(2, mock_method.call_count)
     mock_wait.assert_called_once_with(mock_session, self.vm_ref,
                                       "fake_dom_id", mock_method)
Esempio n. 11
0
    def _call_agent_setup(self, session, mock_uuid,
                          returncode='0', success_codes=None,
                          exception=None):
        session.XenAPI.Failure = xenapi_fake.Failure
        instance = {"uuid": "fake"}

        session.call_xenapi.return_value = {'domid': '42'}
        mock_uuid.return_value = 1
        if exception:
            session.call_plugin.side_effect = exception
        else:
            session.call_plugin.return_value = {'returncode': returncode,
                                                'message': "asdf\\r\\n"}

        return agent._call_agent(session, instance, "vm_ref", "method",
                                 success_codes=success_codes)
Esempio n. 12
0
    def _call_agent_setup(self, session, mock_uuid,
                          returncode='0', success_codes=None,
                          exception=None):
        session.XenAPI.Failure = xenapi_fake.Failure
        instance = {"uuid": "fake"}

        session.VM.get_domid.return_value = 42
        mock_uuid.return_value = 1
        if exception:
            session.call_plugin.side_effect = exception
        else:
            session.call_plugin.return_value = {'returncode': returncode,
                                                'message': "asdf\\r\\n"}

        return agent._call_agent(session, instance, "vm_ref", "method",
                                 success_codes=success_codes)
Esempio n. 13
0
    def _call_agent_setup(self, session, mock_uuid, mock_method,
                          returncode='0', success_codes=None,
                          exception=None):
        session.XenAPI.Failure = XenAPI.Failure
        instance = {"uuid": "fake"}
        addl_args = {"foo": "bar"}

        session.VM.get_domid.return_value = "42"
        mock_uuid.return_value = 1
        if exception:
            mock_method.side_effect = exception
        else:
            mock_method.return_value = {'returncode': returncode,
                                        'message': "asdf\\r\\n"}

        return agent._call_agent(session, instance, "vm_ref", mock_method,
                                 addl_args, success_codes=success_codes)
Esempio n. 14
0
    def test_call_agent_success(self, mock_uuid):
        session = mock.Mock()
        instance = {"uuid": "fake"}
        addl_args = {"foo": "bar"}

        session.VM.get_domid.return_value = '42'
        mock_uuid.return_value = 1
        mock_method = mock.Mock().method()
        mock_method.return_value = {'returncode': '4', 'message': "asdf\\r\\n"}
        mock_method.__name__ = "mock_method"

        self.assertEqual("asdf",
                         agent._call_agent(session, instance, "vm_ref",
                                           mock_method, addl_args, timeout=300,
                                           success_codes=['0', '4']))

        expected_args = {}
        expected_args.update(addl_args)
        mock_method.assert_called_once_with(session, 1, '42', 300,
                                            **expected_args)
        session.VM.get_domid.assert_called_once_with("vm_ref")
Esempio n. 15
0
    def test_retry_on_reboot(self, mock_wait):
        mock_session = mock.Mock()

        def fake_call_plugin(*args, **kwargs):
            if fake_call_plugin.called:
                return {"returncode": '0', "message": "done"}
            else:
                fake_call_plugin.called = True
                raise FakeRebootException()

        fake_call_plugin.called = False
        mock_session.XenAPI.Failure = FakeRebootException
        mock_session.VM.get_domid.return_value = "fake_dom_id"
        mock_session.call_plugin.side_effect = fake_call_plugin

        agent = self._create_agent(None, mock_session)

        result = agent._call_agent("asdf")
        self.assertEqual("done", result)
        self.assertTrue(mock_session.VM.get_domid.called)
        self.assertEqual(2, mock_session.call_plugin.call_count)
        mock_wait.called_once_with(mock_session, self.vm_ref,
                                   "fake_dom_id", "asdf")
Esempio n. 16
0
    def test_retry_on_reboot(self, mock_wait):
        mock_session = mock.Mock()

        def fake_call_plugin(*args, **kwargs):
            if fake_call_plugin.called:
                return {"returncode": '0', "message": "done"}
            else:
                fake_call_plugin.called = True
                raise FakeRebootException()

        fake_call_plugin.called = False
        mock_session.XenAPI.Failure = FakeRebootException
        mock_session.VM.get_domid.return_value = "fake_dom_id"
        mock_session.call_plugin.side_effect = fake_call_plugin

        agent = self._create_agent(None, mock_session)

        result = agent._call_agent("asdf")
        self.assertEqual("done", result)
        self.assertTrue(mock_session.VM.get_domid.called)
        self.assertEqual(2, mock_session.call_plugin.call_count)
        mock_wait.assert_called_once_with(mock_session, self.vm_ref,
                                          "fake_dom_id", "asdf")
Esempio n. 17
0
    def test_call_agent_success(self, mock_uuid):
        session = mock.Mock()
        instance = {"uuid": "fake"}
        addl_args = {"foo": "bar"}

        session.VM.get_domid.return_value = '42'
        mock_uuid.return_value = 1
        session.call_plugin.return_value = {'returncode': '4',
                                            'message': "asdf\\r\\n"}

        self.assertEqual("asdf",
                         agent._call_agent(session, instance, "vm_ref",
                                           "method", addl_args, timeout=300,
                                           success_codes=['0', '4']))

        expected_args = {
            'id': '1',
            'dom_id': '42',
            'timeout': '300',
        }
        expected_args.update(addl_args)
        session.VM.get_domid.assert_called_once_with("vm_ref")
        session.call_plugin.assert_called_once_with("agent", "method",
                                                    expected_args)