X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/1795bd4ba883c85c8277848f4e1f7a4cf9983ec8..47e42f1129363c2565e69c36ff26ce9c42731fb8:/services/datamanager/keep/keep_test.go diff --git a/services/datamanager/keep/keep_test.go b/services/datamanager/keep/keep_test.go index 38bb1b8c9f..ca8797ea6e 100644 --- a/services/datamanager/keep/keep_test.go +++ b/services/datamanager/keep/keep_test.go @@ -2,6 +2,8 @@ package keep import ( "encoding/json" + "fmt" + "net" "net/http" "net/http/httptest" "net/url" @@ -10,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" @@ -40,15 +43,15 @@ func (s *KeepSuite) TestSendTrashLists(c *C) { defer server.Close() tl := map[string]TrashList{ - server.URL: TrashList{TrashRequest{"000000000000000000000000deadbeef", 99}}} + server.URL: {TrashRequest{"000000000000000000000000deadbeef", 99}}} - arv := arvadosclient.ArvadosClient{ApiToken: "abc123"} - kc := keepclient.KeepClient{Arvados: &arv, Client: &http.Client{}} + arv := &arvadosclient.ArvadosClient{ApiToken: "abc123"} + kc := keepclient.KeepClient{Arvados: arv, Client: &http.Client{}} kc.SetServiceRoots(map[string]string{"xxxx": server.URL}, map[string]string{"xxxx": server.URL}, map[string]string{}) - err := SendTrashLists(&kc, tl) + err := SendTrashLists(nil, &kc, tl, false) c.Check(err, IsNil) @@ -67,15 +70,15 @@ func (tse *TestHandlerError) ServeHTTP(writer http.ResponseWriter, req *http.Req func sendTrashListError(c *C, server *httptest.Server) { tl := map[string]TrashList{ - server.URL: TrashList{TrashRequest{"000000000000000000000000deadbeef", 99}}} + server.URL: {TrashRequest{"000000000000000000000000deadbeef", 99}}} - arv := arvadosclient.ArvadosClient{ApiToken: "abc123"} - kc := keepclient.KeepClient{Arvados: &arv, Client: &http.Client{}} + arv := &arvadosclient.ArvadosClient{ApiToken: "abc123"} + kc := keepclient.KeepClient{Arvados: arv, Client: &http.Client{}} kc.SetServiceRoots(map[string]string{"xxxx": server.URL}, map[string]string{"xxxx": server.URL}, map[string]string{}) - err := SendTrashLists(&kc, tl) + err := SendTrashLists(nil, &kc, tl, false) c.Check(err, NotNil) c.Check(err[0], NotNil) @@ -91,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 @@ -151,18 +101,18 @@ type APITestData struct { } func (s *KeepSuite) TestGetKeepServers_UnsupportedServiceType(c *C) { - testGetKeepServersFromAPI(c, APITestData{1, "notadisk", 200}) + testGetKeepServersFromAPI(c, APITestData{1, "notadisk", 200}, "Found no keepservices with the service type disk") } func (s *KeepSuite) TestGetKeepServers_ReceivedTooFewServers(c *C) { - testGetKeepServersFromAPI(c, APITestData{2, "disk", 200}) + testGetKeepServersFromAPI(c, APITestData{2, "disk", 200}, "Did not receive all available keep servers") } func (s *KeepSuite) TestGetKeepServers_ServerError(c *C) { - testGetKeepServersFromAPI(c, APITestData{-1, "disk", -1}) + testGetKeepServersFromAPI(c, APITestData{-1, "disk", -1}, "arvados API server error") } -func testGetKeepServersFromAPI(c *C, testData APITestData) { +func testGetKeepServersFromAPI(c *C, testData APITestData, expectedError string) { keepServers := ServiceList{ ItemsAvailable: testData.numServers, KeepServers: []ServerAddress{{ @@ -175,21 +125,21 @@ func testGetKeepServersFromAPI(c *C, testData APITestData) { } 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() - arv := arvadosclient.ArvadosClient{ + arv := &arvadosclient.ArvadosClient{ Scheme: "http", ApiServer: api.URL[7:], ApiToken: "abc123", Client: &http.Client{Transport: &http.Transport{}}, } - kc := keepclient.KeepClient{Arvados: &arv, Client: &http.Client{}} + kc := keepclient.KeepClient{Arvados: arv, Client: &http.Client{}} kc.SetServiceRoots(map[string]string{"xxxx": "http://example.com:23456"}, map[string]string{"xxxx": "http://example.com:23456"}, map[string]string{}) @@ -201,11 +151,8 @@ func testGetKeepServersFromAPI(c *C, testData APITestData) { } _, err := GetKeepServersAndSummarize(params) - if testData.numServers > 1 { - c.Assert(err, ErrorMatches, ".*Did not receive all available keep servers.*") - } else if testData.serverType != "disk" { - c.Assert(err, ErrorMatches, ".*Unsupported service type.*") - } + c.Assert(err, NotNil) + c.Assert(err, ErrorMatches, fmt.Sprintf(".*%s.*", expectedError)) } type KeepServerTestData struct { @@ -216,79 +163,84 @@ type KeepServerTestData struct { indexStatusCode int indexResponseBody string - // Is error expected? - expectedError bool + // expected error, if any + expectedError string } func (s *KeepSuite) TestGetKeepServers_ErrorGettingKeepServerStatus(c *C) { - testGetKeepServersAndSummarize(c, KeepServerTestData{500, 200, "ok", true}) + testGetKeepServersAndSummarize(c, KeepServerTestData{500, 200, "ok", + ".*http://.* 500 Internal Server Error"}) } func (s *KeepSuite) TestGetKeepServers_GettingIndex(c *C) { - testGetKeepServersAndSummarize(c, KeepServerTestData{200, -1, "notok", true}) + testGetKeepServersAndSummarize(c, KeepServerTestData{200, -1, "notok", + ".*redirect-loop.*"}) } func (s *KeepSuite) TestGetKeepServers_ErrorReadServerResponse(c *C) { - testGetKeepServersAndSummarize(c, KeepServerTestData{200, 500, "notok", true}) + testGetKeepServersAndSummarize(c, KeepServerTestData{200, 500, "notok", + ".*http://.* 500 Internal Server Error"}) } func (s *KeepSuite) TestGetKeepServers_ReadServerResponseTuncatedAtLineOne(c *C) { - testGetKeepServersAndSummarize(c, KeepServerTestData{200, 200, "notterminatedwithnewline", true}) + testGetKeepServersAndSummarize(c, KeepServerTestData{200, 200, + "notterminatedwithnewline", "Index from http://.* truncated at line 1"}) } func (s *KeepSuite) TestGetKeepServers_InvalidBlockLocatorPattern(c *C) { - testGetKeepServersAndSummarize(c, KeepServerTestData{200, 200, "testing\n", true}) + testGetKeepServersAndSummarize(c, KeepServerTestData{200, 200, "testing\n", + "Error parsing BlockInfo from index line.*"}) } func (s *KeepSuite) TestGetKeepServers_ReadServerResponseEmpty(c *C) { - testGetKeepServersAndSummarize(c, KeepServerTestData{200, 200, "\n", false}) + testGetKeepServersAndSummarize(c, KeepServerTestData{200, 200, "\n", ""}) } func (s *KeepSuite) TestGetKeepServers_ReadServerResponseWithTwoBlocks(c *C) { testGetKeepServersAndSummarize(c, KeepServerTestData{200, 200, - "51752ba076e461ec9ec1d27400a08548+20 1447526361\na048cc05c02ba1ee43ad071274b9e547+52 1447526362\n\n", false}) + "51752ba076e461ec9ec1d27400a08548+20 1447526361\na048cc05c02ba1ee43ad071274b9e547+52 1447526362\n\n", ""}) } 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() ksURL, err := url.Parse(ks.URL) c.Check(err, IsNil) - ksURLParts := strings.Split(ksURL.Host, ":") - ksPort, err := strconv.Atoi(ksURLParts[1]) + ksHost, port, err := net.SplitHostPort(ksURL.Host) + ksPort, err := strconv.Atoi(port) c.Check(err, IsNil) servers_list := ServiceList{ ItemsAvailable: 1, KeepServers: []ServerAddress{{ SSL: false, - Host: strings.Split(ksURL.Host, ":")[0], + Host: ksHost, Port: ksPort, UUID: "abcdefg", ServiceType: "disk", }}, } 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() - arv := arvadosclient.ArvadosClient{ + arv := &arvadosclient.ArvadosClient{ Scheme: "http", ApiServer: api.URL[7:], ApiToken: "abc123", Client: &http.Client{Transport: &http.Transport{}}, } - kc := keepclient.KeepClient{Arvados: &arv, Client: &http.Client{}} + kc := keepclient.KeepClient{Arvados: arv, Client: &http.Client{}} kc.SetServiceRoots(map[string]string{"xxxx": ks.URL}, map[string]string{"xxxx": ks.URL}, map[string]string{}) @@ -302,7 +254,7 @@ func testGetKeepServersAndSummarize(c *C, testData KeepServerTestData) { // GetKeepServersAndSummarize results, err := GetKeepServersAndSummarize(params) - if testData.expectedError == false { + if testData.expectedError == "" { c.Assert(err, IsNil) c.Assert(results, NotNil) @@ -321,6 +273,6 @@ func testGetKeepServersAndSummarize(c *C, testData KeepServerTestData) { } } } else { - c.Assert(err, ErrorMatches, ".*Error during GetServerContents; no host info found.*") + c.Assert(err, ErrorMatches, testData.expectedError) } }