From 5c5c63b7553951aee450288c03a8538495a14bc3 Mon Sep 17 00:00:00 2001 From: Joao Pereira Date: Mon, 15 Nov 2021 16:58:27 -0600 Subject: [PATCH] Resolve race condition on fake registry --- pkg/imgpkg/cmd/copy_repo_src_test.go | 4 ++-- test/helpers/fake_registry.go | 35 ++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/pkg/imgpkg/cmd/copy_repo_src_test.go b/pkg/imgpkg/cmd/copy_repo_src_test.go index 095edaea..37241154 100644 --- a/pkg/imgpkg/cmd/copy_repo_src_test.go +++ b/pkg/imgpkg/cmd/copy_repo_src_test.go @@ -456,7 +456,7 @@ func TestToRepoBundleContainingANestedBundle(t *testing.T) { require.NoError(t, err) // Ensure that the last operation done against the registry is the creation of the tag - userDefinedTagRequest := (*requestLog)[len(*requestLog)-1] + userDefinedTagRequest := requestLog.Last() assert.Equal(t, "/v2/library/bundle-copy/manifests/some-tag", userDefinedTagRequest.URL) require.Equal(t, "PUT", userDefinedTagRequest.Method) }) @@ -993,7 +993,7 @@ images: require.NoError(t, err) // Ensure that the last operation done against the registry is the creation of the tag - userDefinedTagRequest := (*requestLog)[len(*requestLog)-1] + userDefinedTagRequest := requestLog.Last() assert.Equal(t, "/v2/library/copied-img/manifests/some-tag", userDefinedTagRequest.URL) require.Equal(t, "PUT", userDefinedTagRequest.Method) }) diff --git a/test/helpers/fake_registry.go b/test/helpers/fake_registry.go index 16958b86..a2cbc543 100644 --- a/test/helpers/fake_registry.go +++ b/test/helpers/fake_registry.go @@ -16,6 +16,7 @@ import ( "path/filepath" "regexp" "strings" + "sync" "testing" "github.com/google/go-containerregistry/pkg/authn" @@ -186,13 +187,39 @@ type HTTPRequestLog struct { URL string } +// HTTPRequestLogs Slice of HTTP Requests +type HTTPRequestLogs struct { + requests []HTTPRequestLog + lock sync.Mutex +} + +// NewHTTPRequestLogs Build a new HTTPRequestLogs struct +func NewHTTPRequestLogs() *HTTPRequestLogs { + return &HTTPRequestLogs{requests: []HTTPRequestLog{}} +} + +// Add new HTTP Request to the Log +func (h *HTTPRequestLogs) Add(request HTTPRequestLog) { + h.lock.Lock() + defer h.lock.Unlock() + h.requests = append(h.requests, request) +} + +// Last Retrieve Last HTTP Request +func (h *HTTPRequestLogs) Last() HTTPRequestLog { + h.lock.Lock() + defer h.lock.Unlock() + + return h.requests[len(h.requests)-1] +} + // WithRequestLogging enables the logging of the HTTP requests sent to the registry -func (r *FakeTestRegistryBuilder) WithRequestLogging() *[]HTTPRequestLog { - var httpRequestLog []HTTPRequestLog +func (r *FakeTestRegistryBuilder) WithRequestLogging() *HTTPRequestLogs { + httpRequestLog := NewHTTPRequestLogs() parentHandler := r.server.Config.Handler requestLogging := http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { - httpRequestLog = append(httpRequestLog, HTTPRequestLog{ + httpRequestLog.Add(HTTPRequestLog{ Method: request.Method, URL: request.URL.String(), }) @@ -202,7 +229,7 @@ func (r *FakeTestRegistryBuilder) WithRequestLogging() *[]HTTPRequestLog { r.server.Config.Handler = requestLogging - return &httpRequestLog + return httpRequestLog } func (r *FakeTestRegistryBuilder) WithIdentityToken(idToken string) {