Skip to content

Commit

Permalink
[tests] create gantry_jobs.spec.sh, improve setup time of tests.
Browse files Browse the repository at this point in the history
Try to reduce the conflict of ports.
  • Loading branch information
shizunge committed Nov 21, 2024
1 parent 2ef874d commit 39f4da7
Show file tree
Hide file tree
Showing 9 changed files with 373 additions and 328 deletions.
1 change: 1 addition & 0 deletions .github/workflows/on-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ jobs:
- gantry_cleanup_images_spec.sh
- gantry_common_options_spec.sh
- gantry_filters_spec.sh
- gantry_jobs_spec.sh
- gantry_login_docker_config_spec.sh
- gantry_login_negative_spec.sh
- gantry_login_spec.sh
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/on-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
- gantry_cleanup_images_spec.sh
- gantry_common_options_spec.sh
- gantry_filters_spec.sh
- gantry_jobs_spec.sh
- gantry_login_docker_config_spec.sh
- gantry_login_negative_spec.sh
- gantry_login_spec.sh
Expand Down
5 changes: 3 additions & 2 deletions tests/gantry_cleanup_images_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,13 @@ Describe 'cleanup-images'
local SERVICE_NAME0="${SERVICE_NAME}-0"
local SERVICE_NAME1="${SERVICE_NAME}-1"
local TASK_SECONDS=15
local TIMEOUT_SECONDS=1
initialize_test "${TEST_NAME}"
# The task will finish in ${TASK_SECONDS} seconds
build_and_push_test_image "${IMAGE_WITH_TAG0}" "${TASK_SECONDS}"
start_global_service "${SERVICE_NAME0}" "${IMAGE_WITH_TAG0}"
start_global_service "${SERVICE_NAME0}" "${IMAGE_WITH_TAG0}" "${TIMEOUT_SECONDS}"
build_and_push_test_image "${IMAGE_WITH_TAG1}"
start_global_service "${SERVICE_NAME1}" "${IMAGE_WITH_TAG1}"
start_global_service "${SERVICE_NAME1}" "${IMAGE_WITH_TAG1}" "${TIMEOUT_SECONDS}"
# The tasks should exit after TASK_SECONDS seconds sleep. Then it will have 0 running tasks.
wait_zero_running_tasks "${SERVICE_NAME0}"
# Do not creat the Image IMAGE_WITH_TAG2, to run the test on a non-exist image.
Expand Down
4 changes: 2 additions & 2 deletions tests/gantry_common_options_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ Describe 'common-options'
local TEST_NAME="${1}"
local SERVICE_NAME="${2}"
reset_gantry_env "${SERVICE_NAME}"
export GANTRY_SLEEP_SECONDS="5"
export GANTRY_SLEEP_SECONDS="7"
run_gantry "${TEST_NAME}" &
local PID="${!}"
sleep 15
sleep $((GANTRY_SLEEP_SECONDS*2+1))
kill "${PID}"
}
BeforeEach "common_setup_no_new_image ${TEST_NAME} ${IMAGE_WITH_TAG} ${SERVICE_NAME}"
Expand Down
245 changes: 245 additions & 0 deletions tests/gantry_jobs_spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
#!/bin/bash spellspec
# Copyright (C) 2024 Shizun Ge
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#

Describe 'update-jobs'
SUITE_NAME="update-jobs"
BeforeAll "initialize_all_tests ${SUITE_NAME}"
AfterAll "finish_all_tests ${SUITE_NAME}"
Describe "test_update_jobs_skipping" "container_test:true" "coverage:true"
# For `docker service ls --filter`, the name filter matches on all or the prefix of a service's name
# See https://docs.docker.com/engine/reference/commandline/service_ls/#name
# It does not do the exact match of the name. See https://github.com/moby/moby/issues/32985
# This test also checks whether we do an extra step to to perform the exact match.
TEST_NAME="test_update_jobs_skipping"
IMAGE_WITH_TAG=$(get_image_with_tag "${SUITE_NAME}")
SERVICE_NAME="gantry-test-$(unique_id)"
SERVICE_NAME_SUFFIX="${SERVICE_NAME}-suffix"
test_start() {
local TEST_NAME="${1}"
local IMAGE_WITH_TAG="${2}"
local SERVICE_NAME="${3}"
local SERVICE_NAME_SUFFIX="${SERVICE_NAME}-suffix"
common_setup_job "${TEST_NAME}" "${IMAGE_WITH_TAG}" "${SERVICE_NAME_SUFFIX}"
start_replicated_service "${SERVICE_NAME}" "${IMAGE_WITH_TAG}"
}
test_update_jobs_skipping() {
local TEST_NAME="${1}"
local SERVICE_NAME="${2}"
reset_gantry_env "${SERVICE_NAME}"
run_gantry "${TEST_NAME}"
}
test_end() {
local TEST_NAME="${1}"
local IMAGE_WITH_TAG="${2}"
local SERVICE_NAME="${3}"
local SERVICE_NAME_SUFFIX="${SERVICE_NAME}-suffix"
stop_service "${SERVICE_NAME}"
common_cleanup "${TEST_NAME}" "${IMAGE_WITH_TAG}" "${SERVICE_NAME_SUFFIX}"
}
BeforeEach "test_start ${TEST_NAME} ${IMAGE_WITH_TAG} ${SERVICE_NAME}"
AfterEach "test_end ${TEST_NAME} ${IMAGE_WITH_TAG} ${SERVICE_NAME}"
It 'run_test'
When run test_update_jobs_skipping "${TEST_NAME}" "${SERVICE_NAME}"
The status should be success
The stdout should satisfy display_output
The stdout should satisfy spec_expect_no_message ".+"
The stderr should satisfy display_output
The stderr should satisfy spec_expect_no_message "${START_WITHOUT_A_SQUARE_BRACKET}"
# Check whether it is a job before checking whether there is a new image.
The stderr should satisfy spec_expect_message "${SKIP_UPDATING}.*${SERVICE_NAME_SUFFIX}.*${SKIP_REASON_IS_JOB}"
The stderr should satisfy spec_expect_no_message "${PERFORM_UPDATING}.*${SERVICE_NAME_SUFFIX}"
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 "${SET_TIMEOUT_TO}"
The stderr should satisfy spec_expect_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 "${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}"
End
End
Describe "test_update_jobs_UPDATE_JOBS_true" "container_test:true" "coverage:true"
TEST_NAME="test_update_jobs_UPDATE_JOBS_true"
IMAGE_WITH_TAG=$(get_image_with_tag "${SUITE_NAME}")
SERVICE_NAME="gantry-test-$(unique_id)"
test_update_jobs_UPDATE_JOBS_true() {
local TEST_NAME="${1}"
local SERVICE_NAME="${2}"
reset_gantry_env "${SERVICE_NAME}"
export GANTRY_UPDATE_JOBS="true"
# The job may not reach the desired "Complete" state and blocking update CLI. So add "--detach=true"
export GANTRY_UPDATE_OPTIONS="--detach=true"
run_gantry "${TEST_NAME}"
}
BeforeEach "common_setup_job ${TEST_NAME} ${IMAGE_WITH_TAG} ${SERVICE_NAME}"
AfterEach "common_cleanup ${TEST_NAME} ${IMAGE_WITH_TAG} ${SERVICE_NAME}"
It 'run_test'
When run test_update_jobs_UPDATE_JOBS_true "${TEST_NAME}" "${SERVICE_NAME}"
The status should be success
The stdout should satisfy display_output
The stdout should satisfy spec_expect_no_message ".+"
The stderr should satisfy display_output
The stderr should satisfy spec_expect_no_message "${START_WITHOUT_A_SQUARE_BRACKET}"
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 "${SET_TIMEOUT_TO}"
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 "${ADDING_OPTIONS}.*--detach=true.*${SERVICE_NAME}"
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}"
# Since the job may not reach the desired state, they are still using the image. Image remover will fail.
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}"
End
End
Describe "test_update_jobs_label_UPDATE_JOBS_true" "container_test:true" "coverage:true"
TEST_NAME="test_update_jobs_label_UPDATE_JOBS_true"
IMAGE_WITH_TAG=$(get_image_with_tag "${SUITE_NAME}")
SERVICE_NAME="gantry-test-$(unique_id)"
test_update_jobs_label_UPDATE_JOBS_true() {
local TEST_NAME="${1}"
local SERVICE_NAME="${2}"
reset_gantry_env "${SERVICE_NAME}"
# label should override the global environment variable.
export GANTRY_UPDATE_JOBS="false"
local LABEL_AND_VALUE="gantry.update.jobs=true"
docker_service_update --detach=true --label-add "${LABEL_AND_VALUE}" "${SERVICE_NAME}"
# label should override the global environment variable.
export GANTRY_UPDATE_OPTIONS="--incorrect-option"
# The job may not reach the desired "Complete" state and blocking update CLI. So add "--detach=true"
LABEL_AND_VALUE="gantry.update.options=--detach=true"
docker_service_update --detach=true --label-add "${LABEL_AND_VALUE}" "${SERVICE_NAME}"
run_gantry "${TEST_NAME}"
}
BeforeEach "common_setup_job ${TEST_NAME} ${IMAGE_WITH_TAG} ${SERVICE_NAME}"
AfterEach "common_cleanup ${TEST_NAME} ${IMAGE_WITH_TAG} ${SERVICE_NAME}"
It 'run_test'
When run test_update_jobs_label_UPDATE_JOBS_true "${TEST_NAME}" "${SERVICE_NAME}"
The status should be success
The stdout should satisfy display_output
The stdout should satisfy spec_expect_no_message ".+"
The stderr should satisfy display_output
The stderr should satisfy spec_expect_no_message "${START_WITHOUT_A_SQUARE_BRACKET}"
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 "${SET_TIMEOUT_TO}"
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 "${ADDING_OPTIONS}.*--detach=true.*${SERVICE_NAME}"
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}"
# Since the job may not reach the desired state, they are still using the image. Image remover will fail.
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}"
End
End
Describe "test_update_jobs_no_running_tasks" "container_test:true" "coverage:true"
TEST_NAME="test_update_jobs_no_running_tasks"
IMAGE_WITH_TAG=$(get_image_with_tag "${SUITE_NAME}")
SERVICE_NAME="gantry-test-$(unique_id)"
TASK_SECONDS=1
test_start() {
local TEST_NAME="${1}"
local IMAGE_WITH_TAG="${2}"
local SERVICE_NAME="${3}"
local TASK_SECONDS="${4}"
common_setup_job "${TEST_NAME}" "${IMAGE_WITH_TAG}" "${SERVICE_NAME}" "${TASK_SECONDS}"
# The tasks should exit after TASK_SECONDS seconds sleep. Then it will have 0 running tasks.
wait_zero_running_tasks "${SERVICE_NAME}"
}
test_update_jobs_no_running_tasks() {
local TEST_NAME="${1}"
local SERVICE_NAME="${2}"
reset_gantry_env "${SERVICE_NAME}"
export GANTRY_UPDATE_JOBS="true"
run_gantry "${TEST_NAME}"
}
# The task will finish in ${TASK_SECONDS} seconds
BeforeEach "test_start ${TEST_NAME} ${IMAGE_WITH_TAG} ${SERVICE_NAME} ${TASK_SECONDS}"
AfterEach "common_cleanup ${TEST_NAME} ${IMAGE_WITH_TAG} ${SERVICE_NAME}"
It 'run_test'
When run test_update_jobs_no_running_tasks "${TEST_NAME}" "${SERVICE_NAME}"
The status should be success
The stdout should satisfy display_output
The stdout should satisfy spec_expect_no_message ".+"
The stderr should satisfy display_output
The stderr should satisfy spec_expect_no_message "${START_WITHOUT_A_SQUARE_BRACKET}"
The stderr should satisfy spec_expect_message "${ADDING_OPTIONS}.*--detach=true.*${SERVICE_NAME}"
# Cannot add "--replicas" to replicated job
The stderr should satisfy spec_expect_no_message "${ADDING_OPTIONS}.*--replicas=0"
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 "${SET_TIMEOUT_TO}"
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}"
End
End
End # Describe 'update-jobs'
Loading

0 comments on commit 39f4da7

Please sign in to comment.