test_cloud.py 9.0 KB

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