Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tests] Add tests for GANTRY_NOTIFICATION_CONDITION
Browse files Browse the repository at this point in the history
shizunge committed Sep 20, 2024
1 parent 5d0dad1 commit 7fd2c71
Showing 3 changed files with 260 additions and 27 deletions.
1 change: 1 addition & 0 deletions src/lib-gantry.sh
Original file line number Diff line number Diff line change
@@ -405,6 +405,7 @@ _report_services() {
case "${CONDITION}" in
"on-change")
if [ "${NUM_UPDATED}" = "0" ] && [ "${NUM_TOTAL_ERRORS}" = "0" ]; then
log DEBUG "Skip sending notification because there are no updates or errors."
SEND_NOTIFICATION="false"
fi
;;
284 changes: 257 additions & 27 deletions tests/gantry_notify_spec.sh
Original file line number Diff line number Diff line change
@@ -15,50 +15,77 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#

export SEND_NOTIFY_APPRISE="Sent notification via Apprise"
export SKIP_NOTIFY_APPRISE="Skip sending notification via Apprise"
export SKIP_NO_UPDATES="Skip sending notification because there are no updates or errors"

SERVICE_NAME_APPRISE="gantry-test-$(unique_id)-apprise"
export SERVICE_NAME_APPRISE
# APPRISE_PORT is hard coded in the Apprise container.
export APPRISE_PORT=8000
export SMTP_PORT=1025

notify_before_all() {
local SUITE_NAME="${1}"
initialize_all_tests "${SUITE_NAME}"
docker pull caronc/apprise
docker pull axllent/mailpit
# Use docker_run to improve coverage on lib-common.sh. `docker run` can do the same thing.
docker_run -d --restart=on-failure:10 --name="${SERVICE_NAME_APPRISE}" --network=host \
-e "APPRISE_STATELESS_URLS=mailto://localhost:${SMTP_PORT}?user=userid&pass=password" \
caronc/apprise
}

notify_after_all() {
local SUITE_NAME="${1}"
echo "Print Apprise log:"
docker logs "${SERVICE_NAME_APPRISE}" 2>&1
docker_remove "${SERVICE_NAME_APPRISE}" 2>&1
finish_all_tests "${SUITE_NAME}"
}

setup_emails() {
local SERVICE_NAME_MAILPIT="${1}"
local EMAIL_API_PORT="${2}"
docker_run -d --restart=on-failure:10 --name="${SERVICE_NAME_MAILPIT}" --network=host \
axllent/mailpit \
--smtp "localhost:${SMTP_PORT}" --listen "localhost:${EMAIL_API_PORT}" \
--smtp-auth-accept-any --smtp-auth-allow-insecure
}

print_and_cleanup_emails() {
local SERVICE_NAME_MAILPIT="${1}"
local EMAIL_API_PORT="${2}"
echo "Print emails:"
curl --silent "localhost:${EMAIL_API_PORT}/api/v1/messages" 2>&1
echo ""
echo "Print Mailpit log:"
docker logs "${SERVICE_NAME_MAILPIT}" 2>&1
# Use docker_remove to improve coverage on lib-common.sh. `docker stop` and `docker rm` can do the same thing.
docker_remove "${SERVICE_NAME_MAILPIT}" 2>&1
}

Describe 'notify'
SUITE_NAME="notify"
BeforeAll "initialize_all_tests ${SUITE_NAME}"
AfterAll "finish_all_tests ${SUITE_NAME}"
BeforeAll "notify_before_all ${SUITE_NAME}"
AfterAll "notify_after_all ${SUITE_NAME}"
Describe "test_notify_apprise" "container_test:true"
TEST_NAME="test_notify_apprise"
IMAGE_WITH_TAG=$(get_image_with_tag "${SUITE_NAME}")
SERVICE_NAME="gantry-test-$(unique_id)"
test_notify_apprise() {
local TEST_NAME="${1}"
local SERVICE_NAME="${2}"
local SERVICE_NAME_MAILPIT="${SERVICE_NAME}-mailpit"
local RETURN_VALUE=0
local APPRISE_PORT=8000
local SMTP_PORT=1025
local EMAIL_API_PORT=8025
local SERVICE_NAME_APPRISE="${SERVICE_NAME}-apprise"
local SERVICE_NAME_MAILPIT="${SERVICE_NAME}-mailpit"
docker pull caronc/apprise
docker pull axllent/mailpit
# Use docker_run to improve coverage on lib-common.sh. `docker run` can do the same thing.
docker_run -d --restart=on-failure:10 --name="${SERVICE_NAME_APPRISE}" --network=host \
-e "APPRISE_STATELESS_URLS=mailto://localhost:${SMTP_PORT}?user=userid&pass=password" \
caronc/apprise
docker_run -d --restart=on-failure:10 --name="${SERVICE_NAME_MAILPIT}" --network=host \
axllent/mailpit \
--smtp "localhost:${SMTP_PORT}" --listen "localhost:${EMAIL_API_PORT}" \
--smtp-auth-accept-any --smtp-auth-allow-insecure
setup_emails "${SERVICE_NAME_MAILPIT}" "${EMAIL_API_PORT}"
reset_gantry_env "${SERVICE_NAME}"
export GANTRY_NOTIFICATION_APPRISE_URL="http://localhost:${APPRISE_PORT}/notify"
export GANTRY_NOTIFICATION_TITLE="TEST_TITLE"
run_gantry "${TEST_NAME}"
RETURN_VALUE="${?}"
echo "Print emails:"
curl --silent "localhost:${EMAIL_API_PORT}/api/v1/messages" 2>&1
echo ""
echo "Print Apprise log:"
docker logs "${SERVICE_NAME_APPRISE}" 2>&1
echo "Print Mailpit log:"
docker logs "${SERVICE_NAME_MAILPIT}" 2>&1
# Use docker_remove to improve coverage on lib-common.sh. `docker stop` and `docker rm` can do the same thing.
docker_remove "${SERVICE_NAME_APPRISE}"
docker_remove "${SERVICE_NAME_MAILPIT}"
print_and_cleanup_emails "${SERVICE_NAME_MAILPIT}" "${EMAIL_API_PORT}"
return "${RETURN_VALUE}"
}
BeforeEach "common_setup_new_image ${TEST_NAME} ${IMAGE_WITH_TAG} ${SERVICE_NAME}"
@@ -90,7 +117,59 @@ Describe 'notify'
The stderr should satisfy spec_expect_message "${REMOVED_IMAGE}.*${IMAGE_WITH_TAG}"
The stderr should satisfy spec_expect_no_message "${FAILED_TO_REMOVE_IMAGE}.*${IMAGE_WITH_TAG}"
The stderr should satisfy spec_expect_no_message "${SKIP_NOTIFY_APPRISE}"
The stderr should satisfy spec_expect_message "Sent notification via Apprise"
The stderr should satisfy spec_expect_message "${SEND_NOTIFY_APPRISE}"
End
End
Describe "test_notify_apprise_no_new_image" "container_test:true"
TEST_NAME="test_notify_apprise_no_new_image"
IMAGE_WITH_TAG=$(get_image_with_tag "${SUITE_NAME}")
SERVICE_NAME="gantry-test-$(unique_id)"
test_notify_apprise_no_new_image() {
local TEST_NAME="${1}"
local SERVICE_NAME="${2}"
local SERVICE_NAME_MAILPIT="${SERVICE_NAME}-mailpit"
local RETURN_VALUE=0
local EMAIL_API_PORT=8025
setup_emails "${SERVICE_NAME_MAILPIT}" "${EMAIL_API_PORT}"
reset_gantry_env "${SERVICE_NAME}"
export GANTRY_NOTIFICATION_APPRISE_URL="http://localhost:${APPRISE_PORT}/notify"
export GANTRY_NOTIFICATION_TITLE="TEST_TITLE"
run_gantry "${TEST_NAME}"
RETURN_VALUE="${?}"
print_and_cleanup_emails "${SERVICE_NAME_MAILPIT}" "${EMAIL_API_PORT}"
return "${RETURN_VALUE}"
}
BeforeEach "common_setup_no_new_image ${TEST_NAME} ${IMAGE_WITH_TAG} ${SERVICE_NAME}"
AfterEach "common_cleanup ${TEST_NAME} ${IMAGE_WITH_TAG} ${SERVICE_NAME}"
It 'run_test'
When run test_notify_apprise_no_new_image "${TEST_NAME}" "${SERVICE_NAME}"
The status should be success
The stdout should satisfy display_output
The stdout should satisfy spec_expect_message "Subject.*0 services updated 0 failed TEST_TITLE"
The stderr should satisfy display_output
The stderr should satisfy spec_expect_message "${SKIP_UPDATING}.*${SERVICE_NAME}.*${SKIP_REASON_CURRENT_IS_LATEST}"
The stderr should satisfy spec_expect_no_message "${PERFORM_UPDATING}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_no_message "${NUM_SERVICES_SKIP_JOBS}"
The stderr should satisfy spec_expect_no_message "${NUM_SERVICES_INSPECT_FAILURE}"
The stderr should satisfy spec_expect_message "${NUM_SERVICES_NO_NEW_IMAGES}"
The stderr should satisfy spec_expect_no_message "${NUM_SERVICES_UPDATING}"
The stderr should satisfy spec_expect_no_message "${ADDING_OPTIONS}"
The stderr should satisfy spec_expect_no_message "${UPDATED}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_no_message "${NO_UPDATES}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_no_message "${ROLLING_BACK}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_no_message "${FAILED_TO_ROLLBACK}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_no_message "${ROLLED_BACK}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_message "${NO_SERVICES_UPDATED}"
The stderr should satisfy spec_expect_no_message "${NUM_SERVICES_UPDATED}"
The stderr should satisfy spec_expect_no_message "${NUM_SERVICES_UPDATE_FAILED}"
The stderr should satisfy spec_expect_no_message "${NUM_SERVICES_ERRORS}"
The stderr should satisfy spec_expect_message "${NO_IMAGES_TO_REMOVE}"
The stderr should satisfy spec_expect_no_message "${REMOVING_NUM_IMAGES}"
The stderr should satisfy spec_expect_no_message "${SKIP_REMOVING_IMAGES}"
The stderr should satisfy spec_expect_no_message "${REMOVED_IMAGE}.*${IMAGE_WITH_TAG}"
The stderr should satisfy spec_expect_no_message "${FAILED_TO_REMOVE_IMAGE}.*${IMAGE_WITH_TAG}"
The stderr should satisfy spec_expect_no_message "${SKIP_NOTIFY_APPRISE}"
The stderr should satisfy spec_expect_message "${SEND_NOTIFY_APPRISE}"
End
End
Describe "test_notify_apprise_bad_url" "container_test:true"
@@ -135,4 +214,155 @@ Describe 'notify'
The stderr should satisfy spec_expect_message "Failed to send notification via Apprise"
End
End
Describe "test_notify_on_change_new_image" "container_test:true"
TEST_NAME="test_notify_on_change_new_image"
IMAGE_WITH_TAG=$(get_image_with_tag "${SUITE_NAME}")
SERVICE_NAME="gantry-test-$(unique_id)"
test_notify_on_change_new_image() {
local TEST_NAME="${1}"
local SERVICE_NAME="${2}"
local SERVICE_NAME_MAILPIT="${SERVICE_NAME}-mailpit"
local RETURN_VALUE=0
local EMAIL_API_PORT=8025
setup_emails "${SERVICE_NAME_MAILPIT}" "${EMAIL_API_PORT}"
reset_gantry_env "${SERVICE_NAME}"
export GANTRY_NOTIFICATION_APPRISE_URL="http://localhost:${APPRISE_PORT}/notify"
export GANTRY_NOTIFICATION_CONDITION="on-change"
export GANTRY_NOTIFICATION_TITLE="TEST_TITLE"
run_gantry "${TEST_NAME}"
RETURN_VALUE="${?}"
print_and_cleanup_emails "${SERVICE_NAME_MAILPIT}" "${EMAIL_API_PORT}"
return "${RETURN_VALUE}"
}
BeforeEach "common_setup_new_image ${TEST_NAME} ${IMAGE_WITH_TAG} ${SERVICE_NAME}"
AfterEach "common_cleanup ${TEST_NAME} ${IMAGE_WITH_TAG} ${SERVICE_NAME}"
It 'run_test'
When run test_notify_on_change_new_image "${TEST_NAME}" "${SERVICE_NAME}"
The status should be success
The stdout should satisfy display_output
The stdout should satisfy spec_expect_message "Subject.*1 services updated 0 failed TEST_TITLE"
The stderr should satisfy display_output
The stderr should satisfy spec_expect_no_message "${SKIP_UPDATING}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_message "${PERFORM_UPDATING}.*${SERVICE_NAME}.*${PERFORM_REASON_HAS_NEWER_IMAGE}"
The stderr should satisfy spec_expect_no_message "${NUM_SERVICES_SKIP_JOBS}"
The stderr should satisfy spec_expect_no_message "${NUM_SERVICES_INSPECT_FAILURE}"
The stderr should satisfy spec_expect_no_message "${NUM_SERVICES_NO_NEW_IMAGES}"
The stderr should satisfy spec_expect_message "${NUM_SERVICES_UPDATING}"
The stderr should satisfy spec_expect_message "${UPDATED}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_no_message "${NO_UPDATES}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_no_message "${ROLLING_BACK}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_no_message "${FAILED_TO_ROLLBACK}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_no_message "${ROLLED_BACK}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_no_message "${NO_SERVICES_UPDATED}"
The stderr should satisfy spec_expect_message "1 ${SERVICES_UPDATED}"
The stderr should satisfy spec_expect_no_message "${NUM_SERVICES_UPDATE_FAILED}"
The stderr should satisfy spec_expect_no_message "${NUM_SERVICES_ERRORS}"
The stderr should satisfy spec_expect_no_message "${NO_IMAGES_TO_REMOVE}"
The stderr should satisfy spec_expect_message "${REMOVING_NUM_IMAGES}"
The stderr should satisfy spec_expect_no_message "${SKIP_REMOVING_IMAGES}"
The stderr should satisfy spec_expect_message "${REMOVED_IMAGE}.*${IMAGE_WITH_TAG}"
The stderr should satisfy spec_expect_no_message "${FAILED_TO_REMOVE_IMAGE}.*${IMAGE_WITH_TAG}"
The stderr should satisfy spec_expect_no_message "${SKIP_NOTIFY_APPRISE}"
The stderr should satisfy spec_expect_message "${SEND_NOTIFY_APPRISE}"
End
End
Describe "test_notify_on_change_no_updates" "container_test:true"
TEST_NAME="test_notify_on_change_no_updates"
IMAGE_WITH_TAG=$(get_image_with_tag "${SUITE_NAME}")
SERVICE_NAME="gantry-test-$(unique_id)"
test_notify_on_change_no_updates() {
local TEST_NAME="${1}"
local SERVICE_NAME="${2}"
local RETURN_VALUE=0
reset_gantry_env "${SERVICE_NAME}"
export GANTRY_NOTIFICATION_CONDITION="on-change"
export GANTRY_NOTIFICATION_TITLE="TEST_TITLE"
run_gantry "${TEST_NAME}"
RETURN_VALUE="${?}"
return "${RETURN_VALUE}"
}
BeforeEach "common_setup_no_new_image ${TEST_NAME} ${IMAGE_WITH_TAG} ${SERVICE_NAME}"
AfterEach "common_cleanup ${TEST_NAME} ${IMAGE_WITH_TAG} ${SERVICE_NAME}"
It 'run_test'
When run test_notify_on_change_no_updates "${TEST_NAME}" "${SERVICE_NAME}"
The status should be success
The stdout should satisfy display_output
The stdout should satisfy spec_expect_no_message "TEST_TITLE"
The stderr should satisfy display_output
The stderr should satisfy spec_expect_message "${SKIP_UPDATING}.*${SERVICE_NAME}.*${SKIP_REASON_CURRENT_IS_LATEST}"
The stderr should satisfy spec_expect_no_message "${PERFORM_UPDATING}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_no_message "${NUM_SERVICES_SKIP_JOBS}"
The stderr should satisfy spec_expect_no_message "${NUM_SERVICES_INSPECT_FAILURE}"
The stderr should satisfy spec_expect_message "${NUM_SERVICES_NO_NEW_IMAGES}"
The stderr should satisfy spec_expect_no_message "${NUM_SERVICES_UPDATING}"
The stderr should satisfy spec_expect_no_message "${ADDING_OPTIONS}"
The stderr should satisfy spec_expect_no_message "${UPDATED}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_no_message "${NO_UPDATES}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_no_message "${ROLLING_BACK}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_no_message "${FAILED_TO_ROLLBACK}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_no_message "${ROLLED_BACK}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_message "${NO_SERVICES_UPDATED}"
The stderr should satisfy spec_expect_no_message "${NUM_SERVICES_UPDATED}"
The stderr should satisfy spec_expect_no_message "${NUM_SERVICES_UPDATE_FAILED}"
The stderr should satisfy spec_expect_no_message "${NUM_SERVICES_ERRORS}"
The stderr should satisfy spec_expect_message "${NO_IMAGES_TO_REMOVE}"
The stderr should satisfy spec_expect_no_message "${REMOVING_NUM_IMAGES}"
The stderr should satisfy spec_expect_no_message "${SKIP_REMOVING_IMAGES}"
The stderr should satisfy spec_expect_no_message "${REMOVED_IMAGE}.*${IMAGE_WITH_TAG}"
The stderr should satisfy spec_expect_no_message "${FAILED_TO_REMOVE_IMAGE}.*${IMAGE_WITH_TAG}"
The stderr should satisfy spec_expect_message "${SKIP_NO_UPDATES}"
End
End
Describe "test_notify_on_change_errors" "container_test:false"
TEST_NAME="test_notify_on_change_errors"
IMAGE_WITH_TAG=$(get_image_with_tag "${SUITE_NAME}")
SERVICE_NAME="gantry-test-$(unique_id)"
test_notify_on_change_errors() {
local TEST_NAME="${1}"
local SERVICE_NAME="${2}"
local SERVICE_NAME_MAILPIT="${SERVICE_NAME}-mailpit"
local RETURN_VALUE=0
local EMAIL_API_PORT=8025
setup_emails "${SERVICE_NAME_MAILPIT}" "${EMAIL_API_PORT}"
reset_gantry_env "${SERVICE_NAME}"
export GANTRY_UPDATE_OPTIONS="--bad-options-that-causes-error"
export GANTRY_NOTIFICATION_APPRISE_URL="http://localhost:${APPRISE_PORT}/notify"
export GANTRY_NOTIFICATION_CONDITION="on-change"
export GANTRY_NOTIFICATION_TITLE="TEST_TITLE"
run_gantry "${TEST_NAME}"
RETURN_VALUE="${?}"
print_and_cleanup_emails "${SERVICE_NAME_MAILPIT}" "${EMAIL_API_PORT}"
return "${RETURN_VALUE}"
}
BeforeEach "common_setup_new_image ${TEST_NAME} ${IMAGE_WITH_TAG} ${SERVICE_NAME}"
AfterEach "common_cleanup ${TEST_NAME} ${IMAGE_WITH_TAG} ${SERVICE_NAME}"
It 'run_test'
When run test_notify_on_change_errors "${TEST_NAME}" "${SERVICE_NAME}"
The status should be failure
The stdout should satisfy display_output
The stdout should satisfy spec_expect_message "Subject.*0 services updated 1 failed TEST_TITLE"
The stderr should satisfy display_output
The stderr should satisfy spec_expect_no_message "${SKIP_UPDATING}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_message "${PERFORM_UPDATING}.*${SERVICE_NAME}.*${PERFORM_REASON_HAS_NEWER_IMAGE}"
The stderr should satisfy spec_expect_no_message "${NUM_SERVICES_SKIP_JOBS}"
The stderr should satisfy spec_expect_no_message "${NUM_SERVICES_INSPECT_FAILURE}"
The stderr should satisfy spec_expect_no_message "${NUM_SERVICES_NO_NEW_IMAGES}"
The stderr should satisfy spec_expect_message "${NUM_SERVICES_UPDATING}"
The stderr should satisfy spec_expect_no_message "${UPDATED}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_no_message "${NO_UPDATES}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_message "${ROLLING_BACK}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_message "${FAILED_TO_ROLLBACK}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_no_message "${ROLLED_BACK}.*${SERVICE_NAME}"
The stderr should satisfy spec_expect_message "${NO_SERVICES_UPDATED}"
The stderr should satisfy spec_expect_no_message "${NUM_SERVICES_UPDATED}"
The stderr should satisfy spec_expect_message "${NUM_SERVICES_UPDATE_FAILED}"
The stderr should satisfy spec_expect_no_message "${NUM_SERVICES_ERRORS}"
The stderr should satisfy spec_expect_message "${NO_IMAGES_TO_REMOVE}"
The stderr should satisfy spec_expect_no_message "${REMOVING_NUM_IMAGES}"
The stderr should satisfy spec_expect_no_message "${SKIP_REMOVING_IMAGES}"
The stderr should satisfy spec_expect_no_message "${REMOVED_IMAGE}.*${IMAGE_WITH_TAG}"
The stderr should satisfy spec_expect_no_message "${SKIP_NOTIFY_APPRISE}"
The stderr should satisfy spec_expect_message "${SEND_NOTIFY_APPRISE}"
End
End
End # Describe 'Notify'
2 changes: 2 additions & 0 deletions tests/spec_gantry_test_helper.sh
Original file line number Diff line number Diff line change
@@ -344,6 +344,7 @@ reset_gantry_env() {
export GANTRY_CLEANUP_IMAGES_REMOVER=ghcr.io/shizunge/gantry-development
export GANTRY_IMAGES_TO_REMOVE=
export GANTRY_NOTIFICATION_APPRISE_URL=
export GANTRY_NOTIFICATION_CONDITION=
export GANTRY_NOTIFICATION_TITLE=
}

@@ -681,6 +682,7 @@ _run_gantry_container() {
--env "GANTRY_CLEANUP_IMAGES_REMOVER=${GANTRY_CLEANUP_IMAGES_REMOVER}" \
--env "GANTRY_IMAGES_TO_REMOVE=${GANTRY_IMAGES_TO_REMOVE}" \
--env "GANTRY_NOTIFICATION_APPRISE_URL=${GANTRY_NOTIFICATION_APPRISE_URL}" \
--enc "GANTRY_NOTIFICATION_CONDITION=${GANTRY_NOTIFICATION_CONDITION}" \
--env "GANTRY_NOTIFICATION_TITLE=${GANTRY_NOTIFICATION_TITLE}" \
--env "TZ=${TZ}" \
"${SUT_REPO_TAG}" \

0 comments on commit 7fd2c71

Please sign in to comment.