test_dimensiondata.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: `Anthony Shaw <anthonyshaw@apache.org>`
  4. tests.unit.cloud.clouds.dimensiondata_test
  5. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. '''
  7. # Import Python libs
  8. from __future__ import absolute_import, print_function, unicode_literals
  9. try:
  10. import libcloud.security
  11. HAS_LIBCLOUD = True
  12. except ImportError:
  13. HAS_LIBCLOUD = False
  14. # Import Salt Libs
  15. from salt.cloud.clouds import dimensiondata
  16. from salt.exceptions import SaltCloudSystemExit
  17. from salt.utils.versions import LooseVersion
  18. # Import Salt Testing Libs
  19. from tests.support.mixins import LoaderModuleMockMixin
  20. from tests.support.unit import TestCase, skipIf
  21. from tests.support.mock import MagicMock, patch, __version__ as mock_version
  22. from tests.unit.cloud.clouds import _preferred_ip
  23. VM_NAME = 'winterfell'
  24. # Use certifi if installed
  25. try:
  26. if HAS_LIBCLOUD:
  27. # This work-around for Issue #32743 is no longer needed for libcloud >=
  28. # 1.4.0. However, older versions of libcloud must still be supported
  29. # with this work-around. This work-around can be removed when the
  30. # required minimum version of libcloud is 2.0.0 (See PR #40837 - which
  31. # is implemented in Salt 2018.3.0).
  32. if LooseVersion(libcloud.__version__) < LooseVersion('1.4.0'):
  33. import certifi
  34. libcloud.security.CA_CERTS_PATH.append(certifi.where())
  35. except (ImportError, NameError):
  36. pass
  37. class ExtendedTestCase(TestCase):
  38. '''
  39. Extended TestCase class containing additional helper methods.
  40. '''
  41. def assertRaisesWithMessage(self, exc_type, exc_msg, func, *args, **kwargs):
  42. try:
  43. func(*args, **kwargs)
  44. self.assertFail()
  45. except Exception as exc:
  46. self.assertEqual(type(exc), exc_type)
  47. self.assertEqual(exc.message, exc_msg)
  48. class DimensionDataTestCase(ExtendedTestCase, LoaderModuleMockMixin):
  49. '''
  50. Unit TestCase for salt.cloud.clouds.dimensiondata module.
  51. '''
  52. def setup_loader_modules(self):
  53. return {
  54. dimensiondata: {
  55. '__virtual__': MagicMock(return_value='dimensiondata'),
  56. '__active_provider_name__': '',
  57. '__opts__': {
  58. 'providers': {
  59. 'my-dimensiondata-cloud': {
  60. 'dimensiondata': {
  61. 'driver': 'dimensiondata',
  62. 'region': 'dd-au',
  63. 'user_id': 'jon_snow',
  64. 'key': 'IKnowNothing'
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71. def test_avail_images_call(self):
  72. '''
  73. Tests that a SaltCloudSystemExit is raised when trying to call avail_images
  74. with --action or -a.
  75. '''
  76. self.assertRaises(
  77. SaltCloudSystemExit,
  78. dimensiondata.avail_images,
  79. call='action'
  80. )
  81. def test_avail_locations_call(self):
  82. '''
  83. Tests that a SaltCloudSystemExit is raised when trying to call avail_locations
  84. with --action or -a.
  85. '''
  86. self.assertRaises(
  87. SaltCloudSystemExit,
  88. dimensiondata.avail_locations,
  89. call='action'
  90. )
  91. def test_avail_sizes_call(self):
  92. '''
  93. Tests that a SaltCloudSystemExit is raised when trying to call avail_sizes
  94. with --action or -a.
  95. '''
  96. self.assertRaises(
  97. SaltCloudSystemExit,
  98. dimensiondata.avail_sizes,
  99. call='action'
  100. )
  101. def test_list_nodes_call(self):
  102. '''
  103. Tests that a SaltCloudSystemExit is raised when trying to call list_nodes
  104. with --action or -a.
  105. '''
  106. self.assertRaises(
  107. SaltCloudSystemExit,
  108. dimensiondata.list_nodes,
  109. call='action'
  110. )
  111. def test_destroy_call(self):
  112. '''
  113. Tests that a SaltCloudSystemExit is raised when trying to call destroy
  114. with --function or -f.
  115. '''
  116. self.assertRaises(
  117. SaltCloudSystemExit,
  118. dimensiondata.destroy,
  119. name=VM_NAME,
  120. call='function'
  121. )
  122. @skipIf(HAS_LIBCLOUD is False, "Install 'libcloud' to be able to run this unit test.")
  123. def test_avail_sizes(self):
  124. '''
  125. Tests that avail_sizes returns an empty dictionary.
  126. '''
  127. sizes = dimensiondata.avail_sizes(call='foo')
  128. self.assertEqual(
  129. len(sizes),
  130. 1
  131. )
  132. self.assertEqual(
  133. sizes['default']['name'],
  134. 'default'
  135. )
  136. def test_import(self):
  137. """
  138. Test that the module picks up installed deps
  139. """
  140. with patch('salt.config.check_driver_dependencies', return_value=True) as p:
  141. get_deps = dimensiondata.get_dependencies()
  142. self.assertEqual(get_deps, True)
  143. if LooseVersion(mock_version) >= LooseVersion('2.0.0'):
  144. self.assertTrue(p.call_count >= 1)
  145. def test_provider_matches(self):
  146. """
  147. Test that the first configured instance of a dimensiondata driver is matched
  148. """
  149. p = dimensiondata.get_configured_provider()
  150. self.assertNotEqual(p, None)
  151. def test_query_node_data_filter_preferred_ip_addresses(self):
  152. '''
  153. Test if query node data is filtering out unpreferred IP addresses.
  154. '''
  155. zero_ip = '0.0.0.0'
  156. private_ips = [zero_ip, '1.1.1.1', '2.2.2.2']
  157. vm = {'name': None}
  158. data = MagicMock()
  159. data.public_ips = []
  160. dimensiondata.NodeState = MagicMock() # pylint: disable=blacklisted-unmocked-patching
  161. dimensiondata.NodeState.RUNNING = True
  162. with patch('salt.cloud.clouds.dimensiondata.show_instance',
  163. MagicMock(return_value={'state': True,
  164. 'name': 'foo',
  165. 'public_ips': [],
  166. 'private_ips': private_ips})):
  167. with patch('salt.cloud.clouds.dimensiondata.preferred_ip',
  168. _preferred_ip(private_ips, [zero_ip])):
  169. with patch('salt.cloud.clouds.dimensiondata.ssh_interface',
  170. MagicMock(return_value='private_ips')):
  171. self.assertEqual(dimensiondata._query_node_data(vm, data).public_ips, [zero_ip])