test_pkgrepo.py 10 KB

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