Skip to content

Commit

Permalink
Merge pull request etcd-io#737 from Elbehery/add-check-subcommand-pgid
Browse files Browse the repository at this point in the history
feat: add `page-Id` flag to `check` cmd
  • Loading branch information
ahrtr authored Apr 23, 2024
2 parents a718144 + acfa086 commit 2112e9c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 21 deletions.
21 changes: 18 additions & 3 deletions cmd/bbolt/command_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,36 @@ import (
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

bolt "go.etcd.io/bbolt"
"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 {
var o checkOptions
checkCmd := &cobra.Command{
Use: "check <bbolt-file>",
Short: "verify integrity of bbolt database data",
Args: cobra.ExactArgs(1),
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
}

func checkFunc(cmd *cobra.Command, dbPath string) error {
func checkFunc(cmd *cobra.Command, dbPath string, cfg checkOptions) error {
if _, err := checkSourceDBPath(dbPath); err != nil {
return err
}
Expand All @@ -37,10 +48,14 @@ func checkFunc(cmd *cobra.Command, dbPath string) error {
}
defer db.Close()

opts := []bolt.CheckOption{bolt.WithKVStringer(CmdKvStringer())}
if cfg.fromPageID != 0 {
opts = append(opts, bolt.WithPageId(cfg.fromPageID))
}
// Perform consistency check.
return db.View(func(tx *bolt.Tx) error {
var count int
for err := range tx.Check(bolt.WithKVStringer(CmdKvStringer())) {
for err := range tx.Check(opts...) {
fmt.Fprintln(cmd.OutOrStdout(), err)
count++
}
Expand Down
69 changes: 51 additions & 18 deletions cmd/bbolt/command_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,58 @@ import (

main "go.etcd.io/bbolt/cmd/bbolt"
"go.etcd.io/bbolt/internal/btesting"
"go.etcd.io/bbolt/internal/guts_cli"
)

func TestCheckCommand_Run(t *testing.T) {
db := btesting.MustCreateDB(t)
db.Close()
defer requireDBNoChange(t, dbData(t, db.Path()), db.Path())

rootCmd := main.NewRootCommand()
// capture output for assertion
outputBuf := bytes.NewBufferString("")
rootCmd.SetOut(outputBuf)

rootCmd.SetArgs([]string{
"check", db.Path(),
})
err := rootCmd.Execute()
require.NoError(t, err)

output, err := io.ReadAll(outputBuf)
require.NoError(t, err)
require.Equalf(t, "OK\n", string(output), "unexpected stdout:\n\n%s", string(output))
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.Close()
defer requireDBNoChange(t, dbData(t, db.Path()), db.Path())

t.Log("Running check cmd")
rootCmd := main.NewRootCommand()
outputBuf := bytes.NewBufferString("") // capture output for assertion
rootCmd.SetOut(outputBuf)

tc.args[1] = db.Path() // path to be replaced with db.Path()
rootCmd.SetArgs(tc.args)
err := rootCmd.Execute()
require.Equal(t, tc.expErr, err)

t.Log("Checking output")
output, err := io.ReadAll(outputBuf)
require.NoError(t, err)
require.Containsf(t, string(output), tc.expOutput, "unexpected stdout:\n\n%s", string(output))
})
}
}

0 comments on commit 2112e9c

Please sign in to comment.