From 68adbe5767d007373099fa03afe8b38a04d4864c Mon Sep 17 00:00:00 2001 From: Andrew Wilkins Date: Tue, 3 Aug 2021 09:22:58 +0800 Subject: [PATCH] sourcemap: fix data race in test (#5842) (#5855) Fix data race in TestFleetFetch: as there are two servers, one may update hasAuth concurrently with the test checking the value. Change the way the auth header is checked to be similar to how the header would be checked in a real fleet-server, and respond with 401 if the auth header is invalid. --- sourcemap/fleet_store_test.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/sourcemap/fleet_store_test.go b/sourcemap/fleet_store_test.go index d7596205113..4ec96f331bd 100644 --- a/sourcemap/fleet_store_test.go +++ b/sourcemap/fleet_store_test.go @@ -22,7 +22,6 @@ import ( "context" "net/http" "net/http/httptest" - "sync" "sync/atomic" "testing" @@ -34,8 +33,6 @@ import ( func TestFleetFetch(t *testing.T) { var ( - hasAuth = true - mu sync.Mutex apikey = "supersecret" name = "webapp" version = "1.0.0" @@ -46,10 +43,10 @@ func TestFleetFetch(t *testing.T) { h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, sourceMapPath, r.URL.Path) - auth := r.Header.Get("Authorization") - mu.Lock() - hasAuth = hasAuth && (auth == "ApiKey "+apikey) - mu.Unlock() + if auth := r.Header.Get("Authorization"); auth != "ApiKey "+apikey { + w.WriteHeader(http.StatusUnauthorized) + return + } // zlib compress wr := zlib.NewWriter(w) defer wr.Close() @@ -84,7 +81,6 @@ func TestFleetFetch(t *testing.T) { require.NoError(t, err) assert.Contains(t, gotRes, "webpack:///bundle.js") - assert.True(t, hasAuth) } func TestFailedAndSuccessfulFleetHostsFetch(t *testing.T) {