macaroons: drop unused variables in tests

macaroons: defer service.Close() after error check in tests

macaroons: linter requires that nil contexts are changed to context.TODO()
This commit is contained in:
Lars Lehtonen 2019-09-23 14:34:58 +00:00
parent 20a5ee2f1e
commit 3587325438
No known key found for this signature in database
GPG Key ID: 8137D474EBCB04F2
2 changed files with 18 additions and 14 deletions

@ -61,18 +61,19 @@ func TestNewService(t *testing.T) {
// Second, create the new service instance, unlock it and pass in a // Second, create the new service instance, unlock it and pass in a
// checker that we expect it to add to the bakery. // checker that we expect it to add to the bakery.
service, err := macaroons.NewService(tempDir, macaroons.IPLockChecker) service, err := macaroons.NewService(tempDir, macaroons.IPLockChecker)
defer service.Close()
if err != nil { if err != nil {
t.Fatalf("Error creating new service: %v", err) t.Fatalf("Error creating new service: %v", err)
} }
defer service.Close()
err = service.CreateUnlock(&defaultPw) err = service.CreateUnlock(&defaultPw)
if err != nil { if err != nil {
t.Fatalf("Error unlocking root key storage: %v", err) t.Fatalf("Error unlocking root key storage: %v", err)
} }
// Third, check if the created service can bake macaroons. // Third, check if the created service can bake macaroons.
macaroon, err := service.Oven.NewMacaroon(nil, bakery.LatestVersion, macaroon, err := service.Oven.NewMacaroon(
nil, testOperation) context.TODO(), bakery.LatestVersion, nil, testOperation,
)
if err != nil { if err != nil {
t.Fatalf("Error creating macaroon from service: %v", err) t.Fatalf("Error creating macaroon from service: %v", err)
} }
@ -104,18 +105,20 @@ func TestValidateMacaroon(t *testing.T) {
tempDir := setupTestRootKeyStorage(t) tempDir := setupTestRootKeyStorage(t)
defer os.RemoveAll(tempDir) defer os.RemoveAll(tempDir)
service, err := macaroons.NewService(tempDir, macaroons.IPLockChecker) service, err := macaroons.NewService(tempDir, macaroons.IPLockChecker)
defer service.Close()
if err != nil { if err != nil {
t.Fatalf("Error creating new service: %v", err) t.Fatalf("Error creating new service: %v", err)
} }
defer service.Close()
err = service.CreateUnlock(&defaultPw) err = service.CreateUnlock(&defaultPw)
if err != nil { if err != nil {
t.Fatalf("Error unlocking root key storage: %v", err) t.Fatalf("Error unlocking root key storage: %v", err)
} }
// Then, create a new macaroon that we can serialize. // Then, create a new macaroon that we can serialize.
macaroon, err := service.Oven.NewMacaroon(nil, bakery.LatestVersion, macaroon, err := service.Oven.NewMacaroon(
nil, testOperation) context.TODO(), bakery.LatestVersion, nil, testOperation,
)
if err != nil { if err != nil {
t.Fatalf("Error creating macaroon from service: %v", err) t.Fatalf("Error creating macaroon from service: %v", err)
} }

@ -2,6 +2,7 @@ package macaroons_test
import ( import (
"bytes" "bytes"
"context"
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
@ -34,12 +35,12 @@ func TestStore(t *testing.T) {
} }
defer store.Close() defer store.Close()
key, id, err := store.RootKey(nil) _, _, err = store.RootKey(context.TODO())
if err != macaroons.ErrStoreLocked { if err != macaroons.ErrStoreLocked {
t.Fatalf("Received %v instead of ErrStoreLocked", err) t.Fatalf("Received %v instead of ErrStoreLocked", err)
} }
key, err = store.Get(nil, nil) _, err = store.Get(context.TODO(), nil)
if err != macaroons.ErrStoreLocked { if err != macaroons.ErrStoreLocked {
t.Fatalf("Received %v instead of ErrStoreLocked", err) t.Fatalf("Received %v instead of ErrStoreLocked", err)
} }
@ -50,13 +51,13 @@ func TestStore(t *testing.T) {
t.Fatalf("Error creating store encryption key: %v", err) t.Fatalf("Error creating store encryption key: %v", err)
} }
key, id, err = store.RootKey(nil) key, id, err := store.RootKey(context.TODO())
if err != nil { if err != nil {
t.Fatalf("Error getting root key from store: %v", err) t.Fatalf("Error getting root key from store: %v", err)
} }
rootID := id rootID := id
key2, err := store.Get(nil, id) key2, err := store.Get(context.TODO(), id)
if err != nil { if err != nil {
t.Fatalf("Error getting key with ID %s: %v", string(id), err) t.Fatalf("Error getting key with ID %s: %v", string(id), err)
} }
@ -97,12 +98,12 @@ func TestStore(t *testing.T) {
t.Fatalf("Received %v instead of ErrPasswordRequired", err) t.Fatalf("Received %v instead of ErrPasswordRequired", err)
} }
key, id, err = store.RootKey(nil) _, _, err = store.RootKey(context.TODO())
if err != macaroons.ErrStoreLocked { if err != macaroons.ErrStoreLocked {
t.Fatalf("Received %v instead of ErrStoreLocked", err) t.Fatalf("Received %v instead of ErrStoreLocked", err)
} }
key, err = store.Get(nil, nil) _, err = store.Get(context.TODO(), nil)
if err != macaroons.ErrStoreLocked { if err != macaroons.ErrStoreLocked {
t.Fatalf("Received %v instead of ErrStoreLocked", err) t.Fatalf("Received %v instead of ErrStoreLocked", err)
} }
@ -112,7 +113,7 @@ func TestStore(t *testing.T) {
t.Fatalf("Error unlocking root key store: %v", err) t.Fatalf("Error unlocking root key store: %v", err)
} }
key, err = store.Get(nil, rootID) key, err = store.Get(context.TODO(), rootID)
if err != nil { if err != nil {
t.Fatalf("Error getting key with ID %s: %v", t.Fatalf("Error getting key with ID %s: %v",
string(rootID), err) string(rootID), err)
@ -122,7 +123,7 @@ func TestStore(t *testing.T) {
key2, key) key2, key)
} }
key, id, err = store.RootKey(nil) key, id, err = store.RootKey(context.TODO())
if err != nil { if err != nil {
t.Fatalf("Error getting root key from store: %v", err) t.Fatalf("Error getting root key from store: %v", err)
} }