Skip to content

Commit

Permalink
test: use t.TempDir to create temporary test directory (coredns#6164)
Browse files Browse the repository at this point in the history
  • Loading branch information
Juneezee authored Jun 17, 2023
1 parent 06cd843 commit b868350
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 77 deletions.
17 changes: 5 additions & 12 deletions plugin/auto/walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@ www IN A 127.0.0.1
`

func TestWalk(t *testing.T) {
tempdir, err := createFiles()
tempdir, err := createFiles(t)
if err != nil {
if tempdir != "" {
os.RemoveAll(tempdir)
}
t.Fatal(err)
}
defer os.RemoveAll(tempdir)

ldr := loader{
directory: tempdir,
Expand Down Expand Up @@ -65,22 +61,19 @@ func TestWalkNonExistent(t *testing.T) {
a.Walk()
}

func createFiles() (string, error) {
dir, err := os.MkdirTemp(os.TempDir(), "coredns")
if err != nil {
return dir, err
}
func createFiles(t *testing.T) (string, error) {
dir := t.TempDir()

for _, name := range dbFiles {
if err := os.WriteFile(filepath.Join(dir, name), []byte(zoneContent), 0644); err != nil {
return dir, err
}
}
// symlinks
if err = os.Symlink(filepath.Join(dir, "db.example.org"), filepath.Join(dir, "db.example.com")); err != nil {
if err := os.Symlink(filepath.Join(dir, "db.example.org"), filepath.Join(dir, "db.example.com")); err != nil {
return dir, err
}
if err = os.Symlink(filepath.Join(dir, "db.example.org"), filepath.Join(dir, "aa.example.com")); err != nil {
if err := os.Symlink(filepath.Join(dir, "db.example.org"), filepath.Join(dir, "aa.example.com")); err != nil {
return dir, err
}

Expand Down
12 changes: 2 additions & 10 deletions plugin/auto/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@ import (
)

func TestWatcher(t *testing.T) {
tempdir, err := createFiles()
tempdir, err := createFiles(t)
if err != nil {
if tempdir != "" {
os.RemoveAll(tempdir)
}
t.Fatal(err)
}
defer os.RemoveAll(tempdir)

ldr := loader{
directory: tempdir,
Expand Down Expand Up @@ -54,14 +50,10 @@ func TestWatcher(t *testing.T) {
}

func TestSymlinks(t *testing.T) {
tempdir, err := createFiles()
tempdir, err := createFiles(t)
if err != nil {
if tempdir != "" {
os.RemoveAll(tempdir)
}
t.Fatal(err)
}
defer os.RemoveAll(tempdir)

ldr := loader{
directory: tempdir,
Expand Down
7 changes: 1 addition & 6 deletions plugin/grpc/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"net"
"os"
"path"
"testing"

Expand Down Expand Up @@ -72,11 +71,7 @@ func (m testServiceClient) Query(ctx context.Context, in *pb.DnsPacket, opts ...
}

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

fd := path.Join(tdir, "test.grpc")
listener, err := net.Listen("unix", fd)
Expand Down
16 changes: 6 additions & 10 deletions plugin/pkg/tls/tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/coredns/coredns/plugin/test"
)

func getPEMFiles(t *testing.T) (rmFunc func(), cert, key, ca string) {
tempDir, rmFunc, err := test.WritePEMFiles("")
func getPEMFiles(t *testing.T) (cert, key, ca string) {
tempDir, err := test.WritePEMFiles(t)
if err != nil {
t.Fatalf("Could not write PEM files: %s", err)
}
Expand All @@ -21,8 +21,7 @@ func getPEMFiles(t *testing.T) (rmFunc func(), cert, key, ca string) {
}

func TestNewTLSConfig(t *testing.T) {
rmFunc, cert, key, ca := getPEMFiles(t)
defer rmFunc()
cert, key, ca := getPEMFiles(t)

_, err := NewTLSConfig(cert, key, ca)
if err != nil {
Expand All @@ -31,8 +30,7 @@ func TestNewTLSConfig(t *testing.T) {
}

func TestNewTLSClientConfig(t *testing.T) {
rmFunc, _, _, ca := getPEMFiles(t)
defer rmFunc()
_, _, ca := getPEMFiles(t)

_, err := NewTLSClientConfig(ca)
if err != nil {
Expand All @@ -41,8 +39,7 @@ func TestNewTLSClientConfig(t *testing.T) {
}

func TestNewTLSConfigFromArgs(t *testing.T) {
rmFunc, cert, key, ca := getPEMFiles(t)
defer rmFunc()
cert, key, ca := getPEMFiles(t)

_, err := NewTLSConfigFromArgs()
if err != nil {
Expand Down Expand Up @@ -81,8 +78,7 @@ func TestNewTLSConfigFromArgs(t *testing.T) {
}

func TestNewHTTPSTransport(t *testing.T) {
rmFunc, _, _, ca := getPEMFiles(t)
defer rmFunc()
_, _, ca := getPEMFiles(t)

cc, err := NewTLSClientConfig(ca)
if err != nil {
Expand Down
23 changes: 10 additions & 13 deletions plugin/test/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package test
import (
"os"
"path/filepath"
"testing"
)

// TempFile will create a temporary file on disk and returns the name and a cleanup function to remove it later.
Expand All @@ -18,12 +19,9 @@ func TempFile(dir, content string) (string, func(), error) {
return f.Name(), rmFunc, nil
}

// WritePEMFiles creates a tmp dir with ca.pem, cert.pem, and key.pem and the func to remove it
func WritePEMFiles(dir string) (string, func(), error) {
tempDir, err := os.MkdirTemp(dir, "go-test-pemfiles")
if err != nil {
return "", nil, err
}
// WritePEMFiles creates a tmp dir with ca.pem, cert.pem, and key.pem
func WritePEMFiles(t *testing.T) (string, error) {
tempDir := t.TempDir()

data := `-----BEGIN CERTIFICATE-----
MIIC9zCCAd+gAwIBAgIJALGtqdMzpDemMA0GCSqGSIb3DQEBCwUAMBIxEDAOBgNV
Expand All @@ -45,7 +43,7 @@ I1rs/VUGKzcJGVIWbHrgjP68CTStGAvKgbsTqw7aLXTSqtPw88N9XVSyRg==
-----END CERTIFICATE-----`
path := filepath.Join(tempDir, "ca.pem")
if err := os.WriteFile(path, []byte(data), 0644); err != nil {
return "", nil, err
return "", err
}
data = `-----BEGIN CERTIFICATE-----
MIICozCCAYsCCQCRlf5BrvPuqjANBgkqhkiG9w0BAQsFADASMRAwDgYDVQQDDAdr
Expand All @@ -65,8 +63,8 @@ zhDEPP4FhY+Sz+y1yWirphl7A1aZwhXVPcfWIGqpQ3jzNwUeocbH27kuLh+U4hQo
qeg10RdFnw==
-----END CERTIFICATE-----`
path = filepath.Join(tempDir, "cert.pem")
if err = os.WriteFile(path, []byte(data), 0644); err != nil {
return "", nil, err
if err := os.WriteFile(path, []byte(data), 0644); err != nil {
return "", err
}

data = `-----BEGIN RSA PRIVATE KEY-----
Expand Down Expand Up @@ -97,10 +95,9 @@ E/WObVJXDnBdViu0L9abE9iaTToBVri4cmlDlZagLuKVR+TFTCN/DSlVZTDkqkLI
8chzqtkH6b2b2R73hyRysWjsomys34ma3mEEPTX/aXeAF2MSZ/EWT9yL
-----END RSA PRIVATE KEY-----`
path = filepath.Join(tempDir, "key.pem")
if err = os.WriteFile(path, []byte(data), 0644); err != nil {
return "", nil, err
if err := os.WriteFile(path, []byte(data), 0644); err != nil {
return "", err
}

rmFunc := func() { os.RemoveAll(tempDir) }
return tempDir, rmFunc, nil
return tempDir, nil
}
18 changes: 3 additions & 15 deletions test/auto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ import (

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

corefile := `org:0 {
auto {
Expand Down Expand Up @@ -70,11 +66,7 @@ func TestAuto(t *testing.T) {

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

corefile := `.:0 {
auto {
Expand Down Expand Up @@ -109,11 +101,7 @@ func TestAutoNonExistentZone(t *testing.T) {
func TestAutoAXFR(t *testing.T) {
t.Parallel()

tmpdir, err := os.MkdirTemp(os.TempDir(), "coredns")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
tmpdir := t.TempDir()

corefile := `org:0 {
auto {
Expand Down
8 changes: 2 additions & 6 deletions test/erratic_autopath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import (
)

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

content := `
example.org. IN SOA sns.dns.icann.org. noc.dns.icann.org. 1 3600 3600 3600 3600
Expand All @@ -23,7 +19,7 @@ google.com. IN A 172.217.25.110
`

path := filepath.Join(tmpdir, "file")
if err = os.WriteFile(path, []byte(content), 0644); err != nil {
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
t.Fatalf("Could not write to temp file: %s", err)
}
defer os.Remove(path)
Expand Down
6 changes: 1 addition & 5 deletions test/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ func TestMetricsRefused(t *testing.T) {
}

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

corefile := `org:0 {
auto {
Expand Down

0 comments on commit b868350

Please sign in to comment.