Merge branch '7015-update-user-guide'
[arvados.git] / services / keepproxy / keepproxy_test.go
index 1afe37343f1628fc688e156e1447da195383e42d..7643e4b0fa2225492caae1dd0aff3428505bd86d 100644 (file)
@@ -53,7 +53,7 @@ func closeListener() {
 
 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
        arvadostest.StartAPI()
-       arvadostest.StartKeep()
+       arvadostest.StartKeep(2, false)
 }
 
 func (s *ServerRequiredSuite) SetUpTest(c *C) {
@@ -61,7 +61,7 @@ func (s *ServerRequiredSuite) SetUpTest(c *C) {
 }
 
 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
-       arvadostest.StopKeep()
+       arvadostest.StopKeep(2)
        arvadostest.StopAPI()
 }
 
@@ -418,6 +418,7 @@ func (s *ServerRequiredSuite) TestGetIndex(c *C) {
        waitForListener()
        defer closeListener()
 
+       // Put "index-data" blocks
        data := []byte("index-data")
        hash := fmt.Sprintf("%x", md5.Sum(data))
 
@@ -432,39 +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++
-               }
-       }
-       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++
+       // 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 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, "\n")
 
        // GetIndex with invalid prefix
-       indexReader, err = kc.GetIndex("proxy", "xyz")
+       _, err = kc.GetIndex("proxy", "xyz")
        c.Assert((err != nil), Equals, true)
 }