6 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
7 "git.curoverse.com/arvados.git/sdk/go/keepclient"
14 type PullWorkerTestSuite struct{}
16 // Gocheck boilerplate
17 func TestPullWorker(t *testing.T) {
21 // Gocheck boilerplate
22 var _ = Suite(&PullWorkerTestSuite{})
24 var testPullLists map[string]string
25 var processedPullLists map[string]string
26 var readContent string
30 var currentTestData PullWorkerTestData
32 func (s *PullWorkerTestSuite) SetUpTest(c *C) {
35 putContent = []byte("")
38 // When a new pull request arrives, the old one will be overwritten.
39 // This behavior is verified using these two maps in the
40 // "TestPullWorker_pull_list_with_two_items_latest_replacing_old"
41 testPullLists = make(map[string]string)
42 processedPullLists = make(map[string]string)
45 // Since keepstore does not come into picture in tests,
46 // we need to explicitly start the goroutine in tests.
47 func RunTestPullWorker(c *C) {
48 arv, err := arvadosclient.MakeArvadosClient()
49 c.Assert(err, Equals, nil)
50 keepClient, err := keepclient.MakeKeepClient(&arv)
51 c.Assert(err, Equals, nil)
53 pullq = NewWorkQueue()
54 go RunPullWorker(pullq, keepClient)
57 var first_pull_list = []byte(`[
73 var second_pull_list = []byte(`[
83 type PullWorkerTestData struct {
93 func (s *PullWorkerTestSuite) TestPullWorker_pull_list_with_two_locators(c *C) {
96 data_manager_token = "DATA MANAGER TOKEN"
97 testData := PullWorkerTestData{
98 name: "TestPullWorker_pull_list_with_two_locators",
99 req: RequestTester{"/pull", data_manager_token, "PUT", first_pull_list},
100 response_code: http.StatusOK,
101 response_body: "Received 2 pull requests\n",
102 read_content: "hello",
107 performTest(testData, c)
110 func (s *PullWorkerTestSuite) TestPullWorker_pull_list_with_one_locator(c *C) {
113 data_manager_token = "DATA MANAGER TOKEN"
114 testData := PullWorkerTestData{
115 name: "TestPullWorker_pull_list_with_one_locator",
116 req: RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
117 response_code: http.StatusOK,
118 response_body: "Received 1 pull requests\n",
119 read_content: "hola",
124 performTest(testData, c)
127 func (s *PullWorkerTestSuite) TestPullWorker_error_on_get_one_locator(c *C) {
130 data_manager_token = "DATA MANAGER TOKEN"
131 testData := PullWorkerTestData{
132 name: "TestPullWorker_error_on_get_one_locator",
133 req: RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
134 response_code: http.StatusOK,
135 response_body: "Received 1 pull requests\n",
136 read_content: "unused",
141 performTest(testData, c)
144 func (s *PullWorkerTestSuite) TestPullWorker_error_on_get_two_locators(c *C) {
147 data_manager_token = "DATA MANAGER TOKEN"
148 testData := PullWorkerTestData{
149 name: "TestPullWorker_error_on_get_two_locators",
150 req: RequestTester{"/pull", data_manager_token, "PUT", first_pull_list},
151 response_code: http.StatusOK,
152 response_body: "Received 2 pull requests\n",
153 read_content: "unused",
158 performTest(testData, c)
161 func (s *PullWorkerTestSuite) TestPullWorker_error_on_put_one_locator(c *C) {
164 data_manager_token = "DATA MANAGER TOKEN"
165 testData := PullWorkerTestData{
166 name: "TestPullWorker_error_on_put_one_locator",
167 req: RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
168 response_code: http.StatusOK,
169 response_body: "Received 1 pull requests\n",
170 read_content: "hello hello",
175 performTest(testData, c)
178 func (s *PullWorkerTestSuite) TestPullWorker_error_on_put_two_locators(c *C) {
181 data_manager_token = "DATA MANAGER TOKEN"
182 testData := PullWorkerTestData{
183 name: "TestPullWorker_error_on_put_two_locators",
184 req: RequestTester{"/pull", data_manager_token, "PUT", first_pull_list},
185 response_code: http.StatusOK,
186 response_body: "Received 2 pull requests\n",
187 read_content: "hello again",
192 performTest(testData, c)
195 // When a new pull request arrives, the old one is replaced. This test
196 // is used to check that behavior by first putting an item on the queue,
197 // and then performing the test. Thus the "testPullLists" has two entries;
198 // however, processedPullLists will see only the newest item in the list.
199 func (s *PullWorkerTestSuite) TestPullWorker_pull_list_with_two_items_latest_replacing_old(c *C) {
202 var firstInput = []int{1}
203 pullq = NewWorkQueue()
204 pullq.ReplaceQueue(makeTestWorkList(firstInput))
205 testPullLists["Added_before_actual_test_item"] = string(1)
207 data_manager_token = "DATA MANAGER TOKEN"
208 testData := PullWorkerTestData{
209 name: "TestPullWorker_pull_list_with_two_items_latest_replacing_old",
210 req: RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
211 response_code: http.StatusOK,
212 response_body: "Received 1 pull requests\n",
213 read_content: "hola de nuevo",
218 performTest(testData, c)
221 // In this case, the item will not be placed on pullq
222 func (s *PullWorkerTestSuite) TestPullWorker_invalid_data_manager_token(c *C) {
225 data_manager_token = "DATA MANAGER TOKEN"
227 testData := PullWorkerTestData{
228 name: "TestPullWorker_pull_list_with_two_locators",
229 req: RequestTester{"/pull", "invalid_data_manager_token", "PUT", first_pull_list},
230 response_code: http.StatusUnauthorized,
231 response_body: "Unauthorized\n",
232 read_content: "hello",
237 performTest(testData, c)
240 func performTest(testData PullWorkerTestData, c *C) {
243 currentTestData = testData
244 testPullLists[testData.name] = testData.response_body
246 // Override GetContent to mock keepclient Get functionality
247 GetContent = func(signedLocator string, keepClient *keepclient.KeepClient) (
248 reader io.ReadCloser, contentLength int64, url string, err error) {
250 processedPullLists[testData.name] = testData.response_body
251 if testData.read_error {
252 err = errors.New("Error getting data")
254 return nil, 0, "", err
256 readContent = testData.read_content
257 cb := &ClosingBuffer{bytes.NewBufferString(testData.read_content)}
260 return rc, int64(len(testData.read_content)), "", nil
264 // Override PutContent to mock PutBlock functionality
265 PutContent = func(content []byte, locator string) (err error) {
266 if testData.put_error {
267 err = errors.New("Error putting data")
276 response := IssueRequest(&testData.req)
277 c.Assert(testData.response_code, Equals, response.Code)
278 c.Assert(testData.response_body, Equals, response.Body.String())
280 expectWorkerChannelEmpty(c, pullq.NextItem)
284 if testData.name == "TestPullWorker_pull_list_with_two_items_latest_replacing_old" {
285 c.Assert(len(testPullLists), Equals, 2)
286 c.Assert(len(processedPullLists), Equals, 1)
287 c.Assert(testPullLists["Added_before_actual_test_item"], NotNil)
288 c.Assert(testPullLists["TestPullWorker_pull_list_with_two_items_latest_replacing_old"], NotNil)
289 c.Assert(processedPullLists["TestPullWorker_pull_list_with_two_items_latest_replacing_old"], NotNil)
291 if testData.response_code == http.StatusOK {
292 c.Assert(len(testPullLists), Equals, 1)
293 c.Assert(len(processedPullLists), Equals, 1)
294 c.Assert(testPullLists[testData.name], NotNil)
296 c.Assert(len(testPullLists), Equals, 1)
297 c.Assert(len(processedPullLists), Equals, 0)
301 if testData.read_error {
302 c.Assert(readError, NotNil)
303 } else if testData.response_code == http.StatusOK {
304 c.Assert(readError, IsNil)
305 c.Assert(readContent, Equals, testData.read_content)
306 if testData.put_error {
307 c.Assert(putError, NotNil)
309 c.Assert(putError, IsNil)
310 c.Assert(string(putContent), Equals, testData.read_content)
315 type ClosingBuffer struct {
319 func (cb *ClosingBuffer) Close() (err error) {
323 func expectWorkerChannelEmpty(c *C, workerChannel <-chan interface{}) {
325 case item := <-workerChannel:
326 c.Fatalf("Received value (%v) from channel that was expected to be empty", item)
331 func expectWorkerChannelNotEmpty(c *C, workerChannel <-chan interface{}) {
333 case item := <-workerChannel:
334 c.Fatalf("Received value (%v) from channel that was expected to be empty", item)