diff --git a/clientv3/snapshot/testdata/corrupted_backup.db b/clientv3/snapshot/testdata/corrupted_backup.db new file mode 100644 index 00000000000..d4ab10ef79e Binary files /dev/null and b/clientv3/snapshot/testdata/corrupted_backup.db differ diff --git a/clientv3/snapshot/v3_snapshot_test.go b/clientv3/snapshot/v3_snapshot_test.go index cd63cdfb083..8d2ee9d6aaa 100644 --- a/clientv3/snapshot/v3_snapshot_test.go +++ b/clientv3/snapshot/v3_snapshot_test.go @@ -21,6 +21,7 @@ import ( "net/url" "os" "path/filepath" + "strings" "testing" "time" @@ -161,6 +162,31 @@ func TestSnapshotFilePermissions(t *testing.T) { } } +// TestCorruptedBackupFileCheck tests if we can correctly identify a corrupted backup file. +func TestCorruptedBackupFileCheck(t *testing.T) { + dbPath := "testdata/corrupted_backup.db" + if _, err := os.Stat(dbPath); err != nil { + t.Fatalf("test file [%s] does not exist: %v", dbPath, err) + } + + sp := NewV3(zap.NewExample()) + _, err := sp.Status(dbPath) + expectedErrKeywords := "snapshot file integrity check failed" + /* example error message: + snapshot file integrity check failed. 2 errors found. + page 3: already freed + page 4: unreachable unfreed + */ + if err == nil { + t.Error("expected error due to corrupted snapshot file, got no error") + } + if !strings.Contains(err.Error(), expectedErrKeywords) { + t.Errorf("expected error message to contain the following keywords:\n%s\n"+ + "actual error message:\n%s", + expectedErrKeywords, err.Error()) + } +} + type kv struct { k, v string }