mirror of https://github.com/knative/func.git
chore: use t.Cleanup() more (#1315)
Signed-off-by: Matej Vasek <mvasek@redhat.com> Signed-off-by: Matej Vasek <mvasek@redhat.com>
This commit is contained in:
parent
3f5933ec73
commit
bf52811f18
|
@ -74,8 +74,7 @@ func TestCheckAuth(t *testing.T) {
|
|||
incorrectPwd = "badpwd"
|
||||
)
|
||||
|
||||
localhost, localhostTLS, stopServer := startServer(t, uname, pwd)
|
||||
t.Cleanup(stopServer)
|
||||
localhost, localhostTLS := startServer(t, uname, pwd)
|
||||
|
||||
_, portTLS, err := net.SplitHostPort(localhostTLS)
|
||||
if err != nil {
|
||||
|
@ -162,15 +161,14 @@ func TestCheckAuth(t *testing.T) {
|
|||
|
||||
func TestCheckAuthEmptyCreds(t *testing.T) {
|
||||
|
||||
localhost, _, stopServer := startServer(t, "", "")
|
||||
t.Cleanup(stopServer)
|
||||
localhost, _ := startServer(t, "", "")
|
||||
err := creds.CheckAuth(context.Background(), localhost+"/someorg/someimage:sometag", docker.Credentials{}, http.DefaultTransport)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func startServer(t *testing.T, uname, pwd string) (addr, addrTLS string, stopServer func()) {
|
||||
func startServer(t *testing.T, uname, pwd string) (addr, addrTLS string) {
|
||||
// TODO: this should be refactored to use OS-chosen ports so as not to
|
||||
// fail when a user is running a function on the default port.)
|
||||
listener, err := net.Listen("tcp", "localhost:0")
|
||||
|
@ -290,13 +288,15 @@ func startServer(t *testing.T, uname, pwd string) (addr, addrTLS string, stopSer
|
|||
return dc(ctx, network, addr)
|
||||
}
|
||||
|
||||
return addr, addrTLS, func() {
|
||||
t.Cleanup(func() {
|
||||
err := server.Shutdown(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
http.DefaultTransport = oldDefaultTransport
|
||||
}
|
||||
})
|
||||
|
||||
return addr, addrTLS
|
||||
}
|
||||
|
||||
const (
|
||||
|
|
|
@ -23,15 +23,14 @@ import (
|
|||
)
|
||||
|
||||
func TestNewDockerClientWithPodmanMachine(t *testing.T) {
|
||||
defer withCleanHome(t)()
|
||||
withCleanHome(t)
|
||||
|
||||
publicKey, privateKeyPath := prepareKeys(t)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*1)
|
||||
defer cancel()
|
||||
|
||||
sshConf, stopSSH := startSSH(t, publicKey)
|
||||
defer stopSSH()
|
||||
sshConf := startSSH(t, publicKey)
|
||||
_ = sshConf
|
||||
|
||||
uri := fmt.Sprintf("ssh://user@%s%s", sshConf.address, sshDockerSocket)
|
||||
|
|
|
@ -25,15 +25,14 @@ import (
|
|||
)
|
||||
|
||||
func TestNewDockerClientWithSSH(t *testing.T) {
|
||||
defer withCleanHome(t)()
|
||||
withCleanHome(t)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*1)
|
||||
defer cancel()
|
||||
|
||||
sshConf, stopSSH := startSSH(t)
|
||||
defer stopSSH()
|
||||
sshConf := startSSH(t)
|
||||
|
||||
defer withKnowHosts(t, sshConf.address, sshConf.pubHostKey)()
|
||||
withKnowHosts(t, sshConf.address, sshConf.pubHostKey)
|
||||
|
||||
t.Setenv("DOCKER_HOST", fmt.Sprintf("ssh://user:pwd@%s", sshConf.address))
|
||||
|
||||
|
@ -61,7 +60,7 @@ type sshConfig struct {
|
|||
}
|
||||
|
||||
// emulates remote machine with docker unix socket at "/some/path/docker.sock"
|
||||
func startSSH(t *testing.T, authorizedKeys ...ssh.PublicKey) (settings sshConfig, stopSSH func()) {
|
||||
func startSSH(t *testing.T, authorizedKeys ...ssh.PublicKey) (settings sshConfig) {
|
||||
var err error
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
@ -102,7 +101,7 @@ func startSSH(t *testing.T, authorizedKeys ...ssh.PublicKey) (settings sshConfig
|
|||
}
|
||||
|
||||
dockerDaemonServer := http.Server{}
|
||||
stopSSH = func() {
|
||||
t.Cleanup(func() {
|
||||
var err error
|
||||
cancel()
|
||||
|
||||
|
@ -125,7 +124,7 @@ func startSSH(t *testing.T, authorizedKeys ...ssh.PublicKey) (settings sshConfig
|
|||
t.Error(err)
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
settings.address = sshTCPListener.Addr().String()
|
||||
|
||||
|
@ -331,7 +330,7 @@ func (l listener) Addr() net.Addr {
|
|||
|
||||
// sets clean temporary $HOME for test
|
||||
// this prevents interaction with actual user home which may contain .ssh/
|
||||
func withCleanHome(t *testing.T) func() {
|
||||
func withCleanHome(t *testing.T) {
|
||||
t.Helper()
|
||||
homeName := "HOME"
|
||||
if runtime.GOOS == "windows" {
|
||||
|
@ -344,18 +343,18 @@ func withCleanHome(t *testing.T) func() {
|
|||
oldHome, hadHome := os.LookupEnv(homeName)
|
||||
os.Setenv(homeName, tmpDir)
|
||||
|
||||
return func() {
|
||||
t.Cleanup(func() {
|
||||
if hadHome {
|
||||
os.Setenv(homeName, oldHome)
|
||||
} else {
|
||||
os.Unsetenv(homeName)
|
||||
}
|
||||
os.RemoveAll(tmpDir)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// withKnowHosts creates $HOME/.ssh/known_hosts that trust the host
|
||||
func withKnowHosts(t *testing.T, host string, pubKey ssh.PublicKey) func() {
|
||||
func withKnowHosts(t *testing.T, host string, pubKey ssh.PublicKey) {
|
||||
t.Helper()
|
||||
|
||||
var err error
|
||||
|
@ -385,7 +384,7 @@ func withKnowHosts(t *testing.T, host string, pubKey ssh.PublicKey) func() {
|
|||
|
||||
fmt.Fprintf(knownHostFile, "%s %s\n", host, string(ssh.MarshalAuthorizedKey(pubKey)))
|
||||
|
||||
return func() {
|
||||
t.Cleanup(func() {
|
||||
os.Remove(knownHosts)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ func TestNewClient(t *testing.T) {
|
|||
sock := filepath.Join(tmpDir, "docker.sock")
|
||||
dockerHost := fmt.Sprintf("unix://%s", sock)
|
||||
|
||||
defer startMockDaemonUnix(t, sock)()
|
||||
startMockDaemonUnix(t, sock)
|
||||
|
||||
t.Setenv("DOCKER_HOST", dockerHost)
|
||||
|
||||
|
@ -89,7 +89,7 @@ func TestNewClient_DockerHost(t *testing.T) {
|
|||
|
||||
}
|
||||
|
||||
func startMockDaemon(t *testing.T, listener net.Listener) func() {
|
||||
func startMockDaemon(t *testing.T, listener net.Listener) {
|
||||
server := http.Server{}
|
||||
// mimics /_ping endpoint
|
||||
server.Handler = http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
|
@ -102,7 +102,7 @@ func startMockDaemon(t *testing.T, listener net.Listener) func() {
|
|||
go func() {
|
||||
serErrChan <- server.Serve(listener)
|
||||
}()
|
||||
return func() {
|
||||
t.Cleanup(func() {
|
||||
err := server.Shutdown(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
@ -111,13 +111,13 @@ func startMockDaemon(t *testing.T, listener net.Listener) func() {
|
|||
if err != nil && !strings.Contains(err.Error(), "Server closed") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func startMockDaemonUnix(t *testing.T, sock string) func() {
|
||||
func startMockDaemonUnix(t *testing.T, sock string) {
|
||||
l, err := net.Listen("unix", sock)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return startMockDaemon(t, l)
|
||||
startMockDaemon(t, l)
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ func TestNewClientWinPipe(t *testing.T) {
|
|||
|
||||
const testNPipe = "test-npipe"
|
||||
|
||||
defer startMockDaemonWinPipe(t, testNPipe)()
|
||||
startMockDaemonWinPipe(t, testNPipe)
|
||||
t.Setenv("DOCKER_HOST", fmt.Sprintf("npipe:////./pipe/%s", testNPipe))
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*1)
|
||||
|
@ -37,10 +37,10 @@ func TestNewClientWinPipe(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func startMockDaemonWinPipe(t *testing.T, pipeName string) func() {
|
||||
func startMockDaemonWinPipe(t *testing.T, pipeName string) {
|
||||
p, err := winio.ListenPipe(`\\.\pipe\`+pipeName, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return startMockDaemon(t, p)
|
||||
startMockDaemon(t, p)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue