3761: additional tests
[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                 put_error     bool
53         }
54         var testcases = []PullWorkerTestData{
55                 {
56                         "Pull request 1 from the data manager in worker",
57                         RequestTester{"/pull", data_manager_token, "PUT", first_pull_list},
58                         http.StatusOK,
59                         "Received 2 pull requests\n",
60                         "hello",
61                         false,
62                         false,
63                 },
64                 {
65                         "Pull request 2 from the data manager in worker",
66                         RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
67                         http.StatusOK,
68                         "Received 1 pull requests\n",
69                         "hola",
70                         false,
71                         false,
72                 },
73                 {
74                         "Pull request with error on get",
75                         RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
76                         http.StatusOK,
77                         "Received 1 pull requests\n",
78                         "unused",
79                         true,
80                         false,
81                 },
82                 {
83                         "Pull request with error on put",
84                         RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
85                         http.StatusOK,
86                         "Received 1 pull requests\n",
87                         "unused",
88                         false,
89                         true,
90                 },
91         }
92
93         for _, testData := range testcases {
94                 // Override GetContent to mock keepclient functionality
95                 GetContent = func(addr string, locator string) ([]byte, error) {
96                         if testData.read_error {
97                                 return nil, errors.New("Error getting data")
98                         } else {
99                                 return []byte(testData.read_content), nil
100                         }
101                 }
102
103                 // Override PutContent to mock PutBlock functionality
104                 PutContent = func(content []byte, locator string) (err error) {
105                         if testData.put_error {
106                                 return errors.New("Error putting data")
107                         } else {
108                                 return nil
109                         }
110                 }
111
112                 response := IssueRequest(&testData.req)
113                 ExpectStatusCode(t, testData.name, testData.response_code, response)
114                 ExpectBody(t, testData.name, testData.response_body, response)
115
116                 // give the channel a second to read and process all pull list entries
117                 time.Sleep(1000 * time.Millisecond)
118
119                 expectChannelEmpty(t, pullq.NextItem)
120         }
121 }