test_thin.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import pytest
  2. import salt.exceptions
  3. import salt.utils.stringutils
  4. import salt.utils.thin
  5. from tests.support.mock import MagicMock, patch
  6. def _mock_popen(return_value=None, side_effect=None, returncode=0):
  7. proc = MagicMock()
  8. proc.communicate = MagicMock(return_value=return_value, side_effect=side_effect)
  9. proc.returncode = returncode
  10. popen = MagicMock(return_value=proc)
  11. return popen
  12. @pytest.mark.parametrize("version", [[2, 7], [3, 0], [3, 7]])
  13. def test_get_tops_python(version):
  14. """
  15. Tests 'distro' is only included when targeting
  16. python 3 in get_tops_python
  17. """
  18. python3 = False
  19. if tuple(version) >= (3, 0):
  20. python3 = True
  21. mods = ["jinja2"]
  22. if python3:
  23. mods.append("distro")
  24. popen_ret = tuple(salt.utils.stringutils.to_bytes(x) for x in ("", ""))
  25. mock_popen = _mock_popen(return_value=popen_ret)
  26. patch_proc = patch("salt.utils.thin.subprocess.Popen", mock_popen)
  27. patch_which = patch("salt.utils.path.which", return_value=True)
  28. with patch_proc, patch_which:
  29. salt.utils.thin.get_tops_python("python2", ext_py_ver=version)
  30. cmds = [x[0][0] for x in mock_popen.call_args_list]
  31. assert [x for x in cmds if "jinja2" in x]
  32. if python3:
  33. assert [x for x in cmds if "distro" in x]
  34. else:
  35. assert not [x for x in cmds if "distro" in x]
  36. @pytest.mark.parametrize("version", [[2, 7], [3, 0], [3, 7]])
  37. def test_get_ext_tops(version):
  38. """
  39. Tests 'distro' is only included when targeting
  40. python 3 in get_ext_tops
  41. """
  42. python3 = False
  43. if tuple(version) >= (3, 0):
  44. python3 = True
  45. cfg = {
  46. "namespace": {
  47. "path": "/foo",
  48. "py-version": version,
  49. "dependencies": {
  50. "jinja2": "/jinja/foo.py",
  51. "yaml": "/yaml/",
  52. "tornado": "/tornado/tornado.py",
  53. "msgpack": "msgpack.py",
  54. },
  55. }
  56. }
  57. with patch("salt.utils.thin.os.path.isfile", MagicMock(return_value=True)):
  58. if python3:
  59. with pytest.raises(salt.exceptions.SaltSystemExit) as err:
  60. salt.utils.thin.get_ext_tops(cfg)
  61. else:
  62. ret = salt.utils.thin.get_ext_tops(cfg)
  63. if python3:
  64. assert "distro" in err.value.code
  65. else:
  66. assert not [x for x in ret["namespace"]["dependencies"] if "distro" in x]
  67. assert [x for x in ret["namespace"]["dependencies"] if "msgpack" in x]