X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/560ceadf0e0e5f513de82de282f3b09bf211477d..6a983720b3d1ee8e613bf3902e69fd14f505b995:/services/keepstore/pull_worker_integration_test.go?ds=inline diff --git a/services/keepstore/pull_worker_integration_test.go b/services/keepstore/pull_worker_integration_test.go index 27c8fcdc3a..2c307e4df3 100644 --- a/services/keepstore/pull_worker_integration_test.go +++ b/services/keepstore/pull_worker_integration_test.go @@ -2,78 +2,192 @@ package main import ( "crypto/tls" + "encoding/json" + "fmt" "git.curoverse.com/arvados.git/sdk/go/arvadosclient" "git.curoverse.com/arvados.git/sdk/go/arvadostest" "git.curoverse.com/arvados.git/sdk/go/keepclient" "net/http" "os" + "strings" "testing" ) +var keepClient keepclient.KeepClient + type PullWorkIntegrationTestData struct { - Name string - Locator string - Content string + Name string + Locator string + Content string + GetError string } -func TestPullWorkerIntegration_GetLocator(t *testing.T) { +func SetupPullWorkerIntegrationTest(t *testing.T, testData PullWorkIntegrationTestData, wantData bool) PullRequest { + os.Setenv("ARVADOS_API_HOST_INSECURE", "true") + arvadostest.StartAPI() arvadostest.StartKeep() - testData := PullWorkIntegrationTestData{ - Name: "TestPullWorkerIntegration_GetLocator", - Locator: "5d41402abc4b2a76b9719d911017c592", - Content: "hello", + arv, err := arvadosclient.MakeArvadosClient() + if err != nil { + t.Error("Error creating arv") } - performPullWorkerIntegrationTest(testData, t) -} + servers := GetKeepServices(t) -func performPullWorkerIntegrationTest(testData PullWorkIntegrationTestData, t *testing.T) { - os.Setenv("ARVADOS_API_HOST_INSECURE", "true") + random_token := GenerateRandomApiToken() - PermissionSecret = []byte("abc123") + // Put content if the test needs it + if wantData { + keepClient = keepclient.KeepClient{ + Arvados: &arv, + Want_replicas: 1, + Using_proxy: true, + Client: &http.Client{}, + } + keepClient.Arvados.ApiToken = random_token + + service_roots := make(map[string]string) + for _, addr := range servers { + service_roots[addr] = addr + } + keepClient.SetServiceRoots(service_roots) + + locator, _, err := keepClient.PutB([]byte(testData.Content)) + if err != nil { + t.Errorf("Error putting test data in setup for %s %s %v", testData.Content, locator, err) + } + if locator == "" { + t.Errorf("No locator found after putting test data") + } + } - arv, err := arvadosclient.MakeArvadosClient() - if err != nil { - t.Error("Error creating arv") + // Create pullRequest for the test + keepClient = keepclient.KeepClient{ + Arvados: &arv, + Want_replicas: 1, + Using_proxy: true, + Client: &http.Client{}, } + keepClient.Arvados.ApiToken = random_token + pullRequest := PullRequest{ + Locator: testData.Locator, + Servers: servers, + } + return pullRequest +} + +func GetKeepServices(t *testing.T) []string { client := &http.Client{Transport: &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}} - keepClient := keepclient.KeepClient{ - Arvados: &arv, - Want_replicas: 2, - Using_proxy: true, - Client: client, + req, err := http.NewRequest("GET", fmt.Sprintf("https://%s/arvados/v1/keep_services", os.Getenv("ARVADOS_API_HOST")), nil) + if err != nil { + t.Errorf("Error getting keep services: ", err) } + req.Header.Set("Authorization", fmt.Sprintf("OAuth2 %s", os.Getenv("ARVADOS_API_TOKEN"))) - random_token := GenerateRandomApiToken() - keepClient.Arvados.ApiToken = random_token + resp, err := client.Do(req) + if err != nil { + t.Errorf("Error getting keep services: ", err) + } + if resp.StatusCode != 200 { + t.Errorf("Error status code getting keep services", resp.StatusCode) + } + defer resp.Body.Close() + var servers []string + + decoder := json.NewDecoder(resp.Body) + + var respJSON map[string]interface{} + err = decoder.Decode(&respJSON) if err != nil { - t.Error("Error creating keepclient") + t.Errorf("Error decoding response for keep services: ", err) } - pullq = NewWorkQueue() - go RunPullWorker(pullq, keepClient) + var service_names []string + var service_ports []string + for _, v1 := range respJSON { + switch v1_type := v1.(type) { + case []interface{}: + for _, v2 := range v1_type { + switch v2_type := v2.(type) { + case map[string]interface{}: + for name, value := range v2_type { + if name == "service_host" { + service_names = append(service_names, fmt.Sprintf("%s", value)) + } else if name == "service_port" { + service_ports = append(service_ports, strings.Split(fmt.Sprintf("%f", value), ".")[0]) + } + } + default: + } + } + default: + } + } - servers := make([]string, 1) - servers[0] = "https://" + os.Getenv("ARVADOS_API_HOST") + "/arvados/v1/keep_services" - pullRequest := PullRequest{ - Locator: testData.Locator, - Servers: servers, + for i, port := range service_ports { + servers = append(servers, "http://"+service_names[i]+":"+port) + } + + return servers +} + +// Do a get on a block that is not existing in any of the keep servers. +// Expect "block not found" error. +func TestPullWorkerIntegration_GetNonExistingLocator(t *testing.T) { + testData := PullWorkIntegrationTestData{ + Name: "TestPullWorkerIntegration_GetLocator", + Locator: "5d41402abc4b2a76b9719d911017c592", + Content: "hello", + GetError: "Block not found", } - PullItemAndProcess(pullRequest, random_token, keepClient) + pullRequest := SetupPullWorkerIntegrationTest(t, testData, false) + + performPullWorkerIntegrationTest(testData, pullRequest, t) +} + +// Do a get on a block that exists on one of the keep servers. +// The setup method will create this block before doing the get. +func TestPullWorkerIntegration_GetExistingLocator(t *testing.T) { + testData := PullWorkIntegrationTestData{ + Name: "TestPullWorkerIntegration_GetLocator", + Locator: "5d41402abc4b2a76b9719d911017c592", + Content: "hello", + GetError: "", + } + + pullRequest := SetupPullWorkerIntegrationTest(t, testData, true) + + performPullWorkerIntegrationTest(testData, pullRequest, t) +} + +// Perform the test. +// The test directly invokes the "PullItemAndProcess" rather than +// putting an item on the pullq so that the errors can be verified. +func performPullWorkerIntegrationTest(testData PullWorkIntegrationTestData, pullRequest PullRequest, t *testing.T) { // Override PutContent to mock PutBlock functionality PutContent = func(content []byte, locator string) (err error) { - // do nothing + if string(content) != testData.Content { + t.Errorf("PutContent invoked with unexpected data. Expected: %s; Found: %s", testData.Content, content) + } return } - pullq.Close() - pullq = nil + err := PullItemAndProcess(pullRequest, keepClient.Arvados.ApiToken, keepClient) + + if len(testData.GetError) > 0 { + if (err == nil) || (!strings.Contains(err.Error(), testData.GetError)) { + t.Errorf("Got error %v", err) + } + } else { + if err != nil { + t.Errorf("Got error %v", err) + } + } }