1
0

test_dev_data.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. )
  30. f.write("CELERY_CONFIG = {'task_always_eager': True}\n")
  31. env = {
  32. "USER_NAME": "testuser",
  33. "USER_EMAIL": "testuser@example.com",
  34. "FORCE_DELETE": "yes",
  35. "PAGURE_CONFIG": config_path,
  36. }
  37. proc1 = subprocess.Popen(
  38. [sys.executable, "dev-data.py", "--all"],
  39. cwd=REPO_PATH,
  40. stdout=subprocess.PIPE,
  41. env=env,
  42. )
  43. stdout, stderr = proc1.communicate()
  44. if isinstance(stdout, six.binary_type):
  45. stdout = stdout.decode("utf-8")
  46. output = (
  47. """Database created
  48. User created: pingou <bar@pingou.com>, testing123
  49. User created: foo <foo@bar.com>, testing123
  50. User created: testuser <testuser@example.com>, testing123
  51. Created "admin" group. Pingou is a member.
  52. Created "group" group. Pingou is a member.
  53. Created "rel-eng" group. Pingou is a member.
  54. git folder already deleted
  55. docs folder already deleted
  56. tickets folder already deleted
  57. requests folder already deleted
  58. WARNING: Deleting all data from sqlite:///%s/db_dev_data.sqlite
  59. """
  60. % self.path
  61. )
  62. self.assertEqual(len(stdout.split("\n")), 14)
  63. self.assertEqual(stdout, output)
  64. def test_dev_data_delete(self):
  65. """Check how dev-data --init --delete performs
  66. """
  67. config_path = os.path.join(self.path, "config")
  68. env = {
  69. "USER_NAME": "testuser",
  70. "USER_EMAIL": "testuser@example.com",
  71. "FORCE_DELETE": "yes",
  72. "PAGURE_CONFIG": config_path,
  73. }
  74. proc1 = subprocess.Popen(
  75. [sys.executable, "dev-data.py", "--init", "--delete"],
  76. cwd=REPO_PATH,
  77. stdout=subprocess.PIPE,
  78. env=env,
  79. )
  80. stdout, stderr = proc1.communicate()
  81. if isinstance(stdout, six.binary_type):
  82. stdout = stdout.decode("utf-8")
  83. output = (
  84. """Database created
  85. WARNING: Deleting all data from %s
  86. """
  87. % self.dbpath
  88. )
  89. self.assertEqual(len(stdout.split("\n")), 4)
  90. self.assertEqual(stdout.split("\n"), output.split("\n"))
  91. def test_dev_data_init(self):
  92. """Check how dev-data --init performs
  93. """
  94. config_path = os.path.join(self.path, "config")
  95. env = {
  96. "USER_NAME": "testuser",
  97. "USER_EMAIL": "testuser@example.com",
  98. "FORCE_DELETE": "yes",
  99. "PAGURE_CONFIG": config_path,
  100. }
  101. proc1 = subprocess.Popen(
  102. [sys.executable, "dev-data.py", "--init"],
  103. cwd=REPO_PATH,
  104. stdout=subprocess.PIPE,
  105. env=env,
  106. )
  107. stdout, stderr = proc1.communicate()
  108. if isinstance(stdout, six.binary_type):
  109. stdout = stdout.decode("utf-8")
  110. output = "Database created\n"
  111. self.assertEqual(len(stdout.split("\n")), 2)
  112. self.assertEqual(stdout.split("\n"), output.split("\n"))
  113. if __name__ == "__main__":
  114. unittest.main(verbosity=2)