From 284e6647e903d5b4e0c933b13cb76a7f1e42474f Mon Sep 17 00:00:00 2001 From: kimsm28 Date: Mon, 13 Apr 2026 17:53:48 +0900 Subject: [PATCH] test: improve client_scope_test.go to avoid data races and brittle assertions - Fix data race by using channel instead of shared variable for requestURL - Make assertions more robust by parsing URL and checking query parameters instead of exact string matching Signed-off-by: kimsm28 --- pkg/registry/client_scope_test.go | 36 +++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/pkg/registry/client_scope_test.go b/pkg/registry/client_scope_test.go index 3bde58a1d..06ae157cd 100644 --- a/pkg/registry/client_scope_test.go +++ b/pkg/registry/client_scope_test.go @@ -21,8 +21,10 @@ import ( "net" "net/http" "net/http/httptest" + "net/url" "os" "testing" + "time" "github.com/stretchr/testify/suite" ) @@ -43,9 +45,9 @@ func (suite *RegistryScopeTestSuite) TearDownSuite() { func (suite *RegistryScopeTestSuite) Test_1_Check_Push_Request_Scope() { - var requestURL string + requestURL := make(chan string, 1) handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - requestURL = r.URL.String() + requestURL <- r.URL.String() w.WriteHeader(http.StatusOK) }) listener, err := net.Listen("tcp", suite.AuthServerHost) @@ -67,14 +69,25 @@ func (suite *RegistryScopeTestSuite) Test_1_Check_Push_Request_Scope() { suite.Error(err, "error pushing good ref because auth server doesn't give proper token") //check the url that authentication server received - suite.Equal("/auth?scope=repository%3Atestrepo%2Flocal-subchart%3Apull%2Cpush&service=testservice", requestURL) + select { + case urlStr := <-requestURL: + u, err := url.Parse(urlStr) + suite.NoError(err, "no error parsing requested URL") + + suite.Equal("/auth", u.Path) + suite.Equal("testservice", u.Query().Get("service")) + scope := u.Query().Get("scope") + suite.Contains(scope, "repository:testrepo/local-subchart:pull,push") + case <-time.After(5 * time.Second): + suite.T().Fatal("timeout waiting for auth request") + } } func (suite *RegistryScopeTestSuite) Test_2_Check_Pull_Request_Scope() { - var requestURL string + requestURL := make(chan string, 1) handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - requestURL = r.URL.String() + requestURL <- r.URL.String() w.WriteHeader(http.StatusOK) }) listener, err := net.Listen("tcp", suite.AuthServerHost) @@ -96,7 +109,18 @@ func (suite *RegistryScopeTestSuite) Test_2_Check_Pull_Request_Scope() { suite.Error(err, "error pulling a simple chart because auth server doesn't give proper token") //check the url that authentication server received - suite.Equal("/auth?scope=repository%3Atestrepo%2Flocal-subchart%3Apull&service=testservice", requestURL) + select { + case urlStr := <-requestURL: + u, err := url.Parse(urlStr) + suite.NoError(err, "no error parsing requested URL") + + suite.Equal("/auth", u.Path) + suite.Equal("testservice", u.Query().Get("service")) + scope := u.Query().Get("scope") + suite.Contains(scope, "repository:testrepo/local-subchart:pull") + case <-time.After(5 * time.Second): + suite.T().Fatal("timeout waiting for auth request") + } } func TestRegistryScopeTestSuite(t *testing.T) {