test_cloud.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Eric Radman <ericshane@eradman.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import
  7. # Import Salt Testing libs
  8. from tests.support.unit import skipIf, TestCase
  9. from tests.support.mock import (
  10. MagicMock,
  11. patch,
  12. NO_MOCK,
  13. NO_MOCK_REASON,
  14. )
  15. # Import Salt libs
  16. import salt.cloud
  17. EXAMPLE_PROVIDERS = {
  18. 'nyc_vcenter': {'vmware': {'driver': 'vmware',
  19. 'password': '123456',
  20. 'url': 'vca1.saltstack.com',
  21. 'minion': {
  22. 'master': 'providermaster',
  23. 'grains': {
  24. 'providergrain': True
  25. }
  26. },
  27. 'profiles': {},
  28. 'user': 'root'}},
  29. 'nj_vcenter': {'vmware': {'driver': 'vmware',
  30. 'password': '333',
  31. 'profiles': {},
  32. 'minion': {
  33. 'master': 'providermaster',
  34. 'grains': {
  35. 'providergrain': True
  36. }
  37. },
  38. 'image': 'rhel6_64prod',
  39. 'url': 'vca2.saltstack.com',
  40. 'user': 'root'}}
  41. }
  42. EXAMPLE_PROFILES = {
  43. 'nyc-vm': {'cluster': 'nycvirt',
  44. 'datastore': 'datastore1',
  45. 'devices': {'disk': {'Hard disk 1': {'controller': 'SCSI controller 1',
  46. 'size': 20}},
  47. 'network': {'Network Adapter 1': {'mac': '88:88:88:88:88:42',
  48. 'name': 'vlan50',
  49. 'switch_type': 'standard'}},
  50. 'scsi': {'SCSI controller 1': {'type': 'paravirtual'}}},
  51. 'extra_config': {'mem.hotadd': 'yes'},
  52. 'folder': 'coreinfra',
  53. 'image': 'rhel6_64Guest',
  54. 'minion': {
  55. 'master': 'profilemaster',
  56. 'grains': {
  57. 'profilegrain': True
  58. }
  59. },
  60. 'memory': '8GB',
  61. 'num_cpus': 2,
  62. 'power_on': True,
  63. 'profile': 'nyc-vm',
  64. 'provider': 'nyc_vcenter:vmware',
  65. 'resourcepool': 'Resources'},
  66. 'nj-vm': {'cluster': 'njvirt',
  67. 'folder': 'coreinfra',
  68. 'image': 'rhel6_64Guest',
  69. 'memory': '8GB',
  70. 'num_cpus': 2,
  71. 'power_on': True,
  72. 'profile': 'nj-vm',
  73. 'provider': 'nj_vcenter:vmware',
  74. 'resourcepool': 'Resources'},
  75. }
  76. EXAMPLE_MAP = {
  77. 'nyc-vm': {'db1': {'cpus': 4,
  78. 'devices': {'disk': {'Hard disk 1': {'size': 40}},
  79. 'network': {'Network Adapter 1': {'mac': '22:4a:b2:92:b3:eb'}}},
  80. 'memory': '16GB',
  81. 'minion': {
  82. 'master': 'mapmaster',
  83. 'grains': {
  84. 'mapgrain': True
  85. }
  86. },
  87. 'name': 'db1'},
  88. 'db2': {'name': 'db2',
  89. 'password': '456',
  90. 'provider': 'nj_vcenter:vmware'}},
  91. 'nj-vm': {'db3': {'name': 'db3',
  92. 'password': '789',
  93. }}
  94. }
  95. @skipIf(NO_MOCK, NO_MOCK_REASON)
  96. class MapConfTest(TestCase):
  97. '''
  98. Validate evaluation of salt-cloud map configuration
  99. '''
  100. def test_cloud_map_merge_conf(self):
  101. '''
  102. Ensure that nested values can be selectivly overridden in a map file
  103. '''
  104. with patch('salt.config.check_driver_dependencies', MagicMock(return_value=True)), \
  105. patch('salt.cloud.Map.read', MagicMock(return_value=EXAMPLE_MAP)):
  106. self.maxDiff = None
  107. opts = {'extension_modules': '/var/cache/salt/master/extmods',
  108. 'optimization_order': [0, 1, 2],
  109. 'providers': EXAMPLE_PROVIDERS, 'profiles': EXAMPLE_PROFILES}
  110. cloud_map = salt.cloud.Map(opts)
  111. merged_profile = {
  112. 'create': {'db1': {'cluster': 'nycvirt',
  113. 'cpus': 4,
  114. 'datastore': 'datastore1',
  115. 'devices': {'disk': {'Hard disk 1': {'controller': 'SCSI controller 1',
  116. 'size': 40}},
  117. 'network': {'Network Adapter 1': {'mac': '22:4a:b2:92:b3:eb',
  118. 'name': 'vlan50',
  119. 'switch_type': 'standard'}},
  120. 'scsi': {'SCSI controller 1': {'type': 'paravirtual'}}},
  121. 'driver': 'vmware',
  122. 'extra_config': {'mem.hotadd': 'yes'},
  123. 'folder': 'coreinfra',
  124. 'image': 'rhel6_64Guest',
  125. 'memory': '16GB',
  126. 'minion': {'grains': {'mapgrain': True,
  127. 'profilegrain': True,
  128. 'providergrain': True},
  129. 'master': 'mapmaster'},
  130. 'name': 'db1',
  131. 'num_cpus': 2,
  132. 'password': '123456',
  133. 'power_on': True,
  134. 'profile': 'nyc-vm',
  135. 'provider': 'nyc_vcenter:vmware',
  136. 'resourcepool': 'Resources',
  137. 'url': 'vca1.saltstack.com',
  138. 'user': 'root'},
  139. 'db2': {'cluster': 'nycvirt',
  140. 'datastore': 'datastore1',
  141. 'devices': {'disk': {'Hard disk 1': {'controller': 'SCSI controller 1',
  142. 'size': 20}},
  143. 'network': {'Network Adapter 1': {'mac': '88:88:88:88:88:42',
  144. 'name': 'vlan50',
  145. 'switch_type': 'standard'}},
  146. 'scsi': {'SCSI controller 1': {'type': 'paravirtual'}}},
  147. 'driver': 'vmware',
  148. 'extra_config': {'mem.hotadd': 'yes'},
  149. 'folder': 'coreinfra',
  150. 'image': 'rhel6_64Guest',
  151. 'memory': '8GB',
  152. 'minion': {'grains': {'profilegrain': True,
  153. 'providergrain': True},
  154. 'master': 'profilemaster'},
  155. 'name': 'db2',
  156. 'num_cpus': 2,
  157. 'password': '456',
  158. 'power_on': True,
  159. 'profile': 'nyc-vm',
  160. 'provider': 'nj_vcenter:vmware',
  161. 'resourcepool': 'Resources',
  162. 'url': 'vca2.saltstack.com',
  163. 'user': 'root'},
  164. 'db3': {'cluster': 'njvirt',
  165. 'driver': 'vmware',
  166. 'folder': 'coreinfra',
  167. 'image': 'rhel6_64Guest',
  168. 'memory': '8GB',
  169. 'minion': {'grains': {'providergrain': True},
  170. 'master': 'providermaster'},
  171. 'name': 'db3',
  172. 'num_cpus': 2,
  173. 'password': '789',
  174. 'power_on': True,
  175. 'profile': 'nj-vm',
  176. 'provider': 'nj_vcenter:vmware',
  177. 'resourcepool': 'Resources',
  178. 'url': 'vca2.saltstack.com',
  179. 'user': 'root'}}
  180. }
  181. # what we assert above w.r.t db2 using nj_vcenter:vmware provider:
  182. # - url is from the overriden nj_vcenter provider, not nyc_vcenter
  183. # - image from provider is still overridden by the nyc-vm profile
  184. # - password from map override is still overriding both the provider and profile password
  185. #
  186. # what we assert above about grain handling ( and provider/profile/map data in general )
  187. # - provider grains are able to be overridden by profile data
  188. # - provider grain sare overridden by map data
  189. # - profile data is overriden by map data
  190. # ie, the provider->profile->map inheritance works as expected
  191. map_data = cloud_map.map_data()
  192. self.assertEqual(map_data, merged_profile)