test: use t.TempDir
to create temporary test directory (#6164)
This commit is contained in:
parent
06cd843918
commit
b868350fc2
8 changed files with 30 additions and 77 deletions
|
@ -18,14 +18,10 @@ www IN A 127.0.0.1
|
||||||
`
|
`
|
||||||
|
|
||||||
func TestWalk(t *testing.T) {
|
func TestWalk(t *testing.T) {
|
||||||
tempdir, err := createFiles()
|
tempdir, err := createFiles(t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if tempdir != "" {
|
|
||||||
os.RemoveAll(tempdir)
|
|
||||||
}
|
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer os.RemoveAll(tempdir)
|
|
||||||
|
|
||||||
ldr := loader{
|
ldr := loader{
|
||||||
directory: tempdir,
|
directory: tempdir,
|
||||||
|
@ -65,11 +61,8 @@ func TestWalkNonExistent(t *testing.T) {
|
||||||
a.Walk()
|
a.Walk()
|
||||||
}
|
}
|
||||||
|
|
||||||
func createFiles() (string, error) {
|
func createFiles(t *testing.T) (string, error) {
|
||||||
dir, err := os.MkdirTemp(os.TempDir(), "coredns")
|
dir := t.TempDir()
|
||||||
if err != nil {
|
|
||||||
return dir, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, name := range dbFiles {
|
for _, name := range dbFiles {
|
||||||
if err := os.WriteFile(filepath.Join(dir, name), []byte(zoneContent), 0644); err != nil {
|
if err := os.WriteFile(filepath.Join(dir, name), []byte(zoneContent), 0644); err != nil {
|
||||||
|
@ -77,10 +70,10 @@ func createFiles() (string, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// symlinks
|
// 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
|
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
|
return dir, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,14 +8,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestWatcher(t *testing.T) {
|
func TestWatcher(t *testing.T) {
|
||||||
tempdir, err := createFiles()
|
tempdir, err := createFiles(t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if tempdir != "" {
|
|
||||||
os.RemoveAll(tempdir)
|
|
||||||
}
|
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer os.RemoveAll(tempdir)
|
|
||||||
|
|
||||||
ldr := loader{
|
ldr := loader{
|
||||||
directory: tempdir,
|
directory: tempdir,
|
||||||
|
@ -54,14 +50,10 @@ func TestWatcher(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSymlinks(t *testing.T) {
|
func TestSymlinks(t *testing.T) {
|
||||||
tempdir, err := createFiles()
|
tempdir, err := createFiles(t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if tempdir != "" {
|
|
||||||
os.RemoveAll(tempdir)
|
|
||||||
}
|
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer os.RemoveAll(tempdir)
|
|
||||||
|
|
||||||
ldr := loader{
|
ldr := loader{
|
||||||
directory: tempdir,
|
directory: tempdir,
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"path"
|
"path"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -72,11 +71,7 @@ func (m testServiceClient) Query(ctx context.Context, in *pb.DnsPacket, opts ...
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProxyUnix(t *testing.T) {
|
func TestProxyUnix(t *testing.T) {
|
||||||
tdir, err := os.MkdirTemp("", "tmp*")
|
tdir := t.TempDir()
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tdir)
|
|
||||||
|
|
||||||
fd := path.Join(tdir, "test.grpc")
|
fd := path.Join(tdir, "test.grpc")
|
||||||
listener, err := net.Listen("unix", fd)
|
listener, err := net.Listen("unix", fd)
|
||||||
|
|
|
@ -7,8 +7,8 @@ import (
|
||||||
"github.com/coredns/coredns/plugin/test"
|
"github.com/coredns/coredns/plugin/test"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getPEMFiles(t *testing.T) (rmFunc func(), cert, key, ca string) {
|
func getPEMFiles(t *testing.T) (cert, key, ca string) {
|
||||||
tempDir, rmFunc, err := test.WritePEMFiles("")
|
tempDir, err := test.WritePEMFiles(t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Could not write PEM files: %s", err)
|
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) {
|
func TestNewTLSConfig(t *testing.T) {
|
||||||
rmFunc, cert, key, ca := getPEMFiles(t)
|
cert, key, ca := getPEMFiles(t)
|
||||||
defer rmFunc()
|
|
||||||
|
|
||||||
_, err := NewTLSConfig(cert, key, ca)
|
_, err := NewTLSConfig(cert, key, ca)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -31,8 +30,7 @@ func TestNewTLSConfig(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewTLSClientConfig(t *testing.T) {
|
func TestNewTLSClientConfig(t *testing.T) {
|
||||||
rmFunc, _, _, ca := getPEMFiles(t)
|
_, _, ca := getPEMFiles(t)
|
||||||
defer rmFunc()
|
|
||||||
|
|
||||||
_, err := NewTLSClientConfig(ca)
|
_, err := NewTLSClientConfig(ca)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -41,8 +39,7 @@ func TestNewTLSClientConfig(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewTLSConfigFromArgs(t *testing.T) {
|
func TestNewTLSConfigFromArgs(t *testing.T) {
|
||||||
rmFunc, cert, key, ca := getPEMFiles(t)
|
cert, key, ca := getPEMFiles(t)
|
||||||
defer rmFunc()
|
|
||||||
|
|
||||||
_, err := NewTLSConfigFromArgs()
|
_, err := NewTLSConfigFromArgs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -81,8 +78,7 @@ func TestNewTLSConfigFromArgs(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewHTTPSTransport(t *testing.T) {
|
func TestNewHTTPSTransport(t *testing.T) {
|
||||||
rmFunc, _, _, ca := getPEMFiles(t)
|
_, _, ca := getPEMFiles(t)
|
||||||
defer rmFunc()
|
|
||||||
|
|
||||||
cc, err := NewTLSClientConfig(ca)
|
cc, err := NewTLSClientConfig(ca)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -3,6 +3,7 @@ package test
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TempFile will create a temporary file on disk and returns the name and a cleanup function to remove it later.
|
// 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
|
return f.Name(), rmFunc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// WritePEMFiles creates a tmp dir with ca.pem, cert.pem, and key.pem and the func to remove it
|
// WritePEMFiles creates a tmp dir with ca.pem, cert.pem, and key.pem
|
||||||
func WritePEMFiles(dir string) (string, func(), error) {
|
func WritePEMFiles(t *testing.T) (string, error) {
|
||||||
tempDir, err := os.MkdirTemp(dir, "go-test-pemfiles")
|
tempDir := t.TempDir()
|
||||||
if err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
data := `-----BEGIN CERTIFICATE-----
|
data := `-----BEGIN CERTIFICATE-----
|
||||||
MIIC9zCCAd+gAwIBAgIJALGtqdMzpDemMA0GCSqGSIb3DQEBCwUAMBIxEDAOBgNV
|
MIIC9zCCAd+gAwIBAgIJALGtqdMzpDemMA0GCSqGSIb3DQEBCwUAMBIxEDAOBgNV
|
||||||
|
@ -45,7 +43,7 @@ I1rs/VUGKzcJGVIWbHrgjP68CTStGAvKgbsTqw7aLXTSqtPw88N9XVSyRg==
|
||||||
-----END CERTIFICATE-----`
|
-----END CERTIFICATE-----`
|
||||||
path := filepath.Join(tempDir, "ca.pem")
|
path := filepath.Join(tempDir, "ca.pem")
|
||||||
if err := os.WriteFile(path, []byte(data), 0644); err != nil {
|
if err := os.WriteFile(path, []byte(data), 0644); err != nil {
|
||||||
return "", nil, err
|
return "", err
|
||||||
}
|
}
|
||||||
data = `-----BEGIN CERTIFICATE-----
|
data = `-----BEGIN CERTIFICATE-----
|
||||||
MIICozCCAYsCCQCRlf5BrvPuqjANBgkqhkiG9w0BAQsFADASMRAwDgYDVQQDDAdr
|
MIICozCCAYsCCQCRlf5BrvPuqjANBgkqhkiG9w0BAQsFADASMRAwDgYDVQQDDAdr
|
||||||
|
@ -65,8 +63,8 @@ zhDEPP4FhY+Sz+y1yWirphl7A1aZwhXVPcfWIGqpQ3jzNwUeocbH27kuLh+U4hQo
|
||||||
qeg10RdFnw==
|
qeg10RdFnw==
|
||||||
-----END CERTIFICATE-----`
|
-----END CERTIFICATE-----`
|
||||||
path = filepath.Join(tempDir, "cert.pem")
|
path = filepath.Join(tempDir, "cert.pem")
|
||||||
if err = os.WriteFile(path, []byte(data), 0644); err != nil {
|
if err := os.WriteFile(path, []byte(data), 0644); err != nil {
|
||||||
return "", nil, err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
data = `-----BEGIN RSA PRIVATE KEY-----
|
data = `-----BEGIN RSA PRIVATE KEY-----
|
||||||
|
@ -97,10 +95,9 @@ E/WObVJXDnBdViu0L9abE9iaTToBVri4cmlDlZagLuKVR+TFTCN/DSlVZTDkqkLI
|
||||||
8chzqtkH6b2b2R73hyRysWjsomys34ma3mEEPTX/aXeAF2MSZ/EWT9yL
|
8chzqtkH6b2b2R73hyRysWjsomys34ma3mEEPTX/aXeAF2MSZ/EWT9yL
|
||||||
-----END RSA PRIVATE KEY-----`
|
-----END RSA PRIVATE KEY-----`
|
||||||
path = filepath.Join(tempDir, "key.pem")
|
path = filepath.Join(tempDir, "key.pem")
|
||||||
if err = os.WriteFile(path, []byte(data), 0644); err != nil {
|
if err := os.WriteFile(path, []byte(data), 0644); err != nil {
|
||||||
return "", nil, err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
rmFunc := func() { os.RemoveAll(tempDir) }
|
return tempDir, nil
|
||||||
return tempDir, rmFunc, nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,11 +11,7 @@ import (
|
||||||
|
|
||||||
func TestAuto(t *testing.T) {
|
func TestAuto(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
tmpdir, err := os.MkdirTemp(os.TempDir(), "coredns")
|
tmpdir := t.TempDir()
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tmpdir)
|
|
||||||
|
|
||||||
corefile := `org:0 {
|
corefile := `org:0 {
|
||||||
auto {
|
auto {
|
||||||
|
@ -70,11 +66,7 @@ func TestAuto(t *testing.T) {
|
||||||
|
|
||||||
func TestAutoNonExistentZone(t *testing.T) {
|
func TestAutoNonExistentZone(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
tmpdir, err := os.MkdirTemp(os.TempDir(), "coredns")
|
tmpdir := t.TempDir()
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tmpdir)
|
|
||||||
|
|
||||||
corefile := `.:0 {
|
corefile := `.:0 {
|
||||||
auto {
|
auto {
|
||||||
|
@ -109,11 +101,7 @@ func TestAutoNonExistentZone(t *testing.T) {
|
||||||
func TestAutoAXFR(t *testing.T) {
|
func TestAutoAXFR(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
tmpdir, err := os.MkdirTemp(os.TempDir(), "coredns")
|
tmpdir := t.TempDir()
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tmpdir)
|
|
||||||
|
|
||||||
corefile := `org:0 {
|
corefile := `org:0 {
|
||||||
auto {
|
auto {
|
||||||
|
|
|
@ -9,11 +9,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func setupProxyTargetCoreDNS(t *testing.T, fn func(string)) {
|
func setupProxyTargetCoreDNS(t *testing.T, fn func(string)) {
|
||||||
tmpdir, err := os.MkdirTemp(os.TempDir(), "coredns")
|
tmpdir := t.TempDir()
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tmpdir)
|
|
||||||
|
|
||||||
content := `
|
content := `
|
||||||
example.org. IN SOA sns.dns.icann.org. noc.dns.icann.org. 1 3600 3600 3600 3600
|
example.org. IN SOA sns.dns.icann.org. noc.dns.icann.org. 1 3600 3600 3600 3600
|
||||||
|
@ -23,7 +19,7 @@ google.com. IN A 172.217.25.110
|
||||||
`
|
`
|
||||||
|
|
||||||
path := filepath.Join(tmpdir, "file")
|
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)
|
t.Fatalf("Could not write to temp file: %s", err)
|
||||||
}
|
}
|
||||||
defer os.Remove(path)
|
defer os.Remove(path)
|
||||||
|
|
|
@ -71,11 +71,7 @@ func TestMetricsRefused(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMetricsAuto(t *testing.T) {
|
func TestMetricsAuto(t *testing.T) {
|
||||||
tmpdir, err := os.MkdirTemp(os.TempDir(), "coredns")
|
tmpdir := t.TempDir()
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tmpdir)
|
|
||||||
|
|
||||||
corefile := `org:0 {
|
corefile := `org:0 {
|
||||||
auto {
|
auto {
|
||||||
|
|
Loading…
Add table
Reference in a new issue