Created
August 2, 2017 23:56
-
-
Save rmb938/bfc92e5cfffd69ba66aabb9bb0e656eb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -e | |
USAGE="USAGE: $(basename "$0") [run|edit] <docker_run_params>" | |
GOSS_FILES_PATH="${GOSS_FILES_PATH:-.}" | |
info() { echo -e "INFO: $*"; } | |
error() { echo -e "ERROR: $*";exit 1; } | |
cleanup() { | |
set +e | |
if [[ $id ]];then | |
info "Deleting container" | |
docker rm -vf "$id" > /dev/null | |
fi | |
} | |
run(){ | |
# Copy in goss | |
info "Starting docker container" | |
id=$(docker run -d "${@:4}") | |
info "Container ID: ${id:0:8}" | |
info "Installing goss" | |
if ! docker exec "$id" sh -c "curl -fsSL https://goss.rocks/install | sh"; then | |
error "error installing goss" | |
fi | |
info "Copying over goss files" | |
if ! docker exec "$id" sh -c "mkdir /goss"; then | |
error "error creating /goss folder" | |
fi | |
[[ -e "${GOSS_FILES_PATH}/goss.yaml" ]] && docker cp "${GOSS_FILES_PATH}/goss.yaml" "$id:/goss" | |
[[ -e "${GOSS_FILES_PATH}/goss_wait.yaml" ]] && docker cp "${GOSS_FILES_PATH}/goss_wait.yaml" "$id:/goss" | |
[[ -z "${GOSS_VARS}" ]] && [[ -e "${GOSS_FILES_PATH}/${GOSS_VARS}" ]] && docker cp "${GOSS_FILES_PATH}/${GOSS_VARS}" "$id:/goss" | |
} | |
get_docker_file() { | |
if docker exec "$id" sh -c "test -e $1" > /dev/null;then | |
mkdir -p "${GOSS_FILES_PATH}" | |
info "Copied '$1' from container to '${GOSS_FILES_PATH}'" | |
docker cp "$id:$1" "${GOSS_FILES_PATH}" | |
fi | |
} | |
# Main | |
trap 'ret=$?;cleanup;exit $ret' EXIT | |
[[ ${GOSS_OPTS+x} ]] || GOSS_OPTS="--color" | |
[[ ${GOSS_WAIT_OPTS+x} ]] || GOSS_WAIT_OPTS="-r 30s -s 1s > /dev/null" | |
GOSS_SLEEP=${GOSS_SLEEP:-0.2} | |
case "$1" in | |
run) | |
run "$@" | |
if [[ -e "${GOSS_FILES_PATH}/goss_wait.yaml" ]]; then | |
info "Found goss_wait.yaml, waiting for it to pass before running tests" | |
if [[ -z "${GOSS_VARS}" ]]; then | |
if ! docker exec "$id" sh -c "goss -g /goss/goss_wait.yaml validate $GOSS_WAIT_OPTS"; then | |
error "goss_wait.yaml never passed" | |
fi | |
else | |
if ! docker exec "$id" sh -c "goss -g /goss/goss_wait.yaml --vars='/goss/${GOSS_VARS}' validate $GOSS_WAIT_OPTS"; then | |
error "goss_wait.yaml never passed" | |
fi | |
fi | |
fi | |
[[ $GOSS_SLEEP ]] && { info "Sleeping for $GOSS_SLEEP"; sleep "$GOSS_SLEEP"; } | |
info "Running Tests" | |
if [[ -z "${GOSS_VARS}" ]]; then | |
docker exec "$id" sh -c "goss -g /goss/goss.yaml validate $GOSS_OPTS" | |
else | |
docker exec "$id" sh -c "goss -g /goss/goss.yaml --vars='/goss/${GOSS_VARS}' validate $GOSS_OPTS" | |
fi | |
;; | |
edit) | |
run "$@" | |
info "Run goss add/autoadd to add resources" | |
docker exec -it "$id" sh -c 'cd /goss; PATH="/goss:$PATH" exec sh' | |
get_docker_file "/goss/goss.yaml" | |
get_docker_file "/goss/goss_wait.yaml" | |
[[ ! -z "${GOSS_VARS}" ]] && get_docker_file "/goss/${GOSS_VARS}" | |
;; | |
*) | |
error "$USAGE" | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment