Skip to content

Commit

Permalink
[tests] add test_login_file_not_exist
Browse files Browse the repository at this point in the history
  • Loading branch information
shizunge committed Feb 15, 2024
1 parent 814cc45 commit 9bf488b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ _read_docker_hub_rate() {
USER=
fi
if ! HOST=$(gantry_read_registry_host 2>&1); then
log ERROR "Failed to read HOST: ${HOST}";
log ERROR "Failed to read registry HOST: ${HOST}";
HOST=
fi
local USER_AND_PASS=
Expand Down Expand Up @@ -133,7 +133,7 @@ gantry() {
TIME_ELAPSED=$(time_elapsed_since "${START_TIME}")
local MESSAGE="Done. Use ${TIME_ELAPSED}. ${ACCUMULATED_ERRORS} errors."
local RETURN_VALUE=0
if [ ${ACCUMULATED_ERRORS} -gt 0 ]; then
if [ "${ACCUMULATED_ERRORS}" -gt 0 ]; then
log ERROR "${MESSAGE}"
RETURN_VALUE=1
else
Expand Down
19 changes: 13 additions & 6 deletions src/lib-gantry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,24 @@ _authenticate_to_registries() {
local ACCUMULATED_ERRORS=0
local CONFIG HOST PASSWORD USER
if ! CONFIG=$(read_config GANTRY_REGISTRY_CONFIG 2>&1); then
log ERROR "Failed to read CONFIG: ${CONFIG}"
log ERROR "Failed to read registry CONFIG: ${CONFIG}"
ACCUMULATED_ERRORS=$((ACCUMULATED_ERRORS + 1))
CONFIG=
fi
if ! HOST=$(gantry_read_registry_host 2>&1); then
log ERROR "Failed to read HOST: ${HOST}"
log ERROR "Failed to read registry HOST: ${HOST}"
ACCUMULATED_ERRORS=$((ACCUMULATED_ERRORS + 1))
HOST=
fi
if ! PASSWORD=$(gantry_read_registry_password 2>&1); then
log ERROR "Failed to read PASSWORD: ${PASSWORD}"
log ERROR "Failed to read registry PASSWORD: ${PASSWORD}"
ACCUMULATED_ERRORS=$((ACCUMULATED_ERRORS + 1))
PASSWORD=
fi
if ! USER=$(gantry_read_registry_username 2>&1); then
log ERROR "Failed to read USER: ${USER}"
log ERROR "Failed to read registry USER: ${USER}"
ACCUMULATED_ERRORS=$((ACCUMULATED_ERRORS + 1))
USER=
fi
if [ "${ACCUMULATED_ERRORS}" -gt 0 ]; then
log ERROR "Skip logging in due to previous errors."
Expand All @@ -88,7 +92,10 @@ _authenticate_to_registries() {
[ "${ACCUMULATED_ERRORS}" -gt 0 ] && return 1
return 0
fi
[ ! -r "${CONFIGS_FILE}" ] && log ERROR "Failed to read ${CONFIGS_FILE}." && return 1
if [ ! -r "${CONFIGS_FILE}" ]; then
log ERROR "Failed to read CONFIGS_FILE ${CONFIGS_FILE}."
return 1
fi
local LINE=
while read -r LINE; do
# skip comments
Expand Down Expand Up @@ -117,7 +124,7 @@ _authenticate_to_registries() {
_login_registry "${USER}" "${PASSWORD}" "${HOST}" "${CONFIG}"
ACCUMULATED_ERRORS=$((ACCUMULATED_ERRORS + $?))
done < <(cat "${CONFIGS_FILE}"; echo;)
[ ${ACCUMULATED_ERRORS} -gt 0 ] && return 1
[ "${ACCUMULATED_ERRORS}" -gt 0 ] && return 1
return 0
}

Expand Down
73 changes: 68 additions & 5 deletions tests/gantry_login_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ Describe 'Login'
local REGISTRY=${3}
local USERNAME=${4}
local PASSWORD=${5}
if [ -z "${USERNAME}" ] || [ -z "${PASSWORD}" ]; then
echo "No USERNAME or PASSWORD provided." >&2
if [ -z "${REGISTRY}" ] || [ -z "${USERNAME}" ] || [ -z "${PASSWORD}" ]; then
echo "No REGISTRY, USERNAME or PASSWORD provided." >&2
return 1
fi
local LABEL="gantry.auth.config"
Expand Down Expand Up @@ -155,11 +155,11 @@ Describe 'Login'
End
End
Describe "test_login_REGISTRY_CONFIGS_FILE_bad_format" "container_test:false"
TEST_NAME="test_login_REGISTRY_CONFIGS_FILE_bad_format_extra"
TEST_NAME="test_login_REGISTRY_CONFIGS_FILE_bad_format"
IMAGE_WITH_TAG=$(get_image_with_tag "${SUITE_NAME}")
SERVICE_NAME="gantry-test-$(unique_id)"
TEST_REGISTRY=$(load_test_registry "${SUITE_NAME}") || return 1
test_login_REGISTRY_CONFIGS_FILE_bad_format_extra() {
test_login_REGISTRY_CONFIGS_FILE_bad_format() {
local TEST_NAME=${1}
local SERVICE_NAME=${2}
local REGISTRY=${3}
Expand Down Expand Up @@ -189,7 +189,7 @@ Describe 'Login'
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_login_REGISTRY_CONFIGS_FILE_bad_format_extra "${TEST_NAME}" "${SERVICE_NAME}" "${TEST_REGISTRY}" "${TEST_USERNAME}" "${TEST_PASSWORD}"
When run test_login_REGISTRY_CONFIGS_FILE_bad_format "${TEST_NAME}" "${SERVICE_NAME}" "${TEST_REGISTRY}" "${TEST_USERNAME}" "${TEST_PASSWORD}"
The status should be failure
The stdout should satisfy display_output
The stderr should satisfy display_output
Expand Down Expand Up @@ -220,4 +220,67 @@ Describe 'Login'
The stderr should satisfy spec_expect_no_message "${FAILED_TO_REMOVE_IMAGE}.*${IMAGE_WITH_TAG}"
End
End
Describe "test_login_file_not_exist" "container_test:false"
TEST_NAME="test_login_file_not_exist"
IMAGE_WITH_TAG=$(get_image_with_tag "${SUITE_NAME}")
SERVICE_NAME="gantry-test-$(unique_id)"
TEST_REGISTRY=$(load_test_registry "${SUITE_NAME}") || return 1
test_login_file_not_exist() {
local TEST_NAME=${1}
local SERVICE_NAME=${2}
local REGISTRY=${3}
local USERNAME=${4}
local PASSWORD=${5}
if [ -z "${REGISTRY}" ] || [ -z "${USERNAME}" ] || [ -z "${PASSWORD}" ]; then
echo "No REGISTRY, USERNAME or PASSWORD provided." >&2
return 1
fi
local LABEL="gantry.auth.config"
CONFIG="C$(unique_id)"
docker service update --quiet --label-add "${LABEL}=${CONFIG}" "${SERVICE_NAME}"
local FILE_NOT_EXIST="/tmp/${CONFIG}"
reset_gantry_env "${SERVICE_NAME}"
export GANTRY_REGISTRY_CONFIG_FILE="${FILE_NOT_EXIST}"
export GANTRY_REGISTRY_CONFIGS_FILE="${FILE_NOT_EXIST}"
export GANTRY_REGISTRY_HOST_FILE="${FILE_NOT_EXIST}"
export GANTRY_REGISTRY_PASSWORD_FILE="${FILE_NOT_EXIST}"
export GANTRY_REGISTRY_USER_FILE="${FILE_NOT_EXIST}"
local RETURN_VALUE=
run_gantry "${TEST_NAME}"
RETURN_VALUE="${?}"
[ -d "${CONFIG}" ] && rm -r "${CONFIG}"
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_login_file_not_exist "${TEST_NAME}" "${SERVICE_NAME}" "${TEST_REGISTRY}" "${TEST_USERNAME}" "${TEST_PASSWORD}"
The status should be failure
The stdout should satisfy display_output
The stderr should satisfy display_output
The stderr should satisfy spec_expect_no_message "Logged into registry *${TEST_REGISTRY} for config ${CONFIG}"
The stderr should satisfy spec_expect_message "${SKIP_UPDATING_ALL}.*${SKIP_REASON_PREVIOUS_ERRORS}"
The stderr should satisfy spec_expect_no_message "${SKIP_UPDATING}.*${SERVICE_NAME}"
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_no_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}.*--config.*"
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
End # Describe 'Login'
2 changes: 1 addition & 1 deletion tests/spec_gantry_test_helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ _get_entrypoint() {
_add_file_to_mount_options() {
local MOUNT_OPTIONS="${1}"
local FILE="${2}"
if [ -n "${FILE}" ]; then
if [ -n "${FILE}" ] && [ -r "${FILE}" ]; then
MOUNT_OPTIONS="${MOUNT_OPTIONS} --mount type=bind,source=${FILE},target=${FILE}"
fi
echo "${MOUNT_OPTIONS}"
Expand Down

0 comments on commit 9bf488b

Please sign in to comment.