Skip to content

Commit

Permalink
restore preflight checks and db path logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nasdf committed Sep 12, 2023
1 parent 8195242 commit 01afa55
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
39 changes: 39 additions & 0 deletions tests/integration/change_detector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package tests

Check failure on line 1 in tests/integration/change_detector.go

View workflow job for this annotation

GitHub Actions / Lint job

Missed header for check (goheader)

import (
"os"
"path"
"testing"

"github.com/stretchr/testify/require"
)

func DetectDbChangesPreTestChecks(
t *testing.T,
collectionNames []string,
) {
if previousTestCaseTestName == t.Name() {
// The database format changer currently only supports running the first test
// case, if a second case is detected we return early
t.Skip()
}
previousTestCaseTestName = t.Name()

if len(collectionNames) == 0 {
// If the test doesn't specify any collections, then we can't use it to check
// the database format, so we skip it
t.SkipNow()
}

if !SetupOnly {
dbDirectory := path.Join(rootDatabaseDir, t.Name())
_, err := os.Stat(dbDirectory)
if os.IsNotExist(err) {
// This is a new test that does not exist in the target branch, we should
// skip it.
t.SkipNow()
} else {
require.NoError(t, err)
}
}
}
33 changes: 26 additions & 7 deletions tests/integration/utils2.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"encoding/json"
"fmt"
"os"
"path"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -43,6 +44,7 @@ const (
memoryBadgerEnvName = "DEFRA_BADGER_MEMORY"
fileBadgerEnvName = "DEFRA_BADGER_FILE"
fileBadgerPathEnvName = "DEFRA_BADGER_FILE_PATH"
rootDBFilePathEnvName = "DEFRA_TEST_ROOT"
inMemoryEnvName = "DEFRA_IN_MEMORY"
setupOnlyEnvName = "DEFRA_SETUP_ONLY"
detectDbChangesEnvName = "DEFRA_DETECT_DATABASE_CHANGES"
Expand Down Expand Up @@ -100,11 +102,16 @@ var (
log = logging.MustNewLogger("tests.integration")
badgerInMemory bool
badgerFile bool
badgerFilePath string
inMemoryStore bool
httpClient bool
goClient bool
mutationType MutationType
// databaseDir is the directory of the badger file db.
databaseDir string
// rootDatabaseDir is the root directroy of the badger file db.
rootDatabaseDir string
// previousTestCaseName is the name of the previous test.
previousTestCaseTestName string
)

const subscriptionTimeout = 1 * time.Second
Expand Down Expand Up @@ -145,7 +152,7 @@ func init() {
inMemoryStore, _ = strconv.ParseBool(os.Getenv(inMemoryEnvName))
DetectDbChanges, _ = strconv.ParseBool(os.Getenv(detectDbChangesEnvName))
SetupOnly, _ = strconv.ParseBool(os.Getenv(setupOnlyEnvName))
badgerFilePath = os.Getenv(fileBadgerPathEnvName)
rootDatabaseDir = os.Getenv(rootDBFilePathEnvName)

if value, ok := os.LookupEnv(mutationTypeEnvName); ok {
mutationType = MutationType(value)
Expand Down Expand Up @@ -216,21 +223,26 @@ func NewInMemoryDB(ctx context.Context) (client.DB, error) {
}

func NewBadgerFileDB(ctx context.Context, t testing.TB) (client.DB, string, error) {
if badgerFilePath == "" {
badgerFilePath = t.TempDir()
var dbPath string
if databaseDir != "" {
dbPath = databaseDir
} else if rootDatabaseDir != "" {
dbPath = path.Join(rootDatabaseDir, t.Name())
} else {
dbPath = t.TempDir()
}
opts := &badgerds.Options{
Options: badger.DefaultOptions(badgerFilePath),
Options: badger.DefaultOptions(dbPath),
}
rootstore, err := badgerds.NewDatastore(badgerFilePath, opts)
rootstore, err := badgerds.NewDatastore(dbPath, opts)
if err != nil {
return nil, "", err
}
db, err := db.NewDB(ctx, rootstore, db.WithUpdateEvents(), db.WithLensPoolSize(lensPoolSize))
if err != nil {
return nil, "", err
}
return db, badgerFilePath, err
return db, dbPath, err
}

// GetDatabase returns the database implementation for the current
Expand Down Expand Up @@ -284,6 +296,10 @@ func ExecuteTestCase(
) {
collectionNames := getCollectionNames(testCase)

if DetectDbChanges {
DetectDbChangesPreTestChecks(t, collectionNames)
}

skipIfMutationTypeUnsupported(t, testCase.SupportedMutationTypes)

var clients []ClientType
Expand Down Expand Up @@ -673,8 +689,11 @@ func restartNodes(

// We need to restart the nodes in reverse order, to avoid dial backoff issues.
for i := len(s.nodes) - 1; i >= 0; i-- {
originalPath := databaseDir
databaseDir = s.dbPaths[i]
db, _, err := GetDatabase(s)
require.Nil(s.t, err)
databaseDir = originalPath

if len(s.nodeConfigs) == 0 {
// If there are no explicit node configuration actions the node will be
Expand Down

0 comments on commit 01afa55

Please sign in to comment.