12199: Rename instance type features: a1.xl => instancetype=a1.xl
[arvados.git] / services / keepstore / pull_worker_integration_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package main
6
7 import (
8         "bytes"
9         "errors"
10         "io"
11         "io/ioutil"
12         "os"
13         "strings"
14         "testing"
15
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"
19 )
20
21 var keepClient *keepclient.KeepClient
22
23 type PullWorkIntegrationTestData struct {
24         Name     string
25         Locator  string
26         Content  string
27         GetError string
28 }
29
30 func SetupPullWorkerIntegrationTest(t *testing.T, testData PullWorkIntegrationTestData, wantData bool) PullRequest {
31         os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
32
33         // start api and keep servers
34         arvadostest.StartAPI()
35         arvadostest.StartKeep(2, false)
36         keepclient.RefreshServiceDiscovery()
37
38         // make arvadosclient
39         arv, err := arvadosclient.MakeArvadosClient()
40         if err != nil {
41                 t.Fatalf("Error creating arv: %s", err)
42         }
43
44         // keep client
45         keepClient, err = keepclient.MakeKeepClient(arv)
46         if err != nil {
47                 t.Fatalf("error creating KeepClient: %s", err)
48         }
49         keepClient.Want_replicas = 1
50
51         // discover keep services
52         var servers []string
53         for _, host := range keepClient.LocalRoots() {
54                 servers = append(servers, host)
55         }
56
57         // Put content if the test needs it
58         if wantData {
59                 locator, _, err := keepClient.PutB([]byte(testData.Content))
60                 if err != nil {
61                         t.Errorf("Error putting test data in setup for %s %s %v", testData.Content, locator, err)
62                 }
63                 if locator == "" {
64                         t.Errorf("No locator found after putting test data")
65                 }
66         }
67
68         // Create pullRequest for the test
69         pullRequest := PullRequest{
70                 Locator: testData.Locator,
71                 Servers: servers,
72         }
73         return pullRequest
74 }
75
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",
82                 Content:  "hello",
83                 GetError: "Block not found",
84         }
85
86         pullRequest := SetupPullWorkerIntegrationTest(t, testData, false)
87         defer arvadostest.StopAPI()
88         defer arvadostest.StopKeep(2)
89
90         performPullWorkerIntegrationTest(testData, pullRequest, t)
91 }
92
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",
99                 Content:  "hello",
100                 GetError: "",
101         }
102
103         pullRequest := SetupPullWorkerIntegrationTest(t, testData, true)
104         defer arvadostest.StopAPI()
105         defer arvadostest.StopKeep(2)
106
107         performPullWorkerIntegrationTest(testData, pullRequest, t)
108 }
109
110 // Perform the test.
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) {
114
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)
120                 }
121         }
122
123         // Override GetContent to mock keepclient Get functionality
124         defer func(orig func(string, *keepclient.KeepClient) (io.ReadCloser, int64, string, error)) {
125                 GetContent = orig
126         }(GetContent)
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)
130                 }
131                 rdr := ioutil.NopCloser(bytes.NewBufferString(testData.Content))
132                 return rdr, int64(len(testData.Content)), "", nil
133         }
134
135         err := PullItemAndProcess(pullRequest, keepClient)
136
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)
140                 }
141         } else {
142                 if err != nil {
143                         t.Errorf("Got error %v, expected nil", err)
144                 }
145         }
146 }