From 21ad3e9eeb635c104e3eb0b33a1e05cb613c2022 Mon Sep 17 00:00:00 2001 From: ViViDboarder Date: Mon, 18 May 2020 18:08:06 -0700 Subject: [PATCH 1/2] Add some tests and simplify push syntax --- Makefile | 11 +++++++++++ Readme.md | 9 +++++++++ push.sh | 13 +++++++------ tests/docker-compose-private.yml | 21 +++++++++++++++++++++ tests/docker-compose-public.yml | 16 ++++++++++++++++ tests/test.txt | 1 + 6 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 Makefile create mode 100644 tests/docker-compose-private.yml create mode 100644 tests/docker-compose-public.yml create mode 100644 tests/test.txt diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ef0693c --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +.PHONY: clean all + +.PHONY: default +default: test + +.PHONY: test +test: + docker-compose -f ./tests/docker-compose-private.yml up \ + --abort-on-container-exit --exit-code-from plugin + docker-compose -f ./tests/docker-compose-public.yml up \ + --abort-on-container-exit --exit-code-from plugin diff --git a/Readme.md b/Readme.md index f0f7795..98cf613 100644 --- a/Readme.md +++ b/Readme.md @@ -46,3 +46,12 @@ The following environment variables can be used for further cutomization: | ``PLUGIN_TIMEOUT`` | Defines a timeout (in seconds) to stop the upload after a certain time. | | ``PLUGIN_ATTEMPTS`` | Defines how often a failed upload should be retried. Normally there is only one upload attempt. | | ``PLUGIN_CUSTOM_ARGUMENTS`` | Additional arguments to be passed to `curl`. | + + +## Development + +There are only two tests right now and they are configured using Docker Compose. To run them, just use + + make test + +If someone wants to make this better (or add a Drone file) I'd gladly accept the patch. diff --git a/push.sh b/push.sh index 1fe127f..b800d2f 100755 --- a/push.sh +++ b/push.sh @@ -1,31 +1,31 @@ #! /bin/sh # Use WEBDAV_USERNAME as default, if provided. -if [ -z "$PLUGIN_USERNAME" ] && [ ! -z "$WEBDAV_USERNAME" ]; then +if [ -z "$PLUGIN_USERNAME" ] && [ -n "$WEBDAV_USERNAME" ]; then PLUGIN_USERNAME="$WEBDAV_USERNAME" fi # Use WEBDAV_PASSWORD as default, if provided. -if [ -z "$PLUGIN_PASSWORD" ] && [ ! -z "$WEBDAV_PASSWORD" ]; then +if [ -z "$PLUGIN_PASSWORD" ] && [ -n "$WEBDAV_PASSWORD" ]; then PLUGIN_PASSWORD="$WEBDAV_PASSWORD" fi # If username and password are provided, add auth -if [ ! -z "$PLUGIN_USERNAME" ] && [ ! -z "$PLUGIN_PASSWORD" ]; then +if [ -n "$PLUGIN_USERNAME" ] && [ -n "$PLUGIN_PASSWORD" ]; then - AUTH="--user '${PLUGIN_USERNAME}':'${PLUGIN_PASSWORD}'" + AUTH="--user '${PLUGIN_USERNAME}:${PLUGIN_PASSWORD}'" fi # Use a proxy, if one is specified -if [ ! -z "$PLUGIN_PROXY_URL" ]; then +if [ -n "$PLUGIN_PROXY_URL" ]; then PLUGIN_PROXY_URL="--proxy '${PLUGIN_PROXY_URL}'" fi # If a timeout is specified, make use of it. -if [ ! -z "$PLUGIN_TIMEOUT" ]; then +if [ -n "$PLUGIN_TIMEOUT" ]; then PLUGIN_TIMEOUT="--max-time '${PLUGIN_TIMEOUT}'" fi @@ -55,6 +55,7 @@ while [ "${PLUGIN_ATTEMPTS}" -gt 0 ]; do } + sleep 5 PLUGIN_ATTEMPTS=$((PLUGIN_ATTEMPTS-1)) done diff --git a/tests/docker-compose-private.yml b/tests/docker-compose-private.yml new file mode 100644 index 0000000..8a2c0d9 --- /dev/null +++ b/tests/docker-compose-private.yml @@ -0,0 +1,21 @@ +--- +version: '2.4' +services: + webdav: + image: sashgorokhov/webdav + environment: + USERNAME: jdoe + PASSWORD: hunter2 + + plugin: + build: + context: ../ + dockerfile: Dockerfile + volumes: + - './test.txt:/test.txt' + environment: + PLUGIN_FILE: '/test.txt' + PLUGIN_DESTINATION: 'http://webdav/' + PLUGIN_USERNAME: jdoe + PLUGIN_PASSWORD: hunter2 + PLUGIN_ATTEMPTS: 4 diff --git a/tests/docker-compose-public.yml b/tests/docker-compose-public.yml new file mode 100644 index 0000000..e364aba --- /dev/null +++ b/tests/docker-compose-public.yml @@ -0,0 +1,16 @@ +--- +version: '2.4' +services: + webdav: + image: sashgorokhov/webdav + + plugin: + build: + context: ../ + dockerfile: Dockerfile + volumes: + - './test.txt:/test.txt' + environment: + PLUGIN_FILE: '/test.txt' + PLUGIN_DESTINATION: 'http://webdav/' + PLUGIN_ATTEMPTS: 4 diff --git a/tests/test.txt b/tests/test.txt new file mode 100644 index 0000000..75c620d --- /dev/null +++ b/tests/test.txt @@ -0,0 +1 @@ +ohai From d5004efddf21c1379299340e2078e3e617244f56 Mon Sep 17 00:00:00 2001 From: ViViDboarder Date: Mon, 18 May 2020 19:06:17 -0700 Subject: [PATCH 2/2] Fix arg quoting --- Dockerfile | 2 +- Makefile | 2 ++ push.sh | 40 ++++++++++++++------------------ tests/docker-compose-private.yml | 1 + tests/docker-compose-public.yml | 1 + 5 files changed, 22 insertions(+), 24 deletions(-) diff --git a/Dockerfile b/Dockerfile index 86a67cf..f318806 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ FROM alpine MAINTAINER ViViDboarder -RUN apk -Uuv add curl ca-certificates +RUN apk -Uuv add bash curl ca-certificates COPY push.sh /bin/ RUN chmod +x /bin/push.sh diff --git a/Makefile b/Makefile index ef0693c..f16f13f 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,8 @@ default: test .PHONY: test test: docker-compose -f ./tests/docker-compose-private.yml up \ + --build --force-recreate \ --abort-on-container-exit --exit-code-from plugin docker-compose -f ./tests/docker-compose-public.yml up \ + --build --force-recreate \ --abort-on-container-exit --exit-code-from plugin diff --git a/push.sh b/push.sh index b800d2f..aa1d21d 100755 --- a/push.sh +++ b/push.sh @@ -1,62 +1,56 @@ -#! /bin/sh +#! /bin/bash +set -e + +ARGS=() # Use WEBDAV_USERNAME as default, if provided. if [ -z "$PLUGIN_USERNAME" ] && [ -n "$WEBDAV_USERNAME" ]; then - - PLUGIN_USERNAME="$WEBDAV_USERNAME" + PLUGIN_USERNAME="$WEBDAV_USERNAME" fi # Use WEBDAV_PASSWORD as default, if provided. if [ -z "$PLUGIN_PASSWORD" ] && [ -n "$WEBDAV_PASSWORD" ]; then - - PLUGIN_PASSWORD="$WEBDAV_PASSWORD" + PLUGIN_PASSWORD="$WEBDAV_PASSWORD" fi # If username and password are provided, add auth if [ -n "$PLUGIN_USERNAME" ] && [ -n "$PLUGIN_PASSWORD" ]; then - - AUTH="--user '${PLUGIN_USERNAME}:${PLUGIN_PASSWORD}'" + ARGS+=(--user "${PLUGIN_USERNAME}:${PLUGIN_PASSWORD}") fi # Use a proxy, if one is specified if [ -n "$PLUGIN_PROXY_URL" ]; then - - PLUGIN_PROXY_URL="--proxy '${PLUGIN_PROXY_URL}'" + ARGS+=(--proxy "${PLUGIN_PROXY_URL}") fi # If a timeout is specified, make use of it. if [ -n "$PLUGIN_TIMEOUT" ]; then - - PLUGIN_TIMEOUT="--max-time '${PLUGIN_TIMEOUT}'" + ARGS+=(--max-time "${PLUGIN_TIMEOUT}") fi # Set PLUGIN_ATTEMPTS to one if nothing else is specified if [ -z "$PLUGIN_ATTEMPTS" ]; then - - PLUGIN_ATTEMPTS=1 + PLUGIN_ATTEMPTS=1 fi # Repeat the upload as long as specified. while [ "${PLUGIN_ATTEMPTS}" -gt 0 ]; do # Uploading the file - curl $PLUGIN_PROXY_URL $PLUGIN_TIMEOUT $PLUGIN_CUSTOM_ARGUMENTS --upload-file $PLUGIN_FILE $AUTH $PLUGIN_DESTINATION && { - + curl "${ARGS[@]}" --upload-file "$PLUGIN_FILE" "$PLUGIN_DESTINATION" && { # Terminate the script as soon as the upload is successful - echo "[INFO] Upload was successful." - exit 0 + echo "[INFO] Upload was successful." + exit 0 - } + } # Show messages in case uploads have failed [ "$PLUGIN_ATTEMPTS" -gt 1 ] && { - - echo "[INFO] Upload failed. Attempting a new upload, if possible." - + echo "[INFO] Upload failed. Attempting a new upload, if possible." } - sleep 5 - PLUGIN_ATTEMPTS=$((PLUGIN_ATTEMPTS-1)) + sleep 5 + PLUGIN_ATTEMPTS=$((PLUGIN_ATTEMPTS-1)) done diff --git a/tests/docker-compose-private.yml b/tests/docker-compose-private.yml index 8a2c0d9..a396d26 100644 --- a/tests/docker-compose-private.yml +++ b/tests/docker-compose-private.yml @@ -18,4 +18,5 @@ services: PLUGIN_DESTINATION: 'http://webdav/' PLUGIN_USERNAME: jdoe PLUGIN_PASSWORD: hunter2 + PLUGIN_TIMEOUT: 10 PLUGIN_ATTEMPTS: 4 diff --git a/tests/docker-compose-public.yml b/tests/docker-compose-public.yml index e364aba..56ddc71 100644 --- a/tests/docker-compose-public.yml +++ b/tests/docker-compose-public.yml @@ -13,4 +13,5 @@ services: environment: PLUGIN_FILE: '/test.txt' PLUGIN_DESTINATION: 'http://webdav/' + PLUGIN_TIMEOUT: 10 PLUGIN_ATTEMPTS: 4