3761: Run pull list worker, which processes pull reqests from the list.
[arvados.git] / services / keepstore / pull_worker_test.go
1 package main
2
3 import (
4         "errors"
5         "net/http"
6         "testing"
7         "time"
8 )
9
10 func TestPullWorker(t *testing.T) {
11         defer teardown()
12
13         // Since keepstore does not come into picture in tests,
14         // we need to explicitly start the goroutine in tests.
15         go RunPullWorker(pullq.NextItem)
16
17         data_manager_token = "DATA MANAGER TOKEN"
18
19         first_pull_list := []byte(`[
20                 {
21                         "locator":"locator1_to_verify_first_pull_list",
22                         "servers":[
23                                 "server_1",
24                                 "server_2"
25                         ]
26                 },
27     {
28                         "locator":"locator2_to_verify_first_pull_list",
29                         "servers":[
30                                 "server_1"
31                         ]
32                 }
33         ]`)
34
35         second_pull_list := []byte(`[
36                 {
37                         "locator":"locator_to_verify_second_pull_list",
38                         "servers":[
39                                 "server_1",
40         "server_2"
41                         ]
42                 }
43         ]`)
44
45         type PullWorkerTestData struct {
46                 name          string
47                 req           RequestTester
48                 response_code int
49                 response_body string
50                 read_content  string
51                 read_error    bool
52         }
53         var testcases = []PullWorkerTestData{
54                 {
55                         "Pull request 1 from the data manager in worker",
56                         RequestTester{"/pull", data_manager_token, "PUT", first_pull_list},
57                         http.StatusOK,
58                         "Received 2 pull requests\n",
59                         "hello",
60                         false,
61                 },
62                 {
63                         "Pull request 2 from the data manager in worker",
64                         RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
65                         http.StatusOK,
66                         "Received 1 pull requests\n",
67                         "hola",
68                         false,
69                 },
70                 {
71                         "Pull request with error on get",
72                         RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
73                         http.StatusOK,
74                         "Received 1 pull requests\n",
75                         "unused",
76                         true,
77                 },
78         }
79
80         for _, testData := range testcases {
81                 // Override GetContent to mock keepclient functionality
82                 GetContent = func(addr string, locator string) ([]byte, error) {
83                         if testData.read_error {
84                                 return nil, errors.New("Error getting data")
85                         } else {
86                                 return []byte(testData.read_content), nil
87                         }
88                 }
89
90                 response := IssueRequest(&testData.req)
91                 ExpectStatusCode(t, testData.name, testData.response_code, response)
92                 ExpectBody(t, testData.name, testData.response_body, response)
93
94                 // give the channel a second to read and process all pull list entries
95                 time.Sleep(1000 * time.Millisecond)
96
97                 expectChannelEmpty(t, pullq.NextItem)
98         }
99 }