test_cloud.py 9.1 KB

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