Adjusted tests for esxdatacenter proxy

This commit is contained in:
Alexandru Bleotu 2017-09-10 16:37:09 -04:00
parent 7f08983288
commit 0e7b8e0c92

View File

@ -38,7 +38,7 @@ class InitTestCase(TestCase, LoaderModuleMockMixin):
def setup_loader_modules(self):
return {esxdatacenter: {'__virtual__':
MagicMock(return_value='esxdatacenter'),
'DETAILS': {}}}
'DETAILS': {}, '__pillar__': {}}}
def setUp(self):
self.opts_userpass = {'proxy': {'proxytype': 'esxdatacenter',
@ -57,6 +57,22 @@ class InitTestCase(TestCase, LoaderModuleMockMixin):
'principal': 'fake_principal',
'protocol': 'fake_protocol',
'port': 100}}
patches = (('salt.proxy.esxdatacenter.merge',
MagicMock(return_value=self.opts_sspi['proxy'])),)
for mod, mock in patches:
patcher = patch(mod, mock)
patcher.start()
self.addCleanup(patcher.stop)
def test_merge(self):
mock_pillar_proxy = MagicMock()
mock_opts_proxy = MagicMock()
mock_merge = MagicMock(return_value=self.opts_sspi['proxy'])
with patch.dict(esxdatacenter.__pillar__,
{'proxy': mock_pillar_proxy}):
with patch('salt.proxy.esxdatacenter.merge', mock_merge):
esxdatacenter.init(opts={'proxy': mock_opts_proxy})
mock_merge.assert_called_once_with(mock_opts_proxy, mock_pillar_proxy)
def test_esxdatacenter_schema(self):
mock_json_validate = MagicMock()
@ -80,9 +96,11 @@ class InitTestCase(TestCase, LoaderModuleMockMixin):
def test_no_username(self):
opts = self.opts_userpass.copy()
del opts['proxy']['username']
with self.assertRaises(salt.exceptions.InvalidConfigError) as \
excinfo:
esxdatacenter.init(opts)
with patch('salt.proxy.esxdatacenter.merge',
MagicMock(return_value=opts['proxy'])):
with self.assertRaises(salt.exceptions.InvalidConfigError) as \
excinfo:
esxdatacenter.init(opts)
self.assertEqual(excinfo.exception.strerror,
'Mechanism is set to \'userpass\', but no '
'\'username\' key found in proxy config.')
@ -90,9 +108,11 @@ class InitTestCase(TestCase, LoaderModuleMockMixin):
def test_no_passwords(self):
opts = self.opts_userpass.copy()
del opts['proxy']['passwords']
with self.assertRaises(salt.exceptions.InvalidConfigError) as \
excinfo:
esxdatacenter.init(opts)
with patch('salt.proxy.esxdatacenter.merge',
MagicMock(return_value=opts['proxy'])):
with self.assertRaises(salt.exceptions.InvalidConfigError) as \
excinfo:
esxdatacenter.init(opts)
self.assertEqual(excinfo.exception.strerror,
'Mechanism is set to \'userpass\', but no '
'\'passwords\' key found in proxy config.')
@ -100,9 +120,11 @@ class InitTestCase(TestCase, LoaderModuleMockMixin):
def test_no_domain(self):
opts = self.opts_sspi.copy()
del opts['proxy']['domain']
with self.assertRaises(salt.exceptions.InvalidConfigError) as \
excinfo:
esxdatacenter.init(opts)
with patch('salt.proxy.esxdatacenter.merge',
MagicMock(return_value=opts['proxy'])):
with self.assertRaises(salt.exceptions.InvalidConfigError) as \
excinfo:
esxdatacenter.init(opts)
self.assertEqual(excinfo.exception.strerror,
'Mechanism is set to \'sspi\', but no '
'\'domain\' key found in proxy config.')
@ -110,9 +132,11 @@ class InitTestCase(TestCase, LoaderModuleMockMixin):
def test_no_principal(self):
opts = self.opts_sspi.copy()
del opts['proxy']['principal']
with self.assertRaises(salt.exceptions.InvalidConfigError) as \
excinfo:
esxdatacenter.init(opts)
with patch('salt.proxy.esxdatacenter.merge',
MagicMock(return_value=opts['proxy'])):
with self.assertRaises(salt.exceptions.InvalidConfigError) as \
excinfo:
esxdatacenter.init(opts)
self.assertEqual(excinfo.exception.strerror,
'Mechanism is set to \'sspi\', but no '
'\'principal\' key found in proxy config.')
@ -120,17 +144,21 @@ class InitTestCase(TestCase, LoaderModuleMockMixin):
def test_find_credentials(self):
mock_find_credentials = MagicMock(return_value=('fake_username',
'fake_password'))
with patch('salt.proxy.esxdatacenter.find_credentials',
mock_find_credentials):
esxdatacenter.init(self.opts_userpass)
with patch('salt.proxy.esxdatacenter.merge',
MagicMock(return_value=self.opts_userpass['proxy'])):
with patch('salt.proxy.esxdatacenter.find_credentials',
mock_find_credentials):
esxdatacenter.init(self.opts_userpass)
mock_find_credentials.assert_called_once_with()
def test_details_userpass(self):
mock_find_credentials = MagicMock(return_value=('fake_username',
'fake_password'))
with patch('salt.proxy.esxdatacenter.find_credentials',
mock_find_credentials):
esxdatacenter.init(self.opts_userpass)
with patch('salt.proxy.esxdatacenter.merge',
MagicMock(return_value=self.opts_userpass['proxy'])):
with patch('salt.proxy.esxdatacenter.find_credentials',
mock_find_credentials):
esxdatacenter.init(self.opts_userpass)
self.assertDictEqual(esxdatacenter.DETAILS,
{'vcenter': 'fake_vcenter',
'datacenter': 'fake_dc',