3a3069ab7745c1efc0a21fd8c706565f65c525e0
[arvados.git] / services / keepstore / pull_worker_integration_test.go
1 package main
2
3 import (
4         "bytes"
5         "errors"
6         "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
7         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
8         "git.curoverse.com/arvados.git/sdk/go/keepclient"
9         "io"
10         "net/http"
11         "os"
12         "strings"
13         "testing"
14 )
15
16 var keepClient *keepclient.KeepClient
17
18 type PullWorkIntegrationTestData struct {
19         Name     string
20         Locator  string
21         Content  string
22         GetError string
23 }
24
25 func SetupPullWorkerIntegrationTest(t *testing.T, testData PullWorkIntegrationTestData, wantData bool) PullRequest {
26         os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
27
28         // start api and keep servers
29         arvadostest.StartAPI()
30         arvadostest.StartKeep(2, false)
31
32         // make arvadosclient
33         arv, err := arvadosclient.MakeArvadosClient()
34         if err != nil {
35                 t.Error("Error creating arv")
36         }
37
38         // keep client
39         keepClient = &keepclient.KeepClient{
40                 Arvados:       &arv,
41                 Want_replicas: 1,
42                 Using_proxy:   true,
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
86         performPullWorkerIntegrationTest(testData, pullRequest, t)
87 }
88
89 // Do a get on a block that exists on one of the keep servers.
90 // The setup method will create this block before doing the get.
91 func TestPullWorkerIntegration_GetExistingLocator(t *testing.T) {
92         testData := PullWorkIntegrationTestData{
93                 Name:     "TestPullWorkerIntegration_GetLocator",
94                 Locator:  "5d41402abc4b2a76b9719d911017c592",
95                 Content:  "hello",
96                 GetError: "",
97         }
98
99         pullRequest := SetupPullWorkerIntegrationTest(t, testData, true)
100
101         performPullWorkerIntegrationTest(testData, pullRequest, t)
102 }
103
104 // Perform the test.
105 // The test directly invokes the "PullItemAndProcess" rather than
106 // putting an item on the pullq so that the errors can be verified.
107 func performPullWorkerIntegrationTest(testData PullWorkIntegrationTestData, pullRequest PullRequest, t *testing.T) {
108
109         // Override PutContent to mock PutBlock functionality
110         defer func(orig func([]byte, string) error) { PutContent = orig }(PutContent)
111         PutContent = func(content []byte, locator string) (err error) {
112                 if string(content) != testData.Content {
113                         t.Errorf("PutContent invoked with unexpected data. Expected: %s; Found: %s", testData.Content, content)
114                 }
115                 return
116         }
117
118         // Override GetContent to mock keepclient Get functionality
119         defer func(orig func(string, *keepclient.KeepClient) (io.ReadCloser, int64, string, error)) {
120                 GetContent = orig
121         }(GetContent)
122         GetContent = func(signedLocator string, keepClient *keepclient.KeepClient) (
123                 reader io.ReadCloser, contentLength int64, url string, err error) {
124                 if testData.GetError != "" {
125                         return nil, 0, "", errors.New(testData.GetError)
126                 }
127                 rdr := &ClosingBuffer{bytes.NewBufferString(testData.Content)}
128                 return rdr, int64(len(testData.Content)), "", nil
129         }
130
131         keepClient.Arvados.ApiToken = GenerateRandomAPIToken()
132         err := PullItemAndProcess(pullRequest, keepClient.Arvados.ApiToken, keepClient)
133
134         if len(testData.GetError) > 0 {
135                 if (err == nil) || (!strings.Contains(err.Error(), testData.GetError)) {
136                         t.Errorf("Got error %v, expected %v", err, testData.GetError)
137                 }
138         } else {
139                 if err != nil {
140                         t.Errorf("Got error %v, expected nil", err)
141                 }
142         }
143 }