Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

135 строки
3.8 KiB

  1. # imports - standard imports
  2. import getpass
  3. import json
  4. import os
  5. import shutil
  6. import subprocess
  7. import sys
  8. import traceback
  9. import unittest
  10. # imports - module imports
  11. from bench.utils import paths_in_bench, exec_cmd
  12. from bench.utils.system import init
  13. from bench.bench import Bench
  14. PYTHON_VER = sys.version_info
  15. XHIVEFRAMEWORK_BRANCH = "version-13-hotfix"
  16. if PYTHON_VER.major == 3:
  17. if PYTHON_VER.minor >= 10:
  18. XHIVEFRAMEWORK_BRANCH = "develop"
  19. class TestBenchBase(unittest.TestCase):
  20. def setUp(self):
  21. self.benches_path = "."
  22. self.benches = []
  23. def tearDown(self):
  24. for bench_name in self.benches:
  25. bench_path = os.path.join(self.benches_path, bench_name)
  26. bench = Bench(bench_path)
  27. mariadb_password = (
  28. "travis"
  29. if os.environ.get("CI")
  30. else getpass.getpass(prompt="Enter MariaDB root Password: ")
  31. )
  32. if bench.exists:
  33. for site in bench.sites:
  34. subprocess.call(
  35. [
  36. "bench",
  37. "drop-site",
  38. site,
  39. "--force",
  40. "--no-backup",
  41. "--root-password",
  42. mariadb_password,
  43. ],
  44. cwd=bench_path,
  45. )
  46. shutil.rmtree(bench_path, ignore_errors=True)
  47. def assert_folders(self, bench_name):
  48. for folder in paths_in_bench:
  49. self.assert_exists(bench_name, folder)
  50. self.assert_exists(bench_name, "apps", "xhiveframework")
  51. def assert_virtual_env(self, bench_name):
  52. bench_path = os.path.abspath(bench_name)
  53. python_path = os.path.abspath(os.path.join(bench_path, "env", "bin", "python"))
  54. self.assertTrue(python_path.startswith(bench_path))
  55. for subdir in ("bin", "lib", "share"):
  56. self.assert_exists(bench_name, "env", subdir)
  57. def assert_config(self, bench_name):
  58. for config, search_key in (
  59. ("redis_queue.conf", "redis_queue.rdb"),
  60. ("redis_cache.conf", "redis_cache.rdb"),
  61. ):
  62. self.assert_exists(bench_name, "config", config)
  63. with open(os.path.join(bench_name, "config", config)) as f:
  64. self.assertTrue(search_key in f.read())
  65. def assert_common_site_config(self, bench_name, expected_config):
  66. common_site_config_path = os.path.join(
  67. self.benches_path, bench_name, "sites", "common_site_config.json"
  68. )
  69. self.assertTrue(os.path.exists(common_site_config_path))
  70. with open(common_site_config_path) as f:
  71. config = json.load(f)
  72. for key, value in list(expected_config.items()):
  73. self.assertEqual(config.get(key), value)
  74. def assert_exists(self, *args):
  75. self.assertTrue(os.path.exists(os.path.join(*args)))
  76. def new_site(self, site_name, bench_name):
  77. new_site_cmd = ["bench", "new-site", site_name, "--admin-password", "admin"]
  78. if os.environ.get("CI"):
  79. new_site_cmd.extend(["--mariadb-root-password", "travis"])
  80. subprocess.call(new_site_cmd, cwd=os.path.join(self.benches_path, bench_name))
  81. def init_bench(self, bench_name, **kwargs):
  82. self.benches.append(bench_name)
  83. xhiveframework_tmp_path = "/tmp/xhiveframework"
  84. if not os.path.exists(xhiveframework_tmp_path):
  85. exec_cmd(
  86. f"git clone https://lab.membtech.com/xhiveframework/xhiveframework15 -b {XHIVEFRAMEWORK_BRANCH} --depth 1 --origin upstream {xhiveframework_tmp_path}"
  87. )
  88. kwargs.update(
  89. dict(
  90. python=sys.executable,
  91. no_procfile=True,
  92. no_backups=True,
  93. xhiveframework_path=xhiveframework_tmp_path,
  94. )
  95. )
  96. if not os.path.exists(os.path.join(self.benches_path, bench_name)):
  97. init(bench_name, **kwargs)
  98. exec_cmd(
  99. "git remote set-url upstream https://lab.membtech.com/xhiveframework/xhiveframework15",
  100. cwd=os.path.join(self.benches_path, bench_name, "apps", "xhiveframework"),
  101. )
  102. def file_exists(self, path):
  103. if os.environ.get("CI"):
  104. return not subprocess.call(["sudo", "test", "-f", path])
  105. return os.path.isfile(path)
  106. def get_traceback(self):
  107. exc_type, exc_value, exc_tb = sys.exc_info()
  108. trace_list = traceback.format_exception(exc_type, exc_value, exc_tb)
  109. return "".join(str(t) for t in trace_list)