test_dev_data.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. config_path = os.path.join(self.path, "config")
  22. with open(config_path, "w") as f:
  23. f.write("DB_URL = 'sqlite:///%s/db_dev_data.sqlite'\n" % self.path)
  24. f.write("GIT_FOLDER = '%s/repos'\n" % self.path)
  25. f.write(
  26. "BROKER_URL = 'redis+socket://%(global_path)s/broker'\n"
  27. % self.config_values
  28. )
  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. config_path = os.path.join(self.path, "config")
  66. env = {
  67. "USER_NAME": "testuser",
  68. "USER_EMAIL": "testuser@example.com",
  69. "FORCE_DELETE": "yes",
  70. "PAGURE_CONFIG": config_path,
  71. }
  72. proc1 = subprocess.Popen(
  73. [sys.executable, "dev-data.py", "--init", "--delete"],
  74. cwd=REPO_PATH,
  75. stdout=subprocess.PIPE,
  76. env=env,
  77. )
  78. stdout, stderr = proc1.communicate()
  79. if isinstance(stdout, six.binary_type):
  80. stdout = stdout.decode("utf-8")
  81. output = (
  82. """Database created
  83. WARNING: Deleting all data from %s
  84. """
  85. % self.dbpath
  86. )
  87. self.assertEqual(len(stdout.split("\n")), 4)
  88. self.assertEqual(stdout.split("\n"), output.split("\n"))
  89. def test_dev_data_init(self):
  90. """Check how dev-data --init performs"""
  91. config_path = os.path.join(self.path, "config")
  92. env = {
  93. "USER_NAME": "testuser",
  94. "USER_EMAIL": "testuser@example.com",
  95. "FORCE_DELETE": "yes",
  96. "PAGURE_CONFIG": config_path,
  97. }
  98. proc1 = subprocess.Popen(
  99. [sys.executable, "dev-data.py", "--init"],
  100. cwd=REPO_PATH,
  101. stdout=subprocess.PIPE,
  102. env=env,
  103. )
  104. stdout, stderr = proc1.communicate()
  105. if isinstance(stdout, six.binary_type):
  106. stdout = stdout.decode("utf-8")
  107. output = "Database created\n"
  108. self.assertEqual(len(stdout.split("\n")), 2)
  109. self.assertEqual(stdout.split("\n"), output.split("\n"))
  110. if __name__ == "__main__":
  111. unittest.main(verbosity=2)