test_ext.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, unicode_literals
  4. import logging
  5. import os
  6. import subprocess
  7. import sys
  8. import tempfile
  9. import pytest
  10. # Import Salt libs
  11. import salt
  12. import salt.ext.six
  13. import salt.modules.cmdmod
  14. import salt.utils.files
  15. import salt.utils.platform
  16. import tests.support.helpers
  17. from tests.support.runtests import RUNTIME_VARS
  18. # Import Salt Testing libs
  19. from tests.support.unit import TestCase, skipIf
  20. log = logging.getLogger(__name__)
  21. @skipIf(not salt.utils.path.which("bash"), "Bash needed for this test")
  22. class VendorTornadoTest(TestCase):
  23. """
  24. Ensure we are not using any non vendor'ed tornado
  25. """
  26. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  27. def test_import_override(self):
  28. tmp = tempfile.mkdtemp()
  29. test_source = tests.support.helpers.dedent(
  30. """
  31. from __future__ import absolute_import, print_function
  32. import salt
  33. import tornado
  34. print(tornado.__name__)
  35. """
  36. )
  37. test_source_path = os.path.join(tmp, "test.py")
  38. tornado_source = tests.support.helpers.dedent(
  39. """
  40. foo = 'bar'
  41. """
  42. )
  43. tornado_source_path = os.path.join(tmp, "tornado.py")
  44. with salt.utils.files.fopen(test_source_path, "w") as fp:
  45. fp.write(test_source)
  46. with salt.utils.files.fopen(tornado_source_path, "w") as fp:
  47. fp.write(tornado_source)
  48. # Preserve the virtual environment
  49. env = os.environ.copy()
  50. if salt.utils.platform.is_windows():
  51. if salt.ext.six.PY2:
  52. env[b"PYTHONPATH"] = b";".join([a.encode() for a in sys.path])
  53. else:
  54. env["PYTHONPATH"] = ";".join(sys.path)
  55. else:
  56. env["PYTHONPATH"] = ":".join(sys.path)
  57. p = subprocess.Popen(
  58. [sys.executable, test_source_path],
  59. stderr=subprocess.PIPE,
  60. stdout=subprocess.PIPE,
  61. env=env,
  62. )
  63. p.wait()
  64. pout = p.stdout.read().strip().decode()
  65. assert pout == "salt.ext.tornado", pout
  66. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  67. def test_vendored_tornado_import(self):
  68. grep_call = salt.modules.cmdmod.run_stdout(
  69. cmd="bash -c 'grep -r \"import tornado\" ./salt/*'",
  70. cwd=RUNTIME_VARS.CODE_DIR,
  71. ignore_retcode=True,
  72. ).split("\n")
  73. valid_lines = []
  74. for line in grep_call:
  75. if line == "":
  76. continue
  77. # Skip salt/ext/tornado/.. since there are a bunch of imports like
  78. # this in docstrings.
  79. if "salt/ext/tornado/" in line:
  80. continue
  81. log.error("Test found bad line: %s", line)
  82. valid_lines.append(line)
  83. assert valid_lines == [], len(valid_lines)
  84. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  85. def test_vendored_tornado_import_from(self):
  86. grep_call = salt.modules.cmdmod.run_stdout(
  87. cmd="bash -c 'grep -r \"from tornado\" ./salt/*'",
  88. cwd=RUNTIME_VARS.CODE_DIR,
  89. ignore_retcode=True,
  90. ).split("\n")
  91. valid_lines = []
  92. for line in grep_call:
  93. if line == "":
  94. continue
  95. log.error("Test found bad line: %s", line)
  96. valid_lines.append(line)
  97. assert valid_lines == [], len(valid_lines)
  98. def test_regression_56063(self):
  99. importer = salt.TornadoImporter()
  100. try:
  101. importer.find_module("tornado")
  102. except TypeError:
  103. assert False, "TornadoImporter raised type error when one argument passed"