test_ext.py 3.1 KB

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