Skip to content

Commit

Permalink
add pageId flag to check cmd
Browse files Browse the repository at this point in the history
Signed-off-by: Mustafa Elbehery <melbeher@redhat.com>
  • Loading branch information
Elbehery committed Apr 22, 2024
1 parent a718144 commit acd7d05
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 20 deletions.
11 changes: 9 additions & 2 deletions cmd/bbolt/command_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ import (
"go.etcd.io/bbolt/internal/guts_cli"
)

var pageId uint64

func newCheckCommand() *cobra.Command {
checkCmd := &cobra.Command{
Use: "check <bbolt-file>",
Short: "verify integrity of bbolt database data",
Args: cobra.ExactArgs(1),
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return checkFunc(cmd, args[0])
},
}

checkCmd.Flags().Uint64VarP(&pageId, "pageId", "", pageId, "check db integrity starting from the given pageId")
return checkCmd
}

Expand All @@ -37,10 +40,14 @@ func checkFunc(cmd *cobra.Command, dbPath string) error {
}
defer db.Close()

opts := []bolt.CheckOption{bolt.WithKVStringer(CmdKvStringer())}
if pageId != 0 {
opts = append(opts, bolt.WithPageId(pageId))
}
// 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", "--pageId", "3"},
expErr: nil,
expOutput: "OK\n",
},
{
name: "check invalid pageId",
args: []string{"check", "path", "--pageId", "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 acd7d05

Please sign in to comment.