X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/d1327f9456b561c059c18c76f63391728ce1780d..a72b3d08c70f615c5a67ffd4cf816e5502edd629:/services/keepproxy/keepproxy_test.go diff --git a/services/keepproxy/keepproxy_test.go b/services/keepproxy/keepproxy_test.go index 22cc72ea15..c6234544cd 100644 --- a/services/keepproxy/keepproxy_test.go +++ b/services/keepproxy/keepproxy_test.go @@ -406,3 +406,81 @@ func (s *ServerRequiredSuite) TestStripHint(c *C) { "http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+K@zzzzz-zzzzz-zzzzzzzzzzzzzzz+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73") } + +// Test GetIndex +// Put one block, with 2 replicas +// With no prefix (expect the block locator, twice) +// With an existing prefix (expect the block locator, twice) +// With a valid but non-existing prefix (expect "\n") +// With an invalid prefix (expect error) +func (s *ServerRequiredSuite) TestGetIndex(c *C) { + kc := runProxy(c, []string{"keepproxy"}, 28852, false) + waitForListener() + defer closeListener() + + // Write "index-data" blocks + data := []byte("index-data") + hash := fmt.Sprintf("%x", md5.Sum(data)) + + hash2, rep, err := kc.PutB(data) + c.Check(hash2, Matches, fmt.Sprintf(`^%s\+10(\+.+)?$`, hash)) + c.Check(rep, Equals, 2) + c.Check(err, Equals, nil) + + reader, blocklen, _, err := kc.Get(hash) + c.Assert(err, Equals, nil) + c.Check(blocklen, Equals, int64(10)) + all, err := ioutil.ReadAll(reader) + c.Check(all, DeepEquals, data) + + // Write some more blocks + otherData := []byte("some-more-index-data") + otherHash := fmt.Sprintf("%x", md5.Sum(otherData)) + _, rep, err = kc.PutB(otherData) + c.Check(err, Equals, nil) + + // GetIndex with no prefix; expect both data and otherData blocks + indexReader, err := kc.GetIndex("proxy", "") + c.Assert(err, Equals, nil) + indexResp, err := ioutil.ReadAll(indexReader) + locators := strings.Split(string(indexResp), "\n") + matchingLocators := 0 + otherLocators := 0 + for _, locator := range locators { + if strings.HasPrefix(locator, hash) { + matchingLocators++ + } else if strings.HasPrefix(locator, otherHash) { + otherLocators++ + } + } + c.Assert(2, Equals, matchingLocators) + c.Assert(2, Equals, otherLocators) + + // GetIndex with prefix + indexReader, err = kc.GetIndex("proxy", hash[0:3]) + c.Assert(err, Equals, nil) + indexResp, err = ioutil.ReadAll(indexReader) + locators = strings.Split(string(indexResp), "\n") + totalLocators := 0 + matchingLocators = 0 + for _, locator := range locators { + if locator != "" { + totalLocators++ + } + if strings.HasPrefix(locator, hash[0:3]) { + matchingLocators++ + } + } + c.Assert(2, Equals, matchingLocators) + c.Assert(totalLocators, Equals, matchingLocators) + + // GetIndex with valid but no such prefix + indexReader, err = kc.GetIndex("proxy", "abcd") + c.Assert(err, Equals, nil) + indexResp, err = ioutil.ReadAll(indexReader) + c.Assert(string(indexResp), Equals, "") + + // GetIndex with invalid prefix + indexReader, err = kc.GetIndex("proxy", "xyz") + c.Assert((err != nil), Equals, true) +}