File: command_check_test.go

package info (click to toggle)
golang-github-coreos-bbolt 1.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid
  • size: 1,300 kB
  • sloc: makefile: 87; sh: 57
file content (66 lines) | stat: -rw-r--r-- 1,567 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main_test

import (
	"bytes"
	"io"
	"testing"

	"github.com/stretchr/testify/require"

	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) {
	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))
		})
	}
}