Skip to content

Commit

Permalink
storage: add a unit test to test RocksDB's error message is expected
Browse files Browse the repository at this point in the history
Add a unit test to test `OpenFile`, `ReadFile`, and `DeleteFile` in RocksDB
return `os.ErrNotExist` error when the file or directory is not found.

Related to: cockroachdb#26259.
Release note: None
  • Loading branch information
windchan7 committed May 31, 2018
1 parent 29c0135 commit 23d5e62
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions pkg/storage/engine/rocksdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 23d5e62

Please sign in to comment.