X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/07f18157a5f8b72c20c0050c4617564effe91bd2..dcab7519db659e2487db893fda2c8237203c3294:/services/datamanager/keep/keep_test.go diff --git a/services/datamanager/keep/keep_test.go b/services/datamanager/keep/keep_test.go index 3ca7b8218a..0691e647bf 100644 --- a/services/datamanager/keep/keep_test.go +++ b/services/datamanager/keep/keep_test.go @@ -12,6 +12,7 @@ import ( "testing" "git.curoverse.com/arvados.git/sdk/go/arvadosclient" + "git.curoverse.com/arvados.git/sdk/go/arvadostest" "git.curoverse.com/arvados.git/sdk/go/blockdigest" "git.curoverse.com/arvados.git/sdk/go/keepclient" @@ -93,59 +94,6 @@ func (s *KeepSuite) TestSendTrashListUnreachable(c *C) { sendTrashListError(c, httptest.NewUnstartedServer(&TestHandler{})) } -type StatusAndBody struct { - respStatus int - responseBody string -} - -type APIStub struct { - data map[string]StatusAndBody -} - -func (stub *APIStub) ServeHTTP(resp http.ResponseWriter, req *http.Request) { - if req.URL.Path == "/redirect-loop" { - http.Redirect(resp, req, "/redirect-loop", http.StatusFound) - return - } - - pathResponse := stub.data[req.URL.Path] - if pathResponse.responseBody != "" { - if pathResponse.respStatus == -1 { - http.Redirect(resp, req, "/redirect-loop", http.StatusFound) - } else { - resp.WriteHeader(pathResponse.respStatus) - resp.Write([]byte(pathResponse.responseBody)) - } - } else { - resp.WriteHeader(500) - resp.Write([]byte(``)) - } -} - -type KeepServerStub struct { - data map[string]StatusAndBody -} - -func (stub *KeepServerStub) ServeHTTP(resp http.ResponseWriter, req *http.Request) { - if req.URL.Path == "/redirect-loop" { - http.Redirect(resp, req, "/redirect-loop", http.StatusFound) - return - } - - pathResponse := stub.data[req.URL.Path] - if pathResponse.responseBody != "" { - if pathResponse.respStatus == -1 { - http.Redirect(resp, req, "/redirect-loop", http.StatusFound) - } else { - resp.WriteHeader(pathResponse.respStatus) - resp.Write([]byte(pathResponse.responseBody)) - } - } else { - resp.WriteHeader(500) - resp.Write([]byte(``)) - } -} - type APITestData struct { numServers int serverType string @@ -177,9 +125,9 @@ func testGetKeepServersFromAPI(c *C, testData APITestData, expectedError string) } ksJSON, _ := json.Marshal(keepServers) - apiData := make(map[string]StatusAndBody) - apiData["/arvados/v1/keep_services"] = StatusAndBody{testData.statusCode, string(ksJSON)} - apiStub := APIStub{apiData} + apiStubResponses := make(map[string]arvadostest.StubResponse) + apiStubResponses["/arvados/v1/keep_services"] = arvadostest.StubResponse{testData.statusCode, string(ksJSON)} + apiStub := arvadostest.ServerStub{apiStubResponses} api := httptest.NewServer(&apiStub) defer api.Close() @@ -221,27 +169,27 @@ type KeepServerTestData struct { func (s *KeepSuite) TestGetKeepServers_ErrorGettingKeepServerStatus(c *C) { testGetKeepServersAndSummarize(c, KeepServerTestData{500, 200, "ok", - "500 Internal Server Error"}) + ".*http://.* 500 Internal Server Error"}) } func (s *KeepSuite) TestGetKeepServers_GettingIndex(c *C) { testGetKeepServersAndSummarize(c, KeepServerTestData{200, -1, "notok", - "redirect-loop"}) + ".*redirect-loop.*"}) } func (s *KeepSuite) TestGetKeepServers_ErrorReadServerResponse(c *C) { testGetKeepServersAndSummarize(c, KeepServerTestData{200, 500, "notok", - "500 Internal Server Error"}) + ".*http://.* 500 Internal Server Error"}) } func (s *KeepSuite) TestGetKeepServers_ReadServerResponseTuncatedAtLineOne(c *C) { testGetKeepServersAndSummarize(c, KeepServerTestData{200, 200, - "notterminatedwithnewline", "truncated at line 1"}) + "notterminatedwithnewline", "Index from http://.* truncated at line 1"}) } func (s *KeepSuite) TestGetKeepServers_InvalidBlockLocatorPattern(c *C) { testGetKeepServersAndSummarize(c, KeepServerTestData{200, 200, "testing\n", - "Error parsing BlockInfo from index line"}) + "Error parsing BlockInfo from index line.*"}) } func (s *KeepSuite) TestGetKeepServers_ReadServerResponseEmpty(c *C) { @@ -254,10 +202,10 @@ func (s *KeepSuite) TestGetKeepServers_ReadServerResponseWithTwoBlocks(c *C) { } func testGetKeepServersAndSummarize(c *C, testData KeepServerTestData) { - ksData := make(map[string]StatusAndBody) - ksData["/status.json"] = StatusAndBody{testData.statusStatusCode, string(`{}`)} - ksData["/index"] = StatusAndBody{testData.indexStatusCode, testData.indexResponseBody} - ksStub := KeepServerStub{ksData} + ksStubResponses := make(map[string]arvadostest.StubResponse) + ksStubResponses["/status.json"] = arvadostest.StubResponse{testData.statusStatusCode, string(`{}`)} + ksStubResponses["/index"] = arvadostest.StubResponse{testData.indexStatusCode, testData.indexResponseBody} + ksStub := arvadostest.ServerStub{ksStubResponses} ks := httptest.NewServer(&ksStub) defer ks.Close() @@ -278,9 +226,9 @@ func testGetKeepServersAndSummarize(c *C, testData KeepServerTestData) { }}, } ksJSON, _ := json.Marshal(servers_list) - apiData := make(map[string]StatusAndBody) - apiData["/arvados/v1/keep_services"] = StatusAndBody{200, string(ksJSON)} - apiStub := APIStub{apiData} + apiStubResponses := make(map[string]arvadostest.StubResponse) + apiStubResponses["/arvados/v1/keep_services"] = arvadostest.StubResponse{200, string(ksJSON)} + apiStub := arvadostest.ServerStub{apiStubResponses} api := httptest.NewServer(&apiStub) defer api.Close() @@ -325,11 +273,6 @@ func testGetKeepServersAndSummarize(c *C, testData KeepServerTestData) { } } } else { - if testData.expectedError == "Error parsing BlockInfo from index line" { - // In this case ErrorMatches does not work because the error message contains regexp match characters - strings.Contains(err.Error(), testData.expectedError) - } else { - c.Assert(err, ErrorMatches, fmt.Sprintf(".*%s.*", testData.expectedError)) - } + c.Assert(err, ErrorMatches, testData.expectedError) } }