mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-08 23:18:15 -05:00
Replace context.Background with testing.TB.Context where possible (#15416)
* Replace context.Background with testing.TB.Context where possible * Fix failing tests
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package derived
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
@@ -24,7 +23,7 @@ const (
|
||||
// We test that using a '25th word' mnemonic passphrase leads to different
|
||||
// public keys derived than not specifying the passphrase.
|
||||
func TestDerivedKeymanager_MnemonicPassphrase_DifferentResults(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
ctx := t.Context()
|
||||
wallet := &mock.Wallet{
|
||||
Files: make(map[string]map[string][]byte),
|
||||
AccountPasswords: make(map[string]string),
|
||||
@@ -84,7 +83,7 @@ func TestDerivedKeymanager_FetchValidatingPublicKeys(t *testing.T) {
|
||||
AccountPasswords: make(map[string]string),
|
||||
WalletPassword: password,
|
||||
}
|
||||
ctx := context.Background()
|
||||
ctx := t.Context()
|
||||
dr, err := NewKeymanager(ctx, &SetupConfig{
|
||||
Wallet: wallet,
|
||||
ListenForChanges: false,
|
||||
@@ -123,7 +122,7 @@ func TestDerivedKeymanager_FetchValidatingPrivateKeys(t *testing.T) {
|
||||
AccountPasswords: make(map[string]string),
|
||||
WalletPassword: password,
|
||||
}
|
||||
ctx := context.Background()
|
||||
ctx := t.Context()
|
||||
dr, err := NewKeymanager(ctx, &SetupConfig{
|
||||
Wallet: wallet,
|
||||
ListenForChanges: false,
|
||||
@@ -160,7 +159,7 @@ func TestDerivedKeymanager_Sign(t *testing.T) {
|
||||
AccountPasswords: make(map[string]string),
|
||||
WalletPassword: password,
|
||||
}
|
||||
ctx := context.Background()
|
||||
ctx := t.Context()
|
||||
dr, err := NewKeymanager(ctx, &SetupConfig{
|
||||
Wallet: wallet,
|
||||
ListenForChanges: false,
|
||||
@@ -196,7 +195,7 @@ func TestDerivedKeymanager_Sign_NoPublicKeySpecified(t *testing.T) {
|
||||
PublicKey: nil,
|
||||
}
|
||||
dr := &Keymanager{}
|
||||
_, err := dr.Sign(context.Background(), req)
|
||||
_, err := dr.Sign(t.Context(), req)
|
||||
assert.ErrorContains(t, "nil public key", err)
|
||||
}
|
||||
|
||||
@@ -205,6 +204,6 @@ func TestDerivedKeymanager_Sign_NoPublicKeyInCache(t *testing.T) {
|
||||
PublicKey: []byte("hello world"),
|
||||
}
|
||||
dr := &Keymanager{}
|
||||
_, err := dr.Sign(context.Background(), req)
|
||||
_, err := dr.Sign(t.Context(), req)
|
||||
assert.ErrorContains(t, "no signing key found", err)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package local
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"testing"
|
||||
|
||||
@@ -22,7 +21,7 @@ func TestLocalKeymanager_ExtractKeystores(t *testing.T) {
|
||||
validatingKeys[i] = secretKey
|
||||
secretKeysCache[bytesutil.ToBytes48(secretKey.PublicKey().Marshal())] = secretKey
|
||||
}
|
||||
ctx := context.Background()
|
||||
ctx := t.Context()
|
||||
password := "password"
|
||||
|
||||
// Extracting 0 public keys should return 0 keystores.
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package local
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
@@ -27,7 +26,7 @@ func TestLocalKeymanager_DeleteKeystores(t *testing.T) {
|
||||
accountsStore: &accountStore{},
|
||||
}
|
||||
numAccounts := 5
|
||||
ctx := context.Background()
|
||||
ctx := t.Context()
|
||||
keystores := make([]*keymanager.Keystore, numAccounts)
|
||||
passwords := make([]string, numAccounts)
|
||||
for i := 0; i < numAccounts; i++ {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package local
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"testing"
|
||||
@@ -54,7 +53,7 @@ func TestLocalKeymanager_NoDuplicates(t *testing.T) {
|
||||
dr := &Keymanager{
|
||||
wallet: wallet,
|
||||
}
|
||||
ctx := context.Background()
|
||||
ctx := t.Context()
|
||||
_, err := dr.CreateAccountsKeystore(ctx, privKeys, pubKeys)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -97,7 +96,7 @@ func TestLocalKeymanager_NoDuplicates(t *testing.T) {
|
||||
|
||||
func TestLocalKeymanager_ImportKeystores(t *testing.T) {
|
||||
hook := logTest.NewGlobal()
|
||||
ctx := context.Background()
|
||||
ctx := t.Context()
|
||||
// Setup the keymanager.
|
||||
wallet := &mock.Wallet{
|
||||
Files: make(map[string]map[string][]byte),
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package local
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -27,7 +26,7 @@ func TestLocalKeymanager_FetchValidatingPublicKeys(t *testing.T) {
|
||||
accountsStore: &accountStore{},
|
||||
}
|
||||
// First, generate accounts and their keystore.json files.
|
||||
ctx := context.Background()
|
||||
ctx := t.Context()
|
||||
numAccounts := 10
|
||||
wantedPubKeys := make([][fieldparams.BLSPubkeyLength]byte, 0)
|
||||
for i := 0; i < numAccounts; i++ {
|
||||
@@ -59,7 +58,7 @@ func TestLocalKeymanager_FetchValidatingPrivateKeys(t *testing.T) {
|
||||
accountsStore: &accountStore{},
|
||||
}
|
||||
// First, generate accounts and their keystore.json files.
|
||||
ctx := context.Background()
|
||||
ctx := t.Context()
|
||||
numAccounts := 10
|
||||
wantedPrivateKeys := make([][32]byte, numAccounts)
|
||||
for i := 0; i < numAccounts; i++ {
|
||||
@@ -94,7 +93,7 @@ func TestLocalKeymanager_Sign(t *testing.T) {
|
||||
}
|
||||
|
||||
// First, generate accounts and their keystore.json files.
|
||||
ctx := context.Background()
|
||||
ctx := t.Context()
|
||||
numAccounts := 10
|
||||
keystores := make([]*keymanager.Keystore, numAccounts)
|
||||
passwords := make([]string, numAccounts)
|
||||
@@ -155,7 +154,7 @@ func TestLocalKeymanager_Sign_NoPublicKeySpecified(t *testing.T) {
|
||||
PublicKey: nil,
|
||||
}
|
||||
dr := &Keymanager{}
|
||||
_, err := dr.Sign(context.Background(), req)
|
||||
_, err := dr.Sign(t.Context(), req)
|
||||
assert.ErrorContains(t, "nil public key", err)
|
||||
}
|
||||
|
||||
@@ -165,6 +164,6 @@ func TestLocalKeymanager_Sign_NoPublicKeyInCache(t *testing.T) {
|
||||
}
|
||||
secretKeysCache = make(map[[fieldparams.BLSPubkeyLength]byte]bls.SecretKey)
|
||||
dr := &Keymanager{}
|
||||
_, err := dr.Sign(context.Background(), req)
|
||||
_, err := dr.Sign(t.Context(), req)
|
||||
assert.ErrorContains(t, "no signing key found in keys cache", err)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package local
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
@@ -68,7 +67,7 @@ func TestLocalKeymanager_reloadAccountsFromKeystore(t *testing.T) {
|
||||
pubKeys[i] = privKey.PublicKey().Marshal()
|
||||
}
|
||||
|
||||
accountsStore, err := dr.CreateAccountsKeystore(context.Background(), privKeys, pubKeys)
|
||||
accountsStore, err := dr.CreateAccountsKeystore(t.Context(), privKeys, pubKeys)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, dr.reloadAccountsFromKeystore(accountsStore))
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ package internal_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -45,7 +44,7 @@ func TestClient_Sign_HappyPath(t *testing.T) {
|
||||
cl := internal.ApiClient{BaseURL: u, RestClient: &http.Client{Transport: mock}}
|
||||
jsonRequest, err := json.Marshal(`{message: "hello"}`)
|
||||
assert.NoError(t, err)
|
||||
resp, err := cl.Sign(context.Background(), "a2b5aaad9c6efefe7bb9b1243a043404f3362937cfb6b31833929833173f476630ea2cfeb0d9ddf15f97ca8685948820", jsonRequest)
|
||||
resp, err := cl.Sign(t.Context(), "a2b5aaad9c6efefe7bb9b1243a043404f3362937cfb6b31833929833173f476630ea2cfeb0d9ddf15f97ca8685948820", jsonRequest)
|
||||
assert.NotNil(t, resp)
|
||||
assert.Nil(t, err)
|
||||
assert.EqualValues(t, "0xb3baa751d0a9132cfe93e4e3d5ff9075111100e3789dca219ade5a24d27e19d16b3353149da1833e9b691bb38634e8dc04469be7032132906c927d7e1a49b414730612877bc6b2810c8f202daf793d1ab0d6b5cb21d52f9e52e883859887a5d9", fmt.Sprintf("%#x", resp.Marshal()))
|
||||
@@ -74,7 +73,7 @@ func TestClient_Sign_HappyPath_Jsontype(t *testing.T) {
|
||||
cl := internal.ApiClient{BaseURL: u, RestClient: &http.Client{Transport: mock}}
|
||||
jsonRequest, err := json.Marshal(`{message: "hello"}`)
|
||||
assert.NoError(t, err)
|
||||
resp, err := cl.Sign(context.Background(), "a2b5aaad9c6efefe7bb9b1243a043404f3362937cfb6b31833929833173f476630ea2cfeb0d9ddf15f97ca8685948820", jsonRequest)
|
||||
resp, err := cl.Sign(t.Context(), "a2b5aaad9c6efefe7bb9b1243a043404f3362937cfb6b31833929833173f476630ea2cfeb0d9ddf15f97ca8685948820", jsonRequest)
|
||||
assert.NotNil(t, resp)
|
||||
assert.Nil(t, err)
|
||||
assert.EqualValues(t, "0xb3baa751d0a9132cfe93e4e3d5ff9075111100e3789dca219ade5a24d27e19d16b3353149da1833e9b691bb38634e8dc04469be7032132906c927d7e1a49b414730612877bc6b2810c8f202daf793d1ab0d6b5cb21d52f9e52e883859887a5d9", fmt.Sprintf("%#x", resp.Marshal()))
|
||||
@@ -93,7 +92,7 @@ func TestClient_Sign_500(t *testing.T) {
|
||||
cl := internal.ApiClient{BaseURL: u, RestClient: &http.Client{Transport: mock}}
|
||||
jsonRequest, err := json.Marshal(`{message: "hello"}`)
|
||||
assert.NoError(t, err)
|
||||
resp, err := cl.Sign(context.Background(), "a2b5aaad9c6efefe7bb9b1243a043404f3362937cfb6b31833929833173f476630ea2cfeb0d9ddf15f97ca8685948820", jsonRequest)
|
||||
resp, err := cl.Sign(t.Context(), "a2b5aaad9c6efefe7bb9b1243a043404f3362937cfb6b31833929833173f476630ea2cfeb0d9ddf15f97ca8685948820", jsonRequest)
|
||||
assert.NotNil(t, err)
|
||||
assert.Nil(t, resp)
|
||||
|
||||
@@ -112,7 +111,7 @@ func TestClient_Sign_412(t *testing.T) {
|
||||
cl := internal.ApiClient{BaseURL: u, RestClient: &http.Client{Transport: mock}}
|
||||
jsonRequest, err := json.Marshal(`{message: "hello"}`)
|
||||
assert.NoError(t, err)
|
||||
resp, err := cl.Sign(context.Background(), "a2b5aaad9c6efefe7bb9b1243a043404f3362937cfb6b31833929833173f476630ea2cfeb0d9ddf15f97ca8685948820", jsonRequest)
|
||||
resp, err := cl.Sign(t.Context(), "a2b5aaad9c6efefe7bb9b1243a043404f3362937cfb6b31833929833173f476630ea2cfeb0d9ddf15f97ca8685948820", jsonRequest)
|
||||
assert.NotNil(t, err)
|
||||
assert.Nil(t, resp)
|
||||
|
||||
@@ -131,7 +130,7 @@ func TestClient_Sign_400(t *testing.T) {
|
||||
cl := internal.ApiClient{BaseURL: u, RestClient: &http.Client{Transport: mock}}
|
||||
jsonRequest, err := json.Marshal(`{message: "hello"}`)
|
||||
assert.NoError(t, err)
|
||||
resp, err := cl.Sign(context.Background(), "a2b5aaad9c6efefe7bb9b1243a043404f3362937cfb6b31833929833173f476630ea2cfeb0d9ddf15f97ca8685948820", jsonRequest)
|
||||
resp, err := cl.Sign(t.Context(), "a2b5aaad9c6efefe7bb9b1243a043404f3362937cfb6b31833929833173f476630ea2cfeb0d9ddf15f97ca8685948820", jsonRequest)
|
||||
assert.NotNil(t, err)
|
||||
assert.Nil(t, resp)
|
||||
|
||||
@@ -149,7 +148,7 @@ func TestClient_GetPublicKeys_HappyPath(t *testing.T) {
|
||||
u, err := url.Parse("example.com")
|
||||
assert.NoError(t, err)
|
||||
cl := internal.ApiClient{BaseURL: u, RestClient: &http.Client{Transport: mock}}
|
||||
resp, err := cl.GetPublicKeys(context.Background(), "example.com/api/publickeys")
|
||||
resp, err := cl.GetPublicKeys(t.Context(), "example.com/api/publickeys")
|
||||
assert.NotNil(t, resp)
|
||||
assert.Nil(t, err)
|
||||
// we would like them as 48byte base64 without 0x
|
||||
@@ -165,7 +164,7 @@ func TestClient_ReloadSignerKeys_HappyPath(t *testing.T) {
|
||||
u, err := url.Parse("example.com")
|
||||
assert.NoError(t, err)
|
||||
cl := internal.ApiClient{BaseURL: u, RestClient: &http.Client{Transport: mock}}
|
||||
err = cl.ReloadSignerKeys(context.Background())
|
||||
err = cl.ReloadSignerKeys(t.Context())
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
@@ -180,7 +179,7 @@ func TestClient_GetServerStatus_HappyPath(t *testing.T) {
|
||||
u, err := url.Parse("example.com")
|
||||
assert.NoError(t, err)
|
||||
cl := internal.ApiClient{BaseURL: u, RestClient: &http.Client{Transport: mock}}
|
||||
resp, err := cl.GetServerStatus(context.Background())
|
||||
resp, err := cl.GetServerStatus(t.Context())
|
||||
assert.NotNil(t, resp)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
@@ -142,7 +142,7 @@ func TestNewKeymanager(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
logHook := logTest.NewGlobal()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
if tt.args.KeyFilePath != "" && len(tt.fileContents) != 0 {
|
||||
bytesBuf := new(bytes.Buffer)
|
||||
@@ -187,7 +187,7 @@ func TestNewKeyManager_fileMissing(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNewKeyManager_ChangingFileCreated(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
|
||||
keyFilePath := filepath.Join(t.TempDir(), "keyfile.txt")
|
||||
@@ -242,7 +242,7 @@ func TestNewKeyManager_ChangingFileCreated(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNewKeyManager_FileAndFlagsWithDifferentKeys(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
logHook := logTest.NewGlobal()
|
||||
keyFilePath := filepath.Join(t.TempDir(), "keyfile.txt")
|
||||
@@ -288,7 +288,7 @@ func TestNewKeyManager_FileAndFlagsWithDifferentKeys(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRefreshRemoteKeysFromFileChangesWithRetry(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
logHook := logTest.NewGlobal()
|
||||
root, err := hexutil.Decode("0x270d43e74ce340de4bca2b1936beca0f4f5408d9e78aec4850920baf659d5b69")
|
||||
require.NoError(t, err)
|
||||
@@ -338,7 +338,7 @@ func TestReadKeyFile_PathMissing(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRefreshRemoteKeysFromFileChangesWithRetry_maxRetryReached(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
ctx := t.Context()
|
||||
root, err := hexutil.Decode("0x270d43e74ce340de4bca2b1936beca0f4f5408d9e78aec4850920baf659d5b69")
|
||||
require.NoError(t, err)
|
||||
keyFilePath := filepath.Join(t.TempDir(), "keyfile.txt")
|
||||
@@ -359,7 +359,7 @@ func TestKeymanager_Sign(t *testing.T) {
|
||||
client := &MockClient{
|
||||
Signature: "0xb3baa751d0a9132cfe93e4e3d5ff9075111100e3789dca219ade5a24d27e19d16b3353149da1833e9b691bb38634e8dc04469be7032132906c927d7e1a49b414730612877bc6b2810c8f202daf793d1ab0d6b5cb21d52f9e52e883859887a5d9",
|
||||
}
|
||||
ctx := context.Background()
|
||||
ctx := t.Context()
|
||||
root, err := hexutil.Decode("0x270d43e74ce340de4bca2b1936beca0f4f5408d9e78aec4850920baf659d5b69")
|
||||
if err != nil {
|
||||
fmt.Printf("error: %v", err)
|
||||
@@ -502,7 +502,7 @@ func TestKeymanager_Sign(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestKeymanager_FetchValidatingPublicKeys_HappyPath_WithKeyList(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
ctx := t.Context()
|
||||
decodedKey, err := hexutil.Decode("0xa2b5aaad9c6efefe7bb9b1243a043404f3362937cfb6b31833929833173f476630ea2cfeb0d9ddf15f97ca8685948820")
|
||||
require.NoError(t, err)
|
||||
keys := [][48]byte{
|
||||
@@ -529,7 +529,7 @@ func TestKeymanager_FetchValidatingPublicKeys_HappyPath_WithKeyList(t *testing.T
|
||||
}
|
||||
|
||||
func TestKeymanager_FetchValidatingPublicKeys_HappyPath_WithExternalURL(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
ctx := t.Context()
|
||||
decodedKey, err := hexutil.Decode("0xa2b5aaad9c6efefe7bb9b1243a043404f3362937cfb6b31833929833173f476630ea2cfeb0d9ddf15f97ca8685948820")
|
||||
if err != nil {
|
||||
fmt.Printf("error: %v", err)
|
||||
@@ -564,7 +564,7 @@ func TestKeymanager_FetchValidatingPublicKeys_HappyPath_WithExternalURL(t *testi
|
||||
}
|
||||
|
||||
func TestKeymanager_FetchValidatingPublicKeys_WithExternalURL_ThrowsError(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
ctx := t.Context()
|
||||
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
@@ -587,7 +587,7 @@ func TestKeymanager_FetchValidatingPublicKeys_WithExternalURL_ThrowsError(t *tes
|
||||
}
|
||||
|
||||
func TestKeymanager_AddPublicKeys(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
ctx := t.Context()
|
||||
root, err := hexutil.Decode("0x270d43e74ce340de4bca2b1936beca0f4f5408d9e78aec4850920baf659d5b69")
|
||||
if err != nil {
|
||||
fmt.Printf("error: %v", err)
|
||||
@@ -614,7 +614,7 @@ func TestKeymanager_AddPublicKeys(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestKeymanager_AddPublicKeys_WithFile(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
dir := t.TempDir()
|
||||
stdOutFile, err := os.Create(filepath.Clean(path.Join(dir, "keyfile.txt")))
|
||||
@@ -652,7 +652,7 @@ func TestKeymanager_AddPublicKeys_WithFile(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestKeymanager_DeletePublicKeys(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
ctx := t.Context()
|
||||
root, err := hexutil.Decode("0x270d43e74ce340de4bca2b1936beca0f4f5408d9e78aec4850920baf659d5b69")
|
||||
if err != nil {
|
||||
fmt.Printf("error: %v", err)
|
||||
@@ -686,7 +686,7 @@ func TestKeymanager_DeletePublicKeys(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestKeymanager_DeletePublicKeys_WithFile(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
dir := t.TempDir()
|
||||
stdOutFile, err := os.Create(filepath.Clean(path.Join(dir, "keyfile.txt")))
|
||||
|
||||
Reference in New Issue
Block a user