Skip to content

Commit

Permalink
*: use T.TempDir to create temporary test directory
Browse files Browse the repository at this point in the history
The directory created by `T.TempDir()` and is automatically removed when
the test and all its subtests complete.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
  • Loading branch information
Juneezee committed Jan 24, 2022
1 parent a1fb9ff commit e9edc54
Show file tree
Hide file tree
Showing 20 changed files with 98 additions and 338 deletions.
38 changes: 11 additions & 27 deletions client/pkg/fileutil/fileutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,11 @@ import (
)

func TestIsDirWriteable(t *testing.T) {
tmpdir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatalf("unexpected os.MkdirTemp error: %v", err)
}
defer os.RemoveAll(tmpdir)
if err = IsDirWriteable(tmpdir); err != nil {
tmpdir := t.TempDir()
if err := IsDirWriteable(tmpdir); err != nil {
t.Fatalf("unexpected IsDirWriteable error: %v", err)
}
if err = os.Chmod(tmpdir, 0444); err != nil {
if err := os.Chmod(tmpdir, 0444); err != nil {
t.Fatalf("unexpected os.Chmod error: %v", err)
}
me, err := user.Current()
Expand All @@ -59,22 +55,18 @@ func TestIsDirWriteable(t *testing.T) {
}

func TestCreateDirAll(t *testing.T) {
tmpdir, err := os.MkdirTemp(os.TempDir(), "foo")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
tmpdir := t.TempDir()

tmpdir2 := filepath.Join(tmpdir, "testdir")
if err = CreateDirAll(zaptest.NewLogger(t), tmpdir2); err != nil {
if err := CreateDirAll(zaptest.NewLogger(t), tmpdir2); err != nil {
t.Fatal(err)
}

if err = os.WriteFile(filepath.Join(tmpdir2, "text.txt"), []byte("test text"), PrivateFileMode); err != nil {
if err := os.WriteFile(filepath.Join(tmpdir2, "text.txt"), []byte("test text"), PrivateFileMode); err != nil {
t.Fatal(err)
}

if err = CreateDirAll(zaptest.NewLogger(t), tmpdir2); err == nil || !strings.Contains(err.Error(), "to be empty, got") {
if err := CreateDirAll(zaptest.NewLogger(t), tmpdir2); err == nil || !strings.Contains(err.Error(), "to be empty, got") {
t.Fatalf("unexpected error %v", err)
}
}
Expand Down Expand Up @@ -107,11 +99,7 @@ func TestExist(t *testing.T) {
}

func TestDirEmpty(t *testing.T) {
dir, err := os.MkdirTemp(os.TempDir(), "empty_dir")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

if !DirEmpty(dir) {
t.Fatalf("expected DirEmpty true, got %v", DirEmpty(dir))
Expand Down Expand Up @@ -177,19 +165,15 @@ func TestZeroToEnd(t *testing.T) {
}

func TestDirPermission(t *testing.T) {
tmpdir, err := os.MkdirTemp(os.TempDir(), "foo")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
tmpdir := t.TempDir()

tmpdir2 := filepath.Join(tmpdir, "testpermission")
// create a new dir with 0700
if err = CreateDirAll(zaptest.NewLogger(t), tmpdir2); err != nil {
if err := CreateDirAll(zaptest.NewLogger(t), tmpdir2); err != nil {
t.Fatal(err)
}
// check dir permission with mode different than created dir
if err = CheckDirPermission(tmpdir2, 0600); err == nil {
if err := CheckDirPermission(tmpdir2, 0600); err == nil {
t.Errorf("expected error, got nil")
}
}
Expand Down
6 changes: 1 addition & 5 deletions client/pkg/fileutil/preallocate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@ func testPreallocateFixed(t *testing.T, f *os.File) {
}

func runPreallocTest(t *testing.T, test func(*testing.T, *os.File)) {
p, err := os.MkdirTemp(os.TempDir(), "preallocateTest")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(p)
p := t.TempDir()

f, err := os.CreateTemp(p, "")
if err != nil {
Expand Down
18 changes: 5 additions & 13 deletions client/pkg/fileutil/purge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,13 @@ import (
)

func TestPurgeFile(t *testing.T) {
dir, err := os.MkdirTemp("", "purgefile")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

// minimal file set
for i := 0; i < 3; i++ {
f, ferr := os.Create(filepath.Join(dir, fmt.Sprintf("%d.test", i)))
if ferr != nil {
t.Fatal(err)
t.Fatal(ferr)
}
f.Close()
}
Expand All @@ -56,7 +52,7 @@ func TestPurgeFile(t *testing.T) {
go func(n int) {
f, ferr := os.Create(filepath.Join(dir, fmt.Sprintf("%d.test", n)))
if ferr != nil {
t.Error(err)
t.Error(ferr)
}
f.Close()
}(i)
Expand Down Expand Up @@ -92,15 +88,11 @@ func TestPurgeFile(t *testing.T) {
}

func TestPurgeFileHoldingLockFile(t *testing.T) {
dir, err := os.MkdirTemp("", "purgefile")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

for i := 0; i < 10; i++ {
var f *os.File
f, err = os.Create(filepath.Join(dir, fmt.Sprintf("%d.test", i)))
f, err := os.Create(filepath.Join(dir, fmt.Sprintf("%d.test", i)))
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 1 addition & 5 deletions client/pkg/fileutil/read_dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ import (
)

func TestReadDir(t *testing.T) {
tmpdir, err := os.MkdirTemp("", "")
defer os.RemoveAll(tmpdir)
if err != nil {
t.Fatalf("unexpected os.MkdirTemp error: %v", err)
}
tmpdir := t.TempDir()

files := []string{"def", "abc", "xyz", "ghi"}
for _, f := range files {
Expand Down
3 changes: 1 addition & 2 deletions client/pkg/transport/keepalive_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ func TestNewKeepAliveListener(t *testing.T) {
}

// tls
tlsinfo, del, err := createSelfCert()
tlsinfo, err := createSelfCert(t)
if err != nil {
t.Fatalf("unable to create tmpfile: %v", err)
}
defer del()
tlsInfo := TLSInfo{CertFile: tlsinfo.CertFile, KeyFile: tlsinfo.KeyFile}
tlsInfo.parseFunc = fakeCertificateParserFunc(tls.Certificate{}, nil)
tlscfg, err := tlsInfo.ServerConfig()
Expand Down
48 changes: 16 additions & 32 deletions client/pkg/transport/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,17 @@ import (
"go.uber.org/zap/zaptest"
)

func createSelfCert(hosts ...string) (*TLSInfo, func(), error) {
return createSelfCertEx("127.0.0.1")
func createSelfCert(t *testing.T, hosts ...string) (*TLSInfo, error) {
return createSelfCertEx(t, "127.0.0.1")
}

func createSelfCertEx(host string, additionalUsages ...x509.ExtKeyUsage) (*TLSInfo, func(), error) {
d, terr := os.MkdirTemp("", "etcd-test-tls-")
if terr != nil {
return nil, nil, terr
}
func createSelfCertEx(t *testing.T, host string, additionalUsages ...x509.ExtKeyUsage) (*TLSInfo, error) {
d := t.TempDir()
info, err := SelfCert(zap.NewExample(), d, []string{host + ":0"}, 1, additionalUsages...)
if err != nil {
return nil, nil, err
return nil, err
}
return &info, func() { os.RemoveAll(d) }, nil
return &info, nil
}

func fakeCertificateParserFunc(cert tls.Certificate, err error) func(certPEMBlock, keyPEMBlock []byte) (tls.Certificate, error) {
Expand All @@ -53,20 +50,18 @@ func fakeCertificateParserFunc(cert tls.Certificate, err error) func(certPEMBloc
// TestNewListenerTLSInfo tests that NewListener with valid TLSInfo returns
// a TLS listener that accepts TLS connections.
func TestNewListenerTLSInfo(t *testing.T) {
tlsInfo, del, err := createSelfCert()
tlsInfo, err := createSelfCert(t)
if err != nil {
t.Fatalf("unable to create cert: %v", err)
}
defer del()
testNewListenerTLSInfoAccept(t, *tlsInfo)
}

func TestNewListenerWithOpts(t *testing.T) {
tlsInfo, del, err := createSelfCert()
tlsInfo, err := createSelfCert(t)
if err != nil {
t.Fatalf("unable to create cert: %v", err)
}
defer del()

tests := map[string]struct {
opts []ListenerOption
Expand Down Expand Up @@ -124,11 +119,10 @@ func TestNewListenerWithOpts(t *testing.T) {
}

func TestNewListenerWithSocketOpts(t *testing.T) {
tlsInfo, del, err := createSelfCert()
tlsInfo, err := createSelfCert(t)
if err != nil {
t.Fatalf("unable to create cert: %v", err)
}
defer del()

tests := map[string]struct {
opts []ListenerOption
Expand Down Expand Up @@ -257,21 +251,19 @@ func TestNewListenerTLSInfoSkipClientSANVerify(t *testing.T) {
}

func testNewListenerTLSInfoClientCheck(t *testing.T, skipClientSANVerify, goodClientHost, acceptExpected bool) {
tlsInfo, del, err := createSelfCert()
tlsInfo, err := createSelfCert(t)
if err != nil {
t.Fatalf("unable to create cert: %v", err)
}
defer del()

host := "127.0.0.222"
if goodClientHost {
host = "127.0.0.1"
}
clientTLSInfo, del2, err := createSelfCertEx(host, x509.ExtKeyUsageClientAuth)
clientTLSInfo, err := createSelfCertEx(t, host, x509.ExtKeyUsageClientAuth)
if err != nil {
t.Fatalf("unable to create cert: %v", err)
}
defer del2()

tlsInfo.SkipClientSANVerify = skipClientSANVerify
tlsInfo.TrustedCAFile = clientTLSInfo.CertFile
Expand Down Expand Up @@ -344,11 +336,10 @@ func TestNewListenerTLSEmptyInfo(t *testing.T) {
}

func TestNewTransportTLSInfo(t *testing.T) {
tlsinfo, del, err := createSelfCert()
tlsinfo, err := createSelfCert(t)
if err != nil {
t.Fatalf("unable to create cert: %v", err)
}
defer del()

tests := []TLSInfo{
{},
Expand Down Expand Up @@ -416,11 +407,10 @@ func TestTLSInfoEmpty(t *testing.T) {
}

func TestTLSInfoMissingFields(t *testing.T) {
tlsinfo, del, err := createSelfCert()
tlsinfo, err := createSelfCert(t)
if err != nil {
t.Fatalf("unable to create cert: %v", err)
}
defer del()

tests := []TLSInfo{
{CertFile: tlsinfo.CertFile},
Expand All @@ -441,11 +431,10 @@ func TestTLSInfoMissingFields(t *testing.T) {
}

func TestTLSInfoParseFuncError(t *testing.T) {
tlsinfo, del, err := createSelfCert()
tlsinfo, err := createSelfCert(t)
if err != nil {
t.Fatalf("unable to create cert: %v", err)
}
defer del()

tests := []struct {
info TLSInfo
Expand Down Expand Up @@ -474,11 +463,10 @@ func TestTLSInfoParseFuncError(t *testing.T) {

func TestTLSInfoConfigFuncs(t *testing.T) {
ln := zaptest.NewLogger(t)
tlsinfo, del, err := createSelfCert()
tlsinfo, err := createSelfCert(t)
if err != nil {
t.Fatalf("unable to create cert: %v", err)
}
defer del()

tests := []struct {
info TLSInfo
Expand Down Expand Up @@ -531,11 +519,7 @@ func TestNewListenerUnixSocket(t *testing.T) {

// TestNewListenerTLSInfoSelfCert tests that a new certificate accepts connections.
func TestNewListenerTLSInfoSelfCert(t *testing.T) {
tmpdir, err := os.MkdirTemp(os.TempDir(), "tlsdir")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
tmpdir := t.TempDir()
tlsinfo, err := SelfCert(zap.NewExample(), tmpdir, []string{"127.0.0.1"}, 1)
if err != nil {
t.Fatal(err)
Expand Down
3 changes: 1 addition & 2 deletions client/pkg/transport/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ import (
// TestNewTransportTLSInvalidCipherSuitesTLS12 expects a client with invalid
// cipher suites fail to handshake with the server.
func TestNewTransportTLSInvalidCipherSuitesTLS12(t *testing.T) {
tlsInfo, del, err := createSelfCert()
tlsInfo, err := createSelfCert(t)
if err != nil {
t.Fatalf("unable to create cert: %v", err)
}
defer del()

cipherSuites := []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
Expand Down
7 changes: 1 addition & 6 deletions server/embed/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,13 @@ package embed

import (
"context"
"os"
"testing"

"go.etcd.io/etcd/server/v3/etcdserver/api/v3client"
)

func TestEnableAuth(t *testing.T) {
tdir, err := os.MkdirTemp(os.TempDir(), "auth-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tdir)
tdir := t.TempDir()
cfg := NewConfig()
cfg.Dir = tdir
e, err := StartEtcd(cfg)
Expand Down
8 changes: 2 additions & 6 deletions server/embed/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ import (

// TestStartEtcdWrongToken ensures that StartEtcd with wrong configs returns with error.
func TestStartEtcdWrongToken(t *testing.T) {
tdir, err := os.MkdirTemp(t.TempDir(), "token-test")

if err != nil {
t.Fatal(err)
}
tdir := t.TempDir()

cfg := NewConfig()

Expand All @@ -47,7 +43,7 @@ func TestStartEtcdWrongToken(t *testing.T) {
cfg.Dir = tdir
cfg.AuthToken = "wrong-token"

if _, err = StartEtcd(cfg); err != auth.ErrInvalidAuthOpts {
if _, err := StartEtcd(cfg); err != auth.ErrInvalidAuthOpts {
t.Fatalf("expected %v, got %v", auth.ErrInvalidAuthOpts, err)
}
}
Expand Down
6 changes: 1 addition & 5 deletions server/etcdserver/api/rafthttp/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,7 @@ func TestSnapshotSend(t *testing.T) {
}

func testSnapshotSend(t *testing.T, sm *snap.Message) (bool, []os.DirEntry) {
d, err := os.MkdirTemp(os.TempDir(), "snapdir")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(d)
d := t.TempDir()

r := &fakeRaft{}
tr := &Transport{pipelineRt: &http.Transport{}, ClusterID: types.ID(1), Raft: r}
Expand Down
Loading

0 comments on commit e9edc54

Please sign in to comment.