12 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
13 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
14 "git.curoverse.com/arvados.git/sdk/go/keepclient"
17 var keepClient *keepclient.KeepClient
19 type PullWorkIntegrationTestData struct {
26 func SetupPullWorkerIntegrationTest(t *testing.T, testData PullWorkIntegrationTestData, wantData bool) PullRequest {
27 os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
29 // start api and keep servers
30 arvadostest.StartAPI()
31 arvadostest.StartKeep(2, false)
34 arv, err := arvadosclient.MakeArvadosClient()
36 t.Error("Error creating arv")
40 keepClient = &keepclient.KeepClient{
45 // discover keep services
47 if err := keepClient.DiscoverKeepServers(); err != nil {
48 t.Error("Error discovering keep services")
50 for _, host := range keepClient.LocalRoots() {
51 servers = append(servers, host)
54 // Put content if the test needs it
56 locator, _, err := keepClient.PutB([]byte(testData.Content))
58 t.Errorf("Error putting test data in setup for %s %s %v", testData.Content, locator, err)
61 t.Errorf("No locator found after putting test data")
65 // Create pullRequest for the test
66 pullRequest := PullRequest{
67 Locator: testData.Locator,
73 // Do a get on a block that is not existing in any of the keep servers.
74 // Expect "block not found" error.
75 func TestPullWorkerIntegration_GetNonExistingLocator(t *testing.T) {
76 testData := PullWorkIntegrationTestData{
77 Name: "TestPullWorkerIntegration_GetLocator",
78 Locator: "5d41402abc4b2a76b9719d911017c592",
80 GetError: "Block not found",
83 pullRequest := SetupPullWorkerIntegrationTest(t, testData, false)
84 defer arvadostest.StopAPI()
85 defer arvadostest.StopKeep(2)
87 performPullWorkerIntegrationTest(testData, pullRequest, t)
90 // Do a get on a block that exists on one of the keep servers.
91 // The setup method will create this block before doing the get.
92 func TestPullWorkerIntegration_GetExistingLocator(t *testing.T) {
93 testData := PullWorkIntegrationTestData{
94 Name: "TestPullWorkerIntegration_GetLocator",
95 Locator: "5d41402abc4b2a76b9719d911017c592",
100 pullRequest := SetupPullWorkerIntegrationTest(t, testData, true)
101 defer arvadostest.StopAPI()
102 defer arvadostest.StopKeep(2)
104 performPullWorkerIntegrationTest(testData, pullRequest, t)
108 // The test directly invokes the "PullItemAndProcess" rather than
109 // putting an item on the pullq so that the errors can be verified.
110 func performPullWorkerIntegrationTest(testData PullWorkIntegrationTestData, pullRequest PullRequest, t *testing.T) {
112 // Override writePulledBlock to mock PutBlock functionality
113 defer func(orig func(Volume, []byte, string)) { writePulledBlock = orig }(writePulledBlock)
114 writePulledBlock = func(v Volume, content []byte, locator string) {
115 if string(content) != testData.Content {
116 t.Errorf("writePulledBlock invoked with unexpected data. Expected: %s; Found: %s", testData.Content, content)
120 // Override GetContent to mock keepclient Get functionality
121 defer func(orig func(string, *keepclient.KeepClient) (io.ReadCloser, int64, string, error)) {
124 GetContent = func(signedLocator string, keepClient *keepclient.KeepClient) (reader io.ReadCloser, contentLength int64, url string, err error) {
125 if testData.GetError != "" {
126 return nil, 0, "", errors.New(testData.GetError)
128 rdr := ioutil.NopCloser(bytes.NewBufferString(testData.Content))
129 return rdr, int64(len(testData.Content)), "", nil
132 err := PullItemAndProcess(pullRequest, keepClient)
134 if len(testData.GetError) > 0 {
135 if (err == nil) || (!strings.Contains(err.Error(), testData.GetError)) {
136 t.Errorf("Got error %v, expected %v", err, testData.GetError)
140 t.Errorf("Got error %v, expected nil", err)