1
0

test_cloud.py 9.0 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 os
  8. # Import Salt Testing libs
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import MagicMock, patch
  11. from tests.support.runtests import RUNTIME_VARS
  12. # Import Salt libs
  13. import salt.cloud
  14. import salt.config
  15. EXAMPLE_PROVIDERS = {
  16. 'nyc_vcenter': {'vmware': {'driver': 'vmware',
  17. 'password': '123456',
  18. 'url': 'vca1.saltstack.com',
  19. 'minion': {
  20. 'master': 'providermaster',
  21. 'grains': {
  22. 'providergrain': True
  23. }
  24. },
  25. 'profiles': {},
  26. 'user': 'root'}},
  27. 'nj_vcenter': {'vmware': {'driver': 'vmware',
  28. 'password': '333',
  29. 'profiles': {},
  30. 'minion': {
  31. 'master': 'providermaster',
  32. 'grains': {
  33. 'providergrain': True
  34. }
  35. },
  36. 'image': 'rhel6_64prod',
  37. 'url': 'vca2.saltstack.com',
  38. 'user': 'root'}}
  39. }
  40. EXAMPLE_PROFILES = {
  41. 'nyc-vm': {'cluster': 'nycvirt',
  42. 'datastore': 'datastore1',
  43. 'devices': {'disk': {'Hard disk 1': {'controller': 'SCSI controller 1',
  44. 'size': 20}},
  45. 'network': {'Network Adapter 1': {'mac': '88:88:88:88:88:42',
  46. 'name': 'vlan50',
  47. 'switch_type': 'standard'}},
  48. 'scsi': {'SCSI controller 1': {'type': 'paravirtual'}}},
  49. 'extra_config': {'mem.hotadd': 'yes'},
  50. 'folder': 'coreinfra',
  51. 'image': 'rhel6_64Guest',
  52. 'minion': {
  53. 'master': 'profilemaster',
  54. 'grains': {
  55. 'profilegrain': True
  56. }
  57. },
  58. 'memory': '8GB',
  59. 'num_cpus': 2,
  60. 'power_on': True,
  61. 'profile': 'nyc-vm',
  62. 'provider': 'nyc_vcenter:vmware',
  63. 'resourcepool': 'Resources'},
  64. 'nj-vm': {'cluster': 'njvirt',
  65. 'folder': 'coreinfra',
  66. 'image': 'rhel6_64Guest',
  67. 'memory': '8GB',
  68. 'num_cpus': 2,
  69. 'power_on': True,
  70. 'profile': 'nj-vm',
  71. 'provider': 'nj_vcenter:vmware',
  72. 'resourcepool': 'Resources'},
  73. }
  74. EXAMPLE_MAP = {
  75. 'nyc-vm': {'db1': {'cpus': 4,
  76. 'devices': {'disk': {'Hard disk 1': {'size': 40}},
  77. 'network': {'Network Adapter 1': {'mac': '22:4a:b2:92:b3:eb'}}},
  78. 'memory': '16GB',
  79. 'minion': {
  80. 'master': 'mapmaster',
  81. 'grains': {
  82. 'mapgrain': True
  83. }
  84. },
  85. 'name': 'db1'},
  86. 'db2': {'name': 'db2',
  87. 'password': '456',
  88. 'provider': 'nj_vcenter:vmware'}},
  89. 'nj-vm': {'db3': {'name': 'db3',
  90. 'password': '789',
  91. }}
  92. }
  93. class MapConfTest(TestCase):
  94. '''
  95. Validate evaluation of salt-cloud map configuration
  96. '''
  97. def test_cloud_map_merge_conf(self):
  98. '''
  99. Ensure that nested values can be selectivly overridden in a map file
  100. '''
  101. with patch('salt.config.check_driver_dependencies', MagicMock(return_value=True)), \
  102. patch('salt.cloud.Map.read', MagicMock(return_value=EXAMPLE_MAP)):
  103. self.maxDiff = None
  104. opts = salt.config.cloud_config(os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'cloud'))
  105. opts.update({
  106. 'optimization_order': [0, 1, 2],
  107. 'providers': EXAMPLE_PROVIDERS,
  108. 'profiles': EXAMPLE_PROFILES
  109. })
  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)