test_smartos_imgadm.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  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_imgadm as imgadm
  10. # Mock Results
  11. MOCK_CLEAN_STATE = {'first_run': True, 'images': []}
  12. MOCK_IMAGE_NONE = {}
  13. MOCK_IMAGE_ONE = {
  14. '00000000-0000-0000-0000-000000000001': {
  15. 'description': 'Example Image 1',
  16. 'name': 'example-1',
  17. 'os': 'smartos',
  18. 'published': '2018-01-01T00:42:00Z',
  19. 'source': 'https://images.joyent.com',
  20. 'version': '18.1.0',
  21. },
  22. }
  23. MOCK_IMAGE_TWO = {
  24. '00000000-0000-0000-0000-000000000001': {
  25. 'description': 'Example Image 1',
  26. 'name': 'example-1',
  27. 'os': 'smartos',
  28. 'published': '2018-01-01T00:42:00Z',
  29. 'source': 'https://images.joyent.com',
  30. 'version': '18.1.0',
  31. },
  32. '00000000-0000-0000-0000-000000000002': {
  33. 'description': 'Example Image 2',
  34. 'name': 'example-2',
  35. 'os': 'smartos',
  36. 'published': '2018-01-01T00:42:00Z',
  37. 'source': 'https://images.joyent.com',
  38. 'version': '18.2.0',
  39. },
  40. }
  41. @skipIf(NO_MOCK, NO_MOCK_REASON)
  42. class SmartOSImgAdmBeaconTestCase(TestCase, LoaderModuleMockMixin):
  43. '''
  44. Test case for salt.beacons.imgadm
  45. '''
  46. def setup_loader_modules(self):
  47. return {
  48. imgadm: {
  49. '__context__': {},
  50. '__salt__': {},
  51. }
  52. }
  53. def test_non_list_config(self):
  54. '''
  55. We only have minimal validation so we test that here
  56. '''
  57. assert imgadm.validate({}) == (False, 'Configuration for imgadm beacon must be a list!')
  58. def test_imported_startup(self):
  59. '''
  60. Test with one image and startup_import_event
  61. '''
  62. # NOTE: this should yield 1 imported event
  63. with patch.dict(imgadm.IMGADM_STATE, MOCK_CLEAN_STATE), \
  64. patch.dict(imgadm.__salt__, {'imgadm.list': MagicMock(return_value=MOCK_IMAGE_ONE)}):
  65. config = [{'startup_import_event': True}]
  66. assert imgadm.validate(config) == (True, 'Valid beacon configuration')
  67. ret = imgadm.beacon(config)
  68. res = [{'description': 'Example Image 1',
  69. 'name': 'example-1',
  70. 'os': 'smartos',
  71. 'published': '2018-01-01T00:42:00Z',
  72. 'source': 'https://images.joyent.com',
  73. 'tag': 'imported/00000000-0000-0000-0000-000000000001',
  74. 'version': '18.1.0'}]
  75. assert ret == res
  76. def test_imported_nostartup(self):
  77. '''
  78. Test with one image and startup_import_event unset/false
  79. '''
  80. # NOTE: this should yield 0 imported event
  81. with patch.dict(imgadm.IMGADM_STATE, MOCK_CLEAN_STATE), \
  82. patch.dict(imgadm.__salt__, {'imgadm.list': MagicMock(return_value=MOCK_IMAGE_ONE)}):
  83. config = []
  84. assert imgadm.validate(config) == (True, 'Valid beacon configuration')
  85. assert imgadm.beacon(config) == []
  86. def test_imported(self):
  87. '''
  88. Test with one image and a new image added on the 2nd pass
  89. '''
  90. # NOTE: this should yield 1 imported event
  91. with patch.dict(imgadm.IMGADM_STATE, MOCK_CLEAN_STATE), \
  92. patch.dict(imgadm.__salt__, {'imgadm.list': MagicMock(side_effect=[MOCK_IMAGE_ONE, MOCK_IMAGE_TWO])}):
  93. config = []
  94. assert imgadm.validate(config) == (True, 'Valid beacon configuration')
  95. # Initial pass (Initialized state and do not yield imported images at startup)
  96. imgadm.beacon(config)
  97. # Second pass (After importing a new image)
  98. ret = imgadm.beacon(config)
  99. res = [{'description': 'Example Image 2',
  100. 'name': 'example-2',
  101. 'os': 'smartos',
  102. 'published': '2018-01-01T00:42:00Z',
  103. 'source': 'https://images.joyent.com',
  104. 'tag': 'imported/00000000-0000-0000-0000-000000000002',
  105. 'version': '18.2.0'}]
  106. assert ret == res
  107. def test_deleted(self):
  108. '''
  109. Test with two images and one gets deletes
  110. '''
  111. # NOTE: this should yield 1 deleted event
  112. with patch.dict(imgadm.IMGADM_STATE, MOCK_CLEAN_STATE), \
  113. patch.dict(imgadm.__salt__, {'imgadm.list': MagicMock(side_effect=[MOCK_IMAGE_TWO, MOCK_IMAGE_ONE])}):
  114. config = []
  115. assert imgadm.validate(config) == (True, 'Valid beacon configuration')
  116. # Initial pass (Initialized state and do not yield imported images at startup)
  117. imgadm.beacon(config)
  118. # Second pass (After deleting one image)
  119. ret = imgadm.beacon(config)
  120. res = [{'description': 'Example Image 2',
  121. 'name': 'example-2',
  122. 'os': 'smartos',
  123. 'published': '2018-01-01T00:42:00Z',
  124. 'source': 'https://images.joyent.com',
  125. 'tag': 'deleted/00000000-0000-0000-0000-000000000002',
  126. 'version': '18.2.0'}]
  127. assert ret == res
  128. def test_complex(self):
  129. '''
  130. Test with one image, delete both, import 2
  131. '''
  132. # NOTE: this should yield 1 delete and 2 import events
  133. with patch.dict(imgadm.IMGADM_STATE, MOCK_CLEAN_STATE), \
  134. patch.dict(imgadm.__salt__, {'imgadm.list': MagicMock(side_effect=[MOCK_IMAGE_ONE, MOCK_IMAGE_NONE, MOCK_IMAGE_TWO])}):
  135. config = []
  136. assert imgadm.validate(config), (True, 'Valid beacon configuration')
  137. # Initial pass (Initialized state and do not yield imported images at startup)
  138. imgadm.beacon(config)
  139. # Second pass (After deleting one image)
  140. ret = imgadm.beacon(config)
  141. res = [{'description': 'Example Image 1',
  142. 'name': 'example-1',
  143. 'os': 'smartos',
  144. 'published': '2018-01-01T00:42:00Z',
  145. 'source': 'https://images.joyent.com',
  146. 'tag': 'deleted/00000000-0000-0000-0000-000000000001',
  147. 'version': '18.1.0'}]
  148. assert ret == res
  149. # Third pass (After importing two images)
  150. ret = imgadm.beacon(config)
  151. res = [{'description': 'Example Image 1',
  152. 'name': 'example-1',
  153. 'os': 'smartos',
  154. 'published': '2018-01-01T00:42:00Z',
  155. 'source': 'https://images.joyent.com',
  156. 'tag': 'imported/00000000-0000-0000-0000-000000000001',
  157. 'version': '18.1.0'},
  158. {'description': 'Example Image 2',
  159. 'name': 'example-2',
  160. 'os': 'smartos',
  161. 'published': '2018-01-01T00:42:00Z',
  162. 'source': 'https://images.joyent.com',
  163. 'tag': 'imported/00000000-0000-0000-0000-000000000002',
  164. 'version': '18.2.0'}]
  165. assert len(ret) == 2
  166. for item in ret:
  167. assert item in res