From 2e534a6af7a8d6704cbe82b728959e365fb02bc2 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Mon, 11 Nov 2024 11:46:08 +0200 Subject: [PATCH] remove context.TODOs from tests --- kvdb/etcd/db_test.go | 2 +- kvdb/etcd/readwrite_tx_test.go | 4 ++-- kvdb/etcd/walletdb_interface_test.go | 2 +- macaroons/service_test.go | 13 ++++++++----- macaroons/store_test.go | 11 ++++++----- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/kvdb/etcd/db_test.go b/kvdb/etcd/db_test.go index 59e29fc94ad..9ef68d9fe58 100644 --- a/kvdb/etcd/db_test.go +++ b/kvdb/etcd/db_test.go @@ -19,7 +19,7 @@ func TestDump(t *testing.T) { f := NewEtcdTestFixture(t) - db, err := newEtcdBackend(context.TODO(), f.BackendConfig()) + db, err := newEtcdBackend(context.Background(), f.BackendConfig()) require.NoError(t, err) err = db.Update(func(tx walletdb.ReadWriteTx) error { diff --git a/kvdb/etcd/readwrite_tx_test.go b/kvdb/etcd/readwrite_tx_test.go index b5758f7a3f7..d66b2e25127 100644 --- a/kvdb/etcd/readwrite_tx_test.go +++ b/kvdb/etcd/readwrite_tx_test.go @@ -16,7 +16,7 @@ func TestChangeDuringManualTx(t *testing.T) { f := NewEtcdTestFixture(t) - db, err := newEtcdBackend(context.TODO(), f.BackendConfig()) + db, err := newEtcdBackend(context.Background(), f.BackendConfig()) require.NoError(t, err) tx, err := db.BeginReadWriteTx() @@ -44,7 +44,7 @@ func TestChangeDuringUpdate(t *testing.T) { f := NewEtcdTestFixture(t) - db, err := newEtcdBackend(context.TODO(), f.BackendConfig()) + db, err := newEtcdBackend(context.Background(), f.BackendConfig()) require.NoError(t, err) count := 0 diff --git a/kvdb/etcd/walletdb_interface_test.go b/kvdb/etcd/walletdb_interface_test.go index 13c57e337d2..483becbb2fa 100644 --- a/kvdb/etcd/walletdb_interface_test.go +++ b/kvdb/etcd/walletdb_interface_test.go @@ -15,5 +15,5 @@ import ( func TestWalletDBInterface(t *testing.T) { f := NewEtcdTestFixture(t) cfg := f.BackendConfig() - walletdbtest.TestInterface(t, dbType, context.TODO(), &cfg) + walletdbtest.TestInterface(t, dbType, context.Background(), &cfg) } diff --git a/macaroons/service_test.go b/macaroons/service_test.go index aad8af8db05..e28a03589c8 100644 --- a/macaroons/service_test.go +++ b/macaroons/service_test.go @@ -55,6 +55,7 @@ func setupTestRootKeyStorage(t *testing.T) kvdb.Backend { // TestNewService tests the creation of the macaroon service. func TestNewService(t *testing.T) { t.Parallel() + ctx := context.Background() // First, initialize a dummy DB file with a store that the service // can read from. Make sure the file is removed in the end. @@ -74,13 +75,13 @@ func TestNewService(t *testing.T) { require.NoError(t, err, "Error unlocking root key storage") // Third, check if the created service can bake macaroons. - _, err = service.NewMacaroon(context.TODO(), nil, testOperation) + _, err = service.NewMacaroon(ctx, nil, testOperation) if err != macaroons.ErrMissingRootKeyID { t.Fatalf("Received %v instead of ErrMissingRootKeyID", err) } macaroon, err := service.NewMacaroon( - context.TODO(), macaroons.DefaultRootKeyID, testOperation, + ctx, macaroons.DefaultRootKeyID, testOperation, ) require.NoError(t, err, "Error creating macaroon from service") if macaroon.Namespace().String() != "std:" { @@ -108,6 +109,7 @@ func TestNewService(t *testing.T) { // incoming context. func TestValidateMacaroon(t *testing.T) { t.Parallel() + ctx := context.Background() // First, initialize the service and unlock it. db := setupTestRootKeyStorage(t) @@ -124,7 +126,7 @@ func TestValidateMacaroon(t *testing.T) { // Then, create a new macaroon that we can serialize. macaroon, err := service.NewMacaroon( - context.TODO(), macaroons.DefaultRootKeyID, testOperation, + ctx, macaroons.DefaultRootKeyID, testOperation, testOperationURI, ) require.NoError(t, err, "Error creating macaroon from service") @@ -155,6 +157,7 @@ func TestValidateMacaroon(t *testing.T) { // TestListMacaroonIDs checks that ListMacaroonIDs returns the expected result. func TestListMacaroonIDs(t *testing.T) { t.Parallel() + ctx := context.Background() // First, initialize a dummy DB file with a store that the service // can read from. Make sure the file is removed in the end. @@ -176,12 +179,12 @@ func TestListMacaroonIDs(t *testing.T) { // Third, make 3 new macaroons with different root key IDs. expectedIDs := [][]byte{{1}, {2}, {3}} for _, v := range expectedIDs { - _, err := service.NewMacaroon(context.TODO(), v, testOperation) + _, err := service.NewMacaroon(ctx, v, testOperation) require.NoError(t, err, "Error creating macaroon from service") } // Finally, check that calling List return the expected values. - ids, _ := service.ListMacaroonIDs(context.TODO()) + ids, _ := service.ListMacaroonIDs(ctx) require.Equal(t, expectedIDs, ids, "root key IDs mismatch") } diff --git a/macaroons/store_test.go b/macaroons/store_test.go index ace1e764a1f..2246f632227 100644 --- a/macaroons/store_test.go +++ b/macaroons/store_test.go @@ -58,12 +58,13 @@ func openTestStore(t *testing.T, tempDir string) *macaroons.RootKeyStorage { // TestStore tests the normal use cases of the store like creating, unlocking, // reading keys and closing it. func TestStore(t *testing.T) { + ctx := context.Background() tempDir, store := newTestStore(t) - _, _, err := store.RootKey(context.TODO()) + _, _, err := store.RootKey(ctx) require.Equal(t, macaroons.ErrStoreLocked, err) - _, err = store.Get(context.TODO(), nil) + _, err = store.Get(ctx, nil) require.Equal(t, macaroons.ErrStoreLocked, err) pw := []byte("weks") @@ -72,18 +73,18 @@ func TestStore(t *testing.T) { // Check ErrContextRootKeyID is returned when no root key ID found in // context. - _, _, err = store.RootKey(context.TODO()) + _, _, err = store.RootKey(ctx) require.Equal(t, macaroons.ErrContextRootKeyID, err) // Check ErrMissingRootKeyID is returned when empty root key ID is used. emptyKeyID := make([]byte, 0) - badCtx := macaroons.ContextWithRootKeyID(context.TODO(), emptyKeyID) + badCtx := macaroons.ContextWithRootKeyID(ctx, emptyKeyID) _, _, err = store.RootKey(badCtx) require.Equal(t, macaroons.ErrMissingRootKeyID, err) // Create a context with illegal root key ID value. encryptedKeyID := []byte("enckey") - badCtx = macaroons.ContextWithRootKeyID(context.TODO(), encryptedKeyID) + badCtx = macaroons.ContextWithRootKeyID(ctx, encryptedKeyID) _, _, err = store.RootKey(badCtx) require.Equal(t, macaroons.ErrKeyValueForbidden, err)