From a09bea76bcf9819a706a228729ceaad59c18f6a9 Mon Sep 17 00:00:00 2001 From: krehermann Date: Tue, 18 Jun 2024 15:50:38 -0600 Subject: [PATCH] linter, mocks --- core/chains/evm/forwarders/mocks/orm.go | 67 ------------------- core/chains/evm/forwarders/orm.go | 1 - core/chains/evm/txmgr/txmgr_test.go | 1 - core/cmd/shell_local.go | 3 - core/store/migrate/migrate.go | 8 --- .../plugins/relayer/evm/0002_initial.go | 7 -- .../migrate/plugins/relayer/evm/migrate.go | 55 ++++++++++----- .../plugins/relayer/evm/migrate_test.go | 1 - .../plugins/relayer/evm/testutils/db.go | 2 +- go.mod | 2 +- 10 files changed, 41 insertions(+), 106 deletions(-) diff --git a/core/chains/evm/forwarders/mocks/orm.go b/core/chains/evm/forwarders/mocks/orm.go index 04e252fa7d4..8d406f3eba1 100644 --- a/core/chains/evm/forwarders/mocks/orm.go +++ b/core/chains/evm/forwarders/mocks/orm.go @@ -66,43 +66,6 @@ func (_m *ORM) DeleteForwarder(ctx context.Context, id int64, cleanup func(sqlut return r0 } -// FindForwarders provides a mock function with given fields: ctx, offset, limit -func (_m *ORM) FindForwarders(ctx context.Context, offset int, limit int) ([]forwarders.Forwarder, int, error) { - ret := _m.Called(ctx, offset, limit) - - if len(ret) == 0 { - panic("no return value specified for FindForwarders") - } - - var r0 []forwarders.Forwarder - var r1 int - var r2 error - if rf, ok := ret.Get(0).(func(context.Context, int, int) ([]forwarders.Forwarder, int, error)); ok { - return rf(ctx, offset, limit) - } - if rf, ok := ret.Get(0).(func(context.Context, int, int) []forwarders.Forwarder); ok { - r0 = rf(ctx, offset, limit) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]forwarders.Forwarder) - } - } - - if rf, ok := ret.Get(1).(func(context.Context, int, int) int); ok { - r1 = rf(ctx, offset, limit) - } else { - r1 = ret.Get(1).(int) - } - - if rf, ok := ret.Get(2).(func(context.Context, int, int) error); ok { - r2 = rf(ctx, offset, limit) - } else { - r2 = ret.Error(2) - } - - return r0, r1, r2 -} - // FindForwardersByChain provides a mock function with given fields: ctx, evmChainId func (_m *ORM) FindForwardersByChain(ctx context.Context, evmChainId big.Big) ([]forwarders.Forwarder, error) { ret := _m.Called(ctx, evmChainId) @@ -133,36 +96,6 @@ func (_m *ORM) FindForwardersByChain(ctx context.Context, evmChainId big.Big) ([ return r0, r1 } -// FindForwardersInListByChain provides a mock function with given fields: ctx, evmChainId, addrs -func (_m *ORM) FindForwardersInListByChain(ctx context.Context, evmChainId big.Big, addrs []common.Address) ([]forwarders.Forwarder, error) { - ret := _m.Called(ctx, evmChainId, addrs) - - if len(ret) == 0 { - panic("no return value specified for FindForwardersInListByChain") - } - - var r0 []forwarders.Forwarder - var r1 error - if rf, ok := ret.Get(0).(func(context.Context, big.Big, []common.Address) ([]forwarders.Forwarder, error)); ok { - return rf(ctx, evmChainId, addrs) - } - if rf, ok := ret.Get(0).(func(context.Context, big.Big, []common.Address) []forwarders.Forwarder); ok { - r0 = rf(ctx, evmChainId, addrs) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]forwarders.Forwarder) - } - } - - if rf, ok := ret.Get(1).(func(context.Context, big.Big, []common.Address) error); ok { - r1 = rf(ctx, evmChainId, addrs) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - // NewORM creates a new instance of ORM. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. // The first argument is typically a *testing.T value. func NewORM(t interface { diff --git a/core/chains/evm/forwarders/orm.go b/core/chains/evm/forwarders/orm.go index f9d373951c3..3a2b8ffd5b6 100644 --- a/core/chains/evm/forwarders/orm.go +++ b/core/chains/evm/forwarders/orm.go @@ -45,7 +45,6 @@ func (o *DSORM) schemaName() string { return fmt.Sprintf("evm_%s", o.cid.String()) } return "evm" - } // CreateForwarder creates the Forwarder address associated with the current EVM chain id. diff --git a/core/chains/evm/txmgr/txmgr_test.go b/core/chains/evm/txmgr/txmgr_test.go index 5da9e107c1b..318b673ebab 100644 --- a/core/chains/evm/txmgr/txmgr_test.go +++ b/core/chains/evm/txmgr/txmgr_test.go @@ -303,7 +303,6 @@ func TestTxm_CreateTransaction(t *testing.T) { }) t.Run("forwards tx when a proper forwarder is set up", func(t *testing.T) { - pgtest.MustExec(t, db, `DELETE FROM evm.txes`) pgtest.MustExec(t, db, `DELETE FROM evm.forwarders`) evmConfig.MaxQueued = uint64(1) diff --git a/core/cmd/shell_local.go b/core/cmd/shell_local.go index fac4dd01106..e084081dcff 100644 --- a/core/cmd/shell_local.go +++ b/core/cmd/shell_local.go @@ -988,7 +988,6 @@ func newRelayMigrationOpts(c *cli.Context) *relayMigrationOpts { Relayer: c.String("relayer"), ChainID: c.String("chain-id"), } - } // MigrateDatabase migrates the database @@ -1049,8 +1048,6 @@ func (s *Shell) RollbackDatabase(c *cli.Context) error { err = fmt.Errorf("unknown relayer '%s'", opts.Relayer) } return err - - return nil } // VersionDatabase displays the current database version. diff --git a/core/store/migrate/migrate.go b/core/store/migrate/migrate.go index 081ad797d8b..d6f19dc83e5 100644 --- a/core/store/migrate/migrate.go +++ b/core/store/migrate/migrate.go @@ -23,16 +23,8 @@ import ( //go:embed migrations/*.sql migrations/*.go var embedMigrations embed.FS -// go:embed plugins/relayers/**/*.tmpl.sql -var embedRelayerMigrations embed.FS - -const PLUGIN_MIGRATIONS_DIR string = "plugins" - const MIGRATIONS_DIR string = "migrations" -// go:embed manifest.txt -var migrationManifest string - func init() { setupCoreMigrations() logMigrations := os.Getenv("CL_LOG_SQL_MIGRATIONS") diff --git a/core/store/migrate/plugins/relayer/evm/0002_initial.go b/core/store/migrate/plugins/relayer/evm/0002_initial.go index 2136c6dc885..e9811723529 100644 --- a/core/store/migrate/plugins/relayer/evm/0002_initial.go +++ b/core/store/migrate/plugins/relayer/evm/0002_initial.go @@ -52,10 +52,3 @@ func Register0002(val Cfg) error { goose.AddMigrationContext(upFunc, downFunc) return nil } - -func generateExec(execString string) func(ctx context.Context, tx *sql.Tx) error { - return func(ctx context.Context, tx *sql.Tx) error { - _, err := tx.ExecContext(ctx, execString) - return err - } -} diff --git a/core/store/migrate/plugins/relayer/evm/migrate.go b/core/store/migrate/plugins/relayer/evm/migrate.go index b9f9f5d3268..fc47105b5a7 100644 --- a/core/store/migrate/plugins/relayer/evm/migrate.go +++ b/core/store/migrate/plugins/relayer/evm/migrate.go @@ -11,25 +11,33 @@ import ( "gopkg.in/guregu/null.v4" ) -func setupPluginMigrations(cfg Cfg) { +func setupPluginMigrations(cfg Cfg) error { // reset the base fs and the global migrations goose.SetBaseFS(nil) // we don't want to use the base fs for plugin migrations because the embedded fs contains templates, not sql files goose.ResetGlobalMigrations() goose.SetTableName(fmt.Sprintf("goose_migration_relayer_%s_%s", cfg.Schema, cfg.ChainID.String())) - Register0002(cfg) + err := Register0002(cfg) + if err != nil { + return fmt.Errorf("failed to register migration 0002: %w", err) + } + return nil } // Migrate migrates a subsystem of the chainlink database. // It generates migrations based on the template for the subsystem and applies them to the database. func Migrate(ctx context.Context, db *sql.DB, cfg Cfg) error { - tmpDir := os.TempDir() + tmpDir, err := os.MkdirTemp("", cfg.Schema) + if err != nil { + return fmt.Errorf("failed to create temp dir: %w", err) + } defer os.RemoveAll(tmpDir) - //defer setupCoreMigrations() - setupPluginMigrations(cfg) - - d := filepath.Join(tmpDir, cfg.Schema, fmt.Sprintf("%s", cfg.ChainID)) - err := os.MkdirAll(d, os.ModePerm) + err = setupPluginMigrations(cfg) + if err != nil { + return fmt.Errorf("failed to setup plugin migrations: %w", err) + } + d := filepath.Join(tmpDir, cfg.Schema, cfg.ChainID.String()) + err = os.MkdirAll(d, os.ModePerm) if err != nil { return fmt.Errorf("failed to create directory %s: %w", d, err) } @@ -48,14 +56,19 @@ func Migrate(ctx context.Context, db *sql.DB, cfg Cfg) error { } func Rollback(ctx context.Context, db *sql.DB, version null.Int, cfg Cfg) error { - tmpDir := os.TempDir() + tmpDir, err := os.MkdirTemp("", cfg.Schema) + if err != nil { + return fmt.Errorf("failed to create temp dir: %w", err) + } defer os.RemoveAll(tmpDir) - setupPluginMigrations(cfg) - + err = setupPluginMigrations(cfg) + if err != nil { + return fmt.Errorf("failed to setup plugin migrations: %w", err) + } // TODO: should these be saved somewhere? if so where, if not if the db itself?) - d := filepath.Join(tmpDir, cfg.Schema, fmt.Sprintf("%s", cfg.ChainID)) - err := os.MkdirAll(d, os.ModePerm) + d := filepath.Join(tmpDir, cfg.Schema, cfg.ChainID.String()) + err = os.MkdirAll(d, os.ModePerm) if err != nil { return fmt.Errorf("failed to create directory %s: %w", d, err) } @@ -74,14 +87,24 @@ func Rollback(ctx context.Context, db *sql.DB, version null.Int, cfg Cfg) error } func Current(ctx context.Context, db *sql.DB, cfg Cfg) (int64, error) { - setupPluginMigrations(cfg) + err := setupPluginMigrations(cfg) + if err != nil { + return -1, fmt.Errorf("failed to setup plugin migrations: %w", err) + } + // set the base fs only for status so that the templates are listed + // an alternative would be to somehow keep track of the erated sql files, but that would be more complex + // and error prone WRT to restarts + goose.SetBaseFS(embeddedTmplFS) return goose.EnsureDBVersion(db) } func Status(ctx context.Context, db *sql.DB, cfg Cfg) error { - setupPluginMigrations(cfg) + err := setupPluginMigrations(cfg) + if err != nil { + return fmt.Errorf("failed to setup plugin migrations: %w", err) + } // set the base fs only for status so that the templates are listed - // an alternative would be to somehow keep track of the generated sql files, but that would be more complex + // an alternative would be to somehow keep track of the erated sql files, but that would be more complex // and error prone WRT to restarts goose.SetBaseFS(embeddedTmplFS) return goose.Status(db, MigrationRootDir) diff --git a/core/store/migrate/plugins/relayer/evm/migrate_test.go b/core/store/migrate/plugins/relayer/evm/migrate_test.go index c383ecec55b..b8c886797a3 100644 --- a/core/store/migrate/plugins/relayer/evm/migrate_test.go +++ b/core/store/migrate/plugins/relayer/evm/migrate_test.go @@ -15,7 +15,6 @@ import ( ) func TestMigrate(t *testing.T) { - t.Run("core migration with optional relayer migration", func(t *testing.T) { _, db := heavyweight.FullTestDBEmptyV2(t, nil) diff --git a/core/store/migrate/plugins/relayer/evm/testutils/db.go b/core/store/migrate/plugins/relayer/evm/testutils/db.go index f1c86e39681..0788dca38ee 100644 --- a/core/store/migrate/plugins/relayer/evm/testutils/db.go +++ b/core/store/migrate/plugins/relayer/evm/testutils/db.go @@ -10,8 +10,8 @@ import ( "github.com/google/uuid" "github.com/jmoiron/sqlx" "github.com/scylladb/go-reflectx" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/test-go/testify/assert" "github.com/smartcontractkit/chainlink/v2/core/internal/cltest/heavyweight" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" diff --git a/go.mod b/go.mod index ab60a5377ed..9598e9b5700 100644 --- a/go.mod +++ b/go.mod @@ -86,7 +86,6 @@ require ( github.com/smartcontractkit/wsrpc v0.8.1 github.com/spf13/cast v1.6.0 github.com/stretchr/testify v1.9.0 - github.com/test-go/testify v1.1.4 github.com/theodesp/go-heaps v0.0.0-20190520121037-88e35354fe0a github.com/tidwall/gjson v1.17.0 github.com/ugorji/go/codec v1.2.12 @@ -299,6 +298,7 @@ require ( github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect github.com/tendermint/go-amino v0.16.0 // indirect github.com/teris-io/shortid v0.0.0-20201117134242-e59966efd125 // indirect + github.com/test-go/testify v1.1.4 // indirect github.com/tidwall/btree v1.6.0 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.0 // indirect