1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
16 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
17 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
18 "git.curoverse.com/arvados.git/sdk/go/keepclient"
21 var keepClient *keepclient.KeepClient
23 type PullWorkIntegrationTestData struct {
30 func SetupPullWorkerIntegrationTest(t *testing.T, testData PullWorkIntegrationTestData, wantData bool) PullRequest {
31 os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
33 // start api and keep servers
34 arvadostest.StartAPI()
35 arvadostest.StartKeep(2, false)
38 arv, err := arvadosclient.MakeArvadosClient()
40 t.Fatalf("Error creating arv: %s", err)
44 keepClient, err = keepclient.MakeKeepClient(arv)
46 t.Fatalf("error creating KeepClient: %s", err)
48 keepClient.Want_replicas = 1
49 keepClient.RefreshServiceDiscovery()
51 // discover keep services
53 for _, host := range keepClient.LocalRoots() {
54 servers = append(servers, host)
57 // Put content if the test needs it
59 locator, _, err := keepClient.PutB([]byte(testData.Content))
61 t.Errorf("Error putting test data in setup for %s %s %v", testData.Content, locator, err)
64 t.Errorf("No locator found after putting test data")
68 // Create pullRequest for the test
69 pullRequest := PullRequest{
70 Locator: testData.Locator,
76 // Do a get on a block that is not existing in any of the keep servers.
77 // Expect "block not found" error.
78 func TestPullWorkerIntegration_GetNonExistingLocator(t *testing.T) {
79 testData := PullWorkIntegrationTestData{
80 Name: "TestPullWorkerIntegration_GetLocator",
81 Locator: "5d41402abc4b2a76b9719d911017c592",
83 GetError: "Block not found",
86 pullRequest := SetupPullWorkerIntegrationTest(t, testData, false)
87 defer arvadostest.StopAPI()
88 defer arvadostest.StopKeep(2)
90 performPullWorkerIntegrationTest(testData, pullRequest, t)
93 // Do a get on a block that exists on one of the keep servers.
94 // The setup method will create this block before doing the get.
95 func TestPullWorkerIntegration_GetExistingLocator(t *testing.T) {
96 testData := PullWorkIntegrationTestData{
97 Name: "TestPullWorkerIntegration_GetLocator",
98 Locator: "5d41402abc4b2a76b9719d911017c592",
103 pullRequest := SetupPullWorkerIntegrationTest(t, testData, true)
104 defer arvadostest.StopAPI()
105 defer arvadostest.StopKeep(2)
107 performPullWorkerIntegrationTest(testData, pullRequest, t)
111 // The test directly invokes the "PullItemAndProcess" rather than
112 // putting an item on the pullq so that the errors can be verified.
113 func performPullWorkerIntegrationTest(testData PullWorkIntegrationTestData, pullRequest PullRequest, t *testing.T) {
115 // Override writePulledBlock to mock PutBlock functionality
116 defer func(orig func(Volume, []byte, string)) { writePulledBlock = orig }(writePulledBlock)
117 writePulledBlock = func(v Volume, content []byte, locator string) {
118 if string(content) != testData.Content {
119 t.Errorf("writePulledBlock invoked with unexpected data. Expected: %s; Found: %s", testData.Content, content)
123 // Override GetContent to mock keepclient Get functionality
124 defer func(orig func(string, *keepclient.KeepClient) (io.ReadCloser, int64, string, error)) {
127 GetContent = func(signedLocator string, keepClient *keepclient.KeepClient) (reader io.ReadCloser, contentLength int64, url string, err error) {
128 if testData.GetError != "" {
129 return nil, 0, "", errors.New(testData.GetError)
131 rdr := ioutil.NopCloser(bytes.NewBufferString(testData.Content))
132 return rdr, int64(len(testData.Content)), "", nil
135 err := PullItemAndProcess(pullRequest, keepClient)
137 if len(testData.GetError) > 0 {
138 if (err == nil) || (!strings.Contains(err.Error(), testData.GetError)) {
139 t.Errorf("Got error %v, expected %v", err, testData.GetError)
143 t.Errorf("Got error %v, expected nil", err)