Skip to content

Commit

Permalink
remove context.TODOs from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ellemouton committed Nov 28, 2024
1 parent c5cc6f1 commit 791ac91
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion kvdb/etcd/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions kvdb/etcd/readwrite_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion kvdb/etcd/walletdb_interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
13 changes: 8 additions & 5 deletions macaroons/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:" {
Expand Down Expand Up @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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.
Expand All @@ -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")
}

Expand Down
11 changes: 6 additions & 5 deletions macaroons/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)

Expand Down

0 comments on commit 791ac91

Please sign in to comment.