e2e: Add assert_fail and simplify fail cases

This commit is contained in:
Tim Hockin 2023-09-20 11:04:11 -07:00
parent 627fe23a9a
commit 32c3a0c2a7
1 changed files with 67 additions and 91 deletions

View File

@ -126,6 +126,18 @@ function assert_metric_eq() {
fail "metric $1 was expected to be '$2': ${val}" fail "metric $1 was expected to be '$2': ${val}"
} }
function assert_fail() {
(
set +o errexit
"$@"
RET=$?
if [[ "$RET" != 0 ]]; then
return
fi
fail "expected non-zero exit code, got $RET"
)
}
# Helper: run a docker container. # Helper: run a docker container.
function docker_run() { function docker_run() {
RM="--rm" RM="--rm"
@ -447,21 +459,14 @@ function e2e::sync_subdir_link() {
# Test non-zero exit with a bad ref # Test non-zero exit with a bad ref
############################################## ##############################################
function e2e::bad_ref_non_zero_exit() { function e2e::bad_ref_non_zero_exit() {
( assert_fail \
set +o errexit
GIT_SYNC \ GIT_SYNC \
--one-time \ --one-time \
--repo="file://$REPO" \ --repo="file://$REPO" \
--ref=does-not-exist \ --ref=does-not-exist \
--root="$ROOT" \ --root="$ROOT" \
--link="link" --link="link"
RET=$? assert_file_absent "$ROOT/link"
if [[ "$RET" != 1 ]]; then
fail "expected exit code 1, got $RET"
fi
assert_file_absent "$ROOT/link"
assert_file_absent "$ROOT/link/file"
)
} }
############################################## ##############################################
@ -1312,16 +1317,14 @@ function e2e::sync_repo_switch() {
# Test with slow git, short timeout # Test with slow git, short timeout
############################################## ##############################################
function e2e::error_slow_git_short_timeout() { function e2e::error_slow_git_short_timeout() {
GIT_SYNC \ assert_fail \
--git="/$SLOW_GIT_FETCH" \ GIT_SYNC \
--one-time \ --git="/$SLOW_GIT_FETCH" \
--sync-timeout=1s \ --one-time \
--repo="file://$REPO" \ --sync-timeout=1s \
--root="$ROOT" \ --repo="file://$REPO" \
--link="link" \ --root="$ROOT" \
|| true --link="link"
# check for failure
assert_file_absent "$ROOT/link/file" assert_file_absent "$ROOT/link/file"
} }
@ -1618,25 +1621,25 @@ function e2e::auth_http_password() {
IP=$(docker_ip "$CTR") IP=$(docker_ip "$CTR")
# Try with wrong username # Try with wrong username
GIT_SYNC \ assert_fail \
--one-time \ GIT_SYNC \
--repo="http://$IP/repo" \ --one-time \
--root="$ROOT" \ --repo="http://$IP/repo" \
--link="link" \ --root="$ROOT" \
--username="wrong" \ --link="link" \
--password="testpass" \ --username="wrong" \
|| true --password="testpass"
assert_file_absent "$ROOT/link/file" assert_file_absent "$ROOT/link/file"
# Try with wrong password # Try with wrong password
GIT_SYNC \ assert_fail \
--one-time \ GIT_SYNC \
--repo="http://$IP/repo" \ --one-time \
--root="$ROOT" \ --repo="http://$IP/repo" \
--link="link" \ --root="$ROOT" \
--username="testuser" \ --link="link" \
--password="wrong" \ --username="testuser" \
|| true --password="wrong"
assert_file_absent "$ROOT/link/file" assert_file_absent "$ROOT/link/file"
# Try with the right password # Try with the right password
@ -1666,14 +1669,14 @@ function e2e::auth_http_password_file() {
# Make a password file with a bad password. # Make a password file with a bad password.
echo -n "wrong" > "$WORK/password-file" echo -n "wrong" > "$WORK/password-file"
GIT_SYNC \ assert_fail \
--one-time \ GIT_SYNC \
--repo="http://$IP/repo" \ --one-time \
--root="$ROOT" \ --repo="http://$IP/repo" \
--link="link" \ --root="$ROOT" \
--username="testuser" \ --link="link" \
--password-file="$WORK/password-file" \ --username="testuser" \
|| true --password-file="$WORK/password-file"
assert_file_absent "$ROOT/link/file" assert_file_absent "$ROOT/link/file"
# Make a password file the right password. # Make a password file the right password.
@ -1704,15 +1707,15 @@ function e2e::auth_ssh() {
IP=$(docker_ip "$CTR") IP=$(docker_ip "$CTR")
# Try to sync with key #1. # Try to sync with key #1.
GIT_SYNC \ assert_fail \
--one-time \ GIT_SYNC \
--repo="test@$IP:/src" \ --one-time \
--root="$ROOT" \ --repo="test@$IP:/git/repo" \
--link="link" \ --root="$ROOT" \
--ssh \ --link="link" \
--ssh-known-hosts=false \ --ssh \
--ssh-key-file="/ssh/secret.2" \ --ssh-known-hosts=false \
|| true --ssh-key-file="/ssh/secret.2"
assert_file_absent "$ROOT/link/file" assert_file_absent "$ROOT/link/file"
# Try to sync with multiple keys # Try to sync with multiple keys
@ -1750,16 +1753,14 @@ function e2e::auth_askpass_url_wrong_password() {
') ')
IP=$(docker_ip "$CTR") IP=$(docker_ip "$CTR")
GIT_SYNC \ assert_fail \
--one-time \ GIT_SYNC \
--repo="file://$REPO" \ --one-time \
--root="$ROOT" \ --repo="file://$REPO" \
--link="link" \ --root="$ROOT" \
--git="/$ASKPASS_GIT" \ --link="link" \
--askpass-url="http://$IP/git_askpass" \ --git="/$ASKPASS_GIT" \
|| true --askpass-url="http://$IP/git_askpass"
# check for failure
assert_file_absent "$ROOT/link/file" assert_file_absent "$ROOT/link/file"
} }
@ -2036,9 +2037,7 @@ function e2e::exechook_success_once() {
function e2e::exechook_fail_once() { function e2e::exechook_fail_once() {
cat /dev/null > "$RUNLOG" cat /dev/null > "$RUNLOG"
# First sync - return a failure to ensure that we try again assert_fail \
(
set +o errexit
GIT_SYNC \ GIT_SYNC \
--one-time \ --one-time \
--repo="file://$REPO" \ --repo="file://$REPO" \
@ -2046,11 +2045,6 @@ function e2e::exechook_fail_once() {
--link="link" \ --link="link" \
--exechook-command="/$EXECHOOK_COMMAND_FAIL_SLEEPY" \ --exechook-command="/$EXECHOOK_COMMAND_FAIL_SLEEPY" \
--exechook-backoff=1s --exechook-backoff=1s
RET=$?
if [[ "$RET" != 1 ]]; then
fail "expected exit code 1, got $RET"
fi
)
assert_link_exists "$ROOT/link" assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file" assert_file_exists "$ROOT/link/file"
@ -2227,8 +2221,7 @@ function e2e::webhook_fail_retry_once() {
') ')
IP=$(docker_ip "$CTR") IP=$(docker_ip "$CTR")
( assert_fail \
set +o errexit
GIT_SYNC \ GIT_SYNC \
--period=100ms \ --period=100ms \
--one-time \ --one-time \
@ -2237,11 +2230,6 @@ function e2e::webhook_fail_retry_once() {
--webhook-url="http://$IP" \ --webhook-url="http://$IP" \
--webhook-success-status=200 \ --webhook-success-status=200 \
--link="link" --link="link"
RET=$?
if [[ "$RET" != 1 ]]; then
fail "expected exit code 1, got $RET"
fi
)
assert_link_exists "$ROOT/link" assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file" assert_file_exists "$ROOT/link/file"
@ -2801,22 +2789,16 @@ function e2e::additional_git_configs() {
# Test export-error # Test export-error
############################################## ##############################################
function e2e::export_error() { function e2e::export_error() {
( assert_fail \
set +o errexit
GIT_SYNC \ GIT_SYNC \
--repo="file://$REPO" \ --repo="file://$REPO" \
--ref=does-not-exit \ --ref=does-not-exit \
--root="$ROOT" \ --root="$ROOT" \
--link="link" \ --link="link" \
--error-file="error.json" --error-file="error.json"
RET=$?
if [[ "$RET" != 1 ]]; then
fail "expected exit code 1, got $RET"
fi
assert_file_absent "$ROOT/link" assert_file_absent "$ROOT/link"
assert_file_absent "$ROOT/link/file" assert_file_absent "$ROOT/link/file"
assert_file_contains "$ROOT/error.json" "unknown revision" assert_file_contains "$ROOT/error.json" "unknown revision"
)
# the error.json file should be removed if sync succeeds. # the error.json file should be removed if sync succeeds.
GIT_SYNC \ GIT_SYNC \
@ -2835,22 +2817,16 @@ function e2e::export_error() {
# Test export-error with an absolute path # Test export-error with an absolute path
############################################## ##############################################
function e2e::export_error_abs_path() { function e2e::export_error_abs_path() {
( assert_fail \
set +o errexit
GIT_SYNC \ GIT_SYNC \
--repo="file://$REPO" \ --repo="file://$REPO" \
--ref=does-not-exit \ --ref=does-not-exit \
--root="$ROOT" \ --root="$ROOT" \
--link="link" \ --link="link" \
--error-file="$ROOT/dir/error.json" --error-file="$ROOT/dir/error.json"
RET=$?
if [[ "$RET" != 1 ]]; then
fail "expected exit code 1, got $RET"
fi
assert_file_absent "$ROOT/link" assert_file_absent "$ROOT/link"
assert_file_absent "$ROOT/link/file" assert_file_absent "$ROOT/link/file"
assert_file_contains "$ROOT/dir/error.json" "unknown revision" assert_file_contains "$ROOT/dir/error.json" "unknown revision"
)
} }
############################################## ##############################################