From 675f05215ea61b2ac21c04349d13d9c82a3b7750 Mon Sep 17 00:00:00 2001 From: Sahin Yort Date: Thu, 11 May 2023 19:50:20 -0700 Subject: [PATCH] test: add test for per registry credential helpers (#242) --- e2e/auth/helpers/docker-credential-new | 10 +++++ e2e/auth/main.go | 5 ++- e2e/auth/test.bats | 52 +++++++++++++++++++++++--- 3 files changed, 59 insertions(+), 8 deletions(-) create mode 100755 e2e/auth/helpers/docker-credential-new diff --git a/e2e/auth/helpers/docker-credential-new b/e2e/auth/helpers/docker-credential-new new file mode 100755 index 00000000..8d2f993e --- /dev/null +++ b/e2e/auth/helpers/docker-credential-new @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +read -r URL + + +if [[ "$URL" != "localhost:1447" ]]; then + echo "expected registry url to be localhost:1447"; + exit 1 +fi + +echo "{\"ServerURL\": \"$URL\", \"Username\": \"per-cred\", \"Secret\": \"testing\"}" \ No newline at end of file diff --git a/e2e/auth/main.go b/e2e/auth/main.go index c6f41b0d..451cf40e 100644 --- a/e2e/auth/main.go +++ b/e2e/auth/main.go @@ -8,6 +8,7 @@ import ( "log" "net/http" "os" + "strings" "github.com/google/go-containerregistry/pkg/registry" "github.com/r3labs/diff/v3" @@ -26,11 +27,11 @@ func main() { go func() { for scanner.Scan() { content := scanner.Text() - fmt.Println(content) err := json.Unmarshal([]byte(content), &auth) if err != nil { log.Fatalln(err) } + fmt.Println(content) } if scanner.Err() != nil { log.Fatalln(scanner.Err()) @@ -40,7 +41,7 @@ func main() { reg := registry.New(registry.Logger(log.New(ioutil.Discard, "", log.LstdFlags))) s := &http.Server{ Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.RequestURI == "/v2/empty_image/static/manifests/sha256:c3c3d0230d487c0ad3a0d87ad03ee02ea2ff0b3dcce91ca06a1019e07de05f12" { + if strings.Contains(r.RequestURI, "/v2/empty_image/") { currentAuth := Authn{Authorization: []string{}} if r.Header["Authorization"] != nil { currentAuth.Authorization = r.Header["Authorization"] diff --git a/e2e/auth/test.bats b/e2e/auth/test.bats index 1bfe6e07..a2824b06 100644 --- a/e2e/auth/test.bats +++ b/e2e/auth/test.bats @@ -28,6 +28,7 @@ function teardown_file() { function setup() { export DOCKER_CONFIG=$(mktemp -d) + bazel clean } function update_assert() { @@ -44,7 +45,7 @@ function update_assert() { } EOF update_assert '{"Authorization": ["Basic dGVzdDp0ZXN0"]}' - run bazel build @empty_image//... --repository_cache=$BATS_TEST_TMPDIR + run bazel build @empty_image//... --repository_cache= assert_success } @@ -57,7 +58,7 @@ EOF } EOF update_assert '{"Authorization": ["Basic dGVzdDp0ZXN0"]}' - run bazel build @empty_image//... --repository_cache=$BATS_TEST_TMPDIR + run bazel build @empty_image//... --repository_cache= assert_success } @@ -70,7 +71,7 @@ EOF } EOF update_assert '{"Authorization": ["Basic dGVzdDp0ZXN0"]}' - run bazel build @empty_image//... --repository_cache=$BATS_TEST_TMPDIR + run bazel build @empty_image//... --repository_cache= assert_success } @@ -82,7 +83,7 @@ EOF } EOF update_assert '{"Authorization": ["Basic dGVzdGluZzpvY2k="]}' - run bazel build @empty_image//... --repository_cache=$BATS_TEST_TMPDIR + run bazel build @empty_image//... --repository_cache= assert_success } @@ -93,7 +94,7 @@ EOF "credsStore": "evil" } EOF - run bazel build @empty_image//... --repository_cache=$BATS_TEST_TMPDIR + run bazel build @empty_image//... --repository_cache= assert_failure assert_output -p "can't run at this time" "ERROR: credential helper failed:" } @@ -105,7 +106,46 @@ EOF "credsStore": "missing" } EOF - run bazel build @empty_image//... --repository_cache=$BATS_TEST_TMPDIR + run bazel build @empty_image//... --repository_cache= assert_failure assert_output -p "exec: docker-credential-missing: not found" "ERROR: credential helper failed:" +} + +@test "per registry credHelper fails" { + cat > "$DOCKER_CONFIG/config.json" < "$DOCKER_CONFIG/config.json" < "$DOCKER_CONFIG/config.json" <