1
0

test_pkgrepo.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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_salt_modules,
  15. requires_system_grains,
  16. )
  17. # Import Salt libs
  18. import salt.utils.platform
  19. # Import 3rd-party libs
  20. from salt.ext import six
  21. @destructiveTest
  22. @skipIf(salt.utils.platform.is_windows(), 'minion is windows')
  23. class PkgrepoTest(ModuleCase, SaltReturnAssertsMixin):
  24. '''
  25. pkgrepo state tests
  26. '''
  27. @requires_salt_modules('pkgrepo.managed')
  28. @requires_system_grains
  29. def test_pkgrepo_01_managed(self, grains):
  30. '''
  31. Test adding a repo
  32. '''
  33. if grains['os'] == 'Ubuntu' and grains['osrelease_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. @requires_salt_modules('pkgrepo.absent')
  53. @requires_system_grains
  54. def test_pkgrepo_02_absent(self, grains):
  55. '''
  56. Test removing the repo from the above test
  57. '''
  58. if grains['os'] == 'Ubuntu' and grains['osrelease_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_salt_modules('pkgrepo.absent', 'pkgrepo.managed')
  71. @requires_system_grains
  72. def test_pkgrepo_03_with_comments(self, grains):
  73. '''
  74. Test adding a repo with comments
  75. '''
  76. if grains['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 grains['os_family'] in ('debian',):
  84. self.skipTest('Debian/Ubuntu test case needed')
  85. try:
  86. # Run the state to add the repo
  87. ret = self.run_state('pkgrepo.managed', **kwargs)
  88. self.assertSaltTrueReturn(ret)
  89. # Run again with modified comments
  90. kwargs['comments'].append('This is another comment')
  91. ret = self.run_state('pkgrepo.managed', **kwargs)
  92. self.assertSaltTrueReturn(ret)
  93. ret = ret[next(iter(ret))]
  94. self.assertEqual(
  95. ret['changes'],
  96. {
  97. 'comments': {
  98. 'old': ['This is a comment'],
  99. 'new': ['This is a comment',
  100. 'This is another comment']
  101. }
  102. }
  103. )
  104. # Run a third time, no changes should be made
  105. ret = self.run_state('pkgrepo.managed', **kwargs)
  106. self.assertSaltTrueReturn(ret)
  107. ret = ret[next(iter(ret))]
  108. self.assertFalse(ret['changes'])
  109. self.assertEqual(
  110. ret['comment'],
  111. "Package repo '{0}' already configured".format(kwargs['name'])
  112. )
  113. finally:
  114. # Clean up
  115. self.run_state('pkgrepo.absent', name=kwargs['name'])
  116. @requires_salt_modules('pkgrepo.managed')
  117. @requires_system_grains
  118. def test_pkgrepo_04_apt_with_architectures(self, grains):
  119. '''
  120. Test managing a repo with architectures specified
  121. '''
  122. if grains['os_family'].lower() != 'debian':
  123. self.skipTest('APT-only test')
  124. name = 'deb {{arch}}http://foo.com/bar/latest {oscodename} main'.format(oscodename=grains['oscodename'])
  125. def _get_arch(arch):
  126. return '[arch={0}] '.format(arch) if arch else ''
  127. def _run(arch='', test=False):
  128. ret = self.run_state(
  129. 'pkgrepo.managed',
  130. name=name.format(arch=_get_arch(arch)),
  131. file=fn_,
  132. refresh=False,
  133. test=test)
  134. return ret[next(iter(ret))]
  135. fn_ = salt.utils.files.mkstemp(dir='/etc/apt/sources.list.d', suffix='.list')
  136. try:
  137. # Run with test=True
  138. ret = _run(test=True)
  139. assert ret['changes'] == {'repo': name.format(arch='')}, ret['changes']
  140. assert 'would be' in ret['comment'], ret['comment']
  141. assert ret['result'] is None, ret['result']
  142. # Run for real
  143. ret = _run()
  144. assert ret['changes'] == {'repo': name.format(arch='')}, ret['changes']
  145. assert ret['comment'].startswith('Configured'), ret['comment']
  146. assert ret['result'] is True, ret['result']
  147. # Run again with test=True, should exit with no changes and a True
  148. # result.
  149. ret = _run(test=True)
  150. assert not ret['changes'], ret['changes']
  151. assert 'already' in ret['comment'], ret['comment']
  152. assert ret['result'] is True, ret['result']
  153. # Run for real again, results should be the same as above (i.e. we
  154. # should never get to the point where we exit with a None result).
  155. ret = _run()
  156. assert not ret['changes'], ret['changes']
  157. assert 'already' in ret['comment'], ret['comment']
  158. assert ret['result'] is True, ret['result']
  159. expected_changes = {
  160. 'line': {
  161. 'new': name.format(arch=_get_arch('amd64')),
  162. 'old': name.format(arch=''),
  163. },
  164. 'architectures': {
  165. 'new': ['amd64'],
  166. 'old': [],
  167. },
  168. }
  169. # Run with test=True and the architecture set. We should get a None
  170. # result with some expected changes.
  171. ret = _run(arch='amd64', test=True)
  172. assert ret['changes'] == expected_changes, ret['changes']
  173. assert 'would be' in ret['comment'], ret['comment']
  174. assert ret['result'] is None, ret['result']
  175. # Run for real, with the architecture set. We should get a True
  176. # result with the same changes.
  177. ret = _run(arch='amd64')
  178. assert ret['changes'] == expected_changes, ret['changes']
  179. assert ret['comment'].startswith('Configured'), ret['comment']
  180. assert ret['result'] is True, ret['result']
  181. # Run again with test=True, should exit with no changes and a True
  182. # result.
  183. ret = _run(arch='amd64', test=True)
  184. assert not ret['changes'], ret['changes']
  185. assert 'already' in ret['comment'], ret['comment']
  186. assert ret['result'] is True, ret['result']
  187. # Run for real again, results should be the same as above (i.e. we
  188. # should never get to the point where we exit with a None result).
  189. ret = _run(arch='amd64')
  190. assert not ret['changes'], ret['changes']
  191. assert 'already' in ret['comment'], ret['comment']
  192. assert ret['result'] is True, ret['result']
  193. expected_changes = {
  194. 'line': {
  195. 'new': name.format(arch=''),
  196. 'old': name.format(arch=_get_arch('amd64')),
  197. },
  198. 'architectures': {
  199. 'new': [],
  200. 'old': ['amd64'],
  201. },
  202. }
  203. # Run with test=True and the architecture set back to the original
  204. # value. We should get a None result with some expected changes.
  205. ret = _run(test=True)
  206. assert ret['changes'] == expected_changes, ret['changes']
  207. assert 'would be' in ret['comment'], ret['comment']
  208. assert ret['result'] is None, ret['result']
  209. # Run for real, with the architecture set. We should get a True
  210. # result with the same changes.
  211. ret = _run()
  212. assert ret['changes'] == expected_changes, ret['changes']
  213. assert ret['comment'].startswith('Configured'), ret['comment']
  214. assert ret['result'] is True, ret['result']
  215. # Run again with test=True, should exit with no changes and a True
  216. # result.
  217. ret = _run(test=True)
  218. assert not ret['changes'], ret['changes']
  219. assert 'already' in ret['comment'], ret['comment']
  220. assert ret['result'] is True, ret['result']
  221. # Run for real again, results should be the same as above (i.e. we
  222. # should never get to the point where we exit with a None result).
  223. ret = _run()
  224. assert not ret['changes'], ret['changes']
  225. assert 'already' in ret['comment'], ret['comment']
  226. assert ret['result'] is True, ret['result']
  227. finally:
  228. try:
  229. os.remove(fn_)
  230. except OSError:
  231. pass