test_cloud.py 8.9 KB

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