+func (s *PullWorkerTestSuite) TestPullWorker_error_on_put_two_locators(c *C) {
+ testData := PullWorkerTestData{
+ name: "TestPullWorker_error_on_put_two_locators",
+ req: RequestTester{"/pull", theConfig.systemAuthToken, "PUT", firstPullList},
+ responseCode: http.StatusOK,
+ responseBody: "Received 2 pull requests\n",
+ readContent: "hello again",
+ readError: false,
+ putError: true,
+ }
+
+ s.performTest(testData, c)
+}
+
+// In this case, the item will not be placed on pullq
+func (s *PullWorkerTestSuite) TestPullWorker_invalidToken(c *C) {
+ testData := PullWorkerTestData{
+ name: "TestPullWorkerPullList_with_two_locators",
+ req: RequestTester{"/pull", "invalidToken", "PUT", firstPullList},
+ responseCode: http.StatusUnauthorized,
+ responseBody: "Unauthorized\n",
+ readContent: "hello",
+ readError: false,
+ putError: false,
+ }
+
+ s.performTest(testData, c)
+}
+
+func (s *PullWorkerTestSuite) performTest(testData PullWorkerTestData, c *C) {
+ s.testPullLists[testData.name] = testData.responseBody
+
+ processedPullLists := make(map[string]string)
+
+ // Override GetContent to mock keepclient Get functionality
+ defer func(orig func(string, *keepclient.KeepClient) (io.ReadCloser, int64, string, error)) {
+ GetContent = orig
+ }(GetContent)
+ GetContent = func(signedLocator string, keepClient *keepclient.KeepClient) (reader io.ReadCloser, contentLength int64, url string, err error) {
+ c.Assert(getStatusItem("PullQueue", "InProgress"), Equals, float64(1))
+ processedPullLists[testData.name] = testData.responseBody
+ if testData.readError {
+ err = errors.New("Error getting data")
+ s.readError = err
+ return
+ }
+ s.readContent = testData.readContent
+ reader = ioutil.NopCloser(bytes.NewBufferString(testData.readContent))
+ contentLength = int64(len(testData.readContent))
+ return
+ }
+
+ // Override writePulledBlock to mock PutBlock functionality
+ defer func(orig func(Volume, []byte, string)) { writePulledBlock = orig }(writePulledBlock)
+ writePulledBlock = func(v Volume, content []byte, locator string) {
+ if testData.putError {
+ s.putError = errors.New("Error putting data")
+ return
+ }
+ s.putContent = content
+ }
+
+ c.Check(getStatusItem("PullQueue", "InProgress"), Equals, float64(0))
+ c.Check(getStatusItem("PullQueue", "Queued"), Equals, float64(0))
+ c.Check(getStatusItem("Version"), Not(Equals), "")
+
+ response := IssueRequest(&testData.req)
+ c.Assert(response.Code, Equals, testData.responseCode)
+ c.Assert(response.Body.String(), Equals, testData.responseBody)
+
+ expectEqualWithin(c, time.Second, 0, func() interface{} {
+ st := pullq.Status()
+ return st.InProgress + st.Queued
+ })
+
+ if testData.name == "TestPullWorkerPullList_with_two_items_latest_replacing_old" {
+ c.Assert(len(s.testPullLists), Equals, 2)
+ c.Assert(len(processedPullLists), Equals, 1)
+ c.Assert(s.testPullLists["Added_before_actual_test_item"], NotNil)
+ c.Assert(s.testPullLists["TestPullWorkerPullList_with_two_items_latest_replacing_old"], NotNil)
+ c.Assert(processedPullLists["TestPullWorkerPullList_with_two_items_latest_replacing_old"], NotNil)
+ } else {
+ if testData.responseCode == http.StatusOK {
+ c.Assert(len(s.testPullLists), Equals, 1)
+ c.Assert(len(processedPullLists), Equals, 1)
+ c.Assert(s.testPullLists[testData.name], NotNil)
+ } else {
+ c.Assert(len(s.testPullLists), Equals, 1)
+ c.Assert(len(processedPullLists), Equals, 0)
+ }
+ }
+
+ if testData.readError {
+ c.Assert(s.readError, NotNil)
+ } else if testData.responseCode == http.StatusOK {
+ c.Assert(s.readError, IsNil)
+ c.Assert(s.readContent, Equals, testData.readContent)
+ if testData.putError {
+ c.Assert(s.putError, NotNil)
+ } else {
+ c.Assert(s.putError, IsNil)
+ c.Assert(string(s.putContent), Equals, testData.readContent)
+ }
+ }
+
+ expectChannelEmpty(c, pullq.NextItem)