1
0

test_smartos_vmadm.py 6.8 KB

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