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 const READ_CONTENT = "Hi!"
34 func (s *PullWorkerTestSuite) SetUpTest(c *C) {
37 putContent = []byte("")
40 // When a new pull request arrives, the old one will be overwritten.
41 // This behavior is verified using these maps in the "TestPullWorker_pull_list_with_two_items_latest_replacing_old"
42 testPullLists = make(map[string]string)
43 processedPullLists = make(map[string]string)
46 func (s *PullWorkerTestSuite) TearDownTest(c *C) {
47 expectWorkerChannelEmpty(c, pullq.NextItem)
49 if currentTestData.name == "TestPullWorker_pull_list_with_two_items_latest_replacing_old" {
50 c.Assert(len(testPullLists), Equals, 2)
51 c.Assert(len(processedPullLists), Equals, 1)
52 c.Assert(testPullLists["Added_before_actual_test_item"], NotNil)
53 c.Assert(testPullLists["TestPullWorker_pull_list_with_two_items_latest_replacing_old"], NotNil)
54 c.Assert(processedPullLists["TestPullWorker_pull_list_with_two_items_latest_replacing_old"], NotNil)
57 if currentTestData.read_error {
58 c.Assert(readError, NotNil)
60 c.Assert(readError, IsNil)
61 c.Assert(readContent, Equals, READ_CONTENT)
62 if currentTestData.put_error {
63 c.Assert(putError, NotNil)
65 c.Assert(putError, IsNil)
66 c.Assert(string(putContent), Equals, READ_CONTENT)
71 // Since keepstore does not come into picture in tests,
72 // we need to explicitly start the goroutine in tests.
73 func RunTestPullWorker(c *C) {
74 arv, err := arvadosclient.MakeArvadosClient()
75 c.Assert(err, Equals, nil)
76 keepClient, err := keepclient.MakeKeepClient(&arv)
77 c.Assert(err, Equals, nil)
79 pullq = NewWorkQueue()
80 go RunPullWorker(pullq, keepClient)
83 var first_pull_list = []byte(`[
99 var second_pull_list = []byte(`[
101 "locator":"locator3",
109 type PullWorkerTestData struct {
119 func (s *PullWorkerTestSuite) TestPullWorker_pull_list_with_two_locators(c *C) {
122 data_manager_token = "DATA MANAGER TOKEN"
123 testData := PullWorkerTestData{
124 name: "TestPullWorker_pull_list_with_two_locators",
125 req: RequestTester{"/pull", data_manager_token, "PUT", first_pull_list},
126 response_code: http.StatusOK,
127 response_body: "Received 2 pull requests\n",
128 read_content: "hello",
133 performTest(testData, c)
136 func (s *PullWorkerTestSuite) TestPullWorker_pull_list_with_one_locator(c *C) {
139 data_manager_token = "DATA MANAGER TOKEN"
140 testData := PullWorkerTestData{
141 name: "TestPullWorker_pull_list_with_one_locator",
142 req: RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
143 response_code: http.StatusOK,
144 response_body: "Received 1 pull requests\n",
145 read_content: "hola",
150 performTest(testData, c)
153 func (s *PullWorkerTestSuite) TestPullWorker_error_on_get_one_locator(c *C) {
156 data_manager_token = "DATA MANAGER TOKEN"
157 testData := PullWorkerTestData{
158 name: "TestPullWorker_error_on_get_one_locator",
159 req: RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
160 response_code: http.StatusOK,
161 response_body: "Received 1 pull requests\n",
162 read_content: "unused",
167 performTest(testData, c)
170 func (s *PullWorkerTestSuite) TestPullWorker_error_on_get_two_locators(c *C) {
173 data_manager_token = "DATA MANAGER TOKEN"
174 testData := PullWorkerTestData{
175 name: "TestPullWorker_error_on_get_two_locators",
176 req: RequestTester{"/pull", data_manager_token, "PUT", first_pull_list},
177 response_code: http.StatusOK,
178 response_body: "Received 2 pull requests\n",
179 read_content: "unused",
184 performTest(testData, c)
187 func (s *PullWorkerTestSuite) TestPullWorker_error_on_put_one_locator(c *C) {
190 data_manager_token = "DATA MANAGER TOKEN"
191 testData := PullWorkerTestData{
192 name: "TestPullWorker_error_on_put_one_locator",
193 req: RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
194 response_code: http.StatusOK,
195 response_body: "Received 1 pull requests\n",
196 read_content: "unused",
201 performTest(testData, c)
204 func (s *PullWorkerTestSuite) TestPullWorker_error_on_put_two_locators(c *C) {
207 data_manager_token = "DATA MANAGER TOKEN"
208 testData := PullWorkerTestData{
209 name: "TestPullWorker_error_on_put_two_locators",
210 req: RequestTester{"/pull", data_manager_token, "PUT", first_pull_list},
211 response_code: http.StatusOK,
212 response_body: "Received 2 pull requests\n",
213 read_content: "unused",
218 performTest(testData, c)
221 // When a new pull request arrives, the old one is replaced. This test
222 // is used to check that behavior by first putting an item on the queue,
223 // and then performing the test. Thus the "testPullLists" has two entries;
224 // however, processedPullLists will see only the newest item in the list.
225 func (s *PullWorkerTestSuite) TestPullWorker_pull_list_with_two_items_latest_replacing_old(c *C) {
228 var firstInput = []int{1}
229 pullq = NewWorkQueue()
230 pullq.ReplaceQueue(makeTestWorkList(firstInput))
231 testPullLists["Added_before_actual_test_item"] = string(1)
233 data_manager_token = "DATA MANAGER TOKEN"
234 testData := PullWorkerTestData{
235 name: "TestPullWorker_pull_list_with_two_items_latest_replacing_old",
236 req: RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
237 response_code: http.StatusOK,
238 response_body: "Received 1 pull requests\n",
239 read_content: "hola",
244 performTest(testData, c)
247 func performTest(testData PullWorkerTestData, c *C) {
250 currentTestData = testData
251 testPullLists[testData.name] = testData.response_body
253 // Override GetContent to mock keepclient Get functionality
254 GetContent = func(signedLocator string, keepClient keepclient.KeepClient) (
255 reader io.ReadCloser, contentLength int64, url string, err error) {
257 processedPullLists[testData.name] = testData.response_body
258 if testData.read_error {
259 err = errors.New("Error getting data")
261 return nil, 0, "", err
263 readContent = READ_CONTENT
264 cb := &ClosingBuffer{bytes.NewBufferString(readContent)}
267 return rc, int64(len(READ_CONTENT)), "", nil
271 // Override PutContent to mock PutBlock functionality
272 PutContent = func(content []byte, locator string) (err error) {
273 if testData.put_error {
274 err = errors.New("Error putting data")
283 response := IssueRequest(&testData.req)
284 c.Assert(testData.response_code, Equals, response.Code)
285 c.Assert(testData.response_body, Equals, response.Body.String())
288 type ClosingBuffer struct {
292 func (cb *ClosingBuffer) Close() (err error) {
296 func expectWorkerChannelEmpty(c *C, workerChannel <-chan interface{}) {
298 case item := <-workerChannel:
299 c.Fatalf("Received value (%v) from channel that was expected to be empty", item)
304 func expectWorkerChannelNotEmpty(c *C, workerChannel <-chan interface{}) {
306 case item := <-workerChannel:
307 c.Fatalf("Received value (%v) from channel that was expected to be empty", item)