From 4df95a615ea1a92e6b125fa4eda2e9188136122e Mon Sep 17 00:00:00 2001 From: Sunny Date: Tue, 16 May 2023 22:18:16 +0000 Subject: [PATCH] oci/tests: Add test for repo root login This adds a new test to verify that login works if the URL and name reference are constructed correctly. Signed-off-by: Sunny --- oci/tests/integration/repo_list_test.go | 21 ++++++++++++--- oci/tests/integration/testapp/main.go | 35 ++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/oci/tests/integration/repo_list_test.go b/oci/tests/integration/repo_list_test.go index ee9fcb214..12aaac274 100644 --- a/oci/tests/integration/repo_list_test.go +++ b/oci/tests/integration/repo_list_test.go @@ -22,6 +22,7 @@ package integration import ( "context" "fmt" + "strings" "testing" . "github.com/onsi/gomega" @@ -33,12 +34,26 @@ import ( func TestImageRepositoryListTags(t *testing.T) { for name, repo := range testRepos { t.Run(name, func(t *testing.T) { - testImageRepositoryListTags(t, repo) + args := []string{fmt.Sprintf("-repo=%s", repo)} + testImageRepositoryListTags(t, args) }) } } -func testImageRepositoryListTags(t *testing.T, repoURL string) { +func TestRepositoryRootLoginListTags(t *testing.T) { + for name, repo := range testRepos { + t.Run(name, func(t *testing.T) { + parts := strings.SplitN(repo, "/", 2) + args := []string{ + fmt.Sprintf("-registry=%s", parts[0]), + fmt.Sprintf("-repo=%s", parts[1]), + } + testImageRepositoryListTags(t, args) + }) + } +} + +func testImageRepositoryListTags(t *testing.T, args []string) { g := NewWithT(t) ctx := context.TODO() @@ -49,7 +64,7 @@ func testImageRepositoryListTags(t *testing.T, repoURL string) { { Name: "test-app", Image: testAppImage, - Args: []string{fmt.Sprintf("-repo=%s", repoURL)}, + Args: args, ImagePullPolicy: corev1.PullAlways, }, } diff --git a/oci/tests/integration/testapp/main.go b/oci/tests/integration/testapp/main.go index f8bf05565..c7df88bbc 100644 --- a/oci/tests/integration/testapp/main.go +++ b/oci/tests/integration/testapp/main.go @@ -20,8 +20,10 @@ import ( "context" "flag" "log" + "strings" "time" + "github.com/google/go-containerregistry/pkg/authn" "github.com/google/go-containerregistry/pkg/name" "github.com/google/go-containerregistry/pkg/v1/remote" ctrl "sigs.k8s.io/controller-runtime" @@ -30,7 +32,15 @@ import ( "github.com/fluxcd/pkg/oci/auth/login" ) -var repo = flag.String("repo", "", "repository to list") +// registry and repo flags are to facilitate testing of two login scenarios: +// - when the repository contains the full address, including registry host, +// e.g. foo.azurecr.io/bar. +// - when the repository contains only the repository name and registry name +// is provided separately, e.g. registry: foo.azurecr.io, repo: bar. +var ( + registry = flag.String("registry", "", "registry of the repository") + repo = flag.String("repo", "", "repository to list") +) func main() { flag.Parse() @@ -43,14 +53,31 @@ func main() { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - log.Println("repo:", *repo) + if *repo == "" { + panic("must provide -repo value") + } + + var loginURL string + var auth authn.Authenticator + var ref name.Reference + var err error - ref, err := name.ParseReference(*repo) + if *registry != "" { + // Registry and repository are separate. + log.Printf("registry: %s, repo: %s\n", *registry, *repo) + loginURL = *registry + ref, err = name.ParseReference(strings.Join([]string{*registry, *repo}, "/")) + } else { + // Repository contains the registry host address. + log.Println("repo:", *repo) + loginURL = *repo + ref, err = name.ParseReference(*repo) + } if err != nil { panic(err) } - auth, err := login.NewManager().Login(ctx, *repo, ref, opts) + auth, err = login.NewManager().Login(ctx, loginURL, ref, opts) if err != nil { panic(err) }