Skip to content

Commit fd89d7d

Browse files
darkowlzzMax Jonas Werner
authored andcommitted
Fix race condition in git proxy tests
The variable used to store the information about proxied request was being written to in the proxy server request handler and read for assertion at the end of the test. Replace the boolean variable with an atomic counter to count the number of requests proxied, preventing the race condition. Signed-off-by: Sunny <[email protected]>
1 parent aaf4a4d commit fd89d7d

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

pkg/git/strategy/proxy/strategy_proxy_test.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"net/url"
2525
"os"
2626
"strings"
27+
"sync/atomic"
2728
"testing"
2829
"time"
2930

@@ -48,7 +49,7 @@ func TestCheckoutStrategyForImplementation_Proxied(t *testing.T) {
4849
gitImpl git.Implementation
4950
url string
5051
branch string
51-
setupGitProxy func(g *WithT, proxy *goproxy.ProxyHttpServer, proxyGotRequest *bool) (*git.AuthOptions, cleanupFunc)
52+
setupGitProxy func(g *WithT, proxy *goproxy.ProxyHttpServer, proxiedRequests *int32) (*git.AuthOptions, cleanupFunc)
5253
shortTimeout bool
5354
wantUsedProxy bool
5455
wantError bool
@@ -69,7 +70,7 @@ func TestCheckoutStrategyForImplementation_Proxied(t *testing.T) {
6970
gitImpl: libgit2.Implementation,
7071
url: "https://example.com/bar/test-reponame",
7172
branch: "main",
72-
setupGitProxy: func(g *WithT, proxy *goproxy.ProxyHttpServer, proxyGotRequest *bool) (*git.AuthOptions, cleanupFunc) {
73+
setupGitProxy: func(g *WithT, proxy *goproxy.ProxyHttpServer, proxiedRequests *int32) (*git.AuthOptions, cleanupFunc) {
7374
// Create the git server.
7475
gitServer, err := gittestserver.NewTempGitServer()
7576
g.Expect(err).ToNot(HaveOccurred())
@@ -103,7 +104,7 @@ func TestCheckoutStrategyForImplementation_Proxied(t *testing.T) {
103104
// Check if the host matches with the git server address and the user-agent is the expected git client.
104105
userAgent := ctx.Req.Header.Get("User-Agent")
105106
if strings.Contains(host, "example.com") && strings.Contains(userAgent, "libgit2") {
106-
*proxyGotRequest = true
107+
atomic.AddInt32(proxiedRequests, 1)
107108
return goproxy.OkConnect, u.Host
108109
}
109110
// Reject if it isn't our request.
@@ -130,7 +131,7 @@ func TestCheckoutStrategyForImplementation_Proxied(t *testing.T) {
130131
gitImpl: gogit.Implementation,
131132
url: "http://example.com/bar/test-reponame",
132133
branch: "main",
133-
setupGitProxy: func(g *WithT, proxy *goproxy.ProxyHttpServer, proxyGotRequest *bool) (*git.AuthOptions, cleanupFunc) {
134+
setupGitProxy: func(g *WithT, proxy *goproxy.ProxyHttpServer, proxiedRequests *int32) (*git.AuthOptions, cleanupFunc) {
134135
// Create the git server.
135136
gitServer, err := gittestserver.NewTempGitServer()
136137
g.Expect(err).ToNot(HaveOccurred())
@@ -153,7 +154,7 @@ func TestCheckoutStrategyForImplementation_Proxied(t *testing.T) {
153154
var proxyHandler goproxy.FuncReqHandler = func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
154155
userAgent := req.Header.Get("User-Agent")
155156
if strings.Contains(req.Host, "example.com") && strings.Contains(userAgent, "git") {
156-
*proxyGotRequest = true
157+
atomic.AddInt32(proxiedRequests, 1)
157158
req.Host = u.Host
158159
req.URL.Host = req.Host
159160
return req, nil
@@ -181,13 +182,13 @@ func TestCheckoutStrategyForImplementation_Proxied(t *testing.T) {
181182
gitImpl: gogit.Implementation,
182183
url: "https://github.com/git-fixtures/basic",
183184
branch: "master",
184-
setupGitProxy: func(g *WithT, proxy *goproxy.ProxyHttpServer, proxyGotRequest *bool) (*git.AuthOptions, cleanupFunc) {
185+
setupGitProxy: func(g *WithT, proxy *goproxy.ProxyHttpServer, proxiedRequests *int32) (*git.AuthOptions, cleanupFunc) {
185186
var proxyHandler goproxy.FuncHttpsHandler = func(host string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
186187
// We don't check for user agent as this handler is only going to process CONNECT requests, and because Go's net/http
187188
// is the one making such a request on behalf of go-git, adding a check for the go net/http user agent (Go-http-client)
188189
// would only allow false positives from any request originating from Go's net/http.
189190
if strings.Contains(host, "github.com") {
190-
*proxyGotRequest = true
191+
atomic.AddInt32(proxiedRequests, 1)
191192
return goproxy.OkConnect, host
192193
}
193194
// Reject if it isnt our request.
@@ -208,10 +209,10 @@ func TestCheckoutStrategyForImplementation_Proxied(t *testing.T) {
208209
gitImpl: gogit.Implementation,
209210
url: "https://192.0.2.1/bar/test-reponame",
210211
branch: "main",
211-
setupGitProxy: func(g *WithT, proxy *goproxy.ProxyHttpServer, proxyGotRequest *bool) (*git.AuthOptions, cleanupFunc) {
212+
setupGitProxy: func(g *WithT, proxy *goproxy.ProxyHttpServer, proxiedRequests *int32) (*git.AuthOptions, cleanupFunc) {
212213
var proxyHandler goproxy.FuncHttpsHandler = func(host string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
213214
// We shouldn't hit the proxy so we just want to check for any interaction, then reject.
214-
*proxyGotRequest = true
215+
atomic.AddInt32(proxiedRequests, 1)
215216
return goproxy.RejectConnect, host
216217
}
217218
proxy.OnRequest().HandleConnect(proxyHandler)
@@ -235,8 +236,8 @@ func TestCheckoutStrategyForImplementation_Proxied(t *testing.T) {
235236
proxy := goproxy.NewProxyHttpServer()
236237
proxy.Verbose = true
237238

238-
proxyGotRequest := false
239-
authOpts, cleanup := tt.setupGitProxy(g, proxy, &proxyGotRequest)
239+
proxiedRequests := int32(0)
240+
authOpts, cleanup := tt.setupGitProxy(g, proxy, &proxiedRequests)
240241
defer cleanup()
241242

242243
proxyServer := http.Server{
@@ -281,7 +282,7 @@ func TestCheckoutStrategyForImplementation_Proxied(t *testing.T) {
281282
g.Expect(err).ToNot(HaveOccurred())
282283
}
283284

284-
g.Expect(proxyGotRequest).To(Equal(tt.wantUsedProxy))
285+
g.Expect(atomic.LoadInt32(&proxiedRequests) > 0).To(Equal(tt.wantUsedProxy))
285286

286287
})
287288
}

0 commit comments

Comments
 (0)