Merge branch 'master' into 6260-test-datamanager
[arvados.git] / services / keepstore / pull_worker_test.go
index eb748501516a0095fe537fcd9a6584cf0326d2df..37d83b32802af1432bf7ed8f2af5826a3d757914 100644 (file)
@@ -9,6 +9,7 @@ import (
        "io"
        "net/http"
        "testing"
+       "time"
 )
 
 type PullWorkerTestSuite struct{}
@@ -22,15 +23,12 @@ func TestPullWorker(t *testing.T) {
 var _ = Suite(&PullWorkerTestSuite{})
 
 var testPullLists map[string]string
-var processedPullLists map[string]string
 var readContent string
 var readError error
 var putContent []byte
 var putError error
 var currentTestData PullWorkerTestData
 
-const READ_CONTENT = "Hi!"
-
 func (s *PullWorkerTestSuite) SetUpTest(c *C) {
        readContent = ""
        readError = nil
@@ -38,9 +36,9 @@ func (s *PullWorkerTestSuite) SetUpTest(c *C) {
        putError = nil
 
        // When a new pull request arrives, the old one will be overwritten.
-       // This behavior is verified using these maps in the "TestPullWorker_pull_list_with_two_items_latest_replacing_old"
+       // This behavior is verified using these two maps in the
+       // "TestPullWorker_pull_list_with_two_items_latest_replacing_old"
        testPullLists = make(map[string]string)
-       processedPullLists = make(map[string]string)
 }
 
 // Since keepstore does not come into picture in tests,
@@ -57,14 +55,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"
                        ]
@@ -73,10 +70,10 @@ var first_pull_list = []byte(`[
 
 var second_pull_list = []byte(`[
                {
-                       "locator":"locator3",
+                       "locator":"73feffa4b7f6bb68e44cf984c85f6e88+3",
                        "servers":[
                                "server_1",
-        "server_2"
+                               "server_2"
                        ]
                }
        ]`)
@@ -168,7 +165,7 @@ func (s *PullWorkerTestSuite) TestPullWorker_error_on_put_one_locator(c *C) {
                req:           RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
                response_code: http.StatusOK,
                response_body: "Received 1 pull requests\n",
-               read_content:  "unused",
+               read_content:  "hello hello",
                read_error:    false,
                put_error:     true,
        }
@@ -185,7 +182,7 @@ func (s *PullWorkerTestSuite) TestPullWorker_error_on_put_two_locators(c *C) {
                req:           RequestTester{"/pull", data_manager_token, "PUT", first_pull_list},
                response_code: http.StatusOK,
                response_body: "Received 2 pull requests\n",
-               read_content:  "unused",
+               read_content:  "hello again",
                read_error:    false,
                put_error:     true,
        }
@@ -211,7 +208,26 @@ func (s *PullWorkerTestSuite) TestPullWorker_pull_list_with_two_items_latest_rep
                req:           RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
                response_code: http.StatusOK,
                response_body: "Received 1 pull requests\n",
-               read_content:  "hola",
+               read_content:  "hola de nuevo",
+               read_error:    false,
+               put_error:     false,
+       }
+
+       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,
        }
@@ -220,30 +236,39 @@ func (s *PullWorkerTestSuite) TestPullWorker_pull_list_with_two_items_latest_rep
 }
 
 func performTest(testData PullWorkerTestData, c *C) {
+       KeepVM = MakeTestVolumeManager(2)
+       defer KeepVM.Close()
+
        RunTestPullWorker(c)
+       defer pullq.Close()
 
        currentTestData = testData
        testPullLists[testData.name] = testData.response_body
 
-       // Override GetContent to mock keepclient Get functionality
-       GetContent = func(signedLocator string, keepClient keepclient.KeepClient) (
-               reader io.ReadCloser, contentLength int64, url string, err error) {
+       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.response_body
                if testData.read_error {
                        err = errors.New("Error getting data")
                        readError = err
                        return nil, 0, "", err
                } else {
-                       readContent = READ_CONTENT
-                       cb := &ClosingBuffer{bytes.NewBufferString(readContent)}
+                       readContent = testData.read_content
+                       cb := &ClosingBuffer{bytes.NewBufferString(testData.read_content)}
                        var rc io.ReadCloser
                        rc = cb
-                       return rc, int64(len(READ_CONTENT)), "", nil
+                       return rc, int64(len(testData.read_content)), "", nil
                }
        }
 
        // 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,13 +280,17 @@ 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(getStatusItem("PullQueue", "InProgress"), Equals, float64(0))
+       c.Assert(getStatusItem("PullQueue", "Queued"), Equals, float64(0))
 
-       expectWorkerChannelEmpty(c, pullq.NextItem)
+       response := IssueRequest(&testData.req)
+       c.Assert(response.Code, Equals, testData.response_code)
+       c.Assert(response.Body.String(), Equals, testData.response_body)
 
-       pullq.Close()
+       expectEqualWithin(c, time.Second, 0, func() interface{} {
+               st := pullq.Status()
+               return st.InProgress + st.Queued
+       })
 
        if testData.name == "TestPullWorker_pull_list_with_two_items_latest_replacing_old" {
                c.Assert(len(testPullLists), Equals, 2)
@@ -270,24 +299,30 @@ 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, READ_CONTENT)
+               c.Assert(readContent, Equals, testData.read_content)
                if testData.put_error {
                        c.Assert(putError, NotNil)
                } else {
                        c.Assert(putError, IsNil)
-                       c.Assert(string(putContent), Equals, READ_CONTENT)
+                       c.Assert(string(putContent), Equals, testData.read_content)
                }
        }
 
+       expectChannelEmpty(c, pullq.NextItem)
 }
 
 type ClosingBuffer struct {
@@ -297,19 +332,3 @@ type ClosingBuffer struct {
 func (cb *ClosingBuffer) Close() (err error) {
        return
 }
-
-func expectWorkerChannelEmpty(c *C, workerChannel <-chan interface{}) {
-       select {
-       case item := <-workerChannel:
-               c.Fatalf("Received value (%v) from channel that was expected to be empty", item)
-       default:
-       }
-}
-
-func expectWorkerChannelNotEmpty(c *C, workerChannel <-chan interface{}) {
-       select {
-       case item := <-workerChannel:
-               c.Fatalf("Received value (%v) from channel that was expected to be empty", item)
-       default:
-       }
-}