1
0

test_dev_data.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2019 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import os
  9. import subprocess
  10. import sys
  11. import unittest
  12. import six
  13. REPO_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
  14. import tests # noqa
  15. class TestDevData(tests.Modeltests):
  16. """This test class contains tests pertaining to the dev-data utility
  17. script."""
  18. maxDiff = None
  19. def test_dev_data_all(self):
  20. """Check how dev-data --all performs
  21. """
  22. config_path = os.path.join(self.path, "config")
  23. with open(config_path, "w") as f:
  24. f.write("DB_URL = 'sqlite:///%s/db_dev_data.sqlite'\n" % self.path)
  25. f.write("GIT_FOLDER = '%s/repos'\n" % self.path)
  26. f.write(
  27. "BROKER_URL = 'redis+socket://%(global_path)s/broker'\n" % \
  28. self.config_values)
  29. f.write("CELERY_CONFIG = {'task_always_eager': True}\n")
  30. env = {
  31. "USER_NAME": "testuser",
  32. "USER_EMAIL": "testuser@example.com",
  33. "FORCE_DELETE": "yes",
  34. "PAGURE_CONFIG": config_path,
  35. }
  36. proc1 = subprocess.Popen(
  37. [sys.executable, "dev-data.py", "--all"],
  38. cwd=REPO_PATH,
  39. stdout=subprocess.PIPE,
  40. env=env,
  41. )
  42. stdout, stderr = proc1.communicate()
  43. if isinstance(stdout, six.binary_type):
  44. stdout = stdout.decode("utf-8")
  45. output = (
  46. """Database created
  47. User created: pingou <bar@pingou.com>, testing123
  48. User created: foo <foo@bar.com>, testing123
  49. User created: testuser <testuser@example.com>, testing123
  50. Created "admin" group. Pingou is a member.
  51. Created "group" group. Pingou is a member.
  52. Created "rel-eng" group. Pingou is a member.
  53. git folder already deleted
  54. docs folder already deleted
  55. tickets folder already deleted
  56. requests folder already deleted
  57. WARNING: Deleting all data from sqlite:///%s/db_dev_data.sqlite
  58. """
  59. % self.path
  60. )
  61. self.assertEqual(len(stdout.split("\n")), 14)
  62. self.assertEqual(stdout, output)
  63. def test_dev_data_delete(self):
  64. """Check how dev-data --init --delete performs
  65. """
  66. config_path = os.path.join(self.path, "config")
  67. env = {
  68. "USER_NAME": "testuser",
  69. "USER_EMAIL": "testuser@example.com",
  70. "FORCE_DELETE": "yes",
  71. "PAGURE_CONFIG": config_path,
  72. }
  73. proc1 = subprocess.Popen(
  74. [sys.executable, "dev-data.py", "--init", "--delete"],
  75. cwd=REPO_PATH,
  76. stdout=subprocess.PIPE,
  77. env=env,
  78. )
  79. stdout, stderr = proc1.communicate()
  80. if isinstance(stdout, six.binary_type):
  81. stdout = stdout.decode("utf-8")
  82. output = (
  83. """Database created
  84. WARNING: Deleting all data from %s
  85. """
  86. % self.dbpath
  87. )
  88. self.assertEqual(len(stdout.split("\n")), 4)
  89. self.assertEqual(stdout.split("\n"), output.split("\n"))
  90. def test_dev_data_init(self):
  91. """Check how dev-data --init performs
  92. """
  93. config_path = os.path.join(self.path, "config")
  94. env = {
  95. "USER_NAME": "testuser",
  96. "USER_EMAIL": "testuser@example.com",
  97. "FORCE_DELETE": "yes",
  98. "PAGURE_CONFIG": config_path,
  99. }
  100. proc1 = subprocess.Popen(
  101. [sys.executable, "dev-data.py", "--init"],
  102. cwd=REPO_PATH,
  103. stdout=subprocess.PIPE,
  104. env=env,
  105. )
  106. stdout, stderr = proc1.communicate()
  107. if isinstance(stdout, six.binary_type):
  108. stdout = stdout.decode("utf-8")
  109. output = "Database created\n"
  110. self.assertEqual(len(stdout.split("\n")), 2)
  111. self.assertEqual(stdout.split("\n"), output.split("\n"))
  112. if __name__ == "__main__":
  113. unittest.main(verbosity=2)