run-tests-container.py 5.2 KB

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