Skip to content

Commit

Permalink
Resolve race condition on fake registry
Browse files Browse the repository at this point in the history
  • Loading branch information
joaopapereira committed Nov 15, 2021
1 parent b6aa2d3 commit 5c5c63b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pkg/imgpkg/cmd/copy_repo_src_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down Expand Up @@ -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)
})
Expand Down
35 changes: 31 additions & 4 deletions test/helpers/fake_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"path/filepath"
"regexp"
"strings"
"sync"
"testing"

"github.com/google/go-containerregistry/pkg/authn"
Expand Down Expand Up @@ -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(),
})
Expand All @@ -202,7 +229,7 @@ func (r *FakeTestRegistryBuilder) WithRequestLogging() *[]HTTPRequestLog {

r.server.Config.Handler = requestLogging

return &httpRequestLog
return httpRequestLog
}

func (r *FakeTestRegistryBuilder) WithIdentityToken(idToken string) {
Expand Down

0 comments on commit 5c5c63b

Please sign in to comment.