run-in-container.sh 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/bin/bash
  2. set -eo pipefail
  3. # NOT INTENDED TO BE USED AS A TEST "run.sh" DIRECTLY
  4. # SEE OTHER "run-*-in-container.sh" SCRIPTS FOR USAGE
  5. # arguments to docker
  6. args=()
  7. opts="$(getopt -o '+' --long 'docker-arg:' -- "$@")"
  8. eval set -- "$opts"
  9. while true; do
  10. flag="$1"
  11. shift
  12. case "$flag" in
  13. --docker-arg) args+=( "$1" ) && shift ;;
  14. --) break ;;
  15. *)
  16. {
  17. echo "error: unknown flag: $flag"
  18. #usage
  19. } >&2
  20. exit 1
  21. ;;
  22. esac
  23. done
  24. testDir="$1"
  25. shift
  26. image="$1"
  27. shift
  28. entrypoint="$1"
  29. shift
  30. # do some fancy footwork so that if testDir is /a/b/c, we mount /a/b and use c as the working directory (so relative symlinks work one level up)
  31. thisDir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
  32. testDir="$(readlink -f "$testDir")"
  33. testBase="$(basename "$testDir")"
  34. hostMount="$(dirname "$testDir")"
  35. containerMount="/tmp/test-dir"
  36. workdir="$containerMount/$testBase"
  37. # TODO should we be doing something fancy with $BASH_SOURCE instead so we can be arbitrarily deep and mount the top level always?
  38. newImage="$("$thisDir/image-name.sh" librarytest/run-in-container "$image--$testBase")"
  39. "$thisDir/docker-build.sh" "$hostMount" "$newImage" <<EOD
  40. FROM $image
  41. COPY dir $containerMount
  42. WORKDIR $workdir
  43. ENTRYPOINT ["$entrypoint"]
  44. EOD
  45. args+=( --rm )
  46. # there is strong potential for nokogiri+overlayfs failure
  47. # see https://github.com/docker-library/ruby/issues/55
  48. gemHome="$(docker inspect -f '{{range .Config.Env}}{{println .}}{{end}}' "$newImage" | awk -F '=' '$1 == "GEM_HOME" { print $2; exit }')"
  49. if [ "$gemHome" ]; then
  50. # must be a Ruby image
  51. driver="$(docker info | awk -F ': ' '$1 == "Storage Driver" { print $2 }')"
  52. if [ "$driver" = 'overlay' ]; then
  53. # let's add a volume (_not_ a bind mount) on GEM_HOME to work around nokogiri+overlayfs issues
  54. args+=( -v "$gemHome" )
  55. fi
  56. fi
  57. exec docker run "${args[@]}" "$newImage" "$@"