Merge branch '11644-mounts-api'
[arvados.git] / services / keepstore / pull_worker_integration_test.go
1 package main
2
3 import (
4         "bytes"
5         "errors"
6         "io"
7         "io/ioutil"
8         "net/http"
9         "os"
10         "strings"
11         "testing"
12
13         "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
14         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
15         "git.curoverse.com/arvados.git/sdk/go/keepclient"
16 )
17
18 var keepClient *keepclient.KeepClient
19
20 type PullWorkIntegrationTestData struct {
21         Name     string
22         Locator  string
23         Content  string
24         GetError string
25 }
26
27 func SetupPullWorkerIntegrationTest(t *testing.T, testData PullWorkIntegrationTestData, wantData bool) PullRequest {
28         os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
29
30         // start api and keep servers
31         arvadostest.StartAPI()
32         arvadostest.StartKeep(2, false)
33
34         // make arvadosclient
35         arv, err := arvadosclient.MakeArvadosClient()
36         if err != nil {
37                 t.Error("Error creating arv")
38         }
39
40         // keep client
41         keepClient = &keepclient.KeepClient{
42                 Arvados:       arv,
43                 Want_replicas: 1,
44                 Client:        &http.Client{},
45         }
46
47         // discover keep services
48         var servers []string
49         if err := keepClient.DiscoverKeepServers(); err != nil {
50                 t.Error("Error discovering keep services")
51         }
52         for _, host := range keepClient.LocalRoots() {
53                 servers = append(servers, host)
54         }
55
56         // Put content if the test needs it
57         if wantData {
58                 locator, _, err := keepClient.PutB([]byte(testData.Content))
59                 if err != nil {
60                         t.Errorf("Error putting test data in setup for %s %s %v", testData.Content, locator, err)
61                 }
62                 if locator == "" {
63                         t.Errorf("No locator found after putting test data")
64                 }
65         }
66
67         // Create pullRequest for the test
68         pullRequest := PullRequest{
69                 Locator: testData.Locator,
70                 Servers: servers,
71         }
72         return pullRequest
73 }
74
75 // Do a get on a block that is not existing in any of the keep servers.
76 // Expect "block not found" error.
77 func TestPullWorkerIntegration_GetNonExistingLocator(t *testing.T) {
78         testData := PullWorkIntegrationTestData{
79                 Name:     "TestPullWorkerIntegration_GetLocator",
80                 Locator:  "5d41402abc4b2a76b9719d911017c592",
81                 Content:  "hello",
82                 GetError: "Block not found",
83         }
84
85         pullRequest := SetupPullWorkerIntegrationTest(t, testData, false)
86         defer arvadostest.StopAPI()
87         defer arvadostest.StopKeep(2)
88
89         performPullWorkerIntegrationTest(testData, pullRequest, t)
90 }
91
92 // Do a get on a block that exists on one of the keep servers.
93 // The setup method will create this block before doing the get.
94 func TestPullWorkerIntegration_GetExistingLocator(t *testing.T) {
95         testData := PullWorkIntegrationTestData{
96                 Name:     "TestPullWorkerIntegration_GetLocator",
97                 Locator:  "5d41402abc4b2a76b9719d911017c592",
98                 Content:  "hello",
99                 GetError: "",
100         }
101
102         pullRequest := SetupPullWorkerIntegrationTest(t, testData, true)
103         defer arvadostest.StopAPI()
104         defer arvadostest.StopKeep(2)
105
106         performPullWorkerIntegrationTest(testData, pullRequest, t)
107 }
108
109 // Perform the test.
110 // The test directly invokes the "PullItemAndProcess" rather than
111 // putting an item on the pullq so that the errors can be verified.
112 func performPullWorkerIntegrationTest(testData PullWorkIntegrationTestData, pullRequest PullRequest, t *testing.T) {
113
114         // Override writePulledBlock to mock PutBlock functionality
115         defer func(orig func(Volume, []byte, string)) { writePulledBlock = orig }(writePulledBlock)
116         writePulledBlock = func(v Volume, content []byte, locator string) {
117                 if string(content) != testData.Content {
118                         t.Errorf("writePulledBlock invoked with unexpected data. Expected: %s; Found: %s", testData.Content, content)
119                 }
120         }
121
122         // Override GetContent to mock keepclient Get functionality
123         defer func(orig func(string, *keepclient.KeepClient) (io.ReadCloser, int64, string, error)) {
124                 GetContent = orig
125         }(GetContent)
126         GetContent = func(signedLocator string, keepClient *keepclient.KeepClient) (reader io.ReadCloser, contentLength int64, url string, err error) {
127                 if testData.GetError != "" {
128                         return nil, 0, "", errors.New(testData.GetError)
129                 }
130                 rdr := ioutil.NopCloser(bytes.NewBufferString(testData.Content))
131                 return rdr, int64(len(testData.Content)), "", nil
132         }
133
134         err := PullItemAndProcess(pullRequest, keepClient)
135
136         if len(testData.GetError) > 0 {
137                 if (err == nil) || (!strings.Contains(err.Error(), testData.GetError)) {
138                         t.Errorf("Got error %v, expected %v", err, testData.GetError)
139                 }
140         } else {
141                 if err != nil {
142                         t.Errorf("Got error %v, expected nil", err)
143                 }
144         }
145 }