From 406dbbbf4bdf6a89c92b2dc47b1691598ce2549c Mon Sep 17 00:00:00 2001 From: Philip O'Toole Date: Thu, 24 Sep 2020 20:29:15 -0400 Subject: [PATCH] Support directory path for temp test x509 files --- tcp/transport_test.go | 8 ++++---- testdata/x509/resources.go | 31 +++++++++++++++++-------------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/tcp/transport_test.go b/tcp/transport_test.go index 782f6655..9b444e6e 100644 --- a/tcp/transport_test.go +++ b/tcp/transport_test.go @@ -41,9 +41,9 @@ func Test_TransportDial(t *testing.T) { } func Test_NewTLSTransport(t *testing.T) { - c := x509.CertFile() + c := x509.CertFile("") defer os.Remove(c) - k := x509.KeyFile() + k := x509.KeyFile("") defer os.Remove(k) if NewTLSTransport(c, k, true) == nil { @@ -52,9 +52,9 @@ func Test_NewTLSTransport(t *testing.T) { } func Test_TLSTransportOpenClose(t *testing.T) { - c := x509.CertFile() + c := x509.CertFile("") defer os.Remove(c) - k := x509.KeyFile() + k := x509.KeyFile("") defer os.Remove(k) tn := NewTLSTransport(c, k, true) diff --git a/testdata/x509/resources.go b/testdata/x509/resources.go index 737e0d16..b72e14d0 100644 --- a/testdata/x509/resources.go +++ b/testdata/x509/resources.go @@ -4,32 +4,35 @@ import ( "io/ioutil" ) -// CertFile returns the path to a temporary file containing a cert. -// It is up to the caller to remove the file when finished. -func CertFile() string { - return mustWriteToFile(cert) +// CertFile returns the path to a temporary file, in directory dir, containing a cert. +// It is up to the caller to remove the file when finished. If dir is the empty string +// then the default directory for temporary files is used. +func CertFile(dir string) string { + return mustWriteToFile(dir, cert) } -// KeyFile returns the path to a temporary file containing a key. -// It is up to the caller to remove the file when finished. -func KeyFile() string { - return mustWriteToFile(key) +// KeyFile returns the path to a temporary file, in directory dir, containing a key. +// It is up to the caller to remove the file when finished.If dir is the empty string +// then the default directory for temporary files is used. +func KeyFile(dir string) string { + return mustWriteToFile(dir, key) } -func mustWriteToFile(content string) string { +func mustWriteToFile(dir, content string) string { b := []byte(content) - path := mustTempFile() + path := mustTempFile(dir) if err := ioutil.WriteFile(path, b, 0600); err != nil { panic(err.Error()) } return path } -// mustTempFile returns a path to a temporary file. It is up to the -// caller to remove the file once it is no longer needed. -func mustTempFile() string { - tmpfile, err := ioutil.TempFile("", "rqlite-tls-test") +// mustTempFile returns a path to a temporary file in directory dir. It is up to the +// caller to remove the file once it is no longer needed. If dir is the empty +// string, then the default directory for temporary files is used. +func mustTempFile(dir string) string { + tmpfile, err := ioutil.TempFile(dir, "rqlite-tls-test") if err != nil { panic(err.Error()) }