diff --git a/pkg/storage/engine/rocksdb_test.go b/pkg/storage/engine/rocksdb_test.go index 73495b94cdc9..f27e4b09fa44 100644 --- a/pkg/storage/engine/rocksdb_test.go +++ b/pkg/storage/engine/rocksdb_test.go @@ -1052,3 +1052,64 @@ func TestRocksDBOptions(t *testing.T) { } } } + +func TestRocksDBFileNotFoundError(t *testing.T) { + defer leaktest.AfterTest(t)() + + dir, dirCleanup := testutils.TempDir(t) + defer dirCleanup() + + db, err := NewRocksDB( + RocksDBConfig{ + Settings: cluster.MakeTestingClusterSettings(), + Dir: dir, + }, + RocksDBCache{}, + ) + if err != nil { + t.Fatalf("could not create new rocksdb db instance at %s: %v", dir, err) + } + defer db.Close() + + // Verify DeleteFile returns os.ErrNotExist if file does not exist. + if err := db.DeleteFile("/non/existent/file"); !os.IsNotExist(err) { + t.Fatalf("expected IsNotExist, but got %v (%T)", err, err) + } + + fname := filepath.Join(dir, "random.file") + data := "random data" + if f, err := db.OpenFile(fname); err != nil { + t.Fatalf("unable to open file with filename %s, got err %v", fname, err) + } else { + // Write data to file so we can read it later. + if err := f.Append([]byte(data)); err != nil { + t.Fatalf("error writing data: '%s' to file %s, got err %v", data, fname, err) + } + if err := f.Sync(); err != nil { + t.Fatalf("error syncing data, got err %v", err) + } + if err := f.Close(); err != nil { + t.Fatalf("error closing file %s, got err %v", fname, err) + } + } + + if b, err := db.ReadFile(fname); err != nil { + t.Errorf("unable to read file with filename %s, got err %v", fname, err) + } else if string(b) != data { + t.Errorf("expected content in %s is '%s', got '%s'", fname, data, string(b)) + } + + if err := db.DeleteFile(fname); err != nil { + t.Errorf("unable to delete file with filename %s, got err %v", fname, err) + } + + // Verify ReadFile returns os.ErrNotExist if reading an already deleted file. + if _, err := db.ReadFile(fname); !os.IsNotExist(err) { + t.Fatalf("expected IsNotExist, but got %v (%T)", err, err) + } + + // Verify DeleteFile returns os.ErrNotExist if deleting an already deleted file. + if err := db.DeleteFile(fname); !os.IsNotExist(err) { + t.Fatalf("expected IsNotExist, but got %v (%T)", err, err) + } +}