1
0

test_pkgrepo.py 10 KB

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