test_install.py 14 KB

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