test_pkgrepo.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. # -*- coding: utf-8 -*-
  2. '''
  3. tests for pkgrepo states
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import os
  8. # Import Salt Testing libs
  9. from tests.support.case import ModuleCase
  10. from tests.support.mixins import SaltReturnAssertsMixin
  11. from tests.support.unit import skipIf
  12. from tests.support.helpers import (
  13. destructiveTest,
  14. requires_system_grains,
  15. )
  16. # Import Salt libs
  17. import salt.utils.platform
  18. # Import 3rd-party libs
  19. from salt.ext import six
  20. @destructiveTest
  21. @skipIf(salt.utils.platform.is_windows(), 'minion is windows')
  22. class PkgrepoTest(ModuleCase, SaltReturnAssertsMixin):
  23. '''
  24. pkgrepo state tests
  25. '''
  26. @requires_system_grains
  27. def test_pkgrepo_01_managed(self, grains):
  28. '''
  29. Test adding a repo
  30. '''
  31. os_grain = self.run_function('grains.item', ['os'])['os']
  32. os_release_info = tuple(self.run_function('grains.item', ['osrelease_info'])['osrelease_info'])
  33. if os_grain == 'Ubuntu' and os_release_info >= (15, 10):
  34. self.skipTest(
  35. 'The PPA used for this test does not exist for Ubuntu Wily'
  36. ' (15.10) and later.'
  37. )
  38. if grains['os_family'] == 'Debian':
  39. try:
  40. from aptsources import sourceslist
  41. except ImportError:
  42. self.skipTest(
  43. 'aptsources.sourceslist python module not found'
  44. )
  45. ret = self.run_function('state.sls', mods='pkgrepo.managed', timeout=120)
  46. # If the below assert fails then no states were run, and the SLS in
  47. # tests/integration/files/file/base/pkgrepo/managed.sls needs to be
  48. # corrected.
  49. self.assertReturnNonEmptySaltType(ret)
  50. for state_id, state_result in six.iteritems(ret):
  51. self.assertSaltTrueReturn(dict([(state_id, state_result)]))
  52. def test_pkgrepo_02_absent(self):
  53. '''
  54. Test removing the repo from the above test
  55. '''
  56. os_grain = self.run_function('grains.item', ['os'])['os']
  57. os_release_info = tuple(self.run_function('grains.item', ['osrelease_info'])['osrelease_info'])
  58. if os_grain == 'Ubuntu' and os_release_info >= (15, 10):
  59. self.skipTest(
  60. 'The PPA used for this test does not exist for Ubuntu Wily'
  61. ' (15.10) and later.'
  62. )
  63. ret = self.run_function('state.sls', mods='pkgrepo.absent', timeout=120)
  64. # If the below assert fails then no states were run, and the SLS in
  65. # tests/integration/files/file/base/pkgrepo/absent.sls needs to be
  66. # corrected.
  67. self.assertReturnNonEmptySaltType(ret)
  68. for state_id, state_result in six.iteritems(ret):
  69. self.assertSaltTrueReturn(dict([(state_id, state_result)]))
  70. @requires_system_grains
  71. def test_pkgrepo_03_with_comments(self, grains):
  72. '''
  73. Test adding a repo with comments
  74. '''
  75. os_family = grains['os_family'].lower()
  76. if os_family in ('redhat',):
  77. kwargs = {
  78. 'name': 'examplerepo',
  79. 'baseurl': 'http://example.com/repo',
  80. 'enabled': False,
  81. 'comments': ['This is a comment']
  82. }
  83. elif os_family in ('debian',):
  84. self.skipTest('Debian/Ubuntu test case needed')
  85. else:
  86. self.skipTest("No test case for os_family '{0}'".format(os_family))
  87. try:
  88. # Run the state to add the repo
  89. ret = self.run_state('pkgrepo.managed', **kwargs)
  90. self.assertSaltTrueReturn(ret)
  91. # Run again with modified comments
  92. kwargs['comments'].append('This is another comment')
  93. ret = self.run_state('pkgrepo.managed', **kwargs)
  94. self.assertSaltTrueReturn(ret)
  95. ret = ret[next(iter(ret))]
  96. self.assertEqual(
  97. ret['changes'],
  98. {
  99. 'comments': {
  100. 'old': ['This is a comment'],
  101. 'new': ['This is a comment',
  102. 'This is another comment']
  103. }
  104. }
  105. )
  106. # Run a third time, no changes should be made
  107. ret = self.run_state('pkgrepo.managed', **kwargs)
  108. self.assertSaltTrueReturn(ret)
  109. ret = ret[next(iter(ret))]
  110. self.assertFalse(ret['changes'])
  111. self.assertEqual(
  112. ret['comment'],
  113. "Package repo '{0}' already configured".format(kwargs['name'])
  114. )
  115. finally:
  116. # Clean up
  117. self.run_state('pkgrepo.absent', name=kwargs['name'])
  118. @requires_system_grains
  119. def test_pkgrepo_04_apt_with_architectures(self, grains):
  120. '''
  121. Test managing a repo with architectures specified
  122. '''
  123. if grains['os_family'].lower() != 'debian':
  124. self.skipTest('APT-only test')
  125. name = 'deb {{arch}}http://foo.com/bar/latest {oscodename} main'.format(oscodename=grains['oscodename'])
  126. def _get_arch(arch):
  127. return '[arch={0}] '.format(arch) if arch else ''
  128. def _run(arch='', test=False):
  129. ret = self.run_state(
  130. 'pkgrepo.managed',
  131. name=name.format(arch=_get_arch(arch)),
  132. file=fn_,
  133. refresh=False,
  134. test=test)
  135. return ret[next(iter(ret))]
  136. fn_ = salt.utils.files.mkstemp(dir='/etc/apt/sources.list.d', suffix='.list')
  137. try:
  138. # Run with test=True
  139. ret = _run(test=True)
  140. assert ret['changes'] == {'repo': name.format(arch='')}, ret['changes']
  141. assert 'would be' in ret['comment'], ret['comment']
  142. assert ret['result'] is None, ret['result']
  143. # Run for real
  144. ret = _run()
  145. assert ret['changes'] == {'repo': name.format(arch='')}, ret['changes']
  146. assert ret['comment'].startswith('Configured'), ret['comment']
  147. assert ret['result'] is True, ret['result']
  148. # Run again with test=True, should exit with no changes and a True
  149. # result.
  150. ret = _run(test=True)
  151. assert not ret['changes'], ret['changes']
  152. assert 'already' in ret['comment'], ret['comment']
  153. assert ret['result'] is True, ret['result']
  154. # Run for real again, results should be the same as above (i.e. we
  155. # should never get to the point where we exit with a None result).
  156. ret = _run()
  157. assert not ret['changes'], ret['changes']
  158. assert 'already' in ret['comment'], ret['comment']
  159. assert ret['result'] is True, ret['result']
  160. expected_changes = {
  161. 'line': {
  162. 'new': name.format(arch=_get_arch('amd64')),
  163. 'old': name.format(arch=''),
  164. },
  165. 'architectures': {
  166. 'new': ['amd64'],
  167. 'old': [],
  168. },
  169. }
  170. # Run with test=True and the architecture set. We should get a None
  171. # result with some expected changes.
  172. ret = _run(arch='amd64', test=True)
  173. assert ret['changes'] == expected_changes, ret['changes']
  174. assert 'would be' in ret['comment'], ret['comment']
  175. assert ret['result'] is None, ret['result']
  176. # Run for real, with the architecture set. We should get a True
  177. # result with the same changes.
  178. ret = _run(arch='amd64')
  179. assert ret['changes'] == expected_changes, ret['changes']
  180. assert ret['comment'].startswith('Configured'), ret['comment']
  181. assert ret['result'] is True, ret['result']
  182. # Run again with test=True, should exit with no changes and a True
  183. # result.
  184. ret = _run(arch='amd64', test=True)
  185. assert not ret['changes'], ret['changes']
  186. assert 'already' in ret['comment'], ret['comment']
  187. assert ret['result'] is True, ret['result']
  188. # Run for real again, results should be the same as above (i.e. we
  189. # should never get to the point where we exit with a None result).
  190. ret = _run(arch='amd64')
  191. assert not ret['changes'], ret['changes']
  192. assert 'already' in ret['comment'], ret['comment']
  193. assert ret['result'] is True, ret['result']
  194. expected_changes = {
  195. 'line': {
  196. 'new': name.format(arch=''),
  197. 'old': name.format(arch=_get_arch('amd64')),
  198. },
  199. 'architectures': {
  200. 'new': [],
  201. 'old': ['amd64'],
  202. },
  203. }
  204. # Run with test=True and the architecture set back to the original
  205. # value. We should get a None result with some expected changes.
  206. ret = _run(test=True)
  207. assert ret['changes'] == expected_changes, ret['changes']
  208. assert 'would be' in ret['comment'], ret['comment']
  209. assert ret['result'] is None, ret['result']
  210. # Run for real, with the architecture set. We should get a True
  211. # result with the same changes.
  212. ret = _run()
  213. assert ret['changes'] == expected_changes, ret['changes']
  214. assert ret['comment'].startswith('Configured'), ret['comment']
  215. assert ret['result'] is True, ret['result']
  216. # Run again with test=True, should exit with no changes and a True
  217. # result.
  218. ret = _run(test=True)
  219. assert not ret['changes'], ret['changes']
  220. assert 'already' in ret['comment'], ret['comment']
  221. assert ret['result'] is True, ret['result']
  222. # Run for real again, results should be the same as above (i.e. we
  223. # should never get to the point where we exit with a None result).
  224. ret = _run()
  225. assert not ret['changes'], ret['changes']
  226. assert 'already' in ret['comment'], ret['comment']
  227. assert ret['result'] is True, ret['result']
  228. finally:
  229. try:
  230. os.remove(fn_)
  231. except OSError:
  232. pass