mirror of
https://github.com/ViViDboarder/drone-webdav.git
synced 2025-01-04 19:47:33 +00:00
Merge pull request #10 from ViViDboarder/add-tests
Add tests and fix arg quoting
This commit is contained in:
commit
f261ba1939
@ -1,7 +1,7 @@
|
||||
FROM alpine
|
||||
MAINTAINER ViViDboarder <ViViDboarder@gmail.com>
|
||||
|
||||
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
|
||||
|
||||
|
13
Makefile
Normal file
13
Makefile
Normal file
@ -0,0 +1,13 @@
|
||||
.PHONY: clean all
|
||||
|
||||
.PHONY: default
|
||||
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
|
@ -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.
|
||||
|
49
push.sh
49
push.sh
@ -1,61 +1,56 @@
|
||||
#! /bin/sh
|
||||
#! /bin/bash
|
||||
set -e
|
||||
|
||||
ARGS=()
|
||||
|
||||
# Use WEBDAV_USERNAME as default, if provided.
|
||||
if [ -z "$PLUGIN_USERNAME" ] && [ ! -z "$WEBDAV_USERNAME" ]; then
|
||||
|
||||
PLUGIN_USERNAME="$WEBDAV_USERNAME"
|
||||
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
|
||||
|
||||
PLUGIN_PASSWORD="$WEBDAV_PASSWORD"
|
||||
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
|
||||
|
||||
AUTH="--user '${PLUGIN_USERNAME}':'${PLUGIN_PASSWORD}'"
|
||||
if [ -n "$PLUGIN_USERNAME" ] && [ -n "$PLUGIN_PASSWORD" ]; then
|
||||
ARGS+=(--user "${PLUGIN_USERNAME}:${PLUGIN_PASSWORD}")
|
||||
fi
|
||||
|
||||
# Use a proxy, if one is specified
|
||||
if [ ! -z "$PLUGIN_PROXY_URL" ]; then
|
||||
|
||||
PLUGIN_PROXY_URL="--proxy '${PLUGIN_PROXY_URL}'"
|
||||
if [ -n "$PLUGIN_PROXY_URL" ]; then
|
||||
ARGS+=(--proxy "${PLUGIN_PROXY_URL}")
|
||||
fi
|
||||
|
||||
# If a timeout is specified, make use of it.
|
||||
if [ ! -z "$PLUGIN_TIMEOUT" ]; then
|
||||
|
||||
PLUGIN_TIMEOUT="--max-time '${PLUGIN_TIMEOUT}'"
|
||||
if [ -n "$PLUGIN_TIMEOUT" ]; then
|
||||
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."
|
||||
}
|
||||
|
||||
PLUGIN_ATTEMPTS=$((PLUGIN_ATTEMPTS-1))
|
||||
sleep 5
|
||||
PLUGIN_ATTEMPTS=$((PLUGIN_ATTEMPTS-1))
|
||||
|
||||
done
|
||||
|
||||
|
22
tests/docker-compose-private.yml
Normal file
22
tests/docker-compose-private.yml
Normal file
@ -0,0 +1,22 @@
|
||||
---
|
||||
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_TIMEOUT: 10
|
||||
PLUGIN_ATTEMPTS: 4
|
17
tests/docker-compose-public.yml
Normal file
17
tests/docker-compose-public.yml
Normal file
@ -0,0 +1,17 @@
|
||||
---
|
||||
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_TIMEOUT: 10
|
||||
PLUGIN_ATTEMPTS: 4
|
1
tests/test.txt
Normal file
1
tests/test.txt
Normal file
@ -0,0 +1 @@
|
||||
ohai
|
Loading…
Reference in New Issue
Block a user