1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.arvados.org/arvados.git/sdk/go/arvadostest"
16 "git.arvados.org/arvados.git/sdk/go/keepclient"
17 "github.com/prometheus/client_golang/prometheus"
18 check "gopkg.in/check.v1"
21 type PullWorkIntegrationTestData struct {
28 func (s *HandlerSuite) setupPullWorkerIntegrationTest(c *check.C, testData PullWorkIntegrationTestData, wantData bool) PullRequest {
29 arvadostest.StartKeep(2, false)
30 c.Assert(s.handler.setup(context.Background(), s.cluster, "", prometheus.NewRegistry(), testServiceURL), check.IsNil)
31 // Put content if the test needs it
33 locator, _, err := s.handler.keepClient.PutB([]byte(testData.Content))
35 c.Errorf("Error putting test data in setup for %s %s %v", testData.Content, locator, err)
38 c.Errorf("No locator found after putting test data")
42 // Create pullRequest for the test
43 pullRequest := PullRequest{
44 Locator: testData.Locator,
49 // Do a get on a block that is not existing in any of the keep servers.
50 // Expect "block not found" error.
51 func (s *HandlerSuite) TestPullWorkerIntegration_GetNonExistingLocator(c *check.C) {
52 c.Assert(s.handler.setup(context.Background(), s.cluster, "", prometheus.NewRegistry(), testServiceURL), check.IsNil)
53 testData := PullWorkIntegrationTestData{
54 Name: "TestPullWorkerIntegration_GetLocator",
55 Locator: "5d41402abc4b2a76b9719d911017c592",
57 GetError: "Block not found",
60 pullRequest := s.setupPullWorkerIntegrationTest(c, testData, false)
61 defer arvadostest.StopKeep(2)
63 s.performPullWorkerIntegrationTest(testData, pullRequest, c)
66 // Do a get on a block that exists on one of the keep servers.
67 // The setup method will create this block before doing the get.
68 func (s *HandlerSuite) TestPullWorkerIntegration_GetExistingLocator(c *check.C) {
69 c.Assert(s.handler.setup(context.Background(), s.cluster, "", prometheus.NewRegistry(), testServiceURL), check.IsNil)
70 testData := PullWorkIntegrationTestData{
71 Name: "TestPullWorkerIntegration_GetLocator",
72 Locator: "5d41402abc4b2a76b9719d911017c592",
77 pullRequest := s.setupPullWorkerIntegrationTest(c, testData, true)
78 defer arvadostest.StopKeep(2)
80 s.performPullWorkerIntegrationTest(testData, pullRequest, c)
84 // The test directly invokes the "PullItemAndProcess" rather than
85 // putting an item on the pullq so that the errors can be verified.
86 func (s *HandlerSuite) performPullWorkerIntegrationTest(testData PullWorkIntegrationTestData, pullRequest PullRequest, c *check.C) {
88 // Override writePulledBlock to mock PutBlock functionality
89 defer func(orig func(*RRVolumeManager, Volume, []byte, string) error) { writePulledBlock = orig }(writePulledBlock)
90 writePulledBlock = func(_ *RRVolumeManager, _ Volume, content []byte, _ string) error {
91 c.Check(string(content), check.Equals, testData.Content)
95 // Override GetContent to mock keepclient Get functionality
96 defer func(orig func(string, *keepclient.KeepClient) (io.ReadCloser, int64, string, error)) {
99 GetContent = func(signedLocator string, keepClient *keepclient.KeepClient) (reader io.ReadCloser, contentLength int64, url string, err error) {
100 if testData.GetError != "" {
101 return nil, 0, "", errors.New(testData.GetError)
103 rdr := ioutil.NopCloser(bytes.NewBufferString(testData.Content))
104 return rdr, int64(len(testData.Content)), "", nil
107 err := s.handler.pullItemAndProcess(pullRequest)
109 if len(testData.GetError) > 0 {
110 if (err == nil) || (!strings.Contains(err.Error(), testData.GetError)) {
111 c.Errorf("Got error %v, expected %v", err, testData.GetError)
115 c.Errorf("Got error %v, expected nil", err)