8555: Log statistics in EmptyTrash.
[arvados.git] / services / keepstore / volume_generic_test.go
index f8fe0d0ebce719c6c823fe9caa9fcce12324eb49..bc3e537a89a815037102af7fb920e8b9d2b84f61 100644 (file)
@@ -7,6 +7,7 @@ import (
        "os"
        "regexp"
        "sort"
+       "strconv"
        "strings"
        "time"
 
@@ -355,10 +356,22 @@ func testIndexTo(t TB, factory TestableVolumeFactory) {
        v := factory(t)
        defer v.Teardown()
 
+       // minMtime and maxMtime are the minimum and maximum
+       // acceptable values the index can report for our test
+       // blocks. 1-second precision is acceptable.
+       minMtime := time.Now().UTC().UnixNano()
+       minMtime -= minMtime % 1e9
+
        v.PutRaw(TestHash, TestBlock)
        v.PutRaw(TestHash2, TestBlock2)
        v.PutRaw(TestHash3, TestBlock3)
 
+       maxMtime := time.Now().UTC().UnixNano()
+       if maxMtime%1e9 > 0 {
+               maxMtime -= maxMtime % 1e9
+               maxMtime += 1e9
+       }
+
        // Blocks whose names aren't Keep hashes should be omitted from
        // index
        v.PutRaw("fffffffffnotreallyahashfffffffff", nil)
@@ -371,15 +384,21 @@ func testIndexTo(t TB, factory TestableVolumeFactory) {
        indexRows := strings.Split(string(buf.Bytes()), "\n")
        sort.Strings(indexRows)
        sortedIndex := strings.Join(indexRows, "\n")
-       m, err := regexp.MatchString(
-               `^\n`+TestHash+`\+\d+ \d+\n`+
-                       TestHash3+`\+\d+ \d+\n`+
-                       TestHash2+`\+\d+ \d+$`,
-               sortedIndex)
-       if err != nil {
-               t.Error(err)
-       } else if !m {
+       m := regexp.MustCompile(
+               `^\n` + TestHash + `\+\d+ (\d+)\n` +
+                       TestHash3 + `\+\d+ \d+\n` +
+                       TestHash2 + `\+\d+ \d+$`,
+       ).FindStringSubmatch(sortedIndex)
+       if m == nil {
                t.Errorf("Got index %q for empty prefix", sortedIndex)
+       } else {
+               mtime, err := strconv.ParseInt(m[1], 10, 64)
+               if err != nil {
+                       t.Error(err)
+               } else if mtime < minMtime || mtime > maxMtime {
+                       t.Errorf("got %d for TestHash timestamp, expected %d <= t <= %d",
+                               mtime, minMtime, maxMtime)
+               }
        }
 
        for _, prefix := range []string{"f", "f15", "f15ac"} {
@@ -396,7 +415,7 @@ func testIndexTo(t TB, factory TestableVolumeFactory) {
 
        for _, prefix := range []string{"zero", "zip", "zilch"} {
                buf = new(bytes.Buffer)
-               v.IndexTo(prefix, buf)
+               err := v.IndexTo(prefix, buf)
                if err != nil {
                        t.Errorf("Got error on IndexTo with no such prefix %v", err.Error())
                } else if buf.Len() != 0 {
@@ -722,7 +741,7 @@ func testTrashUntrash(t TB, factory TestableVolumeFactory) {
        v := factory(t)
        defer v.Teardown()
        defer func() {
-               trashLifetime = 0 * time.Second
+               trashLifetime = 0
        }()
 
        trashLifetime = 3600 * time.Second
@@ -811,7 +830,7 @@ func testTrashEmptyTrashUntrash(t TB, factory TestableVolumeFactory) {
 
        // First set: EmptyTrash before reaching the trash deadline.
 
-       trashLifetime = 1 * time.Hour
+       trashLifetime = time.Hour
 
        v.PutRaw(TestHash, TestBlock)
        v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
@@ -861,23 +880,27 @@ func testTrashEmptyTrashUntrash(t TB, factory TestableVolumeFactory) {
        // Because we Touch'ed, need to backdate again for next set of tests
        v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
 
-       // Untrash should fail if the only block in the trash has
-       // already been untrashed.
+       // If the only block in the trash has already been untrashed,
+       // most volumes will fail a subsequent Untrash with a 404, but
+       // it's also acceptable for Untrash to succeed.
        err = v.Untrash(TestHash)
-       if err == nil || !os.IsNotExist(err) {
-               t.Fatalf("os.IsNotExist(%v) should have been true", err)
+       if err != nil && !os.IsNotExist(err) {
+               t.Fatalf("Expected success or os.IsNotExist(), but got: %v", err)
        }
 
-       // The failed Untrash should not interfere with our
+       // The additional Untrash should not interfere with our
        // already-untrashed copy.
        err = checkGet()
        if err != nil {
                t.Fatal(err)
        }
 
+       // Untrash might have updated the timestamp, so backdate again
+       v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
+
        // Second set: EmptyTrash after the trash deadline has passed.
 
-       trashLifetime = 1 * time.Nanosecond
+       trashLifetime = time.Nanosecond
 
        err = v.Trash(TestHash)
        if err != nil {
@@ -908,7 +931,6 @@ func testTrashEmptyTrashUntrash(t TB, factory TestableVolumeFactory) {
        if err == nil || !os.IsNotExist(err) {
                t.Fatalf("os.IsNotExist(%v) should have been true", err)
        }
-       // EmptryTrash
        v.EmptyTrash()
 
        // Untrash won't find it