test_cloud.py 9.0 KB

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