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 committed 2022-08-24 09:03:05 +08:00
1 parent 9acd53a5de
commit f3dec8fbb0
2 files changed
+21 -42

No files matched your search

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