Skip to content

Commit

Permalink
[6.3.0] Move credential helper setup into remote_helpers.sh so it can…
Browse files Browse the repository at this point in the history
… be reused by other shell tests. (#18453)

PiperOrigin-RevId: 527863207
Change-Id: Ib3631e4cf37dd51daa2c58e0fc87bd65d1b0bb87

Co-authored-by: Ian (Hee) Cha <heec@google.com>
  • Loading branch information
tjgq and iancha1992 authored May 19, 2023
1 parent 71cfbba commit bafcfcf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
29 changes: 4 additions & 25 deletions src/test/shell/bazel/remote/remote_execution_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ fi

source "$(rlocation "io_bazel/src/test/shell/integration_test_setup.sh")" \
|| { echo "integration_test_setup.sh not found!" >&2; exit 1; }
source "$(rlocation "io_bazel/src/test/shell/bazel/remote_helpers.sh")" \
|| { echo "remote_helpers.sh not found!" >&2; exit 1; }
source "$(rlocation "io_bazel/src/test/shell/bazel/remote/remote_utils.sh")" \
|| { echo "remote_utils.sh not found!" >&2; exit 1; }

Expand Down Expand Up @@ -77,22 +79,7 @@ function has_utf8_locale() {
}

function setup_credential_helper_test() {
# Each helper call atomically writes one byte to this file.
# We can later read the file to determine how many calls were made.
cat > "${TEST_TMPDIR}/credhelper_log"

cat > "${TEST_TMPDIR}/credhelper" <<'EOF'
#!/usr/bin/env python3
import os
path = os.path.join(os.environ["TEST_TMPDIR"], "credhelper_log")
fd = os.open(path, os.O_WRONLY|os.O_CREAT|os.O_APPEND)
os.write(fd, b"1")
os.close(fd)
print("""{"headers":{"Authorization":["Bearer secret_token"]}}""")
EOF
chmod +x "${TEST_TMPDIR}/credhelper"
setup_credential_helper

mkdir -p a

Expand All @@ -105,15 +92,7 @@ EOF
EOF

stop_worker
start_worker --expected_authorization_token=secret_token
}

function expect_credential_helper_calls() {
local -r expected=$1
local -r actual=$(wc -c "${TEST_TMPDIR}/credhelper_log" | awk '{print $1}')
if [[ "$expected" != "$actual" ]]; then
fail "expected $expected instead of $actual credential helper calls"
fi
start_worker --expected_authorization_token=TOKEN
}

function test_credential_helper_remote_cache() {
Expand Down
31 changes: 31 additions & 0 deletions src/test/shell/bazel/remote_helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,34 @@ function shutdown_server() {
function kill_nc() {
shutdown_server
}

# Sets up a credential helper binary at ${TEST_TMPDIR}/credhelper and resets
# the call counter.
function setup_credential_helper() {
# Each call atomically writes one byte to this file.
# The file can be read later determine how many calls were made.
cat > "${TEST_TMPDIR}/credhelper.callcount"

cat > "${TEST_TMPDIR}/credhelper" <<'EOF'
#!/usr/bin/env python3
import os
path = os.path.join(os.environ["TEST_TMPDIR"], "credhelper.callcount")
fd = os.open(path, os.O_WRONLY|os.O_CREAT|os.O_APPEND)
os.write(fd, b"1")
os.close(fd)
# Must match //src/test/shell/bazel/testing_server.py.
print("""{"headers":{"Authorization":["Bearer TOKEN"]}}""")
EOF
chmod +x "${TEST_TMPDIR}/credhelper"
}

# Asserts how many times the credential helper was called.
function expect_credential_helper_calls() {
local -r expected=$1
local -r actual=$(wc -c "${TEST_TMPDIR}/credhelper.callcount" | awk '{print $1}')
if [[ "$expected" != "$actual" ]]; then
fail "expected $expected instead of $actual credential helper calls"
fi
}

0 comments on commit bafcfcf

Please sign in to comment.