1
0

test_pkgrepo.py 10 KB

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