test: support enabling strict mode in testing

Signed-off-by: Benjamin Wang <wachao@vmware.com>
pull/440/head
Benjamin Wang 2023-03-25 13:56:57 +08:00
parent 7054e45233
commit a12c0c4bd7
3 changed files with 31 additions and 3 deletions

View File

@ -18,6 +18,14 @@ ifdef TIMEOUT
TESTFLAGS_TIMEOUT=$(TIMEOUT) TESTFLAGS_TIMEOUT=$(TIMEOUT)
endif endif
TESTFLAGS_ENABLE_STRICT_MODE=false
ifdef ENABLE_STRICT_MODE
TESTFLAGS_ENABLE_STRICT_MODE=$(ENABLE_STRICT_MODE)
endif
.EXPORT_ALL_VARIABLES:
TEST_ENABLE_STRICT_MODE=${TESTFLAGS_ENABLE_STRICT_MODE}
.PHONY: fmt .PHONY: fmt
fmt: fmt:
!(gofmt -l -s -d $(shell find . -name \*.go) | grep '[a-z]') !(gofmt -l -s -d $(shell find . -name \*.go) | grep '[a-z]')
@ -53,7 +61,7 @@ gofail-enable: install-gofail
gofail enable . gofail enable .
.PHONY: gofail-disable .PHONY: gofail-disable
gofail-disable: gofail-disable: install-gofail
gofail disable . gofail disable .
.PHONY: install-gofail .PHONY: install-gofail

View File

@ -6,6 +6,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings"
"testing" "testing"
"time" "time"
@ -17,8 +18,12 @@ import (
var statsFlag = flag.Bool("stats", false, "show performance stats") var statsFlag = flag.Bool("stats", false, "show performance stats")
// TestFreelistType is used as a env variable for test to indicate the backend type const (
const TestFreelistType = "TEST_FREELIST_TYPE" // TestFreelistType is used as an env variable for test to indicate the backend type.
TestFreelistType = "TEST_FREELIST_TYPE"
// TestEnableStrictMode is used to enable strict check by default after opening each DB.
TestEnableStrictMode = "TEST_ENABLE_STRICT_MODE"
)
// DB is a test wrapper for bolt.DB. // DB is a test wrapper for bolt.DB.
type DB struct { type DB struct {
@ -60,6 +65,7 @@ func MustOpenDBWithOption(t testing.TB, f string, o *bolt.Options) *DB {
o: o, o: o,
t: t, t: t,
} }
resDB.strictModeEnabledDefault()
t.Cleanup(resDB.PostTestCleanup) t.Cleanup(resDB.PostTestCleanup)
return resDB return resDB
} }
@ -113,6 +119,7 @@ func (db *DB) MustReopen() {
indb, err := bolt.Open(db.Path(), 0666, db.o) indb, err := bolt.Open(db.Path(), 0666, db.o)
require.NoError(db.t, err) require.NoError(db.t, err)
db.DB = indb db.DB = indb
db.strictModeEnabledDefault()
} }
// MustCheck runs a consistency check on the database and panics if any errors are found. // MustCheck runs a consistency check on the database and panics if any errors are found.
@ -204,3 +211,12 @@ func (db *DB) PrintStats() {
func truncDuration(d time.Duration) string { func truncDuration(d time.Duration) string {
return regexp.MustCompile(`^(\d+)(\.\d+)`).ReplaceAllString(d.String(), "$1") return regexp.MustCompile(`^(\d+)(\.\d+)`).ReplaceAllString(d.String(), "$1")
} }
func (db *DB) strictModeEnabledDefault() {
strictModeEnabled := strings.ToLower(os.Getenv(TestEnableStrictMode))
db.StrictMode = strictModeEnabled == "true"
}
func (db *DB) ForceDisableStrictMode() {
db.StrictMode = false
}

View File

@ -14,6 +14,7 @@ import (
func TestTx_RecursivelyCheckPages_MisplacedPage(t *testing.T) { func TestTx_RecursivelyCheckPages_MisplacedPage(t *testing.T) {
db := btesting.MustCreateDB(t) db := btesting.MustCreateDB(t)
db.ForceDisableStrictMode()
require.NoError(t, require.NoError(t,
db.Fill([]byte("data"), 1, 10000, db.Fill([]byte("data"), 1, 10000,
func(tx int, k int) []byte { return []byte(fmt.Sprintf("%04d", k)) }, func(tx int, k int) []byte { return []byte(fmt.Sprintf("%04d", k)) },
@ -36,6 +37,7 @@ func TestTx_RecursivelyCheckPages_MisplacedPage(t *testing.T) {
require.NoError(t, surgeon.CopyPage(db.Path(), srcPage, targetPage)) require.NoError(t, surgeon.CopyPage(db.Path(), srcPage, targetPage))
db.MustReopen() db.MustReopen()
db.ForceDisableStrictMode()
require.NoError(t, db.Update(func(tx *bolt.Tx) error { require.NoError(t, db.Update(func(tx *bolt.Tx) error {
// Collect all the errors. // Collect all the errors.
var errors []error var errors []error
@ -51,6 +53,7 @@ func TestTx_RecursivelyCheckPages_MisplacedPage(t *testing.T) {
func TestTx_RecursivelyCheckPages_CorruptedLeaf(t *testing.T) { func TestTx_RecursivelyCheckPages_CorruptedLeaf(t *testing.T) {
db := btesting.MustCreateDB(t) db := btesting.MustCreateDB(t)
db.ForceDisableStrictMode()
require.NoError(t, require.NoError(t,
db.Fill([]byte("data"), 1, 10000, db.Fill([]byte("data"), 1, 10000,
func(tx int, k int) []byte { return []byte(fmt.Sprintf("%04d", k)) }, func(tx int, k int) []byte { return []byte(fmt.Sprintf("%04d", k)) },
@ -72,6 +75,7 @@ func TestTx_RecursivelyCheckPages_CorruptedLeaf(t *testing.T) {
require.NoError(t, guts_cli.WritePage(db.Path(), pbuf)) require.NoError(t, guts_cli.WritePage(db.Path(), pbuf))
db.MustReopen() db.MustReopen()
db.ForceDisableStrictMode()
require.NoError(t, db.Update(func(tx *bolt.Tx) error { require.NoError(t, db.Update(func(tx *bolt.Tx) error {
// Collect all the errors. // Collect all the errors.
var errors []error var errors []error