1
# Copyright (c) 2014 VMware, Inc.
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5
# not use this file except in compliance with the License. You may obtain
6
# a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
# License for the specific language governing permissions and limitations
17
Unit tests for classes to invoke VMware VI SOAP calls.
21
from oslo_i18n import fixture as i18n_fixture
23
from oslo.vmware import exceptions
24
from oslo.vmware import vim
25
from oslo_vmware._i18n import _
26
from tests import base
29
class VimTest(base.TestCase):
30
"""Test class for Vim."""
33
super(VimTest, self).setUp()
34
patcher = mock.patch('suds.client.Client')
35
self.addCleanup(patcher.stop)
36
self.SudsClientMock = patcher.start()
37
self.useFixture(i18n_fixture.ToggleLazy(True))
39
@mock.patch.object(vim.Vim, '__getattr__', autospec=True)
40
def test_service_content(self, getattr_mock):
41
getattr_ret = mock.Mock()
42
getattr_mock.side_effect = lambda *args: getattr_ret
44
vim_obj.service_content
45
getattr_mock.assert_called_once_with(vim_obj, 'RetrieveServiceContent')
46
getattr_ret.assert_called_once_with('ServiceInstance')
47
self.assertEqual(self.SudsClientMock.return_value, vim_obj.client)
48
self.assertEqual(getattr_ret.return_value, vim_obj.service_content)
50
def test_exception_summary_exception_as_list(self):
51
# assert that if a list is fed to the VimException object
53
self.assertRaises(ValueError,
54
exceptions.VimException,
55
[], ValueError('foo'))
57
def test_exception_summary_string(self):
58
e = exceptions.VimException(_("string"), ValueError("foo"))
60
self.assertEqual("string\nCause: foo", string)
62
def test_vim_fault_exception_string(self):
63
self.assertRaises(ValueError,
64
exceptions.VimFaultException,
65
"bad", ValueError("argument"))
67
def test_vim_fault_exception(self):
68
vfe = exceptions.VimFaultException([ValueError("example")], _("cause"))
70
self.assertEqual("cause\nFaults: [ValueError('example',)]", string)
72
def test_vim_fault_exception_with_cause_and_details(self):
73
vfe = exceptions.VimFaultException([ValueError("example")],
78
self.assertEqual("MyMessage\n"
80
"Faults: [ValueError('example',)]\n"
81
"Details: {'foo': 'bar'}",
84
def test_configure_non_default_host_port(self):
85
vim_obj = vim.Vim('https', 'www.test.com', 12345)
86
self.assertEqual('https://www.test.com:12345/sdk/vimService.wsdl',
88
self.assertEqual('https://www.test.com:12345/sdk',
91
def test_configure_ipv6(self):
92
vim_obj = vim.Vim('https', '::1')
93
self.assertEqual('https://[::1]/sdk/vimService.wsdl',
95
self.assertEqual('https://[::1]/sdk',
98
def test_configure_ipv6_and_non_default_host_port(self):
99
vim_obj = vim.Vim('https', '::1', 12345)
100
self.assertEqual('https://[::1]:12345/sdk/vimService.wsdl',
102
self.assertEqual('https://[::1]:12345/sdk',
105
def test_configure_with_wsdl_url_override(self):
106
vim_obj = vim.Vim('https', 'www.example.com',
107
wsdl_url='https://test.com/sdk/vimService.wsdl')
108
self.assertEqual('https://test.com/sdk/vimService.wsdl',
110
self.assertEqual('https://www.example.com/sdk', vim_obj.soap_url)