Skip to content

Commit

Permalink
[tests] Add a test for GANTRY_SLEEP_SECONDS
Browse files Browse the repository at this point in the history
  • Loading branch information
shizunge committed Sep 29, 2024
1 parent f2d4aa4 commit e09e82c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tests/gantry_common_options_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/gantry_service_single_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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'
30 changes: 30 additions & 0 deletions tests/spec_gantry_test_helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#

set -a

# Constant strings for checks.
export MUST_BE_A_NUMBER="must be a number"
export SKIP_UPDATING_ALL="Skip updating all services"
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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}"
}
Expand Down Expand Up @@ -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}"
Expand Down Expand Up @@ -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}"
}

Expand Down Expand Up @@ -707,3 +735,5 @@ run_gantry() {
ENTRYPOINT=$(_get_entrypoint) || return 1
${ENTRYPOINT} "${STACK}"
}

set +a

0 comments on commit e09e82c

Please sign in to comment.