1
0

test_smartos_imgadm.py 7.2 KB

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