run.sh 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/bin/bash
  2. set -e
  3. image="$1"
  4. cname="mongo-container-$RANDOM-$RANDOM"
  5. cid="$(docker run -d --name "$cname" "$image")"
  6. trap "docker rm -vf $cid > /dev/null" EXIT
  7. mongo() {
  8. docker run --rm -i --link "$cname":mongo --entrypoint mongo "$image" --host mongo "$@"
  9. }
  10. mongo_eval() {
  11. mongo --quiet --eval "$@"
  12. }
  13. tries=10
  14. while ! mongo_eval 'quit(db.stats().ok ? 0 : 1);' &> /dev/null; do
  15. (( tries-- ))
  16. if [ $tries -le 0 ]; then
  17. echo >&2 'mongod failed to accept connections in a reasonable amount of time!'
  18. mongo --eval 'db.stats();' # to hopefully get a useful error message
  19. false
  20. fi
  21. sleep 2
  22. done
  23. [ "$(mongo_eval 'db.test.count();')" = 0 ]
  24. mongo_eval 'db.test.save({ _id: 1, a: 2, b: 3, c: "hello" });' > /dev/null
  25. [ "$(mongo_eval 'db.test.count();')" = 1 ]
  26. mongo_eval 'db.test.save({ _id: 1, a: 3, b: 4, c: "hello" });' > /dev/null
  27. [ "$(mongo_eval 'db.test.count();')" = 1 ]
  28. [ "$(mongo_eval 'db.test.findOne().a;')" = 3 ]
  29. [ "$(mongo_eval 'db.test2.count();')" = 0 ]
  30. mongo_eval 'db.test2.save({ _id: "abc" });' > /dev/null
  31. [ "$(mongo_eval 'db.test2.count();')" = 1 ]
  32. [ "$(mongo_eval 'db.test.count();')" = 1 ]
  33. mongo_eval 'db.test2.drop();' > /dev/null
  34. [ "$(mongo_eval 'db.test2.count();')" = 0 ]
  35. [ "$(mongo_eval 'db.test.count();')" = 1 ]
  36. [ "$(mongo_eval 'db.test.count();' database-that-does-not-exist)" = 0 ]
  37. mongo_eval 'db.dropDatabase();' > /dev/null
  38. [ "$(mongo_eval 'db.test.count();')" = 0 ]