From 0a6528d1b80a3cc07f5496836c1a9876fb3e953b Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Wed, 19 Jan 2022 09:48:49 -0500 Subject: [PATCH] Allow running of tests using badger-file as well as IM options Is off by default, should be toggled on for the CI builds though --- .circleci/config.yml | 3 +++ db/tests/utils.go | 38 +++++++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index ee208b488b..9a544e2ebb 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -28,6 +28,9 @@ jobs: - run: name: Run tests command: | + DEFRA_BADGER_MEMORY=true + DEFRA_BADGER_FILE=true + DEFRA_MAP=true mkdir -p /tmp/test-reports gotestsum --junitfile /tmp/test-reports/unit-tests.xml - store_test_results: diff --git a/db/tests/utils.go b/db/tests/utils.go index a5f6641a4c..eccff56306 100644 --- a/db/tests/utils.go +++ b/db/tests/utils.go @@ -42,16 +42,20 @@ type databaseInfo struct { } var badgerInMemory bool +var badgerFile bool var mapStore bool func init() { // We use environment variables instead of flags `go test ./...` throws for all packages that don't have the flag defined _, badgerInMemory = os.LookupEnv("DEFRA_BADGER_MEMORY") + _, badgerFile = os.LookupEnv("DEFRA_BADGER_FILE") _, mapStore = os.LookupEnv("DEFRA_MAP") // default is to run against all - if !badgerInMemory && !mapStore { + if !badgerInMemory && !badgerFile && !mapStore { badgerInMemory = true + // Testing against the file system is off by default + badgerFile = false mapStore = true } } @@ -87,7 +91,27 @@ func newMapDB() (databaseInfo, error) { }, nil } -func getDatabases() ([]databaseInfo, error) { +func newBadgerFileDB(t *testing.T) (databaseInfo, error) { + path := t.TempDir() + + opts := badgerds.Options{Options: badger.DefaultOptions(path)} + rootstore, err := badgerds.NewDatastore(path, &opts) + if err != nil { + return databaseInfo{}, err + } + + db, err := db.NewDB(rootstore, struct{}{}) + if err != nil { + return databaseInfo{}, err + } + + return databaseInfo{ + name: "badger-file-system", + db: db, + }, nil +} + +func getDatabases(t *testing.T) ([]databaseInfo, error) { databases := []databaseInfo{} if badgerInMemory { @@ -98,6 +122,14 @@ func getDatabases() ([]databaseInfo, error) { databases = append(databases, badgerIMDatabase) } + if badgerFile { + badgerIMDatabase, err := newBadgerFileDB(t) + if err != nil { + return nil, err + } + databases = append(databases, badgerIMDatabase) + } + if mapStore { mapDatabase, err := newMapDB() if err != nil { @@ -111,7 +143,7 @@ func getDatabases() ([]databaseInfo, error) { func ExecuteQueryTestCase(t *testing.T, schema string, collectionNames []string, test QueryTestCase) { ctx := context.Background() - dbs, err := getDatabases() + dbs, err := getDatabases(t) assert.NoError(t, err) assert.NotEmpty(t, dbs)