7121: Replace Get(loc,true) with CompareAndTouch(). Add Compare method to Volume...
[arvados.git] / services / keepstore / volume_test.go
index c5a7491b3d542951983ad67e7eec29baa972f966..cbf6fb881679f92a4e25dd9cfa08bd26c1ae3df2 100644 (file)
@@ -1,6 +1,8 @@
 package main
 
 import (
+       "bytes"
+       "crypto/md5"
        "errors"
        "fmt"
        "io"
@@ -71,6 +73,24 @@ func (v *MockVolume) gotCall(method string) {
        }
 }
 
+func (v *MockVolume) Compare(loc string, buf []byte) error {
+       v.gotCall("Compare")
+       <-v.Gate
+       if v.Bad {
+               return errors.New("Bad volume")
+       } else if block, ok := v.Store[loc]; ok {
+               if fmt.Sprintf("%x", md5.Sum(block)) != loc {
+                       return DiskHashError
+               }
+               if bytes.Compare(buf, block) != 0 {
+                       return CollisionError
+               }
+               return nil
+       } else {
+               return NotFoundError
+       }
+}
+
 func (v *MockVolume) Get(loc string) ([]byte, error) {
        v.gotCall("Get")
        <-v.Gate