Exemplo n.º 1
0
 def test_get_session_cookie_with_no_cookie(self):
     svc_obj = service.Service()
     cookie = mock.Mock()
     cookie.name = 'cookie'
     cookie.value = 'xyz'
     svc_obj.client.options.transport.cookiejar = [cookie]
     self.assertIsNone(svc_obj.get_http_cookie())
Exemplo n.º 2
0
    def test_request_handler_with_pbm_session_error(self, fault_name):
        managed_object = 'ProfileManager'
        doc = mock.Mock()

        def side_effect(mo, **kwargs):
            self.assertEqual(managed_object, vim_util.get_moref_type(mo))
            self.assertEqual(managed_object, vim_util.get_moref_value(mo))
            fault = mock.Mock(faultstring="MyFault")

            fault_children = mock.Mock()
            fault_children.name = "name"
            fault_children.getText.return_value = "value"
            child = mock.Mock()
            child.get.return_value = fault_name
            child.getChildren.return_value = [fault_children]
            detail = mock.Mock()
            detail.getChildren.return_value = [child]
            doc.childAtPath.return_value = detail
            raise suds.WebFault(fault, doc)

        svc_obj = service.Service()
        service_mock = svc_obj.client.service
        setattr(service_mock, 'get_profile_id_by_name', side_effect)

        ex = self.assertRaises(exceptions.VimFaultException,
                               svc_obj.get_profile_id_by_name, managed_object)

        self.assertEqual([exceptions.NOT_AUTHENTICATED], ex.fault_list)
        self.assertEqual({'name': 'value'}, ex.details)
        self.assertEqual("MyFault", ex.msg)
        doc.childAtPath.assert_called_once_with('/detail')
Exemplo n.º 3
0
    def test_request_handler_with_web_fault(self):
        managed_object = 'VirtualMachine'
        fault_list = ['Fault']

        doc = mock.Mock()

        def side_effect(mo, **kwargs):
            self.assertEqual(managed_object, mo._type)
            self.assertEqual(managed_object, mo.value)
            fault = mock.Mock(faultstring="MyFault")

            fault_children = mock.Mock()
            fault_children.name = "name"
            fault_children.getText.return_value = "value"
            child = mock.Mock()
            child.get.return_value = fault_list[0]
            child.getChildren.return_value = [fault_children]
            detail = mock.Mock()
            detail.getChildren.return_value = [child]
            doc.childAtPath.return_value = detail
            raise suds.WebFault(fault, doc)

        svc_obj = service.Service()
        service_mock = svc_obj.client.service
        setattr(service_mock, 'powerOn', side_effect)

        ex = self.assertRaises(exceptions.VimFaultException, svc_obj.powerOn,
                               managed_object)

        self.assertEqual(fault_list, ex.fault_list)
        self.assertEqual({'name': 'value'}, ex.details)
        self.assertEqual("MyFault", ex.msg)
        doc.childAtPath.assertCalledOnceWith('/detail')
Exemplo n.º 4
0
 def test_request_handler_with_attribute_error(self):
     managed_object = 'VirtualMachine'
     svc_obj = service.Service()
     # no powerOn method in Service
     service_mock = mock.Mock(spec=service.Service)
     svc_obj.client.service = service_mock
     self.assertRaises(exceptions.VimAttributeException, svc_obj.powerOn,
                       managed_object)
Exemplo n.º 5
0
 def test_get_session_cookie(self):
     svc_obj = service.Service()
     cookie_value = 'xyz'
     cookie = mock.Mock()
     cookie.name = 'vmware_soap_session'
     cookie.value = cookie_value
     svc_obj.client.options.transport.cookiejar = [cookie]
     self.assertEqual(cookie_value, svc_obj.get_http_cookie())
Exemplo n.º 6
0
    def test_soap_headers_pbm(self):
        def fake_set_options(*args, **kwargs):
            headers = kwargs['soapheaders']
            self.assertEqual(2, len(headers))
            self.assertEqual('vc-session-cookie', headers[0].getText())
            self.assertEqual('fira-12345', headers[1].getText())

        svc_obj = service.Service()
        svc_obj._vc_session_cookie = 'vc-session-cookie'
        setattr(svc_obj.client, 'set_options', fake_set_options)
        svc_obj._set_soap_headers('fira-12345')
Exemplo n.º 7
0
    def test_set_soap_headers(self):
        def fake_set_options(*args, **kwargs):
            headers = kwargs['soapheaders']
            self.assertEqual(1, len(headers))
            txt = headers[0].getText()
            self.assertEqual('fira-12345', txt)

        svc_obj = service.Service()
        svc_obj.client.options.soapheaders = None
        setattr(svc_obj.client, 'set_options', fake_set_options)
        svc_obj._set_soap_headers('fira-12345')
Exemplo n.º 8
0
    def _test_request_handler_with_exception(self, message, exception):
        managed_object = 'VirtualMachine'

        def side_effect(mo, **kwargs):
            self.assertEqual(managed_object, mo._type)
            self.assertEqual(managed_object, mo.value)
            raise Exception(message)

        svc_obj = service.Service()
        attr_name = 'powerOn'
        service_mock = svc_obj.client.service
        setattr(service_mock, attr_name, side_effect)
        self.assertRaises(exception, svc_obj.powerOn, managed_object)
Exemplo n.º 9
0
    def test_request_handler_with_http_error(self):
        managed_object = 'VirtualMachine'

        def side_effect(mo, **kwargs):
            self.assertEqual(managed_object, mo._type)
            self.assertEqual(managed_object, mo.value)
            raise requests.HTTPError()

        svc_obj = service.Service()
        attr_name = 'powerOn'
        service_mock = svc_obj.client.service
        setattr(service_mock, attr_name, side_effect)
        self.assertRaises(exceptions.VimConnectionException, svc_obj.powerOn,
                          managed_object)
Exemplo n.º 10
0
    def test_request_handler_with_empty_web_fault_doc(self):
        def side_effect(mo, **kwargs):
            fault = mock.Mock(faultstring="MyFault")
            raise suds.WebFault(fault, None)

        svc_obj = service.Service()
        service_mock = svc_obj.client.service
        setattr(service_mock, 'powerOn', side_effect)

        ex = self.assertRaises(exceptions.VimFaultException, svc_obj.powerOn,
                               'VirtualMachine')
        self.assertEqual([], ex.fault_list)
        self.assertEqual({}, ex.details)
        self.assertEqual("MyFault", ex.msg)
Exemplo n.º 11
0
    def test_request_handler_with_connection_error(self):
        managed_object = 'VirtualMachine'

        def side_effect(mo, **kwargs):
            self.assertEqual(managed_object, vim_util.get_moref_type(mo))
            self.assertEqual(managed_object, vim_util.get_moref_value(mo))
            raise requests.ConnectionError()

        svc_obj = service.Service()
        attr_name = 'powerOn'
        service_mock = svc_obj.client.service
        setattr(service_mock, attr_name, side_effect)
        self.assertRaises(exceptions.VimConnectionException, svc_obj.powerOn,
                          managed_object)
Exemplo n.º 12
0
    def test_request_handler_with_http_response_not_ready_error(self):
        managed_object = 'VirtualMachine'

        def side_effect(mo, **kwargs):
            self.assertEqual(managed_object, vim_util.get_moref_type(mo))
            self.assertEqual(managed_object, vim_util.get_moref_value(mo))
            raise httplib.ResponseNotReady()

        svc_obj = service.Service()
        attr_name = 'powerOn'
        service_mock = svc_obj.client.service
        setattr(service_mock, attr_name, side_effect)
        self.assertRaises(exceptions.VimSessionOverLoadException,
                          svc_obj.powerOn, managed_object)
Exemplo n.º 13
0
    def test_request_handler_with_http_cannot_send_header_error(self):
        managed_object = 'VirtualMachine'

        def side_effect(mo, **kwargs):
            self.assertEqual(managed_object, mo._type)
            self.assertEqual(managed_object, mo.value)
            raise httplib.CannotSendHeader()

        svc_obj = service.Service()
        attr_name = 'powerOn'
        service_mock = svc_obj.client.service
        setattr(service_mock, attr_name, side_effect)
        self.assertRaises(exceptions.VimSessionOverLoadException,
                          svc_obj.powerOn, managed_object)
Exemplo n.º 14
0
    def test_request_handler_with_retrieve_properties_ex_fault(self):
        managed_object = 'Datacenter'

        def side_effect(mo, **kwargs):
            self.assertEqual(managed_object, mo._type)
            self.assertEqual(managed_object, mo.value)
            return None

        svc_obj = service.Service()
        attr_name = 'retrievePropertiesEx'
        service_mock = svc_obj.client.service
        setattr(service_mock, attr_name, side_effect)
        self.assertRaises(exceptions.VimFaultException,
                          svc_obj.retrievePropertiesEx, managed_object)
Exemplo n.º 15
0
    def test_request_handler(self):
        managed_object = 'VirtualMachine'
        resp = mock.Mock()

        def side_effect(mo, **kwargs):
            self.assertEqual(managed_object, mo._type)
            self.assertEqual(managed_object, mo.value)
            return resp

        svc_obj = service.Service()
        attr_name = 'powerOn'
        service_mock = svc_obj.client.service
        setattr(service_mock, attr_name, side_effect)
        ret = svc_obj.powerOn(managed_object)
        self.assertEqual(resp, ret)
Exemplo n.º 16
0
    def test_request_handler_with_vc51_web_fault(self):
        managed_object = 'VirtualMachine'
        fault_list = ['Fault']

        doc = mock.Mock()

        def side_effect(mo, **kwargs):
            self.assertEqual(managed_object, vim_util.get_moref_type(mo))
            self.assertEqual(managed_object, vim_util.get_moref_value(mo))
            fault = mock.Mock(faultstring="MyFault")

            fault_children = mock.Mock()
            fault_children.name = "name"
            fault_children.getText.return_value = "value"
            child = mock.Mock()
            child.get.return_value = fault_list[0]
            child.getChildren.return_value = [fault_children]
            detail = mock.Mock()
            detail.getChildren.return_value = [child]
            doc.childAtPath.side_effect = [None, detail]
            raise suds.WebFault(fault, doc)

        svc_obj = service.Service()
        service_mock = svc_obj.client.service
        setattr(service_mock, 'powerOn', side_effect)

        ex = self.assertRaises(exceptions.VimFaultException, svc_obj.powerOn,
                               managed_object)

        self.assertEqual(fault_list, ex.fault_list)
        self.assertEqual({'name': 'value'}, ex.details)
        self.assertEqual("MyFault", ex.msg)
        exp_calls = [
            mock.call('/detail'),
            mock.call('/Envelope/Body/Fault/detail')
        ]
        self.assertEqual(exp_calls, doc.childAtPath.call_args_list)
Exemplo n.º 17
0
 def test_shared_cache(self, mock_reader):
     cache1 = service.Service().client.options.cache
     cache2 = service.Service().client.options.cache
     self.assertIs(cache1, cache2)
Exemplo n.º 18
0
 def test_request_handler_no_value(self, mock_moref):
     managed_object = 'VirtualMachine'
     svc_obj = service.Service()
     ret = svc_obj.UnregisterVM(managed_object)
     self.assertIsNone(ret)