Skip to content

Commit

Permalink
Merge pull request #75 from Juneezee/test/t.TempDir
Browse files Browse the repository at this point in the history
test: use `t.TempDir` to create temporary test directory
  • Loading branch information
nguyer authored Dec 28, 2023
2 parents 4dc70d4 + 39f251d commit 6f1476e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 50 deletions.
11 changes: 4 additions & 7 deletions internal/persistence/dbmigration/leveldb2postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,22 @@ func TestMigrateLevelDBToPostgres(t *testing.T) {
defer done()

// Configure a test LevelDB
dir, err := os.MkdirTemp("", "ldb_*")
assert.NoError(t, err)
dir := t.TempDir()
config.Set(tmconfig.PersistenceLevelDBPath, dir)

// Run empty migration and check no errors
err = MigrateLevelDBToPostgres(ctx)
err := MigrateLevelDBToPostgres(ctx)
assert.NoError(t, err)

}

func TestMigrateLevelDBToPostgresFailPSQL(t *testing.T) {
tmconfig.Reset()

// Configure a test LevelDB
dir, err := os.MkdirTemp("", "ldb_*")
assert.NoError(t, err)
dir := t.TempDir()
config.Set(tmconfig.PersistenceLevelDBPath, dir)

err = MigrateLevelDBToPostgres(context.Background())
err := MigrateLevelDBToPostgres(context.Background())
assert.Regexp(t, "FF21049", err)
}

Expand Down
4 changes: 1 addition & 3 deletions internal/persistence/leveldb/leveldb_persistence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ func newTestLevelDBPersistence(t *testing.T) (context.Context, *leveldbPersisten

ctx, cancelCtx := context.WithCancel(context.Background())

dir, err := os.MkdirTemp("", "ldb_*")
assert.NoError(t, err)
dir := t.TempDir()

tmconfig.Reset()
config.Set(tmconfig.PersistenceLevelDBPath, dir)
Expand All @@ -62,7 +61,6 @@ func newTestLevelDBPersistence(t *testing.T) (context.Context, *leveldbPersisten

return ctx, p, func() {
p.Close(ctx)
os.RemoveAll(dir)
cancelCtx()
}

Expand Down
26 changes: 8 additions & 18 deletions pkg/fftm/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ func newTestManager(t *testing.T) (string, *manager, func()) {

url := testManagerCommonInit(t, false)

dir, err := os.MkdirTemp("", "ldb_*")
assert.NoError(t, err)
dir := t.TempDir()
config.Set(tmconfig.PersistenceLevelDBPath, dir)

mca := &ffcapimocks.API{}
Expand All @@ -99,7 +98,6 @@ func newTestManager(t *testing.T) (string, *manager, func()) {
m,
func() {
m.Close()
os.RemoveAll(dir)
}
}

Expand Down Expand Up @@ -167,8 +165,7 @@ func newTestManagerWithMetrics(t *testing.T) (string, *manager, func()) {

url := testManagerCommonInit(t, true)

dir, err := os.MkdirTemp("", "ldb_*")
assert.NoError(t, err)
dir := t.TempDir()
config.Set(tmconfig.PersistenceLevelDBPath, dir)

mca := &ffcapimocks.API{}
Expand All @@ -185,7 +182,6 @@ func newTestManagerWithMetrics(t *testing.T) (string, *manager, func()) {
m,
func() {
m.Close()
os.RemoveAll(dir)
}
}

Expand Down Expand Up @@ -251,15 +247,13 @@ func TestNewManagerBadHttpConfig(t *testing.T) {

tmconfig.Reset()
tmconfig.APIConfig.Set(httpserver.HTTPConfAddress, "::::")
dir, err := os.MkdirTemp("", "ldb_*")
defer os.RemoveAll(dir)
assert.NoError(t, err)
dir := t.TempDir()
config.Set(tmconfig.PersistenceLevelDBPath, dir)

txRegistry.RegisterHandler(&simple.TransactionHandlerFactory{})
tmconfig.TransactionHandlerBaseConfig.SubSection("simple").Set(simple.FixedGasPrice, "223344556677")

_, err = NewManager(context.Background(), nil)
_, err := NewManager(context.Background(), nil)
assert.Error(t, err)
assert.Regexp(t, "FF00151", err)

Expand Down Expand Up @@ -300,13 +294,11 @@ func TestNewManagerBadPersistenceConfig(t *testing.T) {
func TestNewManagerInvalidTransactionHandlerName(t *testing.T) {

tmconfig.Reset()
dir, err := os.MkdirTemp("", "ldb_*")
defer os.RemoveAll(dir)
assert.NoError(t, err)
dir := t.TempDir()
config.Set(tmconfig.PersistenceLevelDBPath, dir)
config.Set(tmconfig.TransactionsHandlerName, "wrong")

_, err = NewManager(context.Background(), nil)
_, err := NewManager(context.Background(), nil)
assert.Regexp(t, "FF21070", err)

}
Expand Down Expand Up @@ -335,15 +327,13 @@ func TestNewManagerWithMetricsBadConfig(t *testing.T) {

tmconfig.MetricsConfig.Set("enabled", true)
tmconfig.MetricsConfig.Set(httpserver.HTTPConfAddress, "::::")
dir, err := os.MkdirTemp("", "ldb_*")
defer os.RemoveAll(dir)
assert.NoError(t, err)
dir := t.TempDir()
config.Set(tmconfig.PersistenceLevelDBPath, dir)

txRegistry.RegisterHandler(&simple.TransactionHandlerFactory{})
tmconfig.TransactionHandlerBaseConfig.SubSection("simple").Set(simple.FixedGasPrice, "223344556677")

_, err = NewManager(context.Background(), nil)
_, err := NewManager(context.Background(), nil)
assert.Error(t, err)
assert.Regexp(t, "FF00151", err)
}
Expand Down
21 changes: 7 additions & 14 deletions pkg/txhandler/simple/policyloop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ func sendSampleDeployment(t *testing.T, sth *simpleTransactionHandler, signer st
}

func TestPolicyLoopE2EOk(t *testing.T) {
f, tk, _, conf, cleanup := newTestTransactionHandlerFactoryWithFilePersistence(t)
defer cleanup()
f, tk, _, conf := newTestTransactionHandlerFactoryWithFilePersistence(t)
conf.Set(FixedGasPrice, `12345`)
conf.Set(ResubmitInterval, "100s")
th, err := f.NewTransactionHandler(context.Background(), conf)
Expand Down Expand Up @@ -170,8 +169,7 @@ func TestPolicyLoopE2EOk(t *testing.T) {
}

func TestPolicyLoopIgnoreTransactionInformationalEventHandlingErrors(t *testing.T) {
f, tk, _, conf, cleanup := newTestTransactionHandlerFactoryWithFilePersistence(t)
defer cleanup()
f, tk, _, conf := newTestTransactionHandlerFactoryWithFilePersistence(t)
conf.Set(FixedGasPrice, `12345`)
conf.Set(ResubmitInterval, "100s")
th, err := f.NewTransactionHandler(context.Background(), conf)
Expand Down Expand Up @@ -228,8 +226,7 @@ func TestPolicyLoopIgnoreTransactionInformationalEventHandlingErrors(t *testing.
}

func TestTransactionPreparationErrors(t *testing.T) {
f, tk, _, conf, cleanup := newTestTransactionHandlerFactoryWithFilePersistence(t)
defer cleanup()
f, tk, _, conf := newTestTransactionHandlerFactoryWithFilePersistence(t)
conf.Set(FixedGasPrice, `12345`)
conf.Set(ResubmitInterval, "100s")
th, err := f.NewTransactionHandler(context.Background(), conf)
Expand Down Expand Up @@ -272,8 +269,7 @@ func TestTransactionPreparationErrors(t *testing.T) {

func TestPolicyLoopE2EReverted(t *testing.T) {

f, tk, _, conf, cleanup := newTestTransactionHandlerFactoryWithFilePersistence(t)
defer cleanup()
f, tk, _, conf := newTestTransactionHandlerFactoryWithFilePersistence(t)
conf.Set(FixedGasPrice, `12345`)
conf.Set(ResubmitInterval, "100s")
th, err := f.NewTransactionHandler(context.Background(), conf)
Expand Down Expand Up @@ -355,8 +351,7 @@ func TestPolicyLoopE2EReverted(t *testing.T) {
}

func TestPolicyLoopResubmitNewTXID(t *testing.T) {
f, tk, _, conf, cleanup := newTestTransactionHandlerFactoryWithFilePersistence(t)
defer cleanup()
f, tk, _, conf := newTestTransactionHandlerFactoryWithFilePersistence(t)
conf.Set(FixedGasPrice, `12345`)
conf.Set(ResubmitInterval, "100s")
conf.Set(Interval, "0")
Expand Down Expand Up @@ -444,8 +439,7 @@ func TestPolicyLoopResubmitNewTXID(t *testing.T) {

func TestNotifyConfirmationMgrFail(t *testing.T) {

f, tk, _, conf, cleanup := newTestTransactionHandlerFactoryWithFilePersistence(t)
defer cleanup()
f, tk, _, conf := newTestTransactionHandlerFactoryWithFilePersistence(t)
conf.Set(FixedGasPrice, `12345`)
conf.Set(ResubmitInterval, "100s")
th, err := f.NewTransactionHandler(context.Background(), conf)
Expand Down Expand Up @@ -700,8 +694,7 @@ func TestPolicyLoopUpdateEventHandlerError(t *testing.T) {
}

func TestPolicyEngineFailStaleThenUpdated(t *testing.T) {
f, tk, mockFFCAPI, conf, cleanup := newTestTransactionHandlerFactoryWithFilePersistence(t)
defer cleanup()
f, tk, mockFFCAPI, conf := newTestTransactionHandlerFactoryWithFilePersistence(t)
conf.SubSection(GasOracleConfig).Set(GasOracleMode, GasOracleModeConnector)
th, err := f.NewTransactionHandler(context.Background(), conf)
assert.NoError(t, err)
Expand Down
11 changes: 3 additions & 8 deletions pkg/txhandler/simple/simple_transaction_hander_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"math/big"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"

Expand Down Expand Up @@ -80,7 +79,7 @@ func newTestRunContext(mtx *apitypes.ManagedTX, receipt *ffcapi.TransactionRecei
}
}

func newTestTransactionHandlerFactoryWithFilePersistence(t *testing.T) (*TransactionHandlerFactory, *txhandler.Toolkit, *ffcapimocks.API, config.Section, func()) {
func newTestTransactionHandlerFactoryWithFilePersistence(t *testing.T) (*TransactionHandlerFactory, *txhandler.Toolkit, *ffcapimocks.API, config.Section) {
tmconfig.Reset()
conf := config.RootSection("unittest.simple")
viper.SetDefault(string(tmconfig.TransactionsHandlerName), "simple")
Expand All @@ -90,8 +89,7 @@ func newTestTransactionHandlerFactoryWithFilePersistence(t *testing.T) (*Transac
conf.SubSection(GasOracleConfig).Set(GasOracleMode, GasOracleModeDisabled)
assert.Equal(t, "simple", f.Name())

dir, err := os.MkdirTemp("", "ldb_*")
assert.NoError(t, err)
dir := t.TempDir()
config.Set(tmconfig.PersistenceLevelDBPath, dir)
filePersistence, err := leveldb.NewLevelDBPersistence(context.Background(), 1*time.Hour)
assert.NoError(t, err)
Expand All @@ -106,10 +104,7 @@ func newTestTransactionHandlerFactoryWithFilePersistence(t *testing.T) (*Transac
TXPersistence: filePersistence,
MetricsManager: metrics.NewMetricsManager(context.Background()),
EventHandler: mockEventHandler,
}, mockFFCAPI, conf,
func() {
os.RemoveAll(dir)
}
}, mockFFCAPI, conf
}

func newTestTransactionHandler(t *testing.T) txhandler.TransactionHandler {
Expand Down

0 comments on commit 6f1476e

Please sign in to comment.