X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/25be2534e8746475ddc799042fefa08bd0548e9d..f935965259f9c0476a9c5ffa79e5c27ce9da4800:/services/keepstore/volume_generic_test.go?ds=sidebyside diff --git a/services/keepstore/volume_generic_test.go b/services/keepstore/volume_generic_test.go index f8fe0d0ebc..1738fe9b51 100644 --- a/services/keepstore/volume_generic_test.go +++ b/services/keepstore/volume_generic_test.go @@ -7,9 +7,11 @@ import ( "os" "regexp" "sort" + "strconv" "strings" "time" + "git.curoverse.com/arvados.git/sdk/go/arvados" "git.curoverse.com/arvados.git/sdk/go/arvadostest" ) @@ -355,10 +357,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 +385,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 +416,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 { @@ -411,7 +431,7 @@ func testIndexTo(t TB, factory TestableVolumeFactory) { func testDeleteNewBlock(t TB, factory TestableVolumeFactory) { v := factory(t) defer v.Teardown() - blobSignatureTTL = 300 * time.Second + theConfig.BlobSignatureTTL.Set("5m") if v.Writable() == false { return @@ -432,19 +452,19 @@ func testDeleteNewBlock(t TB, factory TestableVolumeFactory) { } // Calling Delete() for a block with a timestamp older than -// blobSignatureTTL seconds in the past should delete the data. +// BlobSignatureTTL seconds in the past should delete the data. // Test is intended for only writable volumes func testDeleteOldBlock(t TB, factory TestableVolumeFactory) { v := factory(t) defer v.Teardown() - blobSignatureTTL = 300 * time.Second + theConfig.BlobSignatureTTL.Set("5m") if v.Writable() == false { return } v.Put(TestHash, TestBlock) - v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL)) + v.TouchWithDate(TestHash, time.Now().Add(-2*theConfig.BlobSignatureTTL.Duration())) if err := v.Trash(TestHash); err != nil { t.Error(err) @@ -714,7 +734,7 @@ func testPutFullBlock(t TB, factory TestableVolumeFactory) { } } -// With trashLifetime != 0, perform: +// With TrashLifetime != 0, perform: // Trash an old block - which either raises ErrNotImplemented or succeeds // Untrash - which either raises ErrNotImplemented or succeeds // Get - which must succeed @@ -722,14 +742,14 @@ func testTrashUntrash(t TB, factory TestableVolumeFactory) { v := factory(t) defer v.Teardown() defer func() { - trashLifetime = 0 * time.Second + theConfig.TrashLifetime = 0 }() - trashLifetime = 3600 * time.Second + theConfig.TrashLifetime.Set("1h") // put block and backdate it v.PutRaw(TestHash, TestBlock) - v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL)) + v.TouchWithDate(TestHash, time.Now().Add(-2*theConfig.BlobSignatureTTL.Duration())) buf := make([]byte, BlockSize) n, err := v.Get(TestHash, buf) @@ -776,9 +796,9 @@ func testTrashUntrash(t TB, factory TestableVolumeFactory) { func testTrashEmptyTrashUntrash(t TB, factory TestableVolumeFactory) { v := factory(t) defer v.Teardown() - defer func(orig time.Duration) { - trashLifetime = orig - }(trashLifetime) + defer func(orig arvados.Duration) { + theConfig.TrashLifetime = orig + }(theConfig.TrashLifetime) checkGet := func() error { buf := make([]byte, BlockSize) @@ -811,10 +831,10 @@ func testTrashEmptyTrashUntrash(t TB, factory TestableVolumeFactory) { // First set: EmptyTrash before reaching the trash deadline. - trashLifetime = 1 * time.Hour + theConfig.TrashLifetime.Set("1h") v.PutRaw(TestHash, TestBlock) - v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL)) + v.TouchWithDate(TestHash, time.Now().Add(-2*theConfig.BlobSignatureTTL.Duration())) err := checkGet() if err != nil { @@ -825,7 +845,7 @@ func testTrashEmptyTrashUntrash(t TB, factory TestableVolumeFactory) { 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. + // volume types that don't support TrashLifetime>0. return } @@ -859,25 +879,29 @@ 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)) + v.TouchWithDate(TestHash, time.Now().Add(-2*theConfig.BlobSignatureTTL.Duration())) - // 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*theConfig.BlobSignatureTTL.Duration())) + // Second set: EmptyTrash after the trash deadline has passed. - trashLifetime = 1 * time.Nanosecond + theConfig.TrashLifetime.Set("1ns") err = v.Trash(TestHash) if err != nil { @@ -902,13 +926,12 @@ func testTrashEmptyTrashUntrash(t TB, factory TestableVolumeFactory) { // Trash it again, and this time call EmptyTrash so it really // goes away. // (In Azure volumes, un/trash changes Mtime, so first backdate again) - v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL)) + v.TouchWithDate(TestHash, time.Now().Add(-2*theConfig.BlobSignatureTTL.Duration())) err = v.Trash(TestHash) err = checkGet() if err == nil || !os.IsNotExist(err) { t.Fatalf("os.IsNotExist(%v) should have been true", err) } - // EmptryTrash v.EmptyTrash() // Untrash won't find it @@ -928,9 +951,9 @@ func testTrashEmptyTrashUntrash(t TB, factory TestableVolumeFactory) { // un-trashed copy doesn't get deleted along with it. v.PutRaw(TestHash, TestBlock) - v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL)) + v.TouchWithDate(TestHash, time.Now().Add(-2*theConfig.BlobSignatureTTL.Duration())) - trashLifetime = time.Nanosecond + theConfig.TrashLifetime.Set("1ns") err = v.Trash(TestHash) if err != nil { t.Fatal(err) @@ -941,7 +964,7 @@ func testTrashEmptyTrashUntrash(t TB, factory TestableVolumeFactory) { } v.PutRaw(TestHash, TestBlock) - v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL)) + v.TouchWithDate(TestHash, time.Now().Add(-2*theConfig.BlobSignatureTTL.Duration())) // EmptyTrash should not delete the untrashed copy. v.EmptyTrash() @@ -956,18 +979,18 @@ func testTrashEmptyTrashUntrash(t TB, factory TestableVolumeFactory) { // untrash the block whose deadline is "C". v.PutRaw(TestHash, TestBlock) - v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL)) + v.TouchWithDate(TestHash, time.Now().Add(-2*theConfig.BlobSignatureTTL.Duration())) - trashLifetime = time.Nanosecond + theConfig.TrashLifetime.Set("1ns") err = v.Trash(TestHash) if err != nil { t.Fatal(err) } v.PutRaw(TestHash, TestBlock) - v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL)) + v.TouchWithDate(TestHash, time.Now().Add(-2*theConfig.BlobSignatureTTL.Duration())) - trashLifetime = time.Hour + theConfig.TrashLifetime.Set("1h") err = v.Trash(TestHash) if err != nil { t.Fatal(err)