X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/ffa7f6dbbf52ab063610796378e90a054f900581..23ac9d35b87e4930fc01318d44134693f62c1264:/services/keepproxy/keepproxy_test.go diff --git a/services/keepproxy/keepproxy_test.go b/services/keepproxy/keepproxy_test.go index a08d1b6039..6fe8fe7ac3 100644 --- a/services/keepproxy/keepproxy_test.go +++ b/services/keepproxy/keepproxy_test.go @@ -407,11 +407,18 @@ func (s *ServerRequiredSuite) TestStripHint(c *C) { } +// 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() + // Put "index-data" blocks data := []byte("index-data") hash := fmt.Sprintf("%x", md5.Sum(data)) @@ -426,33 +433,43 @@ func (s *ServerRequiredSuite) TestGetIndex(c *C) { all, err := ioutil.ReadAll(reader) c.Check(all, DeepEquals, data) - // GetIndex with no prefix - indexReader, err := kc.GetIndex("proxy", "") - c.Assert(err, Equals, nil) - indexResp, err := ioutil.ReadAll(indexReader) - locators := strings.Split(string(indexResp), "\n") - count := 0 - for _, locator := range locators { - if strings.HasPrefix(locator, hash) { - count += 1 - } - } - c.Assert(2, Equals, count) + // Put some more blocks + _, rep, err = kc.PutB([]byte("some-more-index-data")) + c.Check(err, Equals, nil) - // 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") - count = 0 - for _, locator := range locators { - if strings.HasPrefix(locator, hash) { - count += 1 + // Invoke GetIndex + for _, spec := range []struct { + prefix string + expectTestHash bool + expectOther bool + }{ + {"", true, true}, // with no prefix + {hash[:3], true, false}, // with matching prefix + {"abcdef", false, false}, // with no such prefix + } { + indexReader, err := kc.GetIndex("proxy", spec.prefix) + c.Assert(err, Equals, nil) + indexResp, err := ioutil.ReadAll(indexReader) + c.Assert(err, Equals, nil) + locators := strings.Split(string(indexResp), "\n") + gotTestHash := 0 + gotOther := 0 + for _, locator := range locators { + if locator == "" { + continue + } + c.Check(locator[:len(spec.prefix)], Equals, spec.prefix) + if locator[:32] == hash { + gotTestHash++ + } else { + gotOther++ + } } + c.Check(gotTestHash == 2, Equals, spec.expectTestHash) + c.Check(gotOther > 0, Equals, spec.expectOther) } - c.Assert(2, Equals, count) - // GetIndex with no such prefix - indexReader, err = kc.GetIndex("proxy", "xyz") + // GetIndex with invalid prefix + _, err = kc.GetIndex("proxy", "xyz") c.Assert((err != nil), Equals, true) }