run-tests-container.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #! /usr/bin/env python
  2. import argparse
  3. import os
  4. import subprocess as sp
  5. def setup_parser():
  6. """ Setup the cli arguments """
  7. parser = argparse.ArgumentParser(prog="pagure-test")
  8. parser.add_argument(
  9. "test_case", nargs="?", default="", help="Run the given test case"
  10. )
  11. parser.add_argument(
  12. "--fedora",
  13. action="store_true",
  14. help="Run the tests in fedora environment (DEFAULT)",
  15. )
  16. parser.add_argument(
  17. "--centos",
  18. action="store_true",
  19. help="Run the tests in centos environment",
  20. )
  21. parser.add_argument(
  22. "--pip",
  23. action="store_true",
  24. help="Run the tests in a venv on a Fedora host",
  25. )
  26. parser.add_argument(
  27. "--skip-build",
  28. dest="skip_build",
  29. action="store_false",
  30. help="Skip building the container image",
  31. )
  32. parser.add_argument(
  33. "--shell",
  34. dest="shell",
  35. action="store_true",
  36. help="Gives you a shell into the container instead "
  37. "of running the tests",
  38. )
  39. return parser
  40. if __name__ == "__main__":
  41. parser = setup_parser()
  42. args = parser.parse_args()
  43. if args.centos is True:
  44. container_names = ["pagure-c7-rpms-py2"]
  45. container_files = ["centos7-rpms-py2"]
  46. elif args.fedora is True:
  47. container_names = ["pagure-f29-rpms-py3"]
  48. container_files = ["f29-rpms-py3"]
  49. elif args.pip is True:
  50. container_names = ["pagure-fedora-pip-py3"]
  51. container_files = ["fedora-pip-py3"]
  52. else:
  53. container_names = [
  54. "pagure-f29-rpms-py3",
  55. "pagure-c7-rpms-py2",
  56. "pagure-fedora-pip-py3",
  57. ]
  58. container_files = [
  59. "f29-rpms-py3",
  60. "centos7-rpms-py2",
  61. "fedora-pip-py3",
  62. ]
  63. failed = []
  64. for idx, container_name in enumerate(container_names):
  65. if args.skip_build is not False:
  66. print("------ Building Container Image -----")
  67. output_code = sp.call(
  68. [
  69. "podman",
  70. "build",
  71. "--build-arg",
  72. "branch={}".format(os.environ.get("BRANCH") or "master"),
  73. "--build-arg",
  74. "repo={}".format(
  75. os.environ.get("REPO")
  76. or "https://pagure.io/pagure.git"
  77. ),
  78. "--rm",
  79. "-t",
  80. container_name,
  81. "-f",
  82. "dev/containers/%s" % container_files[idx],
  83. "dev/containers",
  84. ]
  85. )
  86. if output_code:
  87. print("Failed building: %s", container_name)
  88. break
  89. result_path = "{}/results_{}".format(os.getcwd(), container_files[idx])
  90. if not os.path.exists(result_path):
  91. os.mkdir(result_path)
  92. if args.shell:
  93. print("--------- Shelling in the container --------------")
  94. command = [
  95. "podman",
  96. "run",
  97. "-it",
  98. "--rm",
  99. "--name",
  100. container_name,
  101. "-v",
  102. "{}/results_{}:/pagure/results:z".format(
  103. os.getcwd(), container_files[idx]
  104. ),
  105. "-e",
  106. "BRANCH={}".format(os.environ.get("BRANCH") or "master"),
  107. "-e",
  108. "REPO={}".format(
  109. os.environ.get("REPO") or "https://pagure.io/pagure.git"
  110. ),
  111. "--entrypoint=/bin/bash",
  112. container_name,
  113. ]
  114. sp.call(command)
  115. else:
  116. print("--------- Running Test --------------")
  117. command = [
  118. "podman",
  119. "run",
  120. "-it",
  121. "--rm",
  122. "--name",
  123. container_name,
  124. "-v",
  125. "{}/results_{}:/pagure/results:z".format(
  126. os.getcwd(), container_files[idx]
  127. ),
  128. "-e",
  129. "BRANCH={}".format(os.environ.get("BRANCH") or "master"),
  130. "-e",
  131. "REPO={}".format(
  132. os.environ.get("REPO") or "https://pagure.io/pagure.git"
  133. ),
  134. "-e",
  135. "TESTCASE={}".format(args.test_case or ""),
  136. container_name,
  137. ]
  138. output_code = sp.call(command)
  139. if output_code:
  140. failed.append(container_name)
  141. if not args.shell:
  142. print("\nSummary:")
  143. if not failed:
  144. print(" ALL TESTS PASSED")
  145. else:
  146. print(" %s TESTS FAILED:" % len(failed))
  147. for fail in failed:
  148. print(" - %s" % fail)