Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dockerized Mongo Memory Server #332

Closed
higherorderfunctor opened this issue Aug 13, 2020 · 1 comment
Closed

Dockerized Mongo Memory Server #332

higherorderfunctor opened this issue Aug 13, 2020 · 1 comment
Labels

Comments

@higherorderfunctor
Copy link

higherorderfunctor commented Aug 13, 2020

Versions

  • system: Alpine Edge

package: mongo-memory-server

What is your question?

This is less a question, but instead sharing a hacky wrapper for starting the memory server inside docker when on a host that doesn't have any recent mongo builds (e.g. alpine edge). You may have to increase your timeouts if using a testing suite as the startup time is slower. The container will auto-kill itself after 60 seconds in case the scripts exits from some signal I didn't trap.

mongod

#!/bin/bash

PORT=27107
BIND_IP="127.0.0.1"

args=()
while (( "$#" )); do
  case "$1" in
    --dbpath) # strip since running in own container
      if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
        shift 2
      else
        echo "Error: Argument for $1 is missing" >&2
        exit 1
      fi
      ;;
    --bind_ip) # strip to use as part of docker run
      if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
        BIND_IP=$2
        shift 2
      else
        echo "Error: Argument for $1 is missing" >&2
        exit 1
      fi
      ;;
    --port) # extract but don't strip to setup proper port forwarding
      if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
        PORT=$2
        args+=($1 $2)
        shift 2
      else
        echo "Error: Argument for $1 is missing" >&2
        exit 1
      fi
      ;;
    *)
      args+=($1)
      shift
      ;;
  esac
done

trap term SIGTERM INT

function term() {
  kill "$child"
  exit 0
}

docker run --rm -p $BIND_IP:$PORT:$PORT mongo:latest timeout 60s mongod --bind_ip 0.0.0.0 ${args[@]} & child=$!
wait "$child"

In the test script, I use something like the following to use the bash script:

if (process.env.DOCKERIZED_MONGOMS) {
  process.env.MONGOMS_DISABLE_POSTINSTALL = '1'
  process.env.MONGOMS_SYSTEM_BINARY = '/path/to/mongod;
}

You can then switch to using the dockerized memory server by running tests like so:

DOCKERIZED_MONGOMS=1 yarn test

@hasezoey
Copy link
Collaborator

hasezoey commented Sep 8, 2020

closing in favor of #347

@hasezoey hasezoey closed this as completed Sep 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants