From 8719d299bf1e7460d5ce1a0c8861f67452b4bc30 Mon Sep 17 00:00:00 2001 From: "Lixia (Sylvia) Lei" Date: Mon, 25 Sep 2023 17:23:02 +0800 Subject: [PATCH] test new function Signed-off-by: Lixia (Sylvia) Lei --- registry/remote/auth/scope_test.go | 68 ++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/registry/remote/auth/scope_test.go b/registry/remote/auth/scope_test.go index be901499..0b00c5c7 100644 --- a/registry/remote/auth/scope_test.go +++ b/registry/remote/auth/scope_test.go @@ -658,3 +658,71 @@ func Test_cleanActions(t *testing.T) { }) } } + +func Test_getAllScopesForHost(t *testing.T) { + host := "registry.example.com" + tests := []struct { + name string + scopes []string + globalScopes []string + want []string + }{ + { + name: "Empty per-host scopes", + scopes: []string{}, + globalScopes: []string{ + "repository:hello-world:push", + "repository:alpine:delete", + "repository:hello-world:pull", + "repository:alpine:delete", + }, + want: []string{ + "repository:alpine:delete", + "repository:hello-world:pull,push", + }, + }, + { + name: "Empty global scopes", + scopes: []string{ + "repository:hello-world:push", + "repository:alpine:delete", + "repository:hello-world:pull", + "repository:alpine:delete", + }, + globalScopes: []string{}, + want: []string{ + "repository:alpine:delete", + "repository:hello-world:pull,push", + }, + }, + { + name: "Per-host scopes + global scopes", + scopes: []string{ + "repository:hello-world:push", + "repository:alpine:delete", + "repository:hello-world:pull", + "repository:alpine:delete", + }, + globalScopes: []string{ + "repository:foo:pull", + "repository:hello-world:pull", + "repository:alpine:pull", + }, + want: []string{ + "repository:alpine:delete,pull", + "repository:foo:pull", + "repository:hello-world:pull,push", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctx := context.Background() + ctx = WithScopesForHost(ctx, host, tt.scopes...) + ctx = WithScopes(ctx, tt.globalScopes...) + if got := getAllScopesForHost(ctx, host); !reflect.DeepEqual(got, tt.want) { + t.Errorf("getAllScopesForHost() = %v, want %v", got, tt.want) + } + }) + } +}