Skip to content

Commit

Permalink
cleaning up context.Background's in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhuie19 committed Feb 26, 2024
1 parent 572e45c commit 7703bd7
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 47 deletions.
3 changes: 1 addition & 2 deletions core/services/fluxmonitorv2/key_store_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fluxmonitorv2_test

import (
"context"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -41,7 +40,7 @@ func TestKeyStore_EnabledKeysForChain(t *testing.T) {
func TestKeyStore_GetRoundRobinAddress(t *testing.T) {
t.Parallel()

ctx := context.Background()
ctx := testutils.Context(t)

db := pgtest.NewSqlxDB(t)
cfg := pgtest.NewQConfig(true)
Expand Down
10 changes: 5 additions & 5 deletions core/services/job/job_orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ func TestORM_ValidateKeyStoreMatch(t *testing.T) {
}

t.Run("test ETH key validation", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
jb.OCR2OracleSpec.Relay = relay.EVM
err := job.ValidateKeyStoreMatch(ctx, jb.OCR2OracleSpec, keyStore, "bad key")
require.EqualError(t, err, "no EVM key matching: \"bad key\"")
Expand All @@ -911,7 +911,7 @@ func TestORM_ValidateKeyStoreMatch(t *testing.T) {
})

t.Run("test Cosmos key validation", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
jb.OCR2OracleSpec.Relay = relay.Cosmos
err := job.ValidateKeyStoreMatch(ctx, jb.OCR2OracleSpec, keyStore, "bad key")
require.EqualError(t, err, "no Cosmos key matching: \"bad key\"")
Expand All @@ -923,7 +923,7 @@ func TestORM_ValidateKeyStoreMatch(t *testing.T) {
})

t.Run("test Solana key validation", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
jb.OCR2OracleSpec.Relay = relay.Solana

err := job.ValidateKeyStoreMatch(ctx, jb.OCR2OracleSpec, keyStore, "bad key")
Expand All @@ -936,7 +936,7 @@ func TestORM_ValidateKeyStoreMatch(t *testing.T) {
})

t.Run("test Starknet key validation", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
jb.OCR2OracleSpec.Relay = relay.StarkNet
err := job.ValidateKeyStoreMatch(ctx, jb.OCR2OracleSpec, keyStore, "bad key")
require.EqualError(t, err, "no Starknet key matching: \"bad key\"")
Expand All @@ -948,7 +948,7 @@ func TestORM_ValidateKeyStoreMatch(t *testing.T) {
})

t.Run("test Mercury ETH key validation", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
jb.OCR2OracleSpec.PluginType = types.Mercury
err := job.ValidateKeyStoreMatch(ctx, jb.OCR2OracleSpec, keyStore, "bad key")
require.EqualError(t, err, "no CSA key matching: \"bad key\"")
Expand Down
3 changes: 1 addition & 2 deletions core/services/keeper/upkeep_executer_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package keeper_test

import (
"context"
"math/big"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -227,7 +226,7 @@ func Test_UpkeepExecuter_PerformsUpkeep_Happy(t *testing.T) {
})

t.Run("errors if submission key not found", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
_, _, ethMock, executer, registry, _, job, jpv2, _, keyStore, _, _ := setup(t, mockEstimator(t), func(c *chainlink.Config, s *chainlink.Secrets) {
c.EVM[0].ChainID = (*ubig.Big)(testutils.SimulatedChainID)
})
Expand Down
65 changes: 32 additions & 33 deletions core/services/keystore/eth_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package keystore_test

import (
"context"
"fmt"
"math/big"
"sort"
Expand Down Expand Up @@ -44,7 +43,7 @@ func Test_EthKeyStore(t *testing.T) {
const statesTableName = "evm.key_states"

t.Run("Create / GetAll / Get", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
defer reset()
key, err := ethKeyStore.Create(ctx, &cltest.FixtureChainID)
require.NoError(t, err)
Expand Down Expand Up @@ -77,7 +76,7 @@ func Test_EthKeyStore(t *testing.T) {
})

t.Run("GetAll ordering", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
defer reset()
var keys []ethkey.KeyV2
for i := 0; i < 5; i++ {
Expand All @@ -95,7 +94,7 @@ func Test_EthKeyStore(t *testing.T) {
})

t.Run("RemoveKey", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
defer reset()
key, err := ethKeyStore.Create(ctx, &cltest.FixtureChainID)
require.NoError(t, err)
Expand All @@ -108,7 +107,7 @@ func Test_EthKeyStore(t *testing.T) {
})

t.Run("Delete removes key even if evm.txes are present", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
defer reset()
key, err := ethKeyStore.Create(ctx, &cltest.FixtureChainID)
require.NoError(t, err)
Expand All @@ -128,7 +127,7 @@ func Test_EthKeyStore(t *testing.T) {
})

t.Run("EnsureKeys / EnabledKeysForChain", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
defer reset()
err := ethKeyStore.EnsureKeys(ctx, &cltest.FixtureChainID)
assert.NoError(t, err)
Expand All @@ -148,7 +147,7 @@ func Test_EthKeyStore(t *testing.T) {
})

t.Run("EnabledKeysForChain with specified chain ID", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
defer reset()
key, err := ethKeyStore.Create(ctx, testutils.FixtureChainID)
require.NoError(t, err)
Expand All @@ -171,7 +170,7 @@ func Test_EthKeyStore(t *testing.T) {
})

t.Run("EnabledAddressesForChain with specified chain ID", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
defer reset()
key, err := ethKeyStore.Create(ctx, testutils.FixtureChainID)
require.NoError(t, err)
Expand Down Expand Up @@ -214,7 +213,7 @@ func Test_EthKeyStore(t *testing.T) {
}

func Test_EthKeyStore_GetRoundRobinAddress(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
t.Parallel()

db := pgtest.NewSqlxDB(t)
Expand All @@ -224,7 +223,7 @@ func Test_EthKeyStore_GetRoundRobinAddress(t *testing.T) {
ethKeyStore := keyStore.Eth()

t.Run("should error when no addresses", func(t *testing.T) {
ctx1 := context.Background()
ctx1 := testutils.Context(t)
_, err := ethKeyStore.GetRoundRobinAddress(ctx1, testutils.FixtureChainID)
require.Error(t, err)
})
Expand Down Expand Up @@ -339,7 +338,7 @@ func Test_EthKeyStore_GetRoundRobinAddress(t *testing.T) {
func Test_EthKeyStore_SignTx(t *testing.T) {
t.Parallel()

ctx := context.Background()
ctx := testutils.Context(t)

db := pgtest.NewSqlxDB(t)
config := configtest.NewTestGeneralConfig(t)
Expand Down Expand Up @@ -379,22 +378,22 @@ func Test_EthKeyStore_E2E(t *testing.T) {
}

t.Run("initializes with an empty state", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
defer reset()
keys, err := ks.GetAll(ctx)
require.NoError(t, err)
require.Equal(t, 0, len(keys))
})

t.Run("errors when getting non-existent ID", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
defer reset()
_, err := ks.Get(ctx, "non-existent-id")
require.Error(t, err)
})

t.Run("creates a key", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
defer reset()
key, err := ks.Create(ctx, &cltest.FixtureChainID)
require.NoError(t, err)
Expand All @@ -404,7 +403,7 @@ func Test_EthKeyStore_E2E(t *testing.T) {
})

t.Run("imports and exports a key", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
defer reset()
key, err := ks.Create(ctx, &cltest.FixtureChainID)
require.NoError(t, err)
Expand All @@ -423,7 +422,7 @@ func Test_EthKeyStore_E2E(t *testing.T) {
})

t.Run("adds an externally created key / deletes a key", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
defer reset()
newKey, err := ethkey.NewV2()
require.NoError(t, err)
Expand All @@ -443,7 +442,7 @@ func Test_EthKeyStore_E2E(t *testing.T) {
})

t.Run("imports a key exported from a v1 keystore", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
exportedKey := `{"address":"0dd359b4f22a30e44b2fd744b679971941865820","crypto":{"cipher":"aes-128-ctr","ciphertext":"b30af964a3b3f37894e599446b4cf2314bbfcd1062e6b35b620d3d20bd9965cc","cipherparams":{"iv":"58a8d75629cc1945da7cf8c24520d1dc"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"c352887e9d427d8a6a1869082619b73fac4566082a99f6e367d126f11b434f28"},"mac":"fd76a588210e0bf73d01332091e0e83a4584ee2df31eaec0e27f9a1b94f024b4"},"id":"a5ee0802-1d7b-45b6-aeb8-ea8a3351e715","version":3}`
importedKey, err := ks.Import(ctx, []byte(exportedKey), "p4SsW0rD1!@#_", &cltest.FixtureChainID)
require.NoError(t, err)
Expand All @@ -456,7 +455,7 @@ func Test_EthKeyStore_E2E(t *testing.T) {
})

t.Run("fails to export a non-existent key", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
k, err := ks.Export(ctx, "non-existent", cltest.Password)

assert.Empty(t, k)
Expand All @@ -467,7 +466,7 @@ func Test_EthKeyStore_E2E(t *testing.T) {
defer reset()

t.Run("returns states for keys", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
k1, err := ethkey.NewV2()
require.NoError(t, err)
k2, err := ethkey.NewV2()
Expand Down Expand Up @@ -495,7 +494,7 @@ func Test_EthKeyStore_E2E(t *testing.T) {
func Test_EthKeyStore_SubscribeToKeyChanges(t *testing.T) {
t.Parallel()

ctx := context.Background()
ctx := testutils.Context(t)

chDone := make(chan struct{})
defer func() { close(chDone) }()
Expand Down Expand Up @@ -573,7 +572,7 @@ func Test_EthKeyStore_Enable(t *testing.T) {
ks := keyStore.Eth()

t.Run("already existing disabled key gets enabled", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
k, _ := cltest.MustInsertRandomKeyNoChains(t, ks)
require.NoError(t, ks.Add(ctx, k.Address, testutils.SimulatedChainID))
require.NoError(t, ks.Disable(ctx, k.Address, testutils.SimulatedChainID))
Expand All @@ -584,7 +583,7 @@ func Test_EthKeyStore_Enable(t *testing.T) {
})

t.Run("creates key, deletes it unsafely and then enable creates it again", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
k, _ := cltest.MustInsertRandomKeyNoChains(t, ks)
require.NoError(t, ks.Add(ctx, k.Address, testutils.SimulatedChainID))
_, err := db.Exec("DELETE FROM evm.key_states WHERE address = $1", k.Address)
Expand All @@ -596,7 +595,7 @@ func Test_EthKeyStore_Enable(t *testing.T) {
})

t.Run("creates key and enables it if it exists in the keystore, but is missing from key states db table", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
k, _ := cltest.MustInsertRandomKeyNoChains(t, ks)
require.NoError(t, ks.Enable(ctx, k.Address, testutils.SimulatedChainID))
key, err := ks.GetState(ctx, k.Address.Hex(), testutils.SimulatedChainID)
Expand All @@ -605,7 +604,7 @@ func Test_EthKeyStore_Enable(t *testing.T) {
})

t.Run("errors if key is not present in keystore", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
addrNotInKs := testutils.NewAddress()
require.Error(t, ks.Enable(ctx, addrNotInKs, testutils.SimulatedChainID))
_, err := ks.GetState(ctx, addrNotInKs.Hex(), testutils.SimulatedChainID)
Expand All @@ -617,7 +616,7 @@ func Test_EthKeyStore_EnsureKeys(t *testing.T) {
t.Parallel()

t.Run("creates one unique key per chain if none exist", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
db := pgtest.NewSqlxDB(t)
cfg := configtest.NewTestGeneralConfig(t)
keyStore := cltest.NewKeyStore(t, db, cfg.Database())
Expand All @@ -633,7 +632,7 @@ func Test_EthKeyStore_EnsureKeys(t *testing.T) {
})

t.Run("does nothing if a key exists for a chain", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
db := pgtest.NewSqlxDB(t)
cfg := configtest.NewTestGeneralConfig(t)
keyStore := cltest.NewKeyStore(t, db, cfg.Database())
Expand All @@ -657,7 +656,7 @@ func Test_EthKeyStore_EnsureKeys(t *testing.T) {
})

t.Run("does nothing if a key exists but is disabled for a chain", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
db := pgtest.NewSqlxDB(t)
cfg := configtest.NewTestGeneralConfig(t)
keyStore := cltest.NewKeyStore(t, db, cfg.Database())
Expand Down Expand Up @@ -691,7 +690,7 @@ func Test_EthKeyStore_EnsureKeys(t *testing.T) {
func Test_EthKeyStore_Delete(t *testing.T) {
t.Parallel()

ctx := context.Background()
ctx := testutils.Context(t)

db := pgtest.NewSqlxDB(t)
cfg := configtest.NewTestGeneralConfig(t)
Expand Down Expand Up @@ -739,7 +738,7 @@ func Test_EthKeyStore_Delete(t *testing.T) {
func Test_EthKeyStore_CheckEnabled(t *testing.T) {
t.Parallel()

ctx := context.Background()
ctx := testutils.Context(t)

db := pgtest.NewSqlxDB(t)
cfg := configtest.NewTestGeneralConfig(t)
Expand Down Expand Up @@ -775,7 +774,7 @@ func Test_EthKeyStore_CheckEnabled(t *testing.T) {
require.NoError(t, ks.Enable(ctx, k3.Address, testutils.SimulatedChainID))

t.Run("enabling the same key multiple times does not create duplicate states", func(t *testing.T) {
ctx2 := context.Background()
ctx2 := testutils.Context(t)
require.NoError(t, ks.Enable(ctx2, k1.Address, testutils.FixtureChainID))
require.NoError(t, ks.Enable(ctx2, k1.Address, testutils.FixtureChainID))
require.NoError(t, ks.Enable(ctx2, k1.Address, testutils.FixtureChainID))
Expand Down Expand Up @@ -833,7 +832,7 @@ func Test_EthKeyStore_Disable(t *testing.T) {
ks := keyStore.Eth()

t.Run("creates key, deletes it unsafely and then enable creates it again", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
k, _ := cltest.MustInsertRandomKeyNoChains(t, ks)
require.NoError(t, ks.Add(ctx, k.Address, testutils.SimulatedChainID))
_, err := db.Exec("DELETE FROM evm.key_states WHERE address = $1", k.Address)
Expand All @@ -845,7 +844,7 @@ func Test_EthKeyStore_Disable(t *testing.T) {
})

t.Run("creates key and enables it if it exists in the keystore, but is missing from key states db table", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
k, _ := cltest.MustInsertRandomKeyNoChains(t, ks)
require.NoError(t, ks.Disable(ctx, k.Address, testutils.SimulatedChainID))
key, err := ks.GetState(ctx, k.Address.Hex(), testutils.SimulatedChainID)
Expand All @@ -854,7 +853,7 @@ func Test_EthKeyStore_Disable(t *testing.T) {
})

t.Run("errors if key is not present in keystore", func(t *testing.T) {
ctx := context.Background()
ctx := testutils.Context(t)
addrNotInKs := testutils.NewAddress()
require.Error(t, ks.Disable(ctx, addrNotInKs, testutils.SimulatedChainID))
_, err := ks.GetState(ctx, addrNotInKs.Hex(), testutils.SimulatedChainID)
Expand Down
3 changes: 1 addition & 2 deletions core/services/ocr2/plugins/functions/plugin_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package functions_test

import (
"context"
"math/big"
"testing"

Expand All @@ -26,7 +25,7 @@ import (
func TestNewConnector_Success(t *testing.T) {
t.Parallel()

ctx := context.Background()
ctx := testutils.Context(t)

keyV2, err := ethkey.NewV2()
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 7703bd7

Please sign in to comment.