aboutsummaryrefslogtreecommitdiff
path: root/db_test.go
diff options
context:
space:
mode:
authorsasha-s <sasha@scaledinference.com>2015-05-14 15:43:13 -0700
committersasha-s <sasha@scaledinference.com>2015-05-14 15:43:13 -0700
commit019bf5b010caa9b546c17add2ddd68d58c58fc10 (patch)
tree19280f1ed9ab48dbf479f47c226274ae34541d3d /db_test.go
parentMerge pull request #366 from benbjohnson/sync (diff)
downloaddedo-019bf5b010caa9b546c17add2ddd68d58c58fc10.tar.gz
dedo-019bf5b010caa9b546c17add2ddd68d58c58fc10.tar.xz
open read-only databases in read-only mode
Diffstat (limited to 'db_test.go')
-rw-r--r--db_test.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/db_test.go b/db_test.go
index ad17e87..b3d41f3 100644
--- a/db_test.go
+++ b/db_test.go
@@ -224,6 +224,61 @@ func TestDB_Open_FileTooSmall(t *testing.T) {
equals(t, errors.New("file size too small"), err)
}
+// Ensure that a database can be opened in read-only mode.
+func TestOpen_ReadOnly(t *testing.T) {
+ var bucket = []byte(`bucket`)
+ var key = []byte(`key`)
+ var value = []byte(`value`)
+ path := tempfile()
+ defer os.Remove(path)
+ db, err := bolt.Open(path, 0666, nil)
+ ok(t, db.Update(func(tx *bolt.Tx) error {
+ b, err := tx.CreateBucket(bucket)
+ if err != nil {
+ return err
+ }
+ return b.Put(key, value)
+ }))
+ assert(t, db != nil, "")
+ assert(t, !db.IsReadOnly(), "")
+ ok(t, err)
+ ok(t, db.Close())
+ // Make it read-only.
+ ok(t, os.Chmod(path, 0444))
+ // Open again.
+ db0, err := bolt.Open(path, 0666, nil)
+ ok(t, err)
+ defer db0.Close()
+ // And again.
+ db1, err := bolt.Open(path, 0666, nil)
+ ok(t, err)
+ defer db1.Close()
+ for _, db := range []*bolt.DB{db0, db1} {
+ // Verify is is in read only mode indeed.
+ assert(t, db.IsReadOnly(), "")
+ assert(t,
+ bolt.ErrDatabaseReadOnly == db.Update(func(*bolt.Tx) error {
+ panic(`should never get here`)
+ }),
+ "")
+ _, err = db.Begin(true)
+ assert(t, bolt.ErrDatabaseReadOnly == err, "")
+ // Verify the data.
+ ok(t, db.View(func(tx *bolt.Tx) error {
+ b := tx.Bucket(bucket)
+ if b == nil {
+ return fmt.Errorf("expected bucket `%s`", string(bucket))
+ }
+ got := string(b.Get(key))
+ expected := string(value)
+ if got != expected {
+ return fmt.Errorf("expected `%s`, got `%s`", expected, got)
+ }
+ return nil
+ }))
+ }
+}
+
// TODO(benbjohnson): Test corruption at every byte of the first two pages.
// Ensure that a database cannot open a transaction when it's not open.