cmd/main_test.go is using btesting as well.

Signed-off-by: Piotr Tabor <ptab@google.com>
pull/360/head
Piotr Tabor 2022-12-28 13:55:09 +01:00
parent 37d72cc1cd
commit 1f46d6c7e7
1 changed files with 32 additions and 68 deletions

View File

@ -5,6 +5,7 @@ import (
crypto "crypto/rand" crypto "crypto/rand"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"go.etcd.io/bbolt/internal/btesting"
"io" "io"
"math/rand" "math/rand"
"os" "os"
@ -18,15 +19,14 @@ import (
// Ensure the "info" command can print information about a database. // Ensure the "info" command can print information about a database.
func TestInfoCommand_Run(t *testing.T) { func TestInfoCommand_Run(t *testing.T) {
db := MustOpen(0666, nil) db := btesting.MustCreateDB(t)
db.DB.Close() db.Close()
defer db.Close()
defer requireDBNoChange(t, dbData(t, db.Path), db.Path) defer requireDBNoChange(t, dbData(t, db.Path()), db.Path())
// Run the info command. // Run the info command.
m := NewMain() m := NewMain()
if err := m.Run("info", db.Path); err != nil { if err := m.Run("info", db.Path()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
@ -38,11 +38,10 @@ func TestStatsCommand_Run_EmptyDatabase(t *testing.T) {
t.Skip("system does not use 4KB page size") t.Skip("system does not use 4KB page size")
} }
db := MustOpen(0666, nil) db := btesting.MustCreateDB(t)
defer db.Close() db.Close()
db.DB.Close()
defer requireDBNoChange(t, dbData(t, db.Path), db.Path) defer requireDBNoChange(t, dbData(t, db.Path()), db.Path())
// Generate expected result. // Generate expected result.
exp := "Aggregate statistics for 0 buckets\n\n" + exp := "Aggregate statistics for 0 buckets\n\n" +
@ -66,7 +65,7 @@ func TestStatsCommand_Run_EmptyDatabase(t *testing.T) {
// Run the command. // Run the command.
m := NewMain() m := NewMain()
if err := m.Run("stats", db.Path); err != nil { if err := m.Run("stats", db.Path()); err != nil {
t.Fatal(err) t.Fatal(err)
} else if m.Stdout.String() != exp { } else if m.Stdout.String() != exp {
t.Fatalf("unexpected stdout:\n\n%s", m.Stdout.String()) 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") t.Skip("system does not use 4KB page size")
} }
db := MustOpen(0666, nil) db := btesting.MustCreateDB(t)
defer db.Close()
if err := db.Update(func(tx *bolt.Tx) error { if err := db.Update(func(tx *bolt.Tx) error {
// Create "foo" bucket. // Create "foo" bucket.
@ -119,9 +117,9 @@ func TestStatsCommand_Run(t *testing.T) {
}); err != nil { }); err != nil {
t.Fatal(err) 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. // Generate expected result.
exp := "Aggregate statistics for 3 buckets\n\n" + exp := "Aggregate statistics for 3 buckets\n\n" +
@ -145,7 +143,7 @@ func TestStatsCommand_Run(t *testing.T) {
// Run the command. // Run the command.
m := NewMain() m := NewMain()
if err := m.Run("stats", db.Path); err != nil { if err := m.Run("stats", db.Path()); err != nil {
t.Fatal(err) t.Fatal(err)
} else if m.Stdout.String() != exp { } else if m.Stdout.String() != exp {
t.Fatalf("unexpected stdout:\n\n%s", m.Stdout.String()) 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. // Ensure the "buckets" command can print a list of buckets.
func TestBucketsCommand_Run(t *testing.T) { func TestBucketsCommand_Run(t *testing.T) {
db := MustOpen(0666, nil) db := btesting.MustCreateDB(t)
defer db.Close()
if err := db.Update(func(tx *bolt.Tx) error { if err := db.Update(func(tx *bolt.Tx) error {
for _, name := range []string{"foo", "bar", "baz"} { for _, name := range []string{"foo", "bar", "baz"} {
@ -168,15 +165,15 @@ func TestBucketsCommand_Run(t *testing.T) {
}); err != nil { }); err != nil {
t.Fatal(err) 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" expected := "bar\nbaz\nfoo\n"
// Run the command. // Run the command.
m := NewMain() m := NewMain()
if err := m.Run("buckets", db.Path); err != nil { if err := m.Run("buckets", db.Path()); err != nil {
t.Fatal(err) t.Fatal(err)
} else if actual := m.Stdout.String(); actual != expected { } else if actual := m.Stdout.String(); actual != expected {
t.Fatalf("unexpected stdout:\n\n%s", actual) 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. // Ensure the "keys" command can print a list of keys for a bucket.
func TestKeysCommand_Run(t *testing.T) { func TestKeysCommand_Run(t *testing.T) {
db := MustOpen(0666, nil) db := btesting.MustCreateDB(t)
defer db.Close()
if err := db.Update(func(tx *bolt.Tx) error { if err := db.Update(func(tx *bolt.Tx) error {
for _, name := range []string{"foo", "bar"} { for _, name := range []string{"foo", "bar"} {
@ -205,15 +201,15 @@ func TestKeysCommand_Run(t *testing.T) {
}); err != nil { }); err != nil {
t.Fatal(err) 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" expected := "foo-0\nfoo-1\nfoo-2\n"
// Run the command. // Run the command.
m := NewMain() 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) t.Fatal(err)
} else if actual := m.Stdout.String(); actual != expected { } else if actual := m.Stdout.String(); actual != expected {
t.Fatalf("unexpected stdout:\n\n%s", actual) 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. // Ensure the "get" command can print the value of a key in a bucket.
func TestGetCommand_Run(t *testing.T) { func TestGetCommand_Run(t *testing.T) {
db := MustOpen(0666, nil) db := btesting.MustCreateDB(t)
defer db.Close()
if err := db.Update(func(tx *bolt.Tx) error { if err := db.Update(func(tx *bolt.Tx) error {
for _, name := range []string{"foo", "bar"} { for _, name := range []string{"foo", "bar"} {
@ -243,15 +238,15 @@ func TestGetCommand_Run(t *testing.T) {
}); err != nil { }); err != nil {
t.Fatal(err) 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" expected := "val-foo-1\n"
// Run the command. // Run the command.
m := NewMain() 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) t.Fatal(err)
} else if actual := m.Stdout.String(); actual != expected { } else if actual := m.Stdout.String(); actual != expected {
t.Fatalf("unexpected stdout:\n\n%s", actual) t.Fatalf("unexpected stdout:\n\n%s", actual)
@ -275,32 +270,6 @@ func NewMain() *Main {
return m 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) { func TestCompactCommand_Run(t *testing.T) {
var s int64 var s int64
if err := binary.Read(crypto.Reader, binary.BigEndian, &s); err != nil { if err := binary.Read(crypto.Reader, binary.BigEndian, &s); err != nil {
@ -308,11 +277,11 @@ func TestCompactCommand_Run(t *testing.T) {
} }
rand.Seed(s) rand.Seed(s)
dstdb := MustOpen(0666, nil) dstdb := btesting.MustCreateDB(t)
dstdb.Close() dstdb.Close()
// fill the db // fill the db
db := MustOpen(0666, nil) db := btesting.MustCreateDB(t)
if err := db.Update(func(tx *bolt.Tx) error { if err := db.Update(func(tx *bolt.Tx) error {
n := 2 + rand.Intn(5) n := 2 + rand.Intn(5)
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
@ -330,7 +299,6 @@ func TestCompactCommand_Run(t *testing.T) {
} }
return nil return nil
}); err != nil { }); err != nil {
db.Close()
t.Fatal(err) t.Fatal(err)
} }
@ -353,7 +321,6 @@ func TestCompactCommand_Run(t *testing.T) {
} }
return nil return nil
}); err != nil { }); err != nil {
db.Close()
t.Fatal(err) t.Fatal(err)
} }
if err := db.Update(func(tx *bolt.Tx) error { 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")) return tx.DeleteBucket([]byte("large_vals"))
}); err != nil { }); err != nil {
db.Close()
t.Fatal(err) t.Fatal(err)
} }
db.DB.Close() db.Close()
defer db.Close()
defer dstdb.Close()
dbChk, err := chkdb(db.Path) dbChk, err := chkdb(db.Path())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
m := NewMain() 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) t.Fatal(err)
} }
dbChkAfterCompact, err := chkdb(db.Path) dbChkAfterCompact, err := chkdb(db.Path())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
dstdbChk, err := chkdb(dstdb.Path) dstdbChk, err := chkdb(dstdb.Path())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }