From 2375d3bc8fc4b3ff0d5b42059ec771fffe6045b2 Mon Sep 17 00:00:00 2001 From: Hugh Secker-Walker Date: Sat, 28 Mar 2020 12:55:22 -0400 Subject: [PATCH 1/2] feat(docker/dev): Add support for Docker-based dev testing and forensics --- .dockerignore | 2 ++ Dockerfile-docker-scripts | 14 ++++++++ README.md | 14 ++++++++ docker-scripts/default-docker-script.sh | 12 +++++++ docker-scripts/entrypoint.sh | 12 +++++++ run-docker-script.sh | 48 +++++++++++++++++++++++++ 6 files changed, 102 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile-docker-scripts create mode 100755 docker-scripts/default-docker-script.sh create mode 100755 docker-scripts/entrypoint.sh create mode 100755 run-docker-script.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..16ef8145 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +/.git +/node_modules diff --git a/Dockerfile-docker-scripts b/Dockerfile-docker-scripts new file mode 100644 index 00000000..18c40f86 --- /dev/null +++ b/Dockerfile-docker-scripts @@ -0,0 +1,14 @@ +FROM node:lts-alpine + +RUN apk add bash redis + +WORKDIR /bee-queue +COPY package.json package-lock.json /bee-queue/ +RUN cd /bee-queue && npm --version && npm ci + +ENTRYPOINT ["/bee-queue/docker-scripts/entrypoint.sh"] + +COPY .eslintrc.json /bee-queue/ +COPY docker-scripts /bee-queue/docker-scripts/ +COPY test /bee-queue/test/ +COPY lib /bee-queue/lib/ diff --git a/README.md b/README.md index 485909d3..2f0b6006 100644 --- a/README.md +++ b/README.md @@ -857,6 +857,20 @@ Some of these could be worthwhile additions; please comment if you're interested You'll need a local redis server to run the tests. Note that running the tests may delete some keys in the form of `bq:test-*-*:*`. +Alternatively, if you have Docker available, you can run tests and do other dev forensics in a ephemeral container with its own Redis server, e.g.: +```bash +$ ./run-docker-script.sh + +$ ./run-docker-script.sh bash + +$ ./run-docker-script.sh npx ava --fail-fast --verbose + +$ ./run-docker-script.sh npm run ci + +$ ./run-docker-script.sh --help +``` + + [npm-image]: https://img.shields.io/npm/v/bee-queue.svg?style=flat [npm-url]: https://www.npmjs.com/package/bee-queue [travis-image]: https://img.shields.io/travis/bee-queue/bee-queue.svg?style=flat diff --git a/docker-scripts/default-docker-script.sh b/docker-scripts/default-docker-script.sh new file mode 100755 index 00000000..7118fddc --- /dev/null +++ b/docker-scripts/default-docker-script.sh @@ -0,0 +1,12 @@ +#!/bin/sh + +# Default script to run in bee-queue-docker container + +# We don't do `npm run ci` because we may not have credentials to publish to coveralls + +set -eux + +# This ordering supports quick iteration on new testing +npx ava +npm run eslint +npm run coverage diff --git a/docker-scripts/entrypoint.sh b/docker-scripts/entrypoint.sh new file mode 100755 index 00000000..e0501d4f --- /dev/null +++ b/docker-scripts/entrypoint.sh @@ -0,0 +1,12 @@ +#!/bin/sh + +# Docker ENTRYPOINT target +# - start a background Redis +# - evalate user-provided arguments + +# See: https://raw.githubusercontent.com/antirez/redis/5.0/redis.conf +# We don't use --daemonize because this is a dev configuration and we want to see warnings from Redis. +# If someone knows how to prevent the "Transparent Huge Page" warning on Docker for Mac, please advise. +/usr/bin/redis-server --tcp-backlog 128 --loglevel warning & + +eval "$@" diff --git a/run-docker-script.sh b/run-docker-script.sh new file mode 100755 index 00000000..9b1b0133 --- /dev/null +++ b/run-docker-script.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +# Strict, and loud failure +set -euo pipefail +trap 'rc=$?;set +ex;if [[ $rc -ne 0 ]];then trap - ERR EXIT;echo 1>&2;echo "*** fail *** : code $rc : $DIR/$SCRIPT $ARGS" 1>&2;echo 1>&2;exit $rc;fi' ERR EXIT +ARGS="$*" +DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +SCRIPT="$(basename "${BASH_SOURCE[0]}")" + +# A command or the name of a script to run in the container +# Note: scripts must be in ./docker-scripts/ in order to get copied into the Docker image +scriptName=${1:-docker-scripts/default-docker-script.sh} +shift || true + +if [ "$scriptName" = -h -o "$scriptName" = --help ] ; then + cat < Date: Sat, 28 Mar 2020 14:22:14 -0400 Subject: [PATCH 2/2] chore(docker/dev): Improve help for run-docker-script.sh --- run-docker-script.sh | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/run-docker-script.sh b/run-docker-script.sh index 9b1b0133..531185dc 100755 --- a/run-docker-script.sh +++ b/run-docker-script.sh @@ -18,22 +18,29 @@ if [ "$scriptName" = -h -o "$scriptName" = --help ] ; then $SCRIPT -- Run a default test script or commands of your choice in a Docker container with the bee-queue library and a Redis server. -Usage: $SCRIPT [command] +Usage: $SCRIPT [command ...args] The optional command argument is an executable or or a script to run in the container. The default command is docker-scripts/default-docker-script.sh which runs tests, the linter, and test coverage. +You can run Node JS tools and scripts, e.g. + + $SCRIPT npx ava --fail-fast --verbose + If you want to work interactively in the container, use bash, e.g. $SCRIPT bash -Any script you want to use must be in the ./docker-scripts directory. E.g. your +Scripts must be in the ./docker-scripts directory. E.g. your custom testing script ./docker-scripts/my-testing.sh would be run via $SCRIPT docker-scripts/my-testing.sh -Note: You can ignore the Transparent Huge Pages warning from Redis. +Notes: +- The very first time this script is used it takes a while for Docker to + populate its caches. +- You can ignore the Transparent Huge Pages warning from the Redis server. EOF exit 0