1
0

test_smartos_vmadm.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. # Salt libs
  5. import salt.beacons.smartos_vmadm as vmadm
  6. from tests.support.mixins import LoaderModuleMockMixin
  7. from tests.support.mock import MagicMock, patch
  8. # Salt testing libs
  9. from tests.support.unit import TestCase
  10. # Mock Results
  11. MOCK_CLEAN_STATE = {"first_run": True, "vms": []}
  12. MOCK_VM_NONE = {}
  13. MOCK_VM_ONE = {
  14. "00000000-0000-0000-0000-000000000001": {
  15. "state": "running",
  16. "alias": "vm1",
  17. "hostname": "vm1",
  18. "dns_domain": "example.org",
  19. },
  20. }
  21. MOCK_VM_TWO_STOPPED = {
  22. "00000000-0000-0000-0000-000000000001": {
  23. "state": "running",
  24. "alias": "vm1",
  25. "hostname": "vm1",
  26. "dns_domain": "example.org",
  27. },
  28. "00000000-0000-0000-0000-000000000002": {
  29. "state": "stopped",
  30. "alias": "vm2",
  31. "hostname": "vm2",
  32. "dns_domain": "example.org",
  33. },
  34. }
  35. MOCK_VM_TWO_STARTED = {
  36. "00000000-0000-0000-0000-000000000001": {
  37. "state": "running",
  38. "alias": "vm1",
  39. "hostname": "vm1",
  40. "dns_domain": "example.org",
  41. },
  42. "00000000-0000-0000-0000-000000000002": {
  43. "state": "running",
  44. "alias": "vm2",
  45. "hostname": "vm2",
  46. "dns_domain": "example.org",
  47. },
  48. }
  49. class SmartOSImgAdmBeaconTestCase(TestCase, LoaderModuleMockMixin):
  50. """
  51. Test case for salt.beacons.vmadm
  52. """
  53. def setup_loader_modules(self):
  54. return {vmadm: {"__context__": {}, "__salt__": {}}}
  55. def test_non_list_config(self):
  56. """
  57. We only have minimal validation so we test that here
  58. """
  59. config = {}
  60. ret = vmadm.validate(config)
  61. self.assertEqual(ret, (False, "Configuration for vmadm beacon must be a list!"))
  62. def test_created_startup(self):
  63. """
  64. Test with one vm and startup_create_event
  65. """
  66. # NOTE: this should yield 1 created event + one state event
  67. with patch.dict(vmadm.VMADM_STATE, MOCK_CLEAN_STATE), patch.dict(
  68. vmadm.__salt__, {"vmadm.list": MagicMock(return_value=MOCK_VM_ONE)}
  69. ):
  70. config = [{"startup_create_event": True}]
  71. ret = vmadm.validate(config)
  72. self.assertEqual(ret, (True, "Valid beacon configuration"))
  73. ret = vmadm.beacon(config)
  74. res = [
  75. {
  76. "alias": "vm1",
  77. "tag": "created/00000000-0000-0000-0000-000000000001",
  78. "hostname": "vm1",
  79. "dns_domain": "example.org",
  80. },
  81. {
  82. "alias": "vm1",
  83. "tag": "running/00000000-0000-0000-0000-000000000001",
  84. "hostname": "vm1",
  85. "dns_domain": "example.org",
  86. },
  87. ]
  88. self.assertEqual(ret, res)
  89. def test_created_nostartup(self):
  90. """
  91. Test with one image and startup_import_event unset/false
  92. """
  93. # NOTE: this should yield 0 created event _ one state event
  94. with patch.dict(vmadm.VMADM_STATE, MOCK_CLEAN_STATE), patch.dict(
  95. vmadm.__salt__, {"vmadm.list": MagicMock(return_value=MOCK_VM_ONE)}
  96. ):
  97. config = []
  98. ret = vmadm.validate(config)
  99. self.assertEqual(ret, (True, "Valid beacon configuration"))
  100. ret = vmadm.beacon(config)
  101. res = [
  102. {
  103. "alias": "vm1",
  104. "tag": "running/00000000-0000-0000-0000-000000000001",
  105. "hostname": "vm1",
  106. "dns_domain": "example.org",
  107. }
  108. ]
  109. self.assertEqual(ret, res)
  110. def test_created(self):
  111. """
  112. Test with one vm, create a 2nd one
  113. """
  114. # NOTE: this should yield 1 created event + state event
  115. with patch.dict(vmadm.VMADM_STATE, MOCK_CLEAN_STATE), patch.dict(
  116. vmadm.__salt__,
  117. {"vmadm.list": MagicMock(side_effect=[MOCK_VM_ONE, MOCK_VM_TWO_STARTED])},
  118. ):
  119. config = []
  120. ret = vmadm.validate(config)
  121. self.assertEqual(ret, (True, "Valid beacon configuration"))
  122. # Initial pass (Initialized state and do not yield created events at startup)
  123. ret = vmadm.beacon(config)
  124. # Second pass (After create a new vm)
  125. ret = vmadm.beacon(config)
  126. res = [
  127. {
  128. "alias": "vm2",
  129. "tag": "created/00000000-0000-0000-0000-000000000002",
  130. "hostname": "vm2",
  131. "dns_domain": "example.org",
  132. },
  133. {
  134. "alias": "vm2",
  135. "tag": "running/00000000-0000-0000-0000-000000000002",
  136. "hostname": "vm2",
  137. "dns_domain": "example.org",
  138. },
  139. ]
  140. self.assertEqual(ret, res)
  141. def test_deleted(self):
  142. """
  143. Test with two vms and one gets destroyed
  144. """
  145. # NOTE: this should yield 1 destroyed event
  146. with patch.dict(vmadm.VMADM_STATE, MOCK_CLEAN_STATE), patch.dict(
  147. vmadm.__salt__,
  148. {"vmadm.list": MagicMock(side_effect=[MOCK_VM_TWO_STOPPED, MOCK_VM_ONE])},
  149. ):
  150. config = []
  151. ret = vmadm.validate(config)
  152. self.assertEqual(ret, (True, "Valid beacon configuration"))
  153. # Initial pass (Initialized state and do not yield created vms at startup)
  154. ret = vmadm.beacon(config)
  155. # Second pass (Destroying one vm)
  156. ret = vmadm.beacon(config)
  157. res = [
  158. {
  159. "alias": "vm2",
  160. "tag": "deleted/00000000-0000-0000-0000-000000000002",
  161. "hostname": "vm2",
  162. "dns_domain": "example.org",
  163. }
  164. ]
  165. self.assertEqual(ret, res)
  166. def test_complex(self):
  167. """
  168. Test with two vms, stop one, delete one
  169. """
  170. # NOTE: this should yield 1 delete and 2 import events
  171. with patch.dict(vmadm.VMADM_STATE, MOCK_CLEAN_STATE), patch.dict(
  172. vmadm.__salt__,
  173. {
  174. "vmadm.list": MagicMock(
  175. side_effect=[MOCK_VM_TWO_STARTED, MOCK_VM_TWO_STOPPED, MOCK_VM_ONE]
  176. )
  177. },
  178. ):
  179. config = []
  180. ret = vmadm.validate(config)
  181. self.assertEqual(ret, (True, "Valid beacon configuration"))
  182. # Initial pass (Initialized state and do not yield created events at startup)
  183. ret = vmadm.beacon(config)
  184. # Second pass (Stop one vm)
  185. ret = vmadm.beacon(config)
  186. res = [
  187. {
  188. "alias": "vm2",
  189. "tag": "stopped/00000000-0000-0000-0000-000000000002",
  190. "hostname": "vm2",
  191. "dns_domain": "example.org",
  192. }
  193. ]
  194. self.assertEqual(ret, res)
  195. # Third pass (Delete one vm)
  196. ret = vmadm.beacon(config)
  197. res = [
  198. {
  199. "alias": "vm2",
  200. "tag": "deleted/00000000-0000-0000-0000-000000000002",
  201. "hostname": "vm2",
  202. "dns_domain": "example.org",
  203. }
  204. ]
  205. self.assertEqual(ret, res)