run-tests-container.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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", "pagure-c7-rpms-py2",
  55. "pagure-fedora-pip-py3"
  56. ]
  57. container_files = [
  58. "f29-rpms-py3", "centos7-rpms-py2",
  59. "fedora-pip-py3"
  60. ]
  61. failed = []
  62. for idx, container_name in enumerate(container_names):
  63. if args.skip_build is not False:
  64. print("------ Building Container Image -----")
  65. output_code = sp.call(
  66. [
  67. "podman",
  68. "build",
  69. "--rm",
  70. "-t",
  71. container_name,
  72. "-f",
  73. "dev/containers/%s" % container_files[idx],
  74. "dev/containers",
  75. ]
  76. )
  77. if output_code:
  78. print("Failed building: %s", container_name)
  79. break
  80. result_path = "{}/results_{}".format(os.getcwd(), container_files[idx])
  81. if not os.path.exists(result_path):
  82. os.mkdir(result_path)
  83. if args.shell:
  84. print("--------- Shelling in the container --------------")
  85. command = [
  86. "podman",
  87. "run",
  88. "-it",
  89. "--rm",
  90. "--name",
  91. container_name,
  92. "-v",
  93. "{}/results_{}:/pagure/results:z".format(
  94. os.getcwd(), container_files[idx]),
  95. "-e",
  96. "BRANCH=$BRANCH",
  97. "-e",
  98. "REPO=$REPO",
  99. "--entrypoint=/bin/bash",
  100. container_name,
  101. ]
  102. sp.call(command)
  103. else:
  104. print("--------- Running Test --------------")
  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. "-e",
  116. "BRANCH={}".format(os.environ.get("BRANCH") or ""),
  117. "-e",
  118. "REPO={}".format(os.environ.get("REPO") or ""),
  119. container_name,
  120. args.test_case,
  121. ]
  122. output_code = sp.call(command)
  123. if output_code:
  124. failed.append(container_name)
  125. if not args.shell:
  126. print("\nSummary:")
  127. if not failed:
  128. print(" ALL TESTS PASSED")
  129. else:
  130. print(" %s TESTS FAILED:" % len(failed))
  131. for fail in failed:
  132. print(" - %s" % fail)