3761: code refactoring
[arvados.git] / services / keepstore / pull_worker_test.go
1 package main
2
3 import (
4         "bytes"
5         "errors"
6         "io"
7         "net/http"
8         "testing"
9         "time"
10 )
11
12 func TestPullWorker(t *testing.T) {
13         defer teardown()
14
15         // Since keepstore does not come into picture in tests,
16         // we need to explicitly start the goroutine in tests.
17         go RunPullWorker(pullq.NextItem)
18
19         data_manager_token = "DATA MANAGER TOKEN"
20
21         first_pull_list := []byte(`[
22                 {
23                         "locator":"locator1_to_verify_first_pull_list",
24                         "servers":[
25                                 "server_1",
26                                 "server_2"
27                         ]
28                 },
29     {
30                         "locator":"locator2_to_verify_first_pull_list",
31                         "servers":[
32                                 "server_3"
33                         ]
34                 }
35         ]`)
36
37         second_pull_list := []byte(`[
38                 {
39                         "locator":"locator_to_verify_second_pull_list",
40                         "servers":[
41                                 "server_1",
42         "server_2"
43                         ]
44                 }
45         ]`)
46
47         type PullWorkerTestData struct {
48                 name          string
49                 req           RequestTester
50                 response_code int
51                 response_body string
52                 read_content  string
53                 read_error    bool
54                 put_error     bool
55         }
56         var testcases = []PullWorkerTestData{
57                 {
58                         "Pull request 1 from the data manager in worker",
59                         RequestTester{"/pull", data_manager_token, "PUT", first_pull_list},
60                         http.StatusOK,
61                         "Received 2 pull requests\n",
62                         "hello",
63                         false,
64                         false,
65                 },
66                 {
67                         "Pull request 2 from the data manager in worker",
68                         RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
69                         http.StatusOK,
70                         "Received 1 pull requests\n",
71                         "hola",
72                         false,
73                         false,
74                 },
75                 {
76                         "Pull request with error on get",
77                         RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
78                         http.StatusOK,
79                         "Received 1 pull requests\n",
80                         "unused",
81                         true,
82                         false,
83                 },
84                 {
85                         "Pull request with error on put",
86                         RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
87                         http.StatusOK,
88                         "Received 1 pull requests\n",
89                         "unused",
90                         false,
91                         true,
92                 },
93         }
94
95         for _, testData := range testcases {
96                 // Override GetContent to mock keepclient functionality
97                 GetContent = func(signedLocator string) (reader io.ReadCloser, contentLength int64, url string, err error) {
98                         if testData.read_error {
99                                 return nil, 0, "", errors.New("Error getting data")
100                         } else {
101                                 cb := &ClosingBuffer{bytes.NewBufferString("Hi!")}
102                                 var rc io.ReadCloser
103                                 rc = cb
104                                 return rc, 3, "", nil
105                         }
106                 }
107
108                 // Override PutContent to mock PutBlock functionality
109                 PutContent = func(content []byte, locator string) (err error) {
110                         if testData.put_error {
111                                 return errors.New("Error putting data")
112                         } else {
113                                 return nil
114                         }
115                 }
116
117                 response := IssueRequest(&testData.req)
118                 ExpectStatusCode(t, testData.name, testData.response_code, response)
119                 ExpectBody(t, testData.name, testData.response_body, response)
120
121                 // give the channel a second to read and process all pull list entries
122                 time.Sleep(1000 * time.Millisecond)
123
124                 expectChannelEmpty(t, pullq.NextItem)
125         }
126 }
127
128 type ClosingBuffer struct {
129         *bytes.Buffer
130 }
131
132 func (cb *ClosingBuffer) Close() (err error) {
133         return
134 }