macarooons: use T.TempDir to create temporary test directory

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun 2022-08-15 21:07:45 +08:00
parent 9acd53a5de
commit f3dec8fbb0
No known key found for this signature in database
GPG Key ID: DAEBBD2E34C111E6
2 changed files with 21 additions and 42 deletions

View File

@ -3,8 +3,6 @@ package macaroons_test
import ( import (
"context" "context"
"encoding/hex" "encoding/hex"
"io/ioutil"
"os"
"path" "path"
"testing" "testing"
@ -33,11 +31,9 @@ var (
// default password of 'hello'. Only the path to the temporary // default password of 'hello'. Only the path to the temporary
// DB file is returned, because the service will open the file // DB file is returned, because the service will open the file
// and read the store on its own. // and read the store on its own.
func setupTestRootKeyStorage(t *testing.T) (string, kvdb.Backend) { func setupTestRootKeyStorage(t *testing.T) kvdb.Backend {
tempDir, err := ioutil.TempDir("", "macaroonstore-")
require.NoError(t, err, "Error creating temp dir")
db, err := kvdb.Create( db, err := kvdb.Create(
kvdb.BoltBackendName, path.Join(tempDir, "macaroons.db"), true, kvdb.BoltBackendName, path.Join(t.TempDir(), "macaroons.db"), true,
kvdb.DefaultDBTimeout, kvdb.DefaultDBTimeout,
) )
require.NoError(t, err, "Error opening store DB") require.NoError(t, err, "Error opening store DB")
@ -49,15 +45,14 @@ func setupTestRootKeyStorage(t *testing.T) (string, kvdb.Backend) {
defer store.Close() defer store.Close()
err = store.CreateUnlock(&defaultPw) err = store.CreateUnlock(&defaultPw)
require.NoError(t, err, "error creating unlock") require.NoError(t, err, "error creating unlock")
return tempDir, db return db
} }
// TestNewService tests the creation of the macaroon service. // TestNewService tests the creation of the macaroon service.
func TestNewService(t *testing.T) { func TestNewService(t *testing.T) {
// First, initialize a dummy DB file with a store that the service // First, initialize a dummy DB file with a store that the service
// can read from. Make sure the file is removed in the end. // can read from. Make sure the file is removed in the end.
tempDir, db := setupTestRootKeyStorage(t) db := setupTestRootKeyStorage(t)
defer os.RemoveAll(tempDir)
rootKeyStore, err := macaroons.NewRootKeyStorage(db) rootKeyStore, err := macaroons.NewRootKeyStorage(db)
require.NoError(t, err) require.NoError(t, err)
@ -107,8 +102,7 @@ func TestNewService(t *testing.T) {
// incoming context. // incoming context.
func TestValidateMacaroon(t *testing.T) { func TestValidateMacaroon(t *testing.T) {
// First, initialize the service and unlock it. // First, initialize the service and unlock it.
tempDir, db := setupTestRootKeyStorage(t) db := setupTestRootKeyStorage(t)
defer os.RemoveAll(tempDir)
rootKeyStore, err := macaroons.NewRootKeyStorage(db) rootKeyStore, err := macaroons.NewRootKeyStorage(db)
require.NoError(t, err) require.NoError(t, err)
service, err := macaroons.NewService( service, err := macaroons.NewService(
@ -154,8 +148,7 @@ func TestValidateMacaroon(t *testing.T) {
func TestListMacaroonIDs(t *testing.T) { func TestListMacaroonIDs(t *testing.T) {
// First, initialize a dummy DB file with a store that the service // First, initialize a dummy DB file with a store that the service
// can read from. Make sure the file is removed in the end. // can read from. Make sure the file is removed in the end.
tempDir, db := setupTestRootKeyStorage(t) db := setupTestRootKeyStorage(t)
defer os.RemoveAll(tempDir)
// 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.
@ -188,8 +181,7 @@ func TestDeleteMacaroonID(t *testing.T) {
// First, initialize a dummy DB file with a store that the service // First, initialize a dummy DB file with a store that the service
// can read from. Make sure the file is removed in the end. // can read from. Make sure the file is removed in the end.
tempDir, db := setupTestRootKeyStorage(t) db := setupTestRootKeyStorage(t)
defer os.RemoveAll(tempDir)
// 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.

View File

@ -3,8 +3,6 @@ package macaroons_test
import ( import (
"context" "context"
"crypto/rand" "crypto/rand"
"io/ioutil"
"os"
"path" "path"
"testing" "testing"
@ -22,24 +20,17 @@ var (
// newTestStore creates a new bolt DB in a temporary directory and then // newTestStore creates a new bolt DB in a temporary directory and then
// initializes a root key storage for that DB. // initializes a root key storage for that DB.
func newTestStore(t *testing.T) (string, func(), *macaroons.RootKeyStorage) { func newTestStore(t *testing.T) (string, *macaroons.RootKeyStorage) {
tempDir, err := ioutil.TempDir("", "macaroonstore-") tempDir := t.TempDir()
require.NoError(t, err)
cleanup, store := openTestStore(t, tempDir) store := openTestStore(t, tempDir)
cleanup2 := func() {
cleanup()
_ = os.RemoveAll(tempDir)
}
return tempDir, cleanup2, store return tempDir, store
} }
// openTestStore opens an existing bolt DB and then initializes a root key // openTestStore opens an existing bolt DB and then initializes a root key
// storage for that DB. // storage for that DB.
func openTestStore(t *testing.T, tempDir string) (func(), func openTestStore(t *testing.T, tempDir string) *macaroons.RootKeyStorage {
*macaroons.RootKeyStorage) {
db, err := kvdb.Create( db, err := kvdb.Create(
kvdb.BoltBackendName, path.Join(tempDir, "weks.db"), true, kvdb.BoltBackendName, path.Join(tempDir, "weks.db"), true,
kvdb.DefaultDBTimeout, kvdb.DefaultDBTimeout,
@ -52,19 +43,18 @@ func openTestStore(t *testing.T, tempDir string) (func(),
t.Fatalf("Error creating root key store: %v", err) t.Fatalf("Error creating root key store: %v", err)
} }
cleanup := func() { t.Cleanup(func() {
_ = store.Close() _ = store.Close()
_ = db.Close() _ = db.Close()
} })
return cleanup, store return store
} }
// TestStore tests the normal use cases of the store like creating, unlocking, // TestStore tests the normal use cases of the store like creating, unlocking,
// reading keys and closing it. // reading keys and closing it.
func TestStore(t *testing.T) { func TestStore(t *testing.T) {
tempDir, cleanup, store := newTestStore(t) tempDir, store := newTestStore(t)
defer cleanup()
_, _, err := store.RootKey(context.TODO()) _, _, err := store.RootKey(context.TODO())
require.Equal(t, macaroons.ErrStoreLocked, err) require.Equal(t, macaroons.ErrStoreLocked, err)
@ -114,7 +104,7 @@ func TestStore(t *testing.T) {
// Between here and the re-opening of the store, it's possible to get // Between here and the re-opening of the store, it's possible to get
// a double-close, but that's not such a big deal since the tests will // a double-close, but that's not such a big deal since the tests will
// fail anyway in that case. // fail anyway in that case.
_, store = openTestStore(t, tempDir) store = openTestStore(t, tempDir)
err = store.CreateUnlock(&badpw) err = store.CreateUnlock(&badpw)
require.Equal(t, snacl.ErrInvalidPassword, err) require.Equal(t, snacl.ErrInvalidPassword, err)
@ -144,8 +134,7 @@ func TestStore(t *testing.T) {
// TestStoreGenerateNewRootKey tests that a root key can be replaced with a new // TestStoreGenerateNewRootKey tests that a root key can be replaced with a new
// one in the store without changing the password. // one in the store without changing the password.
func TestStoreGenerateNewRootKey(t *testing.T) { func TestStoreGenerateNewRootKey(t *testing.T) {
_, cleanup, store := newTestStore(t) _, store := newTestStore(t)
defer cleanup()
// The store must be unlocked to replace the root key. // The store must be unlocked to replace the root key.
err := store.GenerateNewRootKey() err := store.GenerateNewRootKey()
@ -172,8 +161,7 @@ func TestStoreGenerateNewRootKey(t *testing.T) {
// TestStoreSetRootKey tests that a root key can be set to a specified value. // TestStoreSetRootKey tests that a root key can be set to a specified value.
func TestStoreSetRootKey(t *testing.T) { func TestStoreSetRootKey(t *testing.T) {
_, cleanup, store := newTestStore(t) _, store := newTestStore(t)
defer cleanup()
// Create a new random key // Create a new random key
rootKey := make([]byte, 32) rootKey := make([]byte, 32)
@ -209,8 +197,7 @@ func TestStoreSetRootKey(t *testing.T) {
// TestStoreChangePassword tests that the password for the store can be changed // TestStoreChangePassword tests that the password for the store can be changed
// without changing the root key. // without changing the root key.
func TestStoreChangePassword(t *testing.T) { func TestStoreChangePassword(t *testing.T) {
tempDir, cleanup, store := newTestStore(t) tempDir, store := newTestStore(t)
defer cleanup()
// The store must be unlocked to replace the root key. // The store must be unlocked to replace the root key.
err := store.ChangePassword(nil, nil) err := store.ChangePassword(nil, nil)
@ -251,7 +238,7 @@ func TestStoreChangePassword(t *testing.T) {
require.Error(t, err) require.Error(t, err)
// Let's open it again and try unlocking with the new password. // Let's open it again and try unlocking with the new password.
_, store = openTestStore(t, tempDir) store = openTestStore(t, tempDir)
err = store.CreateUnlock(&newPw) err = store.CreateUnlock(&newPw)
require.NoError(t, err) require.NoError(t, err)