5745: Fix test order dependency (restore mocked methods), tidy up test data.
[arvados.git] / services / keepstore / pull_worker_test.go
index dc6d46276682feb3fc4088a1010ef498bdd627d8..0833bc696763a867bfab287a75805b57ce933ae6 100644 (file)
@@ -56,14 +56,13 @@ func RunTestPullWorker(c *C) {
 
 var first_pull_list = []byte(`[
                {
-                       "locator":"locator1",
+                       "locator":"acbd18db4cc2f85cedef654fccc4a4d8+3",
                        "servers":[
                                "server_1",
                                "server_2"
                        ]
-               },
-    {
-                       "locator":"locator2",
+               },{
+                       "locator":"37b51d194a7513e45b56f6524f2d51f2+3",
                        "servers":[
                                "server_3"
                        ]
@@ -72,10 +71,10 @@ var first_pull_list = []byte(`[
 
 var second_pull_list = []byte(`[
                {
-                       "locator":"locator3",
+                       "locator":"73feffa4b7f6bb68e44cf984c85f6e88+3",
                        "servers":[
                                "server_1",
-        "server_2"
+                               "server_2"
                        ]
                }
        ]`)
@@ -218,6 +217,25 @@ func (s *PullWorkerTestSuite) TestPullWorker_pull_list_with_two_items_latest_rep
        performTest(testData, c)
 }
 
+// In this case, the item will not be placed on pullq
+func (s *PullWorkerTestSuite) TestPullWorker_invalid_data_manager_token(c *C) {
+       defer teardown()
+
+       data_manager_token = "DATA MANAGER TOKEN"
+
+       testData := PullWorkerTestData{
+               name:          "TestPullWorker_pull_list_with_two_locators",
+               req:           RequestTester{"/pull", "invalid_data_manager_token", "PUT", first_pull_list},
+               response_code: http.StatusUnauthorized,
+               response_body: "Unauthorized\n",
+               read_content:  "hello",
+               read_error:    false,
+               put_error:     false,
+       }
+
+       performTest(testData, c)
+}
+
 func performTest(testData PullWorkerTestData, c *C) {
        RunTestPullWorker(c)
 
@@ -225,7 +243,8 @@ func performTest(testData PullWorkerTestData, c *C) {
        testPullLists[testData.name] = testData.response_body
 
        // Override GetContent to mock keepclient Get functionality
-       GetContent = func(signedLocator string, keepClient keepclient.KeepClient) (
+       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) {
 
                processedPullLists[testData.name] = testData.response_body
@@ -243,6 +262,7 @@ func performTest(testData PullWorkerTestData, c *C) {
        }
 
        // Override PutContent to mock PutBlock functionality
+       defer func(orig func([]byte, string)(error)) { PutContent = orig }(PutContent)
        PutContent = func(content []byte, locator string) (err error) {
                if testData.put_error {
                        err = errors.New("Error putting data")
@@ -255,8 +275,8 @@ func performTest(testData PullWorkerTestData, c *C) {
        }
 
        response := IssueRequest(&testData.req)
-       c.Assert(testData.response_code, Equals, response.Code)
-       c.Assert(testData.response_body, Equals, response.Body.String())
+       c.Assert(response.Code, Equals, testData.response_code)
+       c.Assert(response.Body.String(), Equals, testData.response_body)
 
        expectWorkerChannelEmpty(c, pullq.NextItem)
 
@@ -269,14 +289,19 @@ func performTest(testData PullWorkerTestData, c *C) {
                c.Assert(testPullLists["TestPullWorker_pull_list_with_two_items_latest_replacing_old"], NotNil)
                c.Assert(processedPullLists["TestPullWorker_pull_list_with_two_items_latest_replacing_old"], NotNil)
        } else {
-               c.Assert(len(testPullLists), Equals, 1)
-               c.Assert(len(processedPullLists), Equals, 1)
-               c.Assert(testPullLists[testData.name], NotNil)
+               if testData.response_code == http.StatusOK {
+                       c.Assert(len(testPullLists), Equals, 1)
+                       c.Assert(len(processedPullLists), Equals, 1)
+                       c.Assert(testPullLists[testData.name], NotNil)
+               } else {
+                       c.Assert(len(testPullLists), Equals, 1)
+                       c.Assert(len(processedPullLists), Equals, 0)
+               }
        }
 
        if testData.read_error {
                c.Assert(readError, NotNil)
-       } else {
+       } else if testData.response_code == http.StatusOK {
                c.Assert(readError, IsNil)
                c.Assert(readContent, Equals, testData.read_content)
                if testData.put_error {