Skip to content

Commit

Permalink
fix shellcheck and add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
shizunge committed Jan 18, 2024
1 parent 0ada34b commit 97d08d4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
20 changes: 11 additions & 9 deletions src/lib-gantry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}"
Expand Down
1 change: 1 addition & 0 deletions tests/run_all_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
44 changes: 43 additions & 1 deletion tests/test_entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}"
Expand Down

0 comments on commit 97d08d4

Please sign in to comment.