From 97d08d486a15c81177f63000b7ed88dd9f2b2a18 Mon Sep 17 00:00:00 2001 From: Shizun Ge Date: Wed, 17 Jan 2024 21:57:01 -0800 Subject: [PATCH] fix shellcheck and add a test --- src/lib-gantry.sh | 20 ++++++++++-------- tests/run_all_tests.sh | 1 + tests/test_entrypoint.sh | 44 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/src/lib-gantry.sh b/src/lib-gantry.sh index 46bad91..10d6deb 100755 --- a/src/lib-gantry.sh +++ b/src/lib-gantry.sh @@ -248,22 +248,23 @@ in_list() { current_container_name() { local ALL_NETWORKS GWBRIDGE_NETWORK IPS; - ALL_NETWORKS=$(docker network ls --format {{.ID}}) || return 1; + ALL_NETWORKS=$(docker network ls --format '{{.ID}}') || return 1; [ -z "${ALL_NETWORKS}" ] && return 0; - GWBRIDGE_NETWORK=$(docker network ls --format {{.ID}} --filter "name=docker_gwbridge") || return 1; + GWBRIDGE_NETWORK=$(docker network ls --format '{{.ID}}' --filter 'name=docker_gwbridge') || return 1; IPS=$(ip route | grep src | sed -n "s/.* src \(\S*\).*$/\1/p"); [ -z "${IPS}" ] && return 0; local NID; for NID in ${ALL_NETWORKS}; do [ "${NID}" = "${GWBRIDGE_NETWORK}" ] && continue; local ALL_LOCAL_NAME_AND_IP; - ALL_LOCAL_NAME_AND_IP=$(docker network inspect ${NID} --format "{{range .Containers}}{{.Name}}={{println .IPv4Address}}{{end}}") || return 1; + ALL_LOCAL_NAME_AND_IP=$(docker network inspect "${NID}" --format "{{range .Containers}}{{.Name}}={{println .IPv4Address}}{{end}}") || return 1; for NAME_AND_IP in ${ALL_LOCAL_NAME_AND_IP}; do [ -z "${NAME_AND_IP}" ] && continue; for IP in ${IPS}; do - [ ! $(echo ${NAME_AND_IP} | grep ${IP}) ] && continue; - local NAME=$(echo ${NAME_AND_IP} | sed "s/\(.*\)=${IP}.*$/\1/"); - echo ${NAME}; + echo "${NAME_AND_IP}" | grep -q "${IP}" || continue; + local NAME; + NAME=$(echo "${NAME_AND_IP}" | sed "s/\(.*\)=${IP}.*$/\1/"); + echo "${NAME}"; return 0; done; done; @@ -275,13 +276,14 @@ current_service_name() { local CNAME CNAME=$(current_container_name) || return 1 [ -z "${CNAME}" ] && return 0 - SNAME=$(docker container inspect ${CNAME} --format '{{range $key,$value := .Config.Labels}}{{$key}}={{println $value}}{{end}}' | grep "com.docker.swarm.service.name" | sed "s/com.docker.swarm.service.name=\(.*\)$/\1/") || return 1 - echo ${SNAME} + SNAME=$(docker container inspect "${CNAME}" --format '{{range $key,$value := .Config.Labels}}{{$key}}={{println $value}}{{end}}' | grep "com.docker.swarm.service.name" | sed "s/com.docker.swarm.service.name=\(.*\)$/\1/") || return 1 + echo "${SNAME}" } service_is_self() { if [ -z "${GANTRY_SERVICES_SELF}" ]; then - export GANTRY_SERVICES_SELF=$(current_service_name) + GANTRY_SERVICES_SELF=$(current_service_name) + export GANTRY_SERVICES_SELF [ -n "${GANTRY_SERVICES_SELF}" ] && log INFO "Set GANTRY_SERVICES_SELF to ${GANTRY_SERVICES_SELF}." fi local SELF="${GANTRY_SERVICES_SELF}" diff --git a/tests/run_all_tests.sh b/tests/run_all_tests.sh index 478ba52..9946ed6 100755 --- a/tests/run_all_tests.sh +++ b/tests/run_all_tests.sh @@ -92,6 +92,7 @@ main() { test_jobs_UPDATE_JOBS_true \ test_jobs_UPDATE_JOBS_true_no_running_tasks \ test_MANIFEST_CMD_none \ + test_MANIFEST_CMD_none_not_set_SERVICES_SELF \ test_MANIFEST_CMD_none_SERVICES_SELF \ test_MANIFEST_CMD_manifest \ test_no_running_tasks_replicated \ diff --git a/tests/test_entrypoint.sh b/tests/test_entrypoint.sh index e5763ab..720b66d 100755 --- a/tests/test_entrypoint.sh +++ b/tests/test_entrypoint.sh @@ -494,7 +494,7 @@ test_MANIFEST_CMD_none() { finalize_test "${FUNCNAME[0]}" } -test_MANIFEST_CMD_none_SERVICES_SELF() { +test_MANIFEST_CMD_none_not_set_SERVICES_SELF() { # If the service is self, it will always run manifest checking. local IMAGE_WITH_TAG="${1}" local SERVICE_NAME STDOUT @@ -505,11 +505,53 @@ test_MANIFEST_CMD_none_SERVICES_SELF() { start_replicated_service "${SERVICE_NAME}" "${IMAGE_WITH_TAG}" # No image updates after service started. + export GANTRY_SERVICES_FILTERS="name=${SERVICE_NAME}" + export GANTRY_MANIFEST_CMD="none" + STDOUT=$(run_gantry "${FUNCNAME[0]}" 2>&1 | tee >(cat 1>&2)) + + # Do not set GANTRY_SERVICES_SELF, it should be set autoamtically + # If we are not testing gantry inside a container, it should failed to find the service name. + # To test gantry container, we need to use run_gantry_container. + expect_no_message "${STDOUT}" ".*GRANTRY_SERVICES_SELF.*" + expect_no_message "${STDOUT}" "${SKIP_UPDATING_SERVICE}.*${SERVICE_NAME}" + expect_message "${STDOUT}" "${SERVICE_NAME}.*${NO_NEW_IMAGE}" + expect_no_message "${STDOUT}" "${SERVICE_NAME}.*${UPDATED}" + expect_no_message "${STDOUT}" "${SERVICE_NAME}.*${NO_UPDATES}" + expect_no_message "${STDOUT}" "${ROLLING_BACK}.*${SERVICE_NAME}" + expect_no_message "${STDOUT}" "${FAILED_TO_ROLLBACK}.*${SERVICE_NAME}" + expect_no_message "${STDOUT}" "${ROLLED_BACK}.*${SERVICE_NAME}" + expect_message "${STDOUT}" "${NO_SERVICES_UPDATED}" + expect_no_message "${STDOUT}" "${NUM_SERVICES_UPDATED}" + expect_no_message "${STDOUT}" "${NUM_SERVICES_UPDATE_FAILED}" + expect_message "${STDOUT}" "${NO_IMAGES_TO_REMOVE}" + expect_no_message "${STDOUT}" "${REMOVING_NUM_IMAGES}" + expect_no_message "${STDOUT}" "${SKIP_REMOVING_IMAGES}" + expect_no_message "${STDOUT}" "${REMOVED_IMAGE}.*${IMAGE_WITH_TAG}" + expect_no_message "${STDOUT}" "${FAILED_TO_REMOVE_IMAGE}.*${IMAGE_WITH_TAG}" + + stop_service "${SERVICE_NAME}" + prune_local_test_image "${IMAGE_WITH_TAG}" + finalize_test "${FUNCNAME[0]}" +} + +test_MANIFEST_CMD_none_SERVICES_SELF() { + # If the service is self, it will always run manifest checking. Even if the CMD is set to none + local IMAGE_WITH_TAG="${1}" + local SERVICE_NAME STDOUT + SERVICE_NAME="gantry-test-$(unique_id)" + + initialize_test "${FUNCNAME[0]}" + build_and_push_test_image "${IMAGE_WITH_TAG}" + start_replicated_service "${SERVICE_NAME}" "${IMAGE_WITH_TAG}" + # No image updates after service started. + + # Explicitly set GANTRY_SERVICES_SELF export GANTRY_SERVICES_FILTERS="name=${SERVICE_NAME}" export GANTRY_SERVICES_SELF="${SERVICE_NAME}" export GANTRY_MANIFEST_CMD="none" STDOUT=$(run_gantry "${FUNCNAME[0]}" 2>&1 | tee >(cat 1>&2)) + expect_no_message "${STDOUT}" "Set GRANTRY_SERVICES_SELF to ${SERVICE_NAME}." expect_no_message "${STDOUT}" "${SKIP_UPDATING_SERVICE}.*${SERVICE_NAME}" expect_message "${STDOUT}" "${SERVICE_NAME}.*${NO_NEW_IMAGE}" expect_no_message "${STDOUT}" "${SERVICE_NAME}.*${UPDATED}"