Skip to content

Commit

Permalink
test: use T.TempDir to create temporary test directory
Browse files Browse the repository at this point in the history
This commit replaces `ioutil.TempDir` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.

Prior to this commit, temporary directory created using `ioutil.TempDir`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
	defer func() {
		if err := os.RemoveAll(dir); err != nil {
			t.Fatal(err)
		}
	}
is also tedious, but `t.TempDir` handles this for us nicely.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
  • Loading branch information
Juneezee authored and ale-linux committed Sep 30, 2022
1 parent f7af71b commit 0a06fe7
Show file tree
Hide file tree
Showing 108 changed files with 453 additions and 1,248 deletions.
19 changes: 7 additions & 12 deletions bccsp/pkcs11/pkcs11_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"crypto/rand"
"crypto/sha256"
"encoding/asn1"
"io/ioutil"
"os"
"strconv"
"strings"
Expand All @@ -41,38 +40,35 @@ func defaultOptions() PKCS11Opts {
}
}

func newKeyStore(t *testing.T) (bccsp.KeyStore, func()) {
tempDir, err := ioutil.TempDir("", "pkcs11_ks")
require.NoError(t, err)
func newKeyStore(t *testing.T) bccsp.KeyStore {
tempDir := t.TempDir()
ks, err := sw.NewFileBasedKeyStore(nil, tempDir, false)
require.NoError(t, err)

return ks, func() { os.RemoveAll(tempDir) }
return ks
}

func newSWProvider(t *testing.T) bccsp.BCCSP {
ks, _ := newKeyStore(t)
ks := newKeyStore(t)
swCsp, err := sw.NewDefaultSecurityLevelWithKeystore(ks)
require.NoError(t, err)

return swCsp
}

func newProvider(t *testing.T, opts PKCS11Opts, options ...Option) (*Provider, func()) {
ks, ksCleanup := newKeyStore(t)
ks := newKeyStore(t)
csp, err := New(opts, ks, options...)
require.NoError(t, err)

cleanup := func() {
csp.ctx.Destroy()
ksCleanup()
}
return csp, cleanup
}

func TestNew(t *testing.T) {
ks, cleanup := newKeyStore(t)
defer cleanup()
ks := newKeyStore(t)

t.Run("DefaultConfig", func(t *testing.T) {
opts := defaultOptions()
Expand Down Expand Up @@ -113,8 +109,7 @@ func TestNew(t *testing.T) {
}

func TestInvalidNewParameter(t *testing.T) {
ks, cleanup := newKeyStore(t)
defer cleanup()
ks := newKeyStore(t)

t.Run("BadSecurityLevel", func(t *testing.T) {
opts := defaultOptions()
Expand Down
12 changes: 3 additions & 9 deletions bccsp/sw/fileks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ import (
func TestInvalidStoreKey(t *testing.T) {
t.Parallel()

tempDir, err := ioutil.TempDir("", "bccspks")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

ks, err := NewFileBasedKeyStore(nil, filepath.Join(tempDir, "bccspks"), false)
if err != nil {
Expand Down Expand Up @@ -58,9 +56,7 @@ func TestInvalidStoreKey(t *testing.T) {
}

func TestBigKeyFile(t *testing.T) {
ksPath, err := ioutil.TempDir("", "bccspks")
require.NoError(t, err)
defer os.RemoveAll(ksPath)
ksPath := t.TempDir()

ks, err := NewFileBasedKeyStore(nil, ksPath, false)
require.NoError(t, err)
Expand Down Expand Up @@ -97,9 +93,7 @@ func TestBigKeyFile(t *testing.T) {
}

func TestReInitKeyStore(t *testing.T) {
ksPath, err := ioutil.TempDir("", "bccspks")
require.NoError(t, err)
defer os.RemoveAll(ksPath)
ksPath := t.TempDir()

ks, err := NewFileBasedKeyStore(nil, ksPath, false)
require.NoError(t, err)
Expand Down
27 changes: 9 additions & 18 deletions ccaas_builder/cmd/build/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ SPDX-License-Identifier: Apache-2.0
package main

import (
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
"io/ioutil"
"os"
"os/exec"
"path"
"testing"
"time"

. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
)

func TestArguements(t *testing.T) {
Expand Down Expand Up @@ -61,9 +62,7 @@ func TestGoodPath(t *testing.T) {
gt.Expect(err).NotTo(HaveOccurred())
defer gexec.CleanupBuildArtifacts()

testPath, err := ioutil.TempDir("", "test-ccaas-build-")
gt.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(testPath)
testPath := t.TempDir()

// create a basic structure of a chaincode
os.MkdirAll(path.Join(testPath, "in-builder-dir", "META-INF"), 0755)
Expand Down Expand Up @@ -121,9 +120,7 @@ func TestTemplating(t *testing.T) {
gt.Expect(err).NotTo(HaveOccurred())
defer gexec.CleanupBuildArtifacts()

testPath, err := ioutil.TempDir("", "test-ccaas-build-")
gt.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(testPath)
testPath := t.TempDir()

// create a basic structure of the chaincode to use
os.MkdirAll(path.Join(testPath, "in-builder-dir", "META-INF"), 0755)
Expand Down Expand Up @@ -195,9 +192,7 @@ func TestTemplatingFailure(t *testing.T) {
gt.Expect(err).NotTo(HaveOccurred())
defer gexec.CleanupBuildArtifacts()

testPath, err := ioutil.TempDir("", "test-ccaas-build-")
gt.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(testPath)
testPath := t.TempDir()

// create a basic structure of the chaincode to use
os.MkdirAll(path.Join(testPath, "in-builder-dir", "META-INF"), 0755)
Expand Down Expand Up @@ -249,9 +244,7 @@ func TestMissingConnection(t *testing.T) {
gt.Expect(err).NotTo(HaveOccurred())
defer gexec.CleanupBuildArtifacts()

testPath, err := ioutil.TempDir("", "test-ccaas-build-")
gt.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(testPath)
testPath := t.TempDir()

// create a basic structure of a chaincode
os.MkdirAll(path.Join(testPath, "in-builder-dir", "META-INF"), 0755)
Expand Down Expand Up @@ -292,9 +285,7 @@ func TestMissingMetadata(t *testing.T) {
gt.Expect(err).NotTo(HaveOccurred())
defer gexec.CleanupBuildArtifacts()

testPath, err := ioutil.TempDir("", "test-ccaas-build-")
gt.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(testPath)
testPath := t.TempDir()

// create a basic structure of a chaincode
os.MkdirAll(path.Join(testPath, "in-builder-dir", "META-INF"), 0755)
Expand Down
6 changes: 1 addition & 5 deletions ccaas_builder/cmd/detect/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"

"io/ioutil"
"os"
"os/exec"
"testing"
"time"
Expand Down Expand Up @@ -61,9 +59,7 @@ func TestArugments(t *testing.T) {
func TestMissingFile(t *testing.T) {
gt := NewWithT(t)

testPath, err := ioutil.TempDir("", "empty-test-dir")
gt.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(testPath)
testPath := t.TempDir()

detectCmd, err := gexec.Build("github.com/hyperledger/fabric/ccaas_builder/cmd/detect")
gt.Expect(err).NotTo(HaveOccurred())
Expand Down
16 changes: 6 additions & 10 deletions ccaas_builder/cmd/release/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ SPDX-License-Identifier: Apache-2.0
package main

import (
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
"io/ioutil"
"os"
"os/exec"
"path"
"testing"
"time"

. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
)

func TestArugments(t *testing.T) {
Expand Down Expand Up @@ -57,9 +57,7 @@ func TestGoodPath(t *testing.T) {
gt.Expect(err).NotTo(HaveOccurred())
defer gexec.CleanupBuildArtifacts()

testPath, err := ioutil.TempDir("", "test-ccaas-release-")
gt.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(testPath)
testPath := t.TempDir()

os.MkdirAll(path.Join(testPath, "in-builder-dir"), 0755)
gt.Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -96,9 +94,7 @@ func TestMissingConnection(t *testing.T) {
gt.Expect(err).NotTo(HaveOccurred())
defer gexec.CleanupBuildArtifacts()

testPath, err := ioutil.TempDir("", "test-ccaas-release-")
gt.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(testPath)
testPath := t.TempDir()

os.MkdirAll(path.Join(testPath, "in-builder-dir"), 0755)
gt.Expect(err).NotTo(HaveOccurred())
Expand Down
6 changes: 1 addition & 5 deletions cmd/peer/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ package main

import (
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
"path/filepath"
"testing"
Expand All @@ -30,9 +28,7 @@ func TestPluginLoadingFailure(t *testing.T) {
parentDir, err := filepath.Abs("../..")
gt.Expect(err).NotTo(HaveOccurred())

tempDir, err := ioutil.TempDir("", "plugin-failure")
gt.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

peerListener, err := net.Listen("tcp", "localhost:0")
gt.Expect(err).NotTo(HaveOccurred())
Expand Down
5 changes: 1 addition & 4 deletions common/ledger/blkstorage/blkstoragetest/blkstoragetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package blkstoragetest
import (
"crypto/sha256"
"hash"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -41,8 +40,7 @@ var (
func BootstrapBlockstoreFromSnapshot(t *testing.T, ledgerName string, blocks []*common.Block) (*blkstorage.BlockStore, func()) {
require.NotEqual(t, 0, len(blocks))

testDir, err := ioutil.TempDir("", ledgerName)
require.NoError(t, err)
testDir := t.TempDir()
snapshotDir := filepath.Join(testDir, "snapshot")
require.NoError(t, os.Mkdir(snapshotDir, 0o755))

Expand Down Expand Up @@ -76,7 +74,6 @@ func BootstrapBlockstoreFromSnapshot(t *testing.T, ledgerName string, blocks []*

cleanup := func() {
provider.Close()
os.RemoveAll(testDir)
}
return blockStore, cleanup
}
6 changes: 3 additions & 3 deletions common/ledger/blkstorage/block_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestBlockfileStream(t *testing.T) {
}

func testBlockfileStream(t *testing.T, numBlocks int) {
env := newTestEnv(t, NewConf(testPath(), 0))
env := newTestEnv(t, NewConf(t.TempDir(), 0))
defer env.Cleanup()
ledgerid := "testledger"
w := newTestBlockfileWrapper(env, ledgerid)
Expand Down Expand Up @@ -63,7 +63,7 @@ func TestBlockFileStreamUnexpectedEOF(t *testing.T) {
}

func testBlockFileStreamUnexpectedEOF(t *testing.T, numBlocks int, partialBlockBytes []byte) {
env := newTestEnv(t, NewConf(testPath(), 0))
env := newTestEnv(t, NewConf(t.TempDir(), 0))
defer env.Cleanup()
w := newTestBlockfileWrapper(env, "testLedger")
blockfileMgr := w.blockfileMgr
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestBlockStream(t *testing.T) {

func testBlockStream(t *testing.T, numFiles int) {
ledgerID := "testLedger"
env := newTestEnv(t, NewConf(testPath(), 0))
env := newTestEnv(t, NewConf(t.TempDir(), 0))
defer env.Cleanup()
w := newTestBlockfileWrapper(env, ledgerID)
defer w.close()
Expand Down
16 changes: 5 additions & 11 deletions common/ledger/blkstorage/blockfile_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

func TestConstructBlockfilesInfo(t *testing.T) {
ledgerid := "testLedger"
conf := NewConf(testPath(), 0)
conf := NewConf(t.TempDir(), 0)
blkStoreDir := conf.getLedgerBlockDir(ledgerid)
env := newTestEnv(t, conf)
require.NoError(t, os.MkdirAll(blkStoreDir, 0o755))
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestConstructBlockfilesInfo(t *testing.T) {
}

func TestBinarySearchBlockFileNum(t *testing.T) {
blockStoreRootDir := testPath()
blockStoreRootDir := t.TempDir()
blocks := testutil.ConstructTestBlocks(t, 100)
maxFileSie := int(0.1 * float64(testutilEstimateTotalSizeOnDisk(t, blocks)))
env := newTestEnv(t, NewConf(blockStoreRootDir, maxFileSie))
Expand All @@ -117,9 +117,7 @@ func TestBinarySearchBlockFileNum(t *testing.T) {
}

func TestIsBootstrappedFromSnapshot(t *testing.T) {
testDir, err := ioutil.TempDir("", "isbootstrappedfromsnapshot")
require.NoError(t, err)
defer os.RemoveAll(testDir)
testDir := t.TempDir()

t.Run("no_bootstrapping_snapshot_info_file", func(t *testing.T) {
// create chains directory for the ledger without bootstrappingSnapshotInfoFile
Expand All @@ -146,9 +144,7 @@ func TestIsBootstrappedFromSnapshot(t *testing.T) {

func TestGetLedgersBootstrappedFromSnapshot(t *testing.T) {
t.Run("no_bootstrapping_snapshot_info_file", func(t *testing.T) {
testDir, err := ioutil.TempDir("", "getledgersfromsnapshot_nosnapshot_info")
require.NoError(t, err)
defer os.RemoveAll(testDir)
testDir := t.TempDir()

// create chains directories for ledgers without bootstrappingSnapshotInfoFile
for i := 0; i < 5; i++ {
Expand All @@ -161,9 +157,7 @@ func TestGetLedgersBootstrappedFromSnapshot(t *testing.T) {
})

t.Run("with_bootstrapping_snapshot_info_file", func(t *testing.T) {
testDir, err := ioutil.TempDir("", "getledgersfromsnapshot_snapshot_info")
require.NoError(t, err)
defer os.RemoveAll(testDir)
testDir := t.TempDir()

// create chains directories for ledgers
// also create bootstrappingSnapshotInfoFile for ledger_0 and ledger_1
Expand Down
Loading

0 comments on commit 0a06fe7

Please sign in to comment.