X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/edc5c5d88db4f9584db8b8a4ec24e9307ec45244..7083622b12ec5f432fbe095c2230bde41bd84ca5:/services/keepstore/pull_worker_integration_test.go diff --git a/services/keepstore/pull_worker_integration_test.go b/services/keepstore/pull_worker_integration_test.go index 49443808fd..3855b4ecd3 100644 --- a/services/keepstore/pull_worker_integration_test.go +++ b/services/keepstore/pull_worker_integration_test.go @@ -1,19 +1,22 @@ -package main +// Copyright (C) The Arvados Authors. All rights reserved. +// +// SPDX-License-Identifier: AGPL-3.0 + +package keepstore import ( - "crypto/tls" - "encoding/json" - "fmt" - "git.curoverse.com/arvados.git/sdk/go/arvadosclient" - "git.curoverse.com/arvados.git/sdk/go/arvadostest" - "git.curoverse.com/arvados.git/sdk/go/keepclient" - "net/http" - "os" + "bytes" + "context" + "errors" + "io" + "io/ioutil" "strings" - "testing" -) -var keepClient keepclient.KeepClient + "git.arvados.org/arvados.git/sdk/go/arvadostest" + "git.arvados.org/arvados.git/sdk/go/keepclient" + "github.com/prometheus/client_golang/prometheus" + check "gopkg.in/check.v1" +) type PullWorkIntegrationTestData struct { Name string @@ -22,120 +25,31 @@ type PullWorkIntegrationTestData struct { GetError string } -func SetupPullWorkerIntegrationTest(t *testing.T, testData PullWorkIntegrationTestData, wantData bool) PullRequest { - os.Setenv("ARVADOS_API_HOST_INSECURE", "true") - - arvadostest.StartAPI() - arvadostest.StartKeep() - - arv, err := arvadosclient.MakeArvadosClient() - if err != nil { - t.Error("Error creating arv") - } - - servers := GetKeepServices(t) - - random_token := GenerateRandomApiToken() - +func (s *HandlerSuite) setupPullWorkerIntegrationTest(c *check.C, testData PullWorkIntegrationTestData, wantData bool) PullRequest { + arvadostest.StartKeep(2, false) + c.Assert(s.handler.setup(context.Background(), s.cluster, "", prometheus.NewRegistry(), testServiceURL), check.IsNil) // Put content if the test needs it if wantData { - CreateKeepClient(arv, random_token) - keepClient.Arvados.ApiToken = random_token - - service_roots := make(map[string]string) - for _, addr := range servers { - service_roots[addr] = addr - } - keepClient.SetServiceRoots(service_roots) - - locator, _, err := keepClient.PutB([]byte(testData.Content)) + locator, _, err := s.handler.keepClient.PutB([]byte(testData.Content)) if err != nil { - t.Errorf("Error putting test data in setup for %s %s %v", testData.Content, locator, err) + c.Errorf("Error putting test data in setup for %s %s %v", testData.Content, locator, err) + } + if locator == "" { + c.Errorf("No locator found after putting test data") } } // Create pullRequest for the test - CreateKeepClient(arv, random_token) - pullRequest := PullRequest{ Locator: testData.Locator, - Servers: servers, } return pullRequest } -func CreateKeepClient(arv arvadosclient.ArvadosClient, random_token string) { - client := &http.Client{Transport: &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}} - - keepClient = keepclient.KeepClient{ - Arvados: &arv, - Want_replicas: 1, - Using_proxy: true, - Client: client, - } - keepClient.Arvados.ApiToken = random_token -} - -func GetKeepServices(t *testing.T) []string { - client := &http.Client{Transport: &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}} - - req, err := http.NewRequest("GET", fmt.Sprintf("https://%s/arvados/v1/keep_services", os.Getenv("ARVADOS_API_HOST")), nil) - if err != nil { - t.Errorf("Error getting keep services: ", err) - } - req.Header.Set("Authorization", fmt.Sprintf("OAuth2 %s", os.Getenv("ARVADOS_API_TOKEN"))) - - resp, err := client.Do(req) - if err != nil { - t.Errorf("Error getting keep services: ", err) - } - if resp.StatusCode != 200 { - t.Errorf("Error status code getting keep services", resp.StatusCode) - } - - defer resp.Body.Close() - var servers []string - - decoder := json.NewDecoder(resp.Body) - - var respJSON map[string]interface{} - err = decoder.Decode(&respJSON) - if err != nil { - t.Errorf("Error decoding response for keep services: ", err) - } - - var service_names []string - var service_ports []string - for _, v1 := range respJSON { - switch v1_type := v1.(type) { - case []interface{}: - for _, v2 := range v1_type { - switch v2_type := v2.(type) { - case map[string]interface{}: - for name, value := range v2_type { - if name == "service_host" { - service_names = append(service_names, fmt.Sprintf("%s", value)) - } else if name == "service_port" { - service_ports = append(service_ports, strings.Split(fmt.Sprintf("%f", value), ".")[0]) - } - } - default: - } - } - default: - } - } - - for i, port := range service_ports { - servers = append(servers, "https://"+service_names[i]+":"+port) - } - - return servers -} - -func TestPullWorkerIntegration_GetNonExistingLocator(t *testing.T) { +// Do a get on a block that is not existing in any of the keep servers. +// Expect "block not found" error. +func (s *HandlerSuite) TestPullWorkerIntegration_GetNonExistingLocator(c *check.C) { + c.Assert(s.handler.setup(context.Background(), s.cluster, "", prometheus.NewRegistry(), testServiceURL), check.IsNil) testData := PullWorkIntegrationTestData{ Name: "TestPullWorkerIntegration_GetLocator", Locator: "5d41402abc4b2a76b9719d911017c592", @@ -143,12 +57,16 @@ func TestPullWorkerIntegration_GetNonExistingLocator(t *testing.T) { GetError: "Block not found", } - pullRequest := SetupPullWorkerIntegrationTest(t, testData, false) + pullRequest := s.setupPullWorkerIntegrationTest(c, testData, false) + defer arvadostest.StopKeep(2) - performPullWorkerIntegrationTest(testData, pullRequest, t) + s.performPullWorkerIntegrationTest(testData, pullRequest, c) } -func TestPullWorkerIntegration_GetExistingLocator(t *testing.T) { +// Do a get on a block that exists on one of the keep servers. +// The setup method will create this block before doing the get. +func (s *HandlerSuite) TestPullWorkerIntegration_GetExistingLocator(c *check.C) { + c.Assert(s.handler.setup(context.Background(), s.cluster, "", prometheus.NewRegistry(), testServiceURL), check.IsNil) testData := PullWorkIntegrationTestData{ Name: "TestPullWorkerIntegration_GetLocator", Locator: "5d41402abc4b2a76b9719d911017c592", @@ -156,24 +74,45 @@ func TestPullWorkerIntegration_GetExistingLocator(t *testing.T) { GetError: "", } - pullRequest := SetupPullWorkerIntegrationTest(t, testData, true) + pullRequest := s.setupPullWorkerIntegrationTest(c, testData, true) + defer arvadostest.StopKeep(2) - performPullWorkerIntegrationTest(testData, pullRequest, t) + s.performPullWorkerIntegrationTest(testData, pullRequest, c) } -func performPullWorkerIntegrationTest(testData PullWorkIntegrationTestData, pullRequest PullRequest, t *testing.T) { - // Override PutContent to mock PutBlock functionality - PutContent = func(content []byte, locator string) (err error) { - return +// Perform the test. +// The test directly invokes the "PullItemAndProcess" rather than +// putting an item on the pullq so that the errors can be verified. +func (s *HandlerSuite) performPullWorkerIntegrationTest(testData PullWorkIntegrationTestData, pullRequest PullRequest, c *check.C) { + + // Override writePulledBlock to mock PutBlock functionality + defer func(orig func(*RRVolumeManager, Volume, []byte, string) error) { writePulledBlock = orig }(writePulledBlock) + writePulledBlock = func(_ *RRVolumeManager, _ Volume, content []byte, _ string) error { + c.Check(string(content), check.Equals, testData.Content) + return nil } - err := PullItemAndProcess(pullRequest, keepClient.Arvados.ApiToken, keepClient) + // Override GetContent to mock keepclient Get functionality + defer func(orig func(string, *keepclient.KeepClient) (io.ReadCloser, int64, string, error)) { + GetContent = orig + }(GetContent) + GetContent = func(signedLocator string, keepClient *keepclient.KeepClient) (reader io.ReadCloser, contentLength int64, url string, err error) { + if testData.GetError != "" { + return nil, 0, "", errors.New(testData.GetError) + } + rdr := ioutil.NopCloser(bytes.NewBufferString(testData.Content)) + return rdr, int64(len(testData.Content)), "", nil + } + + err := s.handler.pullItemAndProcess(pullRequest) if len(testData.GetError) > 0 { if (err == nil) || (!strings.Contains(err.Error(), testData.GetError)) { - t.Fail() + c.Errorf("Got error %v, expected %v", err, testData.GetError) } } else { - t.Fail() + if err != nil { + c.Errorf("Got error %v, expected nil", err) + } } }