test_ext.py 3.4 KB

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