run-in-container.sh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env bash
  2. set -Eeuo 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=( --rm )
  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="$(readlink -f "$BASH_SOURCE")"
  32. thisDir="$(dirname "$thisDir")"
  33. testDir="$(readlink -f "$testDir")"
  34. testBase="$(basename "$testDir")"
  35. hostMount="$(dirname "$testDir")"
  36. containerMount="/tmp/test-dir"
  37. workdir="$containerMount/$testBase"
  38. # TODO should we be doing something fancy with $BASH_SOURCE instead so we can be arbitrarily deep and mount the top level always?
  39. newImage="$("$thisDir/image-name.sh" librarytest/run-in-container "$image--$testBase")"
  40. "$thisDir/docker-build.sh" "$hostMount" "$newImage" <<EOD
  41. FROM $image
  42. COPY dir $containerMount
  43. WORKDIR $workdir
  44. EOD
  45. # there is strong potential for nokogiri+overlayfs failure
  46. # see https://github.com/docker-library/ruby/issues/55
  47. gemHome="$(docker image inspect --format '{{- range .Config.Env -}}{{- println . -}}{{- end -}}' "$newImage" | awk -F '=' '$1 == "GEM_HOME" { print $2; exit }')"
  48. if [ -n "$gemHome" ]; then
  49. # must be a Ruby image
  50. driver="$(docker info --format '{{ .Driver }}' 2>/dev/null)"
  51. if [ "$driver" = 'overlay' ]; then
  52. # let's add a volume (_not_ a bind mount) on GEM_HOME to work around nokogiri+overlayfs issues
  53. args+=( -v "$gemHome" )
  54. fi
  55. fi
  56. args+=( --entrypoint "$entrypoint" )
  57. # we can't use "exec" here because Windows needs to override "docker" with a function that sets "MSYS_NO_PATHCONV" (see "test/run.sh" for where that's defined)
  58. if ! docker run "${args[@]}" "$newImage" "$@"; then
  59. exit 1
  60. fi
  61. exit 0