Skip to content

Commit

Permalink
can't test old cli, give up
Browse files Browse the repository at this point in the history
  • Loading branch information
Menachem Hornbacher committed Aug 17, 2022
1 parent 689f762 commit 3902813
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
13 changes: 8 additions & 5 deletions hooks/environment
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,6 @@ function login_using_aws_ecr_get_login() {
login_args+=("--region" "${BUILDKITE_PLUGIN_ECR_REGION}")
fi

if [[ -n "${BUILDKITE_PLUGIN_ECR_PROFILE:-}" ]] ; then
login_args+=("--profile" "${BUILDKITE_PLUGIN_ECR_PROFILE}")
fi

if [[ ${#registry_ids[@]} -gt 0 ]] ; then
# amend the ~~~ log heading with ^^^ to add the AWS account IDs
echo "^^^ Authenticating with AWS ECR for ${registry_ids[*]} :ecr: :docker:"
Expand All @@ -144,6 +140,13 @@ function login_using_aws_ecr_get_login_password() {
echo >&2 "AWS region should be specified via plugin config or AWS_DEFAULT_REGION environment."
echo >&2 "Defaulting to $region for legacy compatibility."
fi

login_args=("--region" "${region}")

if [[ -n "${BUILDKITE_PLUGIN_ECR_PROFILE:-}" ]] ; then
login_args+=("--profile" "${BUILDKITE_PLUGIN_ECR_PROFILE}")
fi

account_ids=()
while IFS='' read -r line; do account_ids+=("$line"); done < <(plugin_read_list ACCOUNT_IDS | tr "," "\n")
# check if account_ids is empty, or only contains an empty string.
Expand All @@ -157,7 +160,7 @@ function login_using_aws_ecr_get_login_password() {
fi
# amend the ~~~ log heading with ^^^ to add the AWS account IDs
echo "^^^ Authenticating with AWS ECR in $region for ${account_ids[*]} :ecr: :docker:"
local password; password="$(retry "${BUILDKITE_PLUGIN_ECR_RETRIES:-0}" aws --region "$region" ecr get-login-password)"
local password; password="$(retry "${BUILDKITE_PLUGIN_ECR_RETRIES:-0}" aws ${login_args[@]+"${login_args[@]}"} ecr get-login-password)"
for account_id in "${account_ids[@]}"; do
retry "${BUILDKITE_PLUGIN_ECR_RETRIES:-0}" --with-stdin docker login --username AWS --password-stdin "$account_id.dkr.ecr.$region.amazonaws.com" <<< "$password"
done
Expand Down
25 changes: 25 additions & 0 deletions tests/run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,31 @@ load '/usr/local/lib/bats/load.bash'

# export AWS_STUB_DEBUG=/dev/tty

@test "ECR login; configured account ID, configured region, configured profile" {
export BUILDKITE_PLUGIN_ECR_LOGIN=true
export BUILDKITE_PLUGIN_ECR_ACCOUNT_IDS=321321321321
export BUILDKITE_PLUGIN_ECR_REGION=ap-southeast-2
export BUILDKITE_PLUGIN_ECR_PROFILE=ecr

stub aws \
"--version : echo aws-cli/2.0.0 Python/3.8.1 Linux/5.5.6-arch1-1 botocore/1.15.3" \
"--region ap-southeast-2 --profile ecr ecr get-login-password : echo hunter2"

stub docker \
"login --username AWS --password-stdin 321321321321.dkr.ecr.ap-southeast-2.amazonaws.com : cat > /tmp/password-stdin ; echo logging in to docker"

run "$PWD/hooks/environment"

assert_success
assert_output --partial "~~~ Authenticating with AWS ECR :ecr: :docker:"
assert_output --partial "^^^ Authenticating with AWS ECR in ap-southeast-2 for 321321321321 :ecr: :docker:"
assert_output --partial "logging in to docker"
[[ $(cat /tmp/password-stdin) == "hunter2" ]]

unstub aws
unstub docker
}

@test "ECR login; configured account ID, configured region" {
export BUILDKITE_PLUGIN_ECR_LOGIN=true
export BUILDKITE_PLUGIN_ECR_ACCOUNT_IDS=321321321321
Expand Down

0 comments on commit 3902813

Please sign in to comment.