From 2cad1463584ace80b4916b9d049d5ba8769f8eb0 Mon Sep 17 00:00:00 2001 From: clfreville2 Date: Sat, 11 Mar 2023 21:21:25 +0100 Subject: [PATCH 1/4] Add a script to test the image --- .drone.yml | 3 +++ .gitignore | 3 ++- test/test_image.sh | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100755 test/test_image.sh diff --git a/.drone.yml b/.drone.yml index f10296e..efdd5c4 100644 --- a/.drone.yml +++ b/.drone.yml @@ -28,3 +28,6 @@ steps: from_secret: SECRET_REGISTRY_USERNAME password: from_secret: SECRET_REGISTRY_PASSWORD + when: + branch: + - master diff --git a/.gitignore b/.gitignore index 723ef36..8e94e1c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.idea \ No newline at end of file +.idea +id_rsa* diff --git a/test/test_image.sh b/test/test_image.sh new file mode 100755 index 0000000..90877cd --- /dev/null +++ b/test/test_image.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +set -eu + +root_dir=$(readlink -f $(dirname $(realpath "$0"))/..) +if ! [ -f "$root_dir"/id_rsa ]; then + echo 'Generating SSH key' + ssh-keygen -t rsa -f "$root_dir"/id_rsa -q -N '' +fi + +run_image() { + local branch=$1 + local pubkey=$(<"$root_dir"/id_rsa.pub) + shift + docker network inspect codefirst &> /dev/null || docker network create codefirst + docker volume create --name=codefirst-docdeployer-drone + docker volume create --name=codefirst-docdeployer-docs + docker volume create --name=codefirst-nginx + nginx_id=$(docker run --rm -d -v codefirst-nginx:/usr/share/nginx --net=codefirst -e PUBLIC_KEY="$pubkey" -e USER_NAME=codefirst --hostname=nginx linuxserver/openssh-server) + sleep 1 # Wait to let the container start + docker run -v codefirst-docdeployer-drone:/drone/src \ + -v codefirst-docdeployer-docs:/docs \ + --net=codefirst \ + -e DRONE_REPO_NAME=hello-world \ + -e DRONE_REPO_OWNER=octocat \ + -e DRONE_REPO=octocat/hello-world \ + -e DRONE_BRANCH=$branch \ + -e DRONE_COMMIT=7fd1a60b01f91b314f59955a4e4d4e80d8edf11d \ + --entrypoint '/bin/bash' \ + --rm codefirst-docdeployer \ + -c "sed -i 's/ssh/ssh -p 2222/g' /entrypoint.sh && sed -i 's/root@nginx/codefirst@nginx/g' /entrypoint.sh && $*" + docker stop "$nginx_id" +} + +docker build -t codefirst-docdeployer "$root_dir" + +run_image main 'doxygen -g && /entrypoint.sh -b main -l . -t doxygen -d sourcecode_documentation' From 1bf3a2863c14dd2fd98c0cb72a593a6bb3111e75 Mon Sep 17 00:00:00 2001 From: clfreville2 Date: Sun, 12 Mar 2023 10:31:33 +0100 Subject: [PATCH 2/4] Also recreate the nginx container for testing --- test/nginx/Dockerfile | 3 +++ test/test_image.sh | 20 +++++++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 test/nginx/Dockerfile diff --git a/test/nginx/Dockerfile b/test/nginx/Dockerfile new file mode 100644 index 0000000..11d23f4 --- /dev/null +++ b/test/nginx/Dockerfile @@ -0,0 +1,3 @@ +FROM linuxserver/openssh-server:9.1_p1-r2-ls105 + +RUN apk add --no-cache rsync diff --git a/test/test_image.sh b/test/test_image.sh index 90877cd..239e0df 100755 --- a/test/test_image.sh +++ b/test/test_image.sh @@ -5,19 +5,22 @@ set -eu root_dir=$(readlink -f $(dirname $(realpath "$0"))/..) if ! [ -f "$root_dir"/id_rsa ]; then echo 'Generating SSH key' - ssh-keygen -t rsa -f "$root_dir"/id_rsa -q -N '' + ssh-keygen -t rsa -f "$root_dir/id_rsa" -q -N '' fi run_image() { + # Run the codefirst-docdeployer image with the given branch and command. + # + # The container will automatically be added to the same network as a container + # named 'nginx'. The codefirst-nginx container acts as a SSH server and will + # be started and stopped automatically. The SSH user, port and authorized + # keys are already configured. local branch=$1 - local pubkey=$(<"$root_dir"/id_rsa.pub) + local pubkey=$(<"$root_dir/id_rsa.pub") shift docker network inspect codefirst &> /dev/null || docker network create codefirst - docker volume create --name=codefirst-docdeployer-drone - docker volume create --name=codefirst-docdeployer-docs - docker volume create --name=codefirst-nginx - nginx_id=$(docker run --rm -d -v codefirst-nginx:/usr/share/nginx --net=codefirst -e PUBLIC_KEY="$pubkey" -e USER_NAME=codefirst --hostname=nginx linuxserver/openssh-server) - sleep 1 # Wait to let the container start + nginx_id=$(docker run --rm -d -v codefirst-nginx:/usr/share/nginx --net=codefirst -e PUBLIC_KEY="$pubkey" -e USER_NAME=codefirst -e PUID=0 --hostname=nginx codefirst-nginx) + sleep 2 # Wait to let the container start docker run -v codefirst-docdeployer-drone:/drone/src \ -v codefirst-docdeployer-docs:/docs \ --net=codefirst \ @@ -30,8 +33,11 @@ run_image() { --rm codefirst-docdeployer \ -c "sed -i 's/ssh/ssh -p 2222/g' /entrypoint.sh && sed -i 's/root@nginx/codefirst@nginx/g' /entrypoint.sh && $*" docker stop "$nginx_id" + docker volume rm codefirst-docdeployer-drone codefirst-docdeployer-docs + docker run --rm -i -v codefirst-nginx:/usr/share/nginx busybox find /usr/share/nginx } docker build -t codefirst-docdeployer "$root_dir" +docker build -t codefirst-nginx "$root_dir/test/nginx" run_image main 'doxygen -g && /entrypoint.sh -b main -l . -t doxygen -d sourcecode_documentation' From 038372ab68809af99ab619c22b538a848acc20ca Mon Sep 17 00:00:00 2001 From: clfreville2 Date: Sun, 12 Mar 2023 10:58:32 +0100 Subject: [PATCH 3/4] Successfully pass the doxygen test --- test/test_image.sh | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/test/test_image.sh b/test/test_image.sh index 239e0df..9740eb6 100755 --- a/test/test_image.sh +++ b/test/test_image.sh @@ -2,6 +2,9 @@ set -eu +declare -g EXCEPTED_DOC_BASE_PATH='/usr/share/nginx/html/octocat/hello-world' +declare -g VERBOSE='false' + root_dir=$(readlink -f $(dirname $(realpath "$0"))/..) if ! [ -f "$root_dir"/id_rsa ]; then echo 'Generating SSH key' @@ -34,10 +37,27 @@ run_image() { -c "sed -i 's/ssh/ssh -p 2222/g' /entrypoint.sh && sed -i 's/root@nginx/codefirst@nginx/g' /entrypoint.sh && $*" docker stop "$nginx_id" docker volume rm codefirst-docdeployer-drone codefirst-docdeployer-docs - docker run --rm -i -v codefirst-nginx:/usr/share/nginx busybox find /usr/share/nginx +} + +assert_file_exists() { + # Assert that the given file exists in the codefirst-nginx volume. + if docker run --rm -i -v codefirst-nginx:/usr/share/nginx busybox test -f "$EXCEPTED_DOC_BASE_PATH/$1"; then + echo -e "$1 exists: \e[32mOK\e[0m" + else + echo -e "$1 does not exist: \e[31mFAILED\e[0m" + if [[ "$VERBOSE" = 'true' ]]; then + docker run --rm -i -v codefirst-nginx:/usr/share/nginx busybox find /usr/share/nginx + fi + fi } docker build -t codefirst-docdeployer "$root_dir" docker build -t codefirst-nginx "$root_dir/test/nginx" -run_image main 'doxygen -g && /entrypoint.sh -b main -l . -t doxygen -d sourcecode_documentation' +test_doxygen() { + run_image main 'doxygen -g > /dev/null && /entrypoint.sh -b main -l . -t doxygen -d sourcecode_documentation' + assert_file_exists sourcecode_documentation/doxygen/index.html + docker volume rm codefirst-nginx +} + +test_doxygen From dd2939af3743ef43d7293a067f9bcd657cdc809b Mon Sep 17 00:00:00 2001 From: clfreville2 Date: Sun, 12 Mar 2023 11:19:50 +0100 Subject: [PATCH 4/4] Fix the Swagger generator path --- generators/swagger.sh | 4 ++-- test/test_image.sh | 45 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/generators/swagger.sh b/generators/swagger.sh index bebe2ff..af85dd6 100644 --- a/generators/swagger.sh +++ b/generators/swagger.sh @@ -11,10 +11,10 @@ generate() { - + -

Suivez ce lien.

+

Suivez ce lien.

EOF diff --git a/test/test_image.sh b/test/test_image.sh index 9740eb6..cd8a842 100755 --- a/test/test_image.sh +++ b/test/test_image.sh @@ -36,17 +36,44 @@ run_image() { --rm codefirst-docdeployer \ -c "sed -i 's/ssh/ssh -p 2222/g' /entrypoint.sh && sed -i 's/root@nginx/codefirst@nginx/g' /entrypoint.sh && $*" docker stop "$nginx_id" - docker volume rm codefirst-docdeployer-drone codefirst-docdeployer-docs + docker volume rm codefirst-docdeployer-drone codefirst-docdeployer-docs > /dev/null +} + +check_on_deployed() { + # Run the given command on the codefirst-nginx volume. + docker run --rm -i -v codefirst-nginx:/usr/share/nginx busybox $* +} + +cleanup() { + # Remove the codefirst-nginx volume after a test. + docker volume rm codefirst-nginx > /dev/null } assert_file_exists() { # Assert that the given file exists in the codefirst-nginx volume. - if docker run --rm -i -v codefirst-nginx:/usr/share/nginx busybox test -f "$EXCEPTED_DOC_BASE_PATH/$1"; then + if check_on_deployed test -f "$EXCEPTED_DOC_BASE_PATH/$1"; then echo -e "$1 exists: \e[32mOK\e[0m" else echo -e "$1 does not exist: \e[31mFAILED\e[0m" - if [[ "$VERBOSE" = 'true' ]]; then - docker run --rm -i -v codefirst-nginx:/usr/share/nginx busybox find /usr/share/nginx + if [[ "$VERBOSE" == 'true' ]]; then + check_on_deployed find /usr/share/nginx + fi + fi +} + +assert_file_contains() { + # Assert that the given file contains the given string. + if check_on_deployed grep -q $2 "$EXCEPTED_DOC_BASE_PATH/$1"; then + echo -e "$1 exists and matches content: \e[32mOK\e[0m" + elif check_on_deployed test -f "$EXCEPTED_DOC_BASE_PATH/$1"; then + echo -e "$1 exists but does not match content: \e[31mFAILED\e[0m" + if [[ "$VERBOSE" == 'true' ]]; then + check_on_deployed cat "$EXCEPTED_DOC_BASE_PATH/$1" + fi + else + echo -e "$1 does not exist: \e[31mFAILED\e[0m" + if [[ "$VERBOSE" == 'true' ]]; then + check_on_deployed find /usr/share/nginx fi fi } @@ -57,7 +84,15 @@ docker build -t codefirst-nginx "$root_dir/test/nginx" test_doxygen() { run_image main 'doxygen -g > /dev/null && /entrypoint.sh -b main -l . -t doxygen -d sourcecode_documentation' assert_file_exists sourcecode_documentation/doxygen/index.html - docker volume rm codefirst-nginx + cleanup +} + +test_swagger() { + run_image main 'touch api.yaml && /entrypoint.sh --type swagger --loc api.yaml' + assert_file_exists swagger/api.yaml + assert_file_contains swagger/CLICKME.html '/swagger?url=/documentation/octocat/hello-world/swagger/api.yaml' + cleanup } test_doxygen +test_swagger