run-tests-container.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. parser.add_argument(
  40. "--repo",
  41. dest="repo",
  42. default="https://pagure.io/pagure.git",
  43. help="URL of the public repo to use as source, can be overridden using "
  44. "the REPO environment variable",
  45. )
  46. parser.add_argument(
  47. "--branch",
  48. dest="branch",
  49. default="master",
  50. help="Branch of the repo to use as source, can be overridden using "
  51. "the BRANCH environment variable",
  52. )
  53. return parser
  54. if __name__ == "__main__":
  55. parser = setup_parser()
  56. args = parser.parse_args()
  57. if args.centos is True:
  58. container_names = ["pagure-c7-rpms-py2"]
  59. container_files = ["centos7-rpms-py2"]
  60. elif args.fedora is True:
  61. container_names = ["pagure-f31-rpms-py3"]
  62. container_files = ["f31-rpms-py3"]
  63. elif args.pip is True:
  64. container_names = ["pagure-fedora-pip-py3"]
  65. container_files = ["fedora-pip-py3"]
  66. else:
  67. container_names = [
  68. "pagure-f31-rpms-py3",
  69. "pagure-c7-rpms-py2",
  70. "pagure-fedora-pip-py3",
  71. ]
  72. container_files = [
  73. "f31-rpms-py3",
  74. "centos7-rpms-py2",
  75. "fedora-pip-py3",
  76. ]
  77. failed = []
  78. for idx, container_name in enumerate(container_names):
  79. if args.skip_build is not False:
  80. print("------ Building Container Image -----")
  81. cmd = [
  82. "podman",
  83. "build",
  84. "--build-arg",
  85. "branch={}".format(os.environ.get("BRANCH") or args.branch),
  86. "--build-arg",
  87. "repo={}".format(os.environ.get("REPO") or args.repo),
  88. "--rm",
  89. "-t",
  90. container_name,
  91. "-f",
  92. "dev/containers/%s" % container_files[idx],
  93. "dev/containers",
  94. ]
  95. print(" ".join(cmd))
  96. output_code = sp.call(cmd)
  97. if output_code:
  98. print("Failed building: %s", container_name)
  99. break
  100. result_path = "{}/results_{}".format(os.getcwd(), container_files[idx])
  101. if not os.path.exists(result_path):
  102. os.mkdir(result_path)
  103. if args.shell:
  104. print("--------- Shelling in the container --------------")
  105. command = [
  106. "podman",
  107. "run",
  108. "-it",
  109. "--rm",
  110. "--name",
  111. container_name,
  112. "-v",
  113. "{}/results_{}:/pagure/results:z".format(
  114. os.getcwd(), container_files[idx]
  115. ),
  116. "-e",
  117. "BRANCH={}".format(os.environ.get("BRANCH") or args.branch),
  118. "-e",
  119. "REPO={}".format(os.environ.get("REPO") or args.repo),
  120. "--entrypoint=/bin/bash",
  121. container_name,
  122. ]
  123. sp.call(command)
  124. else:
  125. print("--------- Running Test --------------")
  126. command = [
  127. "podman",
  128. "run",
  129. "-it",
  130. "--rm",
  131. "--name",
  132. container_name,
  133. "-v",
  134. "{}/results_{}:/pagure/results:z".format(
  135. os.getcwd(), container_files[idx]
  136. ),
  137. "-e",
  138. "BRANCH={}".format(os.environ.get("BRANCH") or args.branch),
  139. "-e",
  140. "REPO={}".format(os.environ.get("REPO") or args.repo),
  141. "-e",
  142. "TESTCASE={}".format(args.test_case or ""),
  143. container_name,
  144. ]
  145. output_code = sp.call(command)
  146. if output_code:
  147. failed.append(container_name)
  148. if not args.shell:
  149. print("\nSummary:")
  150. if not failed:
  151. print(" ALL TESTS PASSED")
  152. else:
  153. print(" %s TESTS FAILED:" % len(failed))
  154. for fail in failed:
  155. print(" - %s" % fail)