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