diff --git a/cmd/bbolt/main_test.go b/cmd/bbolt/main_test.go index f65698023..fb0c8f750 100644 --- a/cmd/bbolt/main_test.go +++ b/cmd/bbolt/main_test.go @@ -5,6 +5,7 @@ import ( crypto "crypto/rand" "encoding/binary" "fmt" + "go.etcd.io/bbolt/internal/btesting" "io" "math/rand" "os" @@ -18,15 +19,14 @@ import ( // Ensure the "info" command can print information about a database. func TestInfoCommand_Run(t *testing.T) { - db := MustOpen(0666, nil) - db.DB.Close() - defer db.Close() + db := btesting.MustCreateDB(t) + db.Close() - defer requireDBNoChange(t, dbData(t, db.Path), db.Path) + defer requireDBNoChange(t, dbData(t, db.Path()), db.Path()) // Run the info command. m := NewMain() - if err := m.Run("info", db.Path); err != nil { + if err := m.Run("info", db.Path()); err != nil { t.Fatal(err) } } @@ -38,11 +38,10 @@ func TestStatsCommand_Run_EmptyDatabase(t *testing.T) { t.Skip("system does not use 4KB page size") } - db := MustOpen(0666, nil) - defer db.Close() - db.DB.Close() + db := btesting.MustCreateDB(t) + db.Close() - defer requireDBNoChange(t, dbData(t, db.Path), db.Path) + defer requireDBNoChange(t, dbData(t, db.Path()), db.Path()) // Generate expected result. exp := "Aggregate statistics for 0 buckets\n\n" + @@ -66,7 +65,7 @@ func TestStatsCommand_Run_EmptyDatabase(t *testing.T) { // Run the command. m := NewMain() - if err := m.Run("stats", db.Path); err != nil { + if err := m.Run("stats", db.Path()); err != nil { t.Fatal(err) } else if m.Stdout.String() != exp { t.Fatalf("unexpected stdout:\n\n%s", m.Stdout.String()) @@ -80,8 +79,7 @@ func TestStatsCommand_Run(t *testing.T) { t.Skip("system does not use 4KB page size") } - db := MustOpen(0666, nil) - defer db.Close() + db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { // Create "foo" bucket. @@ -119,9 +117,9 @@ func TestStatsCommand_Run(t *testing.T) { }); err != nil { t.Fatal(err) } - db.DB.Close() + db.Close() - defer requireDBNoChange(t, dbData(t, db.Path), db.Path) + defer requireDBNoChange(t, dbData(t, db.Path()), db.Path()) // Generate expected result. exp := "Aggregate statistics for 3 buckets\n\n" + @@ -145,7 +143,7 @@ func TestStatsCommand_Run(t *testing.T) { // Run the command. m := NewMain() - if err := m.Run("stats", db.Path); err != nil { + if err := m.Run("stats", db.Path()); err != nil { t.Fatal(err) } else if m.Stdout.String() != exp { t.Fatalf("unexpected stdout:\n\n%s", m.Stdout.String()) @@ -154,8 +152,7 @@ func TestStatsCommand_Run(t *testing.T) { // Ensure the "buckets" command can print a list of buckets. func TestBucketsCommand_Run(t *testing.T) { - db := MustOpen(0666, nil) - defer db.Close() + db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { for _, name := range []string{"foo", "bar", "baz"} { @@ -168,15 +165,15 @@ func TestBucketsCommand_Run(t *testing.T) { }); err != nil { t.Fatal(err) } - db.DB.Close() + db.Close() - defer requireDBNoChange(t, dbData(t, db.Path), db.Path) + defer requireDBNoChange(t, dbData(t, db.Path()), db.Path()) expected := "bar\nbaz\nfoo\n" // Run the command. m := NewMain() - if err := m.Run("buckets", db.Path); err != nil { + if err := m.Run("buckets", db.Path()); err != nil { t.Fatal(err) } else if actual := m.Stdout.String(); actual != expected { t.Fatalf("unexpected stdout:\n\n%s", actual) @@ -185,8 +182,7 @@ func TestBucketsCommand_Run(t *testing.T) { // Ensure the "keys" command can print a list of keys for a bucket. func TestKeysCommand_Run(t *testing.T) { - db := MustOpen(0666, nil) - defer db.Close() + db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { for _, name := range []string{"foo", "bar"} { @@ -205,15 +201,15 @@ func TestKeysCommand_Run(t *testing.T) { }); err != nil { t.Fatal(err) } - db.DB.Close() + db.Close() - defer requireDBNoChange(t, dbData(t, db.Path), db.Path) + defer requireDBNoChange(t, dbData(t, db.Path()), db.Path()) expected := "foo-0\nfoo-1\nfoo-2\n" // Run the command. m := NewMain() - if err := m.Run("keys", db.Path, "foo"); err != nil { + if err := m.Run("keys", db.Path(), "foo"); err != nil { t.Fatal(err) } else if actual := m.Stdout.String(); actual != expected { t.Fatalf("unexpected stdout:\n\n%s", actual) @@ -222,8 +218,7 @@ func TestKeysCommand_Run(t *testing.T) { // Ensure the "get" command can print the value of a key in a bucket. func TestGetCommand_Run(t *testing.T) { - db := MustOpen(0666, nil) - defer db.Close() + db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { for _, name := range []string{"foo", "bar"} { @@ -243,15 +238,15 @@ func TestGetCommand_Run(t *testing.T) { }); err != nil { t.Fatal(err) } - db.DB.Close() + db.Close() - defer requireDBNoChange(t, dbData(t, db.Path), db.Path) + defer requireDBNoChange(t, dbData(t, db.Path()), db.Path()) expected := "val-foo-1\n" // Run the command. m := NewMain() - if err := m.Run("get", db.Path, "foo", "foo-1"); err != nil { + if err := m.Run("get", db.Path(), "foo", "foo-1"); err != nil { t.Fatal(err) } else if actual := m.Stdout.String(); actual != expected { t.Fatalf("unexpected stdout:\n\n%s", actual) @@ -275,32 +270,6 @@ func NewMain() *Main { return m } -// MustOpen creates a Bolt database in a temporary location. -func MustOpen(mode os.FileMode, options *bolt.Options) *DB { - // Create temporary path. - f, _ := os.CreateTemp("", "bolt-") - f.Close() - os.Remove(f.Name()) - - db, err := bolt.Open(f.Name(), mode, options) - if err != nil { - panic(err.Error()) - } - return &DB{DB: db, Path: f.Name()} -} - -// DB is a test wrapper for bolt.DB. -type DB struct { - *bolt.DB - Path string -} - -// Close closes and removes the database. -func (db *DB) Close() error { - defer os.Remove(db.Path) - return db.DB.Close() -} - func TestCompactCommand_Run(t *testing.T) { var s int64 if err := binary.Read(crypto.Reader, binary.BigEndian, &s); err != nil { @@ -308,11 +277,11 @@ func TestCompactCommand_Run(t *testing.T) { } rand.Seed(s) - dstdb := MustOpen(0666, nil) + dstdb := btesting.MustCreateDB(t) dstdb.Close() // fill the db - db := MustOpen(0666, nil) + db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { n := 2 + rand.Intn(5) for i := 0; i < n; i++ { @@ -330,7 +299,6 @@ func TestCompactCommand_Run(t *testing.T) { } return nil }); err != nil { - db.Close() t.Fatal(err) } @@ -353,7 +321,6 @@ func TestCompactCommand_Run(t *testing.T) { } return nil }); err != nil { - db.Close() t.Fatal(err) } if err := db.Update(func(tx *bolt.Tx) error { @@ -365,29 +332,26 @@ func TestCompactCommand_Run(t *testing.T) { } return tx.DeleteBucket([]byte("large_vals")) }); err != nil { - db.Close() t.Fatal(err) } - db.DB.Close() - defer db.Close() - defer dstdb.Close() + db.Close() - dbChk, err := chkdb(db.Path) + dbChk, err := chkdb(db.Path()) if err != nil { t.Fatal(err) } m := NewMain() - if err := m.Run("compact", "-o", dstdb.Path, db.Path); err != nil { + if err := m.Run("compact", "-o", dstdb.Path(), db.Path()); err != nil { t.Fatal(err) } - dbChkAfterCompact, err := chkdb(db.Path) + dbChkAfterCompact, err := chkdb(db.Path()) if err != nil { t.Fatal(err) } - dstdbChk, err := chkdb(dstdb.Path) + dstdbChk, err := chkdb(dstdb.Path()) if err != nil { t.Fatal(err) }