From 682c4c9b19664dec6cc626f08f7ace924c6fcdb7 Mon Sep 17 00:00:00 2001 From: Anton Kalpakchiev Date: Mon, 18 Nov 2024 14:58:31 +0100 Subject: [PATCH] Remove regex from readiness tests --- build-index/tagserver/server_test.go | 47 +++++++++++++------------- tracker/trackerserver/announce_test.go | 27 +++++++-------- 2 files changed, 36 insertions(+), 38 deletions(-) diff --git a/build-index/tagserver/server_test.go b/build-index/tagserver/server_test.go index cf2a14d8..c5be1a57 100644 --- a/build-index/tagserver/server_test.go +++ b/build-index/tagserver/server_test.go @@ -19,8 +19,8 @@ import ( "io/ioutil" "net/http" "net/url" - "regexp" "strconv" + "strings" "testing" "time" @@ -153,34 +153,34 @@ func TestHealth(t *testing.T) { func TestCheckReadiness(t *testing.T) { for _, tc := range []struct { - name string - mockStatErr error - mockOriginErr error - expectedErrMsgPattern string + name string + mockStatErr error + mockOriginErr error + wantErr string }{ { - name: "success", - mockStatErr: nil, - mockOriginErr: nil, - expectedErrMsgPattern: "", + name: "success", + mockStatErr: nil, + mockOriginErr: nil, + wantErr: "", }, { - name: "failure, 503 (only Stat fails)", - mockStatErr: errors.New("backend storage error"), - mockOriginErr: nil, - expectedErrMsgPattern: fmt.Sprintf(`build index not ready: GET http://127\.0\.0\.1:\d+/readiness 503: not ready to serve traffic: backend for namespace 'foo-bar/\*' not ready: backend storage error`), + name: "failure, 503 (only Stat fails)", + mockStatErr: errors.New("backend storage error"), + mockOriginErr: nil, + wantErr: "build index not ready: GET http://{address}/readiness 503: not ready to serve traffic: backend for namespace 'foo-bar/*' not ready: backend storage error", }, { - name: "failure, 503 (only origin fails)", - mockStatErr: nil, - mockOriginErr: errors.New("origin error"), - expectedErrMsgPattern: fmt.Sprintf(`build index not ready: GET http://127\.0\.0\.1:\d+/readiness 503: not ready to serve traffic: origin error`), + name: "failure, 503 (only origin fails)", + mockStatErr: nil, + mockOriginErr: errors.New("origin error"), + wantErr: "build index not ready: GET http://{address}/readiness 503: not ready to serve traffic: origin error", }, { - name: "failure, 503 (both fail)", - mockStatErr: errors.New("backend storage error"), - mockOriginErr: errors.New("origin error"), - expectedErrMsgPattern: fmt.Sprintf(`build index not ready: GET http://127\.0\.0\.1:\d+/readiness 503: not ready to serve traffic: backend for namespace 'foo-bar/\*' not ready: backend storage error`), + name: "failure, 503 (both fail)", + mockStatErr: errors.New("backend storage error"), + mockOriginErr: errors.New("origin error"), + wantErr: "build index not ready: GET http://{address}/readiness 503: not ready to serve traffic: backend for namespace 'foo-bar/*' not ready: backend storage error", }, } { t.Run(tc.name, func(t *testing.T) { @@ -204,11 +204,10 @@ func TestCheckReadiness(t *testing.T) { mocks.originClient.EXPECT().CheckReadiness().Return(tc.mockOriginErr).AnyTimes() err := client.CheckReadiness() - if tc.expectedErrMsgPattern == "" { + if tc.wantErr == "" { require.Nil(err) } else { - r, _ := regexp.Compile(tc.expectedErrMsgPattern) - require.True(r.MatchString(err.Error())) + require.EqualError(err, strings.ReplaceAll(tc.wantErr, "{address}", addr)) } }) } diff --git a/tracker/trackerserver/announce_test.go b/tracker/trackerserver/announce_test.go index 7e28d05d..b16d2514 100644 --- a/tracker/trackerserver/announce_test.go +++ b/tracker/trackerserver/announce_test.go @@ -16,7 +16,7 @@ package trackerserver import ( "errors" "fmt" - "regexp" + "strings" "testing" "time" @@ -36,19 +36,19 @@ func newAnnounceClient(pctx core.PeerContext, addr string) announceclient.Client func TestCheckReadiness(t *testing.T) { for _, tc := range []struct { - name string - mockOriginErr error - expectedErrMsgPattern string + name string + originErr error + wantErr string }{ { - name: "success", - mockOriginErr: nil, - expectedErrMsgPattern: "", + name: "success", + originErr: nil, + wantErr: "", }, { - name: "failure, 503 (origin fails)", - mockOriginErr: errors.New("origin error"), - expectedErrMsgPattern: fmt.Sprintf(`tracker not ready: GET http://127\.0\.0\.1:\d+/readiness 503: not ready to serve traffic: origin error`), + name: "failure, 503 (origin fails)", + originErr: errors.New("origin error"), + wantErr: "tracker not ready: GET http://{address}/readiness 503: not ready to serve traffic: origin error", }, } { t.Run(tc.name, func(t *testing.T) { @@ -60,17 +60,16 @@ func TestCheckReadiness(t *testing.T) { addr, stop := testutil.StartServer(mocks.handler()) defer stop() - mocks.originCluster.EXPECT().CheckReadiness().Return(tc.mockOriginErr) + mocks.originCluster.EXPECT().CheckReadiness().Return(tc.originErr) pctx := core.PeerContextFixture() client := newAnnounceClient(pctx, addr) err := client.CheckReadiness() - if tc.expectedErrMsgPattern == "" { + if tc.wantErr == "" { require.Nil(err) } else { - r, _ := regexp.Compile(tc.expectedErrMsgPattern) - require.True(r.MatchString(err.Error())) + require.EqualError(err, strings.ReplaceAll(tc.wantErr, "{address}", addr)) } }) }