Skip to content

Commit

Permalink
cmd/main_test.go is using btesting as well.
Browse files Browse the repository at this point in the history
Signed-off-by: Piotr Tabor <ptab@google.com>
  • Loading branch information
ptabor committed Dec 28, 2022
1 parent 37d72cc commit 1f46d6c
Showing 1 changed file with 32 additions and 68 deletions.
100 changes: 32 additions & 68 deletions cmd/bbolt/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
crypto "crypto/rand"
"encoding/binary"
"fmt"
"go.etcd.io/bbolt/internal/btesting"
"io"
"math/rand"
"os"
Expand All @@ -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)
}
}
Expand All @@ -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" +
Expand All @@ -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())
Expand All @@ -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.
Expand Down Expand Up @@ -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" +
Expand All @@ -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())
Expand All @@ -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"} {
Expand All @@ -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)
Expand All @@ -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"} {
Expand All @@ -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)
Expand All @@ -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"} {
Expand All @@ -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)
Expand All @@ -275,44 +270,18 @@ 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 {
t.Fatal(err)
}
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++ {
Expand All @@ -330,7 +299,6 @@ func TestCompactCommand_Run(t *testing.T) {
}
return nil
}); err != nil {
db.Close()
t.Fatal(err)
}

Expand All @@ -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 {
Expand All @@ -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)
}
Expand Down

0 comments on commit 1f46d6c

Please sign in to comment.