diff --git a/tests/gantry_common_options_spec.sh b/tests/gantry_common_options_spec.sh index a031cb9..3da56c2 100644 --- a/tests/gantry_common_options_spec.sh +++ b/tests/gantry_common_options_spec.sh @@ -62,6 +62,7 @@ Describe 'common-options' 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 "${SCHEDULE_NEXT_UPDATE_AT}" The stderr should satisfy spec_expect_no_message "${SLEEP_SECONDS_BEFORE_NEXT_UPDATE}" End End @@ -129,6 +130,52 @@ Describe 'common-options' 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_message "Post update" + The stderr should satisfy spec_expect_no_message "${SCHEDULE_NEXT_UPDATE_AT}" + The stderr should satisfy spec_expect_no_message "${SLEEP_SECONDS_BEFORE_NEXT_UPDATE}" + End + End + Describe "test_common_SLEEP_SECONDS" "container_test:false" + TEST_NAME="test_common_SLEEP_SECONDS" + IMAGE_WITH_TAG=$(get_image_with_tag "${SUITE_NAME}") + SERVICE_NAME="gantry-test-$(unique_id)" + test_common_SLEEP_SECONDS() { + local TEST_NAME="${1}" + local SERVICE_NAME="${2}" + reset_gantry_env "${SERVICE_NAME}" + export GANTRY_SLEEP_SECONDS="5" + timeout --preserve-status 15 bash -c "run_gantry ${TEST_NAME}" + } + 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_common_SLEEP_SECONDS "${TEST_NAME}" "${SERVICE_NAME}" + The status should be failure + The stdout should satisfy display_output + The stderr should satisfy display_output + The stderr should satisfy spec_expect_multiple_messages "${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_multiple_messages "${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_multiple_messages "${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_multiple_messages "${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}" + # Check messages between iterations. + The stderr should satisfy spec_expect_message "${SCHEDULE_NEXT_UPDATE_AT}" + The stderr should satisfy spec_expect_message "${SLEEP_SECONDS_BEFORE_NEXT_UPDATE}" End End Describe "test_common_SLEEP_SECONDS_not_a_number" "container_test:false" @@ -171,6 +218,7 @@ Describe 'common-options' 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 "${SCHEDULE_NEXT_UPDATE_AT}" The stderr should satisfy spec_expect_no_message "${SLEEP_SECONDS_BEFORE_NEXT_UPDATE}" End End diff --git a/tests/gantry_service_single_spec.sh b/tests/gantry_service_single_spec.sh index 53f0599..061963b 100644 --- a/tests/gantry_service_single_spec.sh +++ b/tests/gantry_service_single_spec.sh @@ -57,6 +57,7 @@ Describe 'service-single-service' 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 "${SCHEDULE_NEXT_UPDATE_AT}" End End Describe "test_new_image_yes" "container_test:true" @@ -97,6 +98,7 @@ Describe 'service-single-service' 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 "${SCHEDULE_NEXT_UPDATE_AT}" End End Describe "test_new_image_no_digest" "container_test:true" @@ -148,6 +150,7 @@ Describe 'service-single-service' # Failed to removing the old image due to it has no digest. The stderr should satisfy spec_expect_no_message "${REMOVED_IMAGE}.*${IMAGE_WITH_TAG}" The stderr should satisfy spec_expect_message "${FAILED_TO_REMOVE_IMAGE}.*${IMAGE_WITH_TAG}" + The stderr should satisfy spec_expect_no_message "${SCHEDULE_NEXT_UPDATE_AT}" End End End # Describe 'Simple' diff --git a/tests/spec_gantry_test_helper.sh b/tests/spec_gantry_test_helper.sh index 848716b..08f2cc3 100644 --- a/tests/spec_gantry_test_helper.sh +++ b/tests/spec_gantry_test_helper.sh @@ -15,6 +15,8 @@ # along with this program. If not, see . # +set -a + # Constant strings for checks. export MUST_BE_A_NUMBER="must be a number" export SKIP_UPDATING_ALL="Skip updating all services" @@ -51,6 +53,7 @@ export REMOVING_NUM_IMAGES="Removing [0-9]+ image\(s\)" export SKIP_REMOVING_IMAGES="Skip removing images" export REMOVED_IMAGE="Removed image" export FAILED_TO_REMOVE_IMAGE="Failed to remove image" +export SCHEDULE_NEXT_UPDATE_AT="Schedule next update at" export SLEEP_SECONDS_BEFORE_NEXT_UPDATE="Sleep [0-9]+ seconds before next update" display_output() { @@ -155,6 +158,10 @@ spec_expect_message() { _expect_message "${spec_expect_message:-""}" "${1}" } +spec_expect_multiple_messages() { + _expect_multiple_messages "${spec_expect_multiple_messages:-""}" "${1}" +} + spec_expect_no_message() { _expect_no_message "${spec_expect_no_message:-""}" "${1}" } @@ -387,6 +394,22 @@ _handle_failure() { echo -e "${RED}ERROR${NO_COLOR} ${MESSAGE}" } +_expect_multiple_messages() { + TEXT="${1}" + MESSAGE="${2}" + local GREEN='\033[0;32m' + local NO_COLOR='\033[0m' + if ! ACTUAL_MSG=$(echo "${TEXT}" | grep -Po "${MESSAGE}"); then + _handle_failure "Failed to find expected message \"${MESSAGE}\"." + return 1 + fi + if ! COUNT=$(echo "${TEXT}" | grep -Poc "${MESSAGE}"); then + _handle_failure "Failed to find multiple expected messages \"${MESSAGE}\" COUNT=${COUNT}." + return 1 + fi + echo -e "${GREEN}EXPECTED${NO_COLOR} found ${COUNT} messages: ${ACTUAL_MSG}" +} + _expect_message() { TEXT="${1}" MESSAGE="${2}" @@ -598,6 +621,11 @@ _get_script_dir() { # SC3054 (warning): In POSIX sh, array references are undefined. # shellcheck disable=SC3054 SCRIPT_DIR="$( cd "$(dirname "${BASH_SOURCE[0]}")" || return 1; pwd -P )" + if [ -d "${SCRIPT_DIR}/.git" ]; then + # We may reach here if SCRIPT_DIR is "pwd -P". + # Assume .git is in the project root folder, but not in the tests folder. + SCRIPT_DIR="${SCRIPT_DIR}/tests" + fi echo "${SCRIPT_DIR}" } @@ -707,3 +735,5 @@ run_gantry() { ENTRYPOINT=$(_get_entrypoint) || return 1 ${ENTRYPOINT} "${STACK}" } + +set +a