}
+// 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))
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)
}