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