macaroons: remove context.TODO() in tests

We want `context.TODO()` to be high signal in the code-base. It should
signal clearly that work is required to thread parent context through to
the call-site. So to keep the signal-to-noise ratio high, we remove any
context.TODO() calls from tests since these will never need to be
replace by a parent context.
This commit is contained in:
Elle Mouton
2025-04-07 10:47:14 +02:00
parent c52a6ddeb8
commit dd5c6d16e3
2 changed files with 17 additions and 11 deletions

View File

@ -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")
@ -136,7 +138,7 @@ func TestValidateMacaroon(t *testing.T) {
md := metadata.New(map[string]string{
"macaroon": hex.EncodeToString(macaroonBinary),
})
mockContext := metadata.NewIncomingContext(context.Background(), md)
mockContext := metadata.NewIncomingContext(ctx, md)
// Finally, validate the macaroon against the required permissions.
err = service.ValidateMacaroon(
@ -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")
}

View File

@ -58,12 +58,15 @@ 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) {
t.Parallel()
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 +75,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)