aboutsummaryrefslogtreecommitdiff
path: root/plugin
diff options
context:
space:
mode:
Diffstat (limited to 'plugin')
-rw-r--r--plugin/auto/walk_test.go17
-rw-r--r--plugin/auto/watcher_test.go12
-rw-r--r--plugin/grpc/proxy_test.go7
-rw-r--r--plugin/pkg/tls/tls_test.go16
-rw-r--r--plugin/test/file.go23
5 files changed, 24 insertions, 51 deletions
diff --git a/plugin/auto/walk_test.go b/plugin/auto/walk_test.go
index bee955c10..062c99243 100644
--- a/plugin/auto/walk_test.go
+++ b/plugin/auto/walk_test.go
@@ -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,
@@ -65,11 +61,8 @@ 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 {
@@ -77,10 +70,10 @@ func createFiles() (string, error) {
}
}
// 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
}
diff --git a/plugin/auto/watcher_test.go b/plugin/auto/watcher_test.go
index 43f1ff374..9a256f443 100644
--- a/plugin/auto/watcher_test.go
+++ b/plugin/auto/watcher_test.go
@@ -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,
@@ -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,
diff --git a/plugin/grpc/proxy_test.go b/plugin/grpc/proxy_test.go
index 534fde3d7..2ca0b1b48 100644
--- a/plugin/grpc/proxy_test.go
+++ b/plugin/grpc/proxy_test.go
@@ -4,7 +4,6 @@ import (
"context"
"errors"
"net"
- "os"
"path"
"testing"
@@ -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)
diff --git a/plugin/pkg/tls/tls_test.go b/plugin/pkg/tls/tls_test.go
index b94efc28c..db1cad052 100644
--- a/plugin/pkg/tls/tls_test.go
+++ b/plugin/pkg/tls/tls_test.go
@@ -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)
}
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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 {
diff --git a/plugin/test/file.go b/plugin/test/file.go
index 969406e96..667b6a3f7 100644
--- a/plugin/test/file.go
+++ b/plugin/test/file.go
@@ -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.
@@ -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
@@ -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
@@ -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-----
@@ -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
}