mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-07 19:38:49 +03:00
Ensure some tests remove temporary directories they created
This commit is contained in:
parent
e2917784d0
commit
36f8759a7b
2 changed files with 15 additions and 9 deletions
|
@ -746,14 +746,16 @@ func TestFiles(t *testing.T) {
|
||||||
|
|
||||||
// Create directory / files from test case.
|
// Create directory / files from test case.
|
||||||
if test.fileNames != nil {
|
if test.fileNames != nil {
|
||||||
dirPath, err = ioutil.TempDir(fmt.Sprintf("%s", context.Root), "caddy_test")
|
dirPath, err = ioutil.TempDir(fmt.Sprintf("%s", context.Root), "caddy_ctxtest")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
os.RemoveAll(dirPath)
|
||||||
t.Fatalf(testPrefix+"Expected no error creating directory, got: '%s'", err.Error())
|
t.Fatalf(testPrefix+"Expected no error creating directory, got: '%s'", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, name := range test.fileNames {
|
for _, name := range test.fileNames {
|
||||||
absFilePath := filepath.Join(dirPath, name)
|
absFilePath := filepath.Join(dirPath, name)
|
||||||
if err = ioutil.WriteFile(absFilePath, []byte(""), os.ModePerm); err != nil {
|
if err = ioutil.WriteFile(absFilePath, []byte(""), os.ModePerm); err != nil {
|
||||||
|
os.RemoveAll(dirPath)
|
||||||
t.Fatalf(testPrefix+"Expected no error creating file, got: '%s'", err.Error())
|
t.Fatalf(testPrefix+"Expected no error creating file, got: '%s'", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -229,10 +229,11 @@ func TestUnixSocketProxy(t *testing.T) {
|
||||||
}))
|
}))
|
||||||
|
|
||||||
// Get absolute path for unix: socket
|
// Get absolute path for unix: socket
|
||||||
dir, err := ioutil.TempDir("", "caddy_test")
|
dir, err := ioutil.TempDir("", "caddy_proxytest")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to make temp dir to contain unix socket. %v", err)
|
t.Fatalf("Failed to make temp dir to contain unix socket. %v", err)
|
||||||
}
|
}
|
||||||
|
defer os.RemoveAll(dir)
|
||||||
socketPath := filepath.Join(dir, "test_socket")
|
socketPath := filepath.Join(dir, "test_socket")
|
||||||
|
|
||||||
// Change httptest.Server listener to listen to unix: socket
|
// Change httptest.Server listener to listen to unix: socket
|
||||||
|
@ -283,20 +284,21 @@ func GetHTTPProxy(messageFormat string, prefix string) (*Proxy, *httptest.Server
|
||||||
return newPrefixedWebSocketTestProxy(ts.URL, prefix), ts
|
return newPrefixedWebSocketTestProxy(ts.URL, prefix), ts
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSocketProxy(messageFormat string, prefix string) (*Proxy, *httptest.Server, error) {
|
func GetSocketProxy(messageFormat string, prefix string) (*Proxy, *httptest.Server, string, error) {
|
||||||
ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprintf(w, messageFormat, r.URL.String())
|
fmt.Fprintf(w, messageFormat, r.URL.String())
|
||||||
}))
|
}))
|
||||||
|
|
||||||
dir, err := ioutil.TempDir("", "caddy_test")
|
dir, err := ioutil.TempDir("", "caddy_proxytest")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("Failed to make temp dir to contain unix socket. %v", err)
|
return nil, nil, dir, fmt.Errorf("Failed to make temp dir to contain unix socket. %v", err)
|
||||||
}
|
}
|
||||||
socketPath := filepath.Join(dir, "test_socket")
|
socketPath := filepath.Join(dir, "test_socket")
|
||||||
|
|
||||||
ln, err := net.Listen("unix", socketPath)
|
ln, err := net.Listen("unix", socketPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("Unable to listen: %v", err)
|
os.RemoveAll(dir)
|
||||||
|
return nil, nil, dir, fmt.Errorf("Unable to listen: %v", err)
|
||||||
}
|
}
|
||||||
ts.Listener = ln
|
ts.Listener = ln
|
||||||
|
|
||||||
|
@ -304,7 +306,7 @@ func GetSocketProxy(messageFormat string, prefix string) (*Proxy, *httptest.Serv
|
||||||
|
|
||||||
tsURL := strings.Replace(ts.URL, "http://", "unix:", 1)
|
tsURL := strings.Replace(ts.URL, "http://", "unix:", 1)
|
||||||
|
|
||||||
return newPrefixedWebSocketTestProxy(tsURL, prefix), ts, nil
|
return newPrefixedWebSocketTestProxy(tsURL, prefix), ts, dir, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTestServerMessage(p *Proxy, ts *httptest.Server, path string) (string, error) {
|
func GetTestServerMessage(p *Proxy, ts *httptest.Server, path string) (string, error) {
|
||||||
|
@ -370,8 +372,7 @@ func TestUnixSocketProxyPaths(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
p, ts, err := GetSocketProxy(greeting, test.prefix)
|
p, ts, tmpdir, err := GetSocketProxy(greeting, test.prefix)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Getting socket proxy failed - %v", err)
|
t.Fatalf("Getting socket proxy failed - %v", err)
|
||||||
}
|
}
|
||||||
|
@ -379,12 +380,15 @@ func TestUnixSocketProxyPaths(t *testing.T) {
|
||||||
actualMsg, err := GetTestServerMessage(p, ts, test.url)
|
actualMsg, err := GetTestServerMessage(p, ts, test.url)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
os.RemoveAll(tmpdir)
|
||||||
t.Fatalf("Getting server message failed - %v", err)
|
t.Fatalf("Getting server message failed - %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if actualMsg != test.expected {
|
if actualMsg != test.expected {
|
||||||
t.Errorf("Expected '%s' but got '%s' instead", test.expected, actualMsg)
|
t.Errorf("Expected '%s' but got '%s' instead", test.expected, actualMsg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
os.RemoveAll(tmpdir)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue