Skip to content

Commit

Permalink
BCF-2666 Add randomisation to testdb sequenced table columns (#11312)
Browse files Browse the repository at this point in the history
* Add randomisation to testdb sequenced table columns

* Fix wrong import in pgtest.go

* Move testdb sequence randomisation to shell testdb prepare

* Use crypto random instead of math rand in test db alter sequences

* Satisfy linter

* Fix randomiseTestDBSequences err messages

* Change randomiseTestDBSequences err msg

* Add Error suffix to failedToRandomiseTestDBSequences custom err

* Exclude unrelated tables from test db seq randomisation just in case

* Rename randomiseTestDBSequences to randomizeTestDBSequences
  • Loading branch information
ilija42 authored Nov 24, 2023
1 parent d5b9c44 commit 853a617
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
52 changes: 49 additions & 3 deletions core/cmd/shell_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"
crand "crypto/rand"
"database/sql"
"fmt"
"log"
Expand Down Expand Up @@ -778,11 +779,13 @@ func (s *Shell) PrepareTestDatabase(c *cli.Context) error {
if userOnly {
fixturePath = "../store/fixtures/users_only_fixture.sql"
}
if err := insertFixtures(dbUrl, fixturePath); err != nil {
if err = insertFixtures(dbUrl, fixturePath); err != nil {
return s.errorOut(err)
}

return s.errorOut(dropDanglingTestDBs(s.Logger, db))
if err = dropDanglingTestDBs(s.Logger, db); err != nil {
return s.errorOut(err)
}
return s.errorOut(randomizeTestDBSequences(db))
}

func dropDanglingTestDBs(lggr logger.Logger, db *sqlx.DB) (err error) {
Expand Down Expand Up @@ -822,6 +825,49 @@ func dropDanglingTestDBs(lggr logger.Logger, db *sqlx.DB) (err error) {
return
}

type failedToRandomizeTestDBSequencesError struct{}

func (m *failedToRandomizeTestDBSequencesError) Error() string {
return "failed to randomize test db sequences"
}

// randomizeTestDBSequences randomizes sequenced table columns sequence
// This is necessary as to avoid false positives in some test cases.
func randomizeTestDBSequences(db *sqlx.DB) error {
seqRows, err := db.Query(`SELECT sequence_schema, sequence_name FROM information_schema.sequences WHERE sequence_schema = $1`, "public")
if err != nil {
return fmt.Errorf("%s: error fetching sequences: %s", failedToRandomizeTestDBSequencesError{}, err)
}

defer seqRows.Close()
for seqRows.Next() {
var sequenceSchema, sequenceName string
if err = seqRows.Scan(&sequenceSchema, &sequenceName); err != nil {
return fmt.Errorf("%s: failed scanning sequence rows: %s", failedToRandomizeTestDBSequencesError{}, err)
}

if sequenceName == "goose_migrations_id_seq" || sequenceName == "configurations_id_seq" {
continue
}

var randNum *big.Int
randNum, err = crand.Int(crand.Reader, utils.NewBigI(10000).ToInt())
if err != nil {
return fmt.Errorf("%s: failed to generate random number", failedToRandomizeTestDBSequencesError{})
}

if _, err = db.Exec(fmt.Sprintf("ALTER SEQUENCE %s.%s RESTART WITH %d", sequenceSchema, sequenceName, randNum)); err != nil {
return fmt.Errorf("%s: failed to alter and restart %s sequence: %w", failedToRandomizeTestDBSequencesError{}, sequenceName, err)
}
}

if err = seqRows.Err(); err != nil {
return fmt.Errorf("%s: failed to iterate through sequences: %w", failedToRandomizeTestDBSequencesError{}, err)
}

return nil
}

// PrepareTestDatabaseUserOnly calls ResetDatabase then loads only user fixtures required for local
// testing against testnets. Does not include fake chain fixtures.
func (s *Shell) PrepareTestDatabaseUserOnly(c *cli.Context) error {
Expand Down
1 change: 0 additions & 1 deletion core/internal/testutils/pgtest/pgtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func NewSqlxDB(t testing.TB) *sqlx.DB {
db, err := sqlx.Open(string(dialects.TransactionWrappedPostgres), uuid.New().String())
require.NoError(t, err)
t.Cleanup(func() { assert.NoError(t, db.Close()) })

db.MapperFunc(reflectx.CamelToSnakeASCII)

return db
Expand Down

0 comments on commit 853a617

Please sign in to comment.