test_install.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. # -*- coding: utf-8 -*-
  2. """
  3. tests.unit.setup.test_install
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. """
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import json
  8. import logging
  9. import os
  10. import pathlib
  11. import re
  12. import sys
  13. import salt.utils.path
  14. import salt.utils.platform
  15. import salt.version
  16. from salt.modules.virtualenv_mod import KNOWN_BINARY_NAMES
  17. from tests.support.helpers import VirtualEnv, slowTest, with_tempdir
  18. from tests.support.runtests import RUNTIME_VARS
  19. from tests.support.unit import TestCase, skipIf
  20. log = logging.getLogger(__name__)
  21. @skipIf(
  22. salt.utils.path.which_bin(KNOWN_BINARY_NAMES) is None, "virtualenv not installed"
  23. )
  24. class InstallTest(TestCase):
  25. """
  26. Tests for building and installing salt
  27. """
  28. @slowTest
  29. @with_tempdir()
  30. def test_wheel(self, tempdir):
  31. """
  32. test building and installing a bdist_wheel package
  33. """
  34. # Let's create the testing virtualenv
  35. with VirtualEnv() as venv:
  36. venv.run(venv.venv_python, "setup.py", "clean", cwd=RUNTIME_VARS.CODE_DIR)
  37. venv.run(
  38. venv.venv_python,
  39. "setup.py",
  40. "bdist_wheel",
  41. "--dist-dir",
  42. tempdir,
  43. cwd=RUNTIME_VARS.CODE_DIR,
  44. )
  45. venv.run(venv.venv_python, "setup.py", "clean", cwd=RUNTIME_VARS.CODE_DIR)
  46. salt_generated_package = list(pathlib.Path(tempdir).glob("*.whl"))
  47. if not salt_generated_package:
  48. self.fail("Could not find the generated wheel file")
  49. salt_generated_package = salt_generated_package[0]
  50. # Assert generate wheel version matches what salt reports as its version
  51. whl_ver = [
  52. x
  53. for x in salt_generated_package.name.split("-")
  54. if re.search(r"^\d.\d*", x)
  55. ][0]
  56. whl_ver_cmp = whl_ver.replace("_", "-")
  57. salt_ver_cmp = salt.version.__version__.replace("/", "-")
  58. assert whl_ver_cmp == salt_ver_cmp, "{} != {}".format(
  59. whl_ver_cmp, salt_ver_cmp
  60. )
  61. # Because bdist_wheel supports pep517, we don't have to pre-install Salt's
  62. # dependencies before installing the wheel package
  63. venv.install(str(salt_generated_package))
  64. # Let's ensure the version is correct
  65. cmd = venv.run(venv.venv_python, "-m", "pip", "list", "--format", "json")
  66. for details in json.loads(cmd.stdout):
  67. if details["name"] != "salt":
  68. continue
  69. installed_version = details["version"]
  70. break
  71. else:
  72. self.fail("Salt was not found installed")
  73. # Let's compare the installed version with the version salt reports
  74. assert installed_version == salt_ver_cmp, "{} != {}".format(
  75. installed_version, salt_ver_cmp
  76. )
  77. # Let's also ensure we have a salt/_version.py from the installed salt wheel
  78. subdir = [
  79. "lib",
  80. "python{}.{}".format(*sys.version_info),
  81. "site-packages",
  82. "salt",
  83. ]
  84. if salt.utils.platform.is_windows():
  85. subdir.pop(1)
  86. installed_salt_path = pathlib.Path(venv.venv_dir)
  87. installed_salt_path = installed_salt_path.joinpath(*subdir)
  88. assert installed_salt_path.is_dir()
  89. salt_generated_version_file_path = installed_salt_path / "_version.py"
  90. assert salt_generated_version_file_path.is_file()
  91. @slowTest
  92. @with_tempdir()
  93. def test_egg(self, tempdir):
  94. """
  95. test building and installing a bdist_egg package
  96. """
  97. # TODO: We should actually dissallow generating an egg file
  98. # Let's create the testing virtualenv
  99. with VirtualEnv() as venv:
  100. venv.run(venv.venv_python, "setup.py", "clean", cwd=RUNTIME_VARS.CODE_DIR)
  101. # Setuptools installs pre-release packages if we don't pin to an exact version
  102. # Let's download and install requirements before, running salt's install test
  103. venv.run(
  104. venv.venv_python,
  105. "-m",
  106. "pip",
  107. "download",
  108. "--dest",
  109. tempdir,
  110. RUNTIME_VARS.CODE_DIR,
  111. )
  112. packages = []
  113. for fname in os.listdir(tempdir):
  114. packages.append(os.path.join(tempdir, fname))
  115. venv.install(*packages)
  116. for package in packages:
  117. os.unlink(package)
  118. venv.run(
  119. venv.venv_python,
  120. "setup.py",
  121. "bdist_egg",
  122. "--dist-dir",
  123. tempdir,
  124. cwd=RUNTIME_VARS.CODE_DIR,
  125. )
  126. venv.run(venv.venv_python, "setup.py", "clean", cwd=RUNTIME_VARS.CODE_DIR)
  127. salt_generated_package = list(pathlib.Path(tempdir).glob("*.egg"))
  128. if not salt_generated_package:
  129. self.fail("Could not find the generated egg file")
  130. salt_generated_package = salt_generated_package[0]
  131. # Assert generate wheel version matches what salt reports as its version
  132. egg_ver = [
  133. x
  134. for x in salt_generated_package.name.split("-")
  135. if re.search(r"^\d.\d*", x)
  136. ][0]
  137. egg_ver_cmp = egg_ver.replace("_", "-")
  138. salt_ver_cmp = salt.version.__version__.replace("/", "-")
  139. assert egg_ver_cmp == salt_ver_cmp, "{} != {}".format(
  140. egg_ver_cmp, salt_ver_cmp
  141. )
  142. # We cannot pip install an egg file, let's go old school
  143. venv.run(
  144. venv.venv_python, "-m", "easy_install", str(salt_generated_package)
  145. )
  146. # Let's ensure the version is correct
  147. cmd = venv.run(venv.venv_python, "-m", "pip", "list", "--format", "json")
  148. for details in json.loads(cmd.stdout):
  149. if details["name"] != "salt":
  150. continue
  151. installed_version = details["version"]
  152. break
  153. else:
  154. self.fail("Salt was not found installed")
  155. # Let's compare the installed version with the version salt reports
  156. assert installed_version == salt_ver_cmp, "{} != {}".format(
  157. installed_version, salt_ver_cmp
  158. )
  159. # Let's also ensure we have a salt/_version.py from the installed salt egg
  160. subdir = [
  161. "lib",
  162. "python{}.{}".format(*sys.version_info),
  163. "site-packages",
  164. ]
  165. if salt.utils.platform.is_windows():
  166. subdir.pop(1)
  167. site_packages_dir = pathlib.Path(venv.venv_dir)
  168. site_packages_dir = site_packages_dir.joinpath(*subdir)
  169. assert site_packages_dir.is_dir()
  170. installed_salt_path = list(site_packages_dir.glob("salt*.egg"))
  171. if not installed_salt_path:
  172. self.fail("Failed to find the installed salt path")
  173. log.debug("Installed salt path glob matches: %s", installed_salt_path)
  174. installed_salt_path = installed_salt_path[0] / "salt"
  175. assert installed_salt_path.is_dir()
  176. salt_generated_version_file_path = installed_salt_path / "_version.py"
  177. assert (
  178. salt_generated_version_file_path.is_file()
  179. ), "{} is not a file".format(salt_generated_version_file_path)
  180. # On python 3.5 Windows sdist fails with encoding errors. This is resolved
  181. # in later versions.
  182. @skipIf(
  183. salt.utils.platform.is_windows()
  184. and sys.version_info > (3,)
  185. and sys.version_info < (3, 6),
  186. "Skip on python 3.5",
  187. )
  188. @slowTest
  189. @with_tempdir()
  190. def test_sdist(self, tempdir):
  191. """
  192. test building and installing a sdist package
  193. """
  194. # Let's create the testing virtualenv
  195. with VirtualEnv() as venv:
  196. venv.run(venv.venv_python, "setup.py", "clean", cwd=RUNTIME_VARS.CODE_DIR)
  197. # Setuptools installs pre-release packages if we don't pin to an exact version
  198. # Let's download and install requirements before, running salt's install test
  199. venv.run(
  200. venv.venv_python,
  201. "-m",
  202. "pip",
  203. "download",
  204. "--dest",
  205. tempdir,
  206. RUNTIME_VARS.CODE_DIR,
  207. )
  208. packages = []
  209. for fname in os.listdir(tempdir):
  210. packages.append(os.path.join(tempdir, fname))
  211. venv.install(*packages)
  212. for package in packages:
  213. os.unlink(package)
  214. venv.run(
  215. venv.venv_python,
  216. "setup.py",
  217. "sdist",
  218. "--dist-dir",
  219. tempdir,
  220. cwd=RUNTIME_VARS.CODE_DIR,
  221. )
  222. venv.run(venv.venv_python, "setup.py", "clean", cwd=RUNTIME_VARS.CODE_DIR)
  223. salt_generated_package = list(pathlib.Path(tempdir).glob("*.tar.gz"))
  224. if not salt_generated_package:
  225. self.fail("Could not find the generated sdist file")
  226. salt_generated_package = salt_generated_package[0]
  227. log.info("Generated sdist file: %s", salt_generated_package.name)
  228. # Assert generated sdist version matches what salt reports as its version
  229. sdist_ver_cmp = salt_generated_package.name.split(".tar.gz")[0].split(
  230. "salt-"
  231. )[-1]
  232. salt_ver_cmp = salt.version.__version__.replace("/", "-")
  233. assert sdist_ver_cmp == salt_ver_cmp, "{} != {}".format(
  234. sdist_ver_cmp, salt.version.__version__
  235. )
  236. venv.install(str(salt_generated_package))
  237. # Let's also ensure we have a salt/_version.py from the installed salt wheel
  238. subdir = [
  239. "lib",
  240. "python{}.{}".format(*sys.version_info),
  241. "site-packages",
  242. "salt",
  243. ]
  244. if salt.utils.platform.is_windows():
  245. subdir.pop(1)
  246. installed_salt_path = pathlib.Path(venv.venv_dir)
  247. installed_salt_path = installed_salt_path.joinpath(*subdir)
  248. assert installed_salt_path.is_dir()
  249. salt_generated_version_file_path = installed_salt_path / "_version.py"
  250. assert salt_generated_version_file_path.is_file()
  251. with salt_generated_version_file_path.open() as rfh:
  252. log.debug("_version.py contents:\n >>>>>>\n%s\n <<<<<<", rfh.read())
  253. # Let's ensure the version is correct
  254. cmd = venv.run(venv.venv_python, "-m", "pip", "list", "--format", "json")
  255. for details in json.loads(cmd.stdout):
  256. if details["name"] != "salt":
  257. continue
  258. installed_version = details["version"]
  259. break
  260. else:
  261. self.fail("Salt was not found installed")
  262. # Let's compare the installed version with the version salt reports
  263. assert installed_version == salt_ver_cmp, "{} != {}".format(
  264. installed_version, salt_ver_cmp
  265. )
  266. @slowTest
  267. @with_tempdir()
  268. def test_setup_install(self, tempdir):
  269. """
  270. test installing directly from source
  271. """
  272. # Let's create the testing virtualenv
  273. with VirtualEnv() as venv:
  274. venv.run(venv.venv_python, "setup.py", "clean", cwd=RUNTIME_VARS.CODE_DIR)
  275. # Setuptools installs pre-release packages if we don't pin to an exact version
  276. # Let's download and install requirements before, running salt's install test
  277. venv.run(
  278. venv.venv_python,
  279. "-m",
  280. "pip",
  281. "download",
  282. "--dest",
  283. tempdir,
  284. RUNTIME_VARS.CODE_DIR,
  285. )
  286. packages = []
  287. for fname in os.listdir(tempdir):
  288. packages.append(os.path.join(tempdir, fname))
  289. venv.install(*packages)
  290. for package in packages:
  291. os.unlink(package)
  292. venv.run(
  293. venv.venv_python,
  294. "setup.py",
  295. "install",
  296. "--prefix",
  297. venv.venv_dir,
  298. cwd=RUNTIME_VARS.CODE_DIR,
  299. )
  300. venv.run(venv.venv_python, "setup.py", "clean", cwd=RUNTIME_VARS.CODE_DIR)
  301. # Let's ensure the version is correct
  302. cmd = venv.run(venv.venv_python, "-m", "pip", "list", "--format", "json")
  303. for details in json.loads(cmd.stdout):
  304. if details["name"] != "salt":
  305. continue
  306. installed_version = details["version"]
  307. break
  308. else:
  309. self.fail("Salt was not found installed")
  310. salt_ver_cmp = salt.version.__version__.replace("/", "-")
  311. # Let's compare the installed version with the version salt reports
  312. assert installed_version == salt_ver_cmp, "{} != {}".format(
  313. installed_version, salt_ver_cmp
  314. )
  315. # Let's also ensure we have a salt/_version.py from the installed salt
  316. subdir = [
  317. "lib",
  318. "python{}.{}".format(*sys.version_info),
  319. "site-packages",
  320. ]
  321. if salt.utils.platform.is_windows():
  322. subdir.pop(1)
  323. site_packages_dir = pathlib.Path(venv.venv_dir)
  324. site_packages_dir = site_packages_dir.joinpath(*subdir)
  325. assert site_packages_dir.is_dir()
  326. installed_salt_path = list(site_packages_dir.glob("salt*.egg"))
  327. if not installed_salt_path:
  328. self.fail("Failed to find the installed salt path")
  329. installed_salt_path = installed_salt_path[0] / "salt"
  330. salt_generated_version_file_path = installed_salt_path / "_version.py"
  331. assert salt_generated_version_file_path.is_file()