mirror of https://github.com/etcd-io/bbolt.git
Merge pull request #737 from Elbehery/add-check-subcommand-pgid
feat: add `page-Id` flag to `check` cmdpull/740/head
commit
2112e9c931
|
@ -4,25 +4,36 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
|
|
||||||
bolt "go.etcd.io/bbolt"
|
bolt "go.etcd.io/bbolt"
|
||||||
"go.etcd.io/bbolt/internal/guts_cli"
|
"go.etcd.io/bbolt/internal/guts_cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type checkOptions struct {
|
||||||
|
fromPageID uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *checkOptions) AddFlags(fs *pflag.FlagSet) {
|
||||||
|
fs.Uint64VarP(&o.fromPageID, "from-page", "", o.fromPageID, "check db integrity starting from the given page ID")
|
||||||
|
}
|
||||||
|
|
||||||
func newCheckCommand() *cobra.Command {
|
func newCheckCommand() *cobra.Command {
|
||||||
|
var o checkOptions
|
||||||
checkCmd := &cobra.Command{
|
checkCmd := &cobra.Command{
|
||||||
Use: "check <bbolt-file>",
|
Use: "check <bbolt-file>",
|
||||||
Short: "verify integrity of bbolt database data",
|
Short: "verify integrity of bbolt database data",
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return checkFunc(cmd, args[0])
|
return checkFunc(cmd, args[0], o)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
o.AddFlags(checkCmd.Flags())
|
||||||
return checkCmd
|
return checkCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkFunc(cmd *cobra.Command, dbPath string) error {
|
func checkFunc(cmd *cobra.Command, dbPath string, cfg checkOptions) error {
|
||||||
if _, err := checkSourceDBPath(dbPath); err != nil {
|
if _, err := checkSourceDBPath(dbPath); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -37,10 +48,14 @@ func checkFunc(cmd *cobra.Command, dbPath string) error {
|
||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
|
opts := []bolt.CheckOption{bolt.WithKVStringer(CmdKvStringer())}
|
||||||
|
if cfg.fromPageID != 0 {
|
||||||
|
opts = append(opts, bolt.WithPageId(cfg.fromPageID))
|
||||||
|
}
|
||||||
// Perform consistency check.
|
// Perform consistency check.
|
||||||
return db.View(func(tx *bolt.Tx) error {
|
return db.View(func(tx *bolt.Tx) error {
|
||||||
var count int
|
var count int
|
||||||
for err := range tx.Check(bolt.WithKVStringer(CmdKvStringer())) {
|
for err := range tx.Check(opts...) {
|
||||||
fmt.Fprintln(cmd.OutOrStdout(), err)
|
fmt.Fprintln(cmd.OutOrStdout(), err)
|
||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,25 +9,58 @@ import (
|
||||||
|
|
||||||
main "go.etcd.io/bbolt/cmd/bbolt"
|
main "go.etcd.io/bbolt/cmd/bbolt"
|
||||||
"go.etcd.io/bbolt/internal/btesting"
|
"go.etcd.io/bbolt/internal/btesting"
|
||||||
|
"go.etcd.io/bbolt/internal/guts_cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCheckCommand_Run(t *testing.T) {
|
func TestCheckCommand_Run(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
args []string
|
||||||
|
expErr error
|
||||||
|
expOutput string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "check whole db",
|
||||||
|
args: []string{"check", "path"},
|
||||||
|
expErr: nil,
|
||||||
|
expOutput: "OK\n",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "check valid pageId",
|
||||||
|
args: []string{"check", "path", "--from-page", "3"},
|
||||||
|
expErr: nil,
|
||||||
|
expOutput: "OK\n",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "check invalid pageId",
|
||||||
|
args: []string{"check", "path", "--from-page", "1"},
|
||||||
|
expErr: guts_cli.ErrCorrupt,
|
||||||
|
expOutput: "page ID (1) out of range [2, 4)",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
|
||||||
|
t.Log("Creating sample DB")
|
||||||
db := btesting.MustCreateDB(t)
|
db := btesting.MustCreateDB(t)
|
||||||
db.Close()
|
db.Close()
|
||||||
defer requireDBNoChange(t, dbData(t, db.Path()), db.Path())
|
defer requireDBNoChange(t, dbData(t, db.Path()), db.Path())
|
||||||
|
|
||||||
|
t.Log("Running check cmd")
|
||||||
rootCmd := main.NewRootCommand()
|
rootCmd := main.NewRootCommand()
|
||||||
// capture output for assertion
|
outputBuf := bytes.NewBufferString("") // capture output for assertion
|
||||||
outputBuf := bytes.NewBufferString("")
|
|
||||||
rootCmd.SetOut(outputBuf)
|
rootCmd.SetOut(outputBuf)
|
||||||
|
|
||||||
rootCmd.SetArgs([]string{
|
tc.args[1] = db.Path() // path to be replaced with db.Path()
|
||||||
"check", db.Path(),
|
rootCmd.SetArgs(tc.args)
|
||||||
})
|
|
||||||
err := rootCmd.Execute()
|
err := rootCmd.Execute()
|
||||||
require.NoError(t, err)
|
require.Equal(t, tc.expErr, err)
|
||||||
|
|
||||||
|
t.Log("Checking output")
|
||||||
output, err := io.ReadAll(outputBuf)
|
output, err := io.ReadAll(outputBuf)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equalf(t, "OK\n", string(output), "unexpected stdout:\n\n%s", string(output))
|
require.Containsf(t, string(output), tc.expOutput, "unexpected stdout:\n\n%s", string(output))
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue