test_pkgrepo.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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.utils.files
  9. # Import Salt libs
  10. import salt.utils.platform
  11. # Import 3rd-party libs
  12. from salt.ext import six
  13. # Import Salt Testing libs
  14. from tests.support.case import ModuleCase
  15. from tests.support.helpers import (
  16. destructiveTest,
  17. requires_salt_modules,
  18. requires_salt_states,
  19. requires_system_grains,
  20. )
  21. from tests.support.mixins import SaltReturnAssertsMixin
  22. from tests.support.unit import skipIf
  23. @destructiveTest
  24. @skipIf(salt.utils.platform.is_windows(), "minion is windows")
  25. class PkgrepoTest(ModuleCase, SaltReturnAssertsMixin):
  26. """
  27. pkgrepo state tests
  28. """
  29. @requires_salt_modules("pkgrepo.managed")
  30. @requires_system_grains
  31. def test_pkgrepo_01_managed(self, grains):
  32. """
  33. Test adding a repo
  34. """
  35. if grains["os"] == "Ubuntu" and grains["osrelease_info"] >= (15, 10):
  36. self.skipTest(
  37. "The PPA used for this test does not exist for Ubuntu Wily"
  38. " (15.10) and later."
  39. )
  40. if grains["os_family"] == "Debian":
  41. try:
  42. from aptsources import sourceslist # pylint: disable=unused-import
  43. except ImportError:
  44. self.skipTest("aptsources.sourceslist python module not found")
  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_states("pkgrepo.absent", "pkgrepo.managed")
  71. @requires_system_grains
  72. @skipIf(True, "SLOWTEST skip")
  73. def test_pkgrepo_03_with_comments(self, grains):
  74. """
  75. Test adding a repo with comments
  76. """
  77. kwargs = {}
  78. if grains["os_family"] == "RedHat":
  79. kwargs = {
  80. "name": "examplerepo",
  81. "baseurl": "http://example.com/repo",
  82. "enabled": False,
  83. "comments": ["This is a comment"],
  84. }
  85. else:
  86. self.skipTest(
  87. "{}/{} test case needed".format(grains["os_family"], grains["os"])
  88. )
  89. try:
  90. # Run the state to add the repo
  91. ret = self.run_state("pkgrepo.managed", **kwargs)
  92. self.assertSaltTrueReturn(ret)
  93. # Run again with modified comments
  94. kwargs["comments"].append("This is another comment")
  95. ret = self.run_state("pkgrepo.managed", **kwargs)
  96. self.assertSaltTrueReturn(ret)
  97. ret = ret[next(iter(ret))]
  98. self.assertEqual(
  99. ret["changes"],
  100. {
  101. "comments": {
  102. "old": ["This is a comment"],
  103. "new": ["This is a comment", "This is another comment"],
  104. }
  105. },
  106. )
  107. # Run a third time, no changes should be made
  108. ret = self.run_state("pkgrepo.managed", **kwargs)
  109. self.assertSaltTrueReturn(ret)
  110. ret = ret[next(iter(ret))]
  111. self.assertFalse(ret["changes"])
  112. self.assertEqual(
  113. ret["comment"],
  114. "Package repo '{0}' already configured".format(kwargs["name"]),
  115. )
  116. finally:
  117. # Clean up
  118. self.run_state("pkgrepo.absent", name=kwargs["name"])
  119. @requires_salt_states("pkgrepo.managed")
  120. @requires_system_grains
  121. @skipIf(True, "SLOWTEST skip")
  122. def test_pkgrepo_04_apt_with_architectures(self, grains):
  123. """
  124. Test managing a repo with architectures specified
  125. """
  126. if grains["os_family"].lower() != "debian":
  127. self.skipTest("APT-only test")
  128. name = "deb {{arch}}http://foo.com/bar/latest {oscodename} main".format(
  129. oscodename=grains["oscodename"]
  130. )
  131. def _get_arch(arch):
  132. return "[arch={0}] ".format(arch) if arch else ""
  133. def _run(arch="", test=False):
  134. ret = self.run_state(
  135. "pkgrepo.managed",
  136. name=name.format(arch=_get_arch(arch)),
  137. file=fn_,
  138. refresh=False,
  139. test=test,
  140. )
  141. return ret[next(iter(ret))]
  142. fn_ = salt.utils.files.mkstemp(dir="/etc/apt/sources.list.d", suffix=".list")
  143. try:
  144. # Run with test=True
  145. ret = _run(test=True)
  146. assert ret["changes"] == {"repo": name.format(arch="")}, ret["changes"]
  147. assert "would be" in ret["comment"], ret["comment"]
  148. assert ret["result"] is None, ret["result"]
  149. # Run for real
  150. ret = _run()
  151. assert ret["changes"] == {"repo": name.format(arch="")}, ret["changes"]
  152. assert ret["comment"].startswith("Configured"), ret["comment"]
  153. assert ret["result"] is True, ret["result"]
  154. # Run again with test=True, should exit with no changes and a True
  155. # result.
  156. ret = _run(test=True)
  157. assert not ret["changes"], ret["changes"]
  158. assert "already" in ret["comment"], ret["comment"]
  159. assert ret["result"] is True, ret["result"]
  160. # Run for real again, results should be the same as above (i.e. we
  161. # should never get to the point where we exit with a None result).
  162. ret = _run()
  163. assert not ret["changes"], ret["changes"]
  164. assert "already" in ret["comment"], ret["comment"]
  165. assert ret["result"] is True, ret["result"]
  166. expected_changes = {
  167. "line": {
  168. "new": name.format(arch=_get_arch("amd64")),
  169. "old": name.format(arch=""),
  170. },
  171. "architectures": {"new": ["amd64"], "old": []},
  172. }
  173. # Run with test=True and the architecture set. We should get a None
  174. # result with some expected changes.
  175. ret = _run(arch="amd64", test=True)
  176. assert ret["changes"] == expected_changes, ret["changes"]
  177. assert "would be" in ret["comment"], ret["comment"]
  178. assert ret["result"] is None, ret["result"]
  179. # Run for real, with the architecture set. We should get a True
  180. # result with the same changes.
  181. ret = _run(arch="amd64")
  182. assert ret["changes"] == expected_changes, ret["changes"]
  183. assert ret["comment"].startswith("Configured"), ret["comment"]
  184. assert ret["result"] is True, ret["result"]
  185. # Run again with test=True, should exit with no changes and a True
  186. # result.
  187. ret = _run(arch="amd64", test=True)
  188. assert not ret["changes"], ret["changes"]
  189. assert "already" in ret["comment"], ret["comment"]
  190. assert ret["result"] is True, ret["result"]
  191. # Run for real again, results should be the same as above (i.e. we
  192. # should never get to the point where we exit with a None result).
  193. ret = _run(arch="amd64")
  194. assert not ret["changes"], ret["changes"]
  195. assert "already" in ret["comment"], ret["comment"]
  196. assert ret["result"] is True, ret["result"]
  197. expected_changes = {
  198. "line": {
  199. "new": name.format(arch=""),
  200. "old": name.format(arch=_get_arch("amd64")),
  201. },
  202. "architectures": {"new": [], "old": ["amd64"]},
  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