1
0

test_smartos_vmadm.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. # Salt testing libs
  5. from tests.support.unit import skipIf, TestCase
  6. from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch, MagicMock
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. # Salt libs
  9. import salt.beacons.smartos_vmadm as vmadm
  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. @skipIf(NO_MOCK, NO_MOCK_REASON)
  50. class SmartOSImgAdmBeaconTestCase(TestCase, LoaderModuleMockMixin):
  51. '''
  52. Test case for salt.beacons.vmadm
  53. '''
  54. def setup_loader_modules(self):
  55. return {
  56. vmadm: {
  57. '__context__': {},
  58. '__salt__': {},
  59. }
  60. }
  61. def test_non_list_config(self):
  62. '''
  63. We only have minimal validation so we test that here
  64. '''
  65. config = {}
  66. ret = vmadm.validate(config)
  67. self.assertEqual(ret, (False, 'Configuration for vmadm beacon must be a list!'))
  68. def test_created_startup(self):
  69. '''
  70. Test with one vm and startup_create_event
  71. '''
  72. # NOTE: this should yield 1 created event + one state event
  73. with patch.dict(vmadm.VMADM_STATE, MOCK_CLEAN_STATE), \
  74. patch.dict(vmadm.__salt__, {'vmadm.list': MagicMock(return_value=MOCK_VM_ONE)}):
  75. config = [{'startup_create_event': True}]
  76. ret = vmadm.validate(config)
  77. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  78. ret = vmadm.beacon(config)
  79. res = [{'alias': 'vm1',
  80. 'tag': 'created/00000000-0000-0000-0000-000000000001',
  81. 'hostname': 'vm1',
  82. 'dns_domain': 'example.org'},
  83. {'alias': 'vm1',
  84. 'tag': 'running/00000000-0000-0000-0000-000000000001',
  85. 'hostname': 'vm1',
  86. 'dns_domain': 'example.org'}]
  87. self.assertEqual(ret, res)
  88. def test_created_nostartup(self):
  89. '''
  90. Test with one image and startup_import_event unset/false
  91. '''
  92. # NOTE: this should yield 0 created event _ one state event
  93. with patch.dict(vmadm.VMADM_STATE, MOCK_CLEAN_STATE), \
  94. patch.dict(vmadm.__salt__, {'vmadm.list': MagicMock(return_value=MOCK_VM_ONE)}):
  95. config = []
  96. ret = vmadm.validate(config)
  97. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  98. ret = vmadm.beacon(config)
  99. res = [{'alias': 'vm1',
  100. 'tag': 'running/00000000-0000-0000-0000-000000000001',
  101. 'hostname': 'vm1',
  102. 'dns_domain': 'example.org'}]
  103. self.assertEqual(ret, res)
  104. def test_created(self):
  105. '''
  106. Test with one vm, create a 2nd one
  107. '''
  108. # NOTE: this should yield 1 created event + state event
  109. with patch.dict(vmadm.VMADM_STATE, MOCK_CLEAN_STATE), \
  110. patch.dict(vmadm.__salt__, {'vmadm.list': MagicMock(side_effect=[MOCK_VM_ONE, MOCK_VM_TWO_STARTED])}):
  111. config = []
  112. ret = vmadm.validate(config)
  113. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  114. # Initial pass (Initialized state and do not yield created events at startup)
  115. ret = vmadm.beacon(config)
  116. # Second pass (After create a new vm)
  117. ret = vmadm.beacon(config)
  118. res = [{'alias': 'vm2',
  119. 'tag': 'created/00000000-0000-0000-0000-000000000002',
  120. 'hostname': 'vm2',
  121. 'dns_domain': 'example.org'},
  122. {'alias': 'vm2',
  123. 'tag': 'running/00000000-0000-0000-0000-000000000002',
  124. 'hostname': 'vm2',
  125. 'dns_domain': 'example.org'}]
  126. self.assertEqual(ret, res)
  127. def test_deleted(self):
  128. '''
  129. Test with two vms and one gets destroyed
  130. '''
  131. # NOTE: this should yield 1 destroyed event
  132. with patch.dict(vmadm.VMADM_STATE, MOCK_CLEAN_STATE), \
  133. patch.dict(vmadm.__salt__, {'vmadm.list': MagicMock(side_effect=[MOCK_VM_TWO_STOPPED, MOCK_VM_ONE])}):
  134. config = []
  135. ret = vmadm.validate(config)
  136. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  137. # Initial pass (Initialized state and do not yield created vms at startup)
  138. ret = vmadm.beacon(config)
  139. # Second pass (Destroying one vm)
  140. ret = vmadm.beacon(config)
  141. res = [{'alias': 'vm2',
  142. 'tag': 'deleted/00000000-0000-0000-0000-000000000002',
  143. 'hostname': 'vm2',
  144. 'dns_domain': 'example.org'}]
  145. self.assertEqual(ret, res)
  146. def test_complex(self):
  147. '''
  148. Test with two vms, stop one, delete one
  149. '''
  150. # NOTE: this should yield 1 delete and 2 import events
  151. with patch.dict(vmadm.VMADM_STATE, MOCK_CLEAN_STATE), \
  152. patch.dict(vmadm.__salt__, {'vmadm.list': MagicMock(side_effect=[MOCK_VM_TWO_STARTED, MOCK_VM_TWO_STOPPED, MOCK_VM_ONE])}):
  153. config = []
  154. ret = vmadm.validate(config)
  155. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  156. # Initial pass (Initialized state and do not yield created events at startup)
  157. ret = vmadm.beacon(config)
  158. # Second pass (Stop one vm)
  159. ret = vmadm.beacon(config)
  160. res = [{'alias': 'vm2',
  161. 'tag': 'stopped/00000000-0000-0000-0000-000000000002',
  162. 'hostname': 'vm2',
  163. 'dns_domain': 'example.org'}]
  164. self.assertEqual(ret, res)
  165. # Third pass (Delete one vm)
  166. ret = vmadm.beacon(config)
  167. res = [{'alias': 'vm2',
  168. 'tag': 'deleted/00000000-0000-0000-0000-000000000002',
  169. 'hostname': 'vm2',
  170. 'dns_domain': 'example.org'}]
  171. self.assertEqual(ret, res)