run.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/bin/bash
  2. set -e
  3. image="$1"
  4. haveSeccomp=
  5. if docker info --format '{{ join .SecurityOptions "\n" }}' 2>/dev/null |tac|tac| grep -q seccomp; then
  6. haveSeccomp=1
  7. # get docker default seccomp profile
  8. seccomp="$(wget -q -O - 'https://raw.githubusercontent.com/docker/docker/v17.03.1-ce/profiles/seccomp/default.json')"
  9. # make container with jq since it is not guaranteed on the host
  10. jqImage='librarytest/mongo-basics-jq:alpine'
  11. docker build -t "$jqImage" - > /dev/null <<-'EOF'
  12. FROM alpine:3.5
  13. RUN apk add --no-cache jq
  14. ENTRYPOINT ["jq"]
  15. EOF
  16. # need set_mempolicy syscall to be able to do numactl for mongodb
  17. # if "set_mempolicy" is not in the always allowed list, add it
  18. extraSeccomp="$(echo "$seccomp" | docker run -i --rm "$jqImage" --tab '
  19. .syscalls[] |= if (
  20. .action == "SCMP_ACT_ALLOW"
  21. and .args == []
  22. and .comment == ""
  23. and .includes == {}
  24. and .excludes == {}
  25. ) then (
  26. if ( .names | index("set_mempolicy") ) > 0 then
  27. .
  28. else (
  29. .names |= . + ["set_mempolicy"]
  30. ) end
  31. )
  32. else
  33. .
  34. end
  35. ')"
  36. else
  37. echo >&2 'warning: the current Docker daemon does not appear to support seccomp'
  38. fi
  39. docker_run_seccomp() {
  40. if [ "$haveSeccomp" ]; then
  41. docker run --security-opt seccomp=<(echo "$extraSeccomp") "$@"
  42. else
  43. docker run "$@"
  44. fi
  45. }
  46. cname="mongo-container-$RANDOM-$RANDOM"
  47. mongodRunArgs=( -d --name "$cname" )
  48. mongoArgs=( --host mongo )
  49. testDir="$(readlink -f "$(dirname "$BASH_SOURCE")")"
  50. testName="$(basename "$testDir")" # "mongo-basics" or "mongo-auth-basics"
  51. case "$testName" in
  52. *auth*)
  53. rootUser="root-$RANDOM"
  54. rootPass="root-$RANDOM-$RANDOM-password"
  55. mongodRunArgs+=(
  56. -e MONGO_INITDB_ROOT_USERNAME="$rootUser"
  57. -e MONGO_INITDB_ROOT_PASSWORD="$rootPass"
  58. )
  59. mongoArgs+=(
  60. --username="$rootUser"
  61. --password="$rootPass"
  62. --authenticationDatabase='admin'
  63. )
  64. ;;
  65. esac
  66. cid="$(docker_run_seccomp "${mongodRunArgs[@]}" "$image")"
  67. trap "docker rm -vf $cid > /dev/null" EXIT
  68. mongo() {
  69. docker_run_seccomp --rm -i --link "$cname":mongo "$image" mongo "${mongoArgs[@]}" "$@"
  70. }
  71. mongo_eval() {
  72. mongo --quiet --eval "$@"
  73. }
  74. tries=10
  75. while ! mongo_eval 'quit(db.stats().ok ? 0 : 1);' &> /dev/null; do
  76. (( tries-- ))
  77. if [ $tries -le 0 ]; then
  78. echo >&2 'mongod failed to accept connections in a reasonable amount of time!'
  79. ( set -x && docker logs "$cid" ) >&2 || true
  80. mongo --eval 'db.stats();' # to hopefully get a useful error message
  81. false
  82. fi
  83. echo >&2 -n .
  84. sleep 2
  85. done
  86. [ "$(mongo_eval 'db.test.count();')" = 0 ]
  87. mongo_eval 'db.test.save({ _id: 1, a: 2, b: 3, c: "hello" });' > /dev/null
  88. [ "$(mongo_eval 'db.test.count();')" = 1 ]
  89. mongo_eval 'db.test.save({ _id: 1, a: 3, b: 4, c: "hello" });' > /dev/null
  90. [ "$(mongo_eval 'db.test.count();')" = 1 ]
  91. [ "$(mongo_eval 'db.test.findOne().a;')" = 3 ]
  92. [ "$(mongo_eval 'db.test2.count();')" = 0 ]
  93. mongo_eval 'db.test2.save({ _id: "abc" });' > /dev/null
  94. [ "$(mongo_eval 'db.test2.count();')" = 1 ]
  95. [ "$(mongo_eval 'db.test.count();')" = 1 ]
  96. mongo_eval 'db.test2.drop();' > /dev/null
  97. [ "$(mongo_eval 'db.test2.count();')" = 0 ]
  98. [ "$(mongo_eval 'db.test.count();')" = 1 ]
  99. [ "$(mongo_eval 'db.test.count();' database-that-does-not-exist)" = 0 ]
  100. mongo_eval 'db.dropDatabase();' > /dev/null
  101. [ "$(mongo_eval 'db.test.count();')" = 0 ]