aboutsummaryrefslogtreecommitdiff
path: root/plugin/test/file.go
diff options
context:
space:
mode:
authorGravatar Eng Zer Jun <engzerjun@gmail.com> 2023-06-17 21:21:01 +0800
committerGravatar GitHub <noreply@github.com> 2023-06-17 15:21:01 +0200
commitb868350fc2823f429cc828558f3425d5135e680f (patch)
treeab6af66c78d1d0ca1c5bf289d6664317af3a397b /plugin/test/file.go
parent06cd8439182be8b9ed17280eaae411f9afb7c8f2 (diff)
downloadcoredns-b868350fc2823f429cc828558f3425d5135e680f.tar.gz
coredns-b868350fc2823f429cc828558f3425d5135e680f.tar.zst
coredns-b868350fc2823f429cc828558f3425d5135e680f.zip
test: use `t.TempDir` to create temporary test directory (#6164)
Diffstat (limited to 'plugin/test/file.go')
-rw-r--r--plugin/test/file.go23
1 files changed, 10 insertions, 13 deletions
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
}