Merge branch 'master' into 8654-arv-jobs-cwl-runner
[arvados.git] / services / keepstore / volume_generic_test.go
index 7e624298d381121239e8a2f9589eab05bc7a74cc..95166c252f004bef5a1ba1f583f4e13f54117f47 100644 (file)
@@ -79,7 +79,6 @@ func DoGenericVolumeTests(t TB, factory TestableVolumeFactory) {
 
        testTrashUntrash(t, factory)
        testTrashEmptyTrashUntrash(t, factory)
-       testTrashUntrashWithEmptyTrashGoroutine(t, factory)
 }
 
 // Put a test block, get it and verify content
@@ -764,238 +763,177 @@ func testTrashUntrash(t TB, factory TestableVolumeFactory) {
 func testTrashEmptyTrashUntrash(t TB, factory TestableVolumeFactory) {
        v := factory(t)
        defer v.Teardown()
-       defer func() {
-               trashLifetime = 0 * time.Second
-       }()
+       defer func(orig time.Duration) {
+               trashLifetime = orig
+       }(trashLifetime)
+
+       checkGet := func() error {
+               buf, err := v.Get(TestHash)
+               if err != nil {
+                       return err
+               }
+               if bytes.Compare(buf, TestBlock) != 0 {
+                       t.Fatalf("Got data %+q, expected %+q", buf, TestBlock)
+               }
+               bufs.Put(buf)
+               return nil
+       }
+
+       // First set: EmptyTrash before reaching the trash deadline.
 
-       // With trashLifetime = 1h, test trash/untrash cycle.
        trashLifetime = 1 * time.Hour
 
-       // put block and backdate it
        v.PutRaw(TestHash, TestBlock)
        v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
 
-       buf, err := v.Get(TestHash)
+       err := checkGet()
        if err != nil {
                t.Fatal(err)
        }
-       if bytes.Compare(buf, TestBlock) != 0 {
-               t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
-       }
-       bufs.Put(buf)
 
-       // Trash it
        err = v.Trash(TestHash)
        if err == MethodDisabledError || err == ErrNotImplemented {
+               // Skip the trash tests for read-only volumes, and
+               // volume types that don't support trashLifetime>0.
                return
        }
-       buf, err = v.Get(TestHash)
+
+       err = checkGet()
        if err == nil || !os.IsNotExist(err) {
-               t.Errorf("os.IsNotExist(%v) should have been true", err)
+               t.Fatalf("os.IsNotExist(%v) should have been true", err)
        }
 
-       // Empty trash; the block is still within trashLifetime and hence is not emptied
        v.EmptyTrash()
 
-       // Untrash will hence rescue it
+       // Even after emptying the trash, we can untrash our block
+       // because the deadline hasn't been reached.
        err = v.Untrash(TestHash)
        if err != nil {
                t.Fatal(err)
        }
-
-       // Get block will find it
-       buf, err = v.Get(TestHash)
+       err = checkGet()
        if err != nil {
                t.Fatal(err)
        }
-       if bytes.Compare(buf, TestBlock) != 0 {
-               t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
+
+       // Untrash should fail if the only block in the trash has
+       // already been untrashed.
+       err = v.Untrash(TestHash)
+       if err == nil || !os.IsNotExist(err) {
+               t.Fatalf("os.IsNotExist(%v) should have been true", err)
        }
-       bufs.Put(buf)
 
-       // With trashLifetime = 1ns, test trash/untrash cycle.
+       // The failed Untrash should not interfere with our
+       // already-untrashed copy.
+       err = checkGet()
+       if err != nil {
+               t.Fatal(err)
+       }
+
+       // Second set: EmptyTrash after the trash deadline has passed.
+
        trashLifetime = 1 * time.Nanosecond
 
-       // Trash it
        err = v.Trash(TestHash)
        if err != nil {
                t.Fatal(err)
        }
-       buf, err = v.Get(TestHash)
+       err = checkGet()
        if err == nil || !os.IsNotExist(err) {
-               t.Errorf("os.IsNotExist(%v) should have been true", err)
+               t.Fatalf("os.IsNotExist(%v) should have been true", err)
        }
 
-       // Untrash
+       // Even though 1ns has passed, we can untrash because we
+       // haven't called EmptyTrash yet.
        err = v.Untrash(TestHash)
        if err != nil {
                t.Fatal(err)
        }
-
-       // Get block will find it
-       buf, err = v.Get(TestHash)
+       err = checkGet()
        if err != nil {
                t.Fatal(err)
        }
-       if bytes.Compare(buf, TestBlock) != 0 {
-               t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
-       }
-       bufs.Put(buf)
 
-       // Trash it again
+       // Trash it again, and this time call EmptyTrash so it really
+       // goes away.
        err = v.Trash(TestHash)
-       if err == MethodDisabledError || err == ErrNotImplemented {
-               return
-       }
-       buf, err = v.Get(TestHash)
+       err = checkGet()
        if err == nil || !os.IsNotExist(err) {
                t.Errorf("os.IsNotExist(%v) should have been true", err)
        }
-
-       // Empty trash will empty it
        v.EmptyTrash()
 
        // Untrash won't find it
        err = v.Untrash(TestHash)
        if err == nil || !os.IsNotExist(err) {
-               t.Errorf("os.IsNotExist(%v) should have been true", err)
+               t.Fatalf("os.IsNotExist(%v) should have been true", err)
        }
 
        // Get block won't find it
-       buf, err = v.Get(TestHash)
+       err = checkGet()
        if err == nil || !os.IsNotExist(err) {
-               t.Errorf("os.IsNotExist(%v) should have been true", err)
+               t.Fatalf("os.IsNotExist(%v) should have been true", err)
        }
 
-       // Still with trashLifetime = 1ns: put, trash, put one more, trash etc
-       // put block and backdate it
+       // Third set: If the same data block gets written again after
+       // being trashed, and then the trash gets emptied, the newer
+       // un-trashed copy doesn't get deleted along with it.
+
        v.PutRaw(TestHash, TestBlock)
        v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
 
-       // Trash
+       trashLifetime = time.Nanosecond
        err = v.Trash(TestHash)
-       if err == MethodDisabledError || err == ErrNotImplemented {
-               return
+       if err != nil {
+               t.Fatal(err)
        }
-       buf, err = v.Get(TestHash)
+       err = checkGet()
        if err == nil || !os.IsNotExist(err) {
-               t.Errorf("os.IsNotExist(%v) should have been true", err)
+               t.Fatalf("os.IsNotExist(%v) should have been true", err)
        }
 
-       // put again
-       err = v.Put(TestHash, TestBlock)
-       if err != nil {
-               t.Fatal(err)
-       }
+       v.PutRaw(TestHash, TestBlock)
        v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
 
-       // Empty trash will empty the trashed block but the second one is untouched
+       // EmptyTrash should not delete the untrashed copy.
        v.EmptyTrash()
-
-       // Get block should work because of the second block
-       buf, err = v.Get(TestHash)
+       err = checkGet()
        if err != nil {
                t.Fatal(err)
        }
-       if bytes.Compare(buf, TestBlock) != 0 {
-               t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
-       }
-       bufs.Put(buf)
 
-       // set trashLifetime to one hour
-       trashLifetime = 1 * time.Hour
+       // Fourth set: If the same data block gets trashed twice with
+       // different deadlines A and C, and then the trash is emptied
+       // at intermediate time B (A < B < C), it is still possible to
+       // untrash the block whose deadline is "C".
 
-       // trash block
-       err = v.Trash(TestHash)
-       if err != nil {
-               t.Fatal(err)
-       }
-
-       // Empty trash won't empty this second block which is still within trashLifetime
-       v.EmptyTrash()
-
-       // Untrash; the second block which is still within trashLifetime will be rescued
-       err = v.Untrash(TestHash)
-       if err != nil {
-               t.Fatal(err)
-       }
+       v.PutRaw(TestHash, TestBlock)
+       v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
 
-       // Get block should work because of the second block
-       buf, err = v.Get(TestHash)
+       trashLifetime = time.Nanosecond
+       err = v.Trash(TestHash)
        if err != nil {
                t.Fatal(err)
        }
-       if bytes.Compare(buf, TestBlock) != 0 {
-               t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
-       }
-       bufs.Put(buf)
-}
-
-// With trashLifetime = 1ns, perform:
-// Run emptyTrash goroutine
-// Trash an old block - which either raises ErrNotImplemented or succeeds
-// Untrash - after emptyTrash goroutine ticks, and hence does not actually untrash
-// Get - which must fail to find the block
-func testTrashUntrashWithEmptyTrashGoroutine(t TB, factory TestableVolumeFactory) {
-       v := factory(t)
-       defer v.Teardown()
 
-       doneEmptyingTrash := make(chan bool)
-       defer func() {
-               trashLifetime = 0 * time.Second
-               doneEmptyingTrash <- true
-       }()
-
-       volumes = append(volumes, v)
-
-       trashLifetime = 1 * time.Nanosecond
-       trashCheckInterval = 1 * time.Nanosecond
-
-       go emptyTrash(doneEmptyingTrash, trashCheckInterval)
-
-       // Trash old block and untrash a little after first trashCheckInterval
        v.PutRaw(TestHash, TestBlock)
        v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
 
-       buf, err := v.Get(TestHash)
+       trashLifetime = time.Hour
+       err = v.Trash(TestHash)
        if err != nil {
                t.Fatal(err)
        }
-       if bytes.Compare(buf, TestBlock) != 0 {
-               t.Fatalf("Got data %+q, expected %+q", buf, TestBlock)
-       }
-       bufs.Put(buf)
 
-       // Trash
-       err = v.Trash(TestHash)
-       if err == MethodDisabledError || err == ErrNotImplemented {
-               return
-       }
-
-       _, err = v.Get(TestHash)
-       if err == nil || !os.IsNotExist(err) {
-               t.Fatalf("os.IsNotExist(%v) should have been true", err)
-       }
-
-       time.Sleep(2 * time.Nanosecond)
-
-       // Untrash
+       // EmptyTrash should not prevent us from recovering the
+       // time.Hour ("C") trash
+       v.EmptyTrash()
        err = v.Untrash(TestHash)
        if err != nil {
                t.Fatal(err)
        }
-
-       // Get is expected to fail due to EmptyTrash before Untrash
-       // It is still found on readonly volumes
-       buf, err = v.Get(TestHash)
+       err = checkGet()
        if err != nil {
-               if !os.IsNotExist(err) {
-                       t.Errorf("os.IsNotExist(%v) should have been true", err)
-               }
-       } else {
-               if bytes.Compare(buf, TestBlock) != 0 {
-                       t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
-               }
-               bufs.Put(buf)
+               t.Fatal(err)
        }
 }