-
Notifications
You must be signed in to change notification settings - Fork 23
/
test_helpers.go
75 lines (59 loc) · 1.7 KB
/
test_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package test_helpers
import (
"net"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"golang.org/x/crypto/ssh"
)
func WaitFor(f func() error) error {
ch := make(chan error)
go func() {
err := f()
ch <- err
}()
var err error
Eventually(ch, 10).Should(Receive(&err))
return err
}
func Pipe() (net.Conn, net.Conn) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
Expect(err).NotTo(HaveOccurred())
address := listener.Addr().String()
serverConnCh := make(chan net.Conn, 1)
go func(serverConnCh chan net.Conn, listener net.Listener) {
defer GinkgoRecover()
conn, err := listener.Accept()
Expect(err).NotTo(HaveOccurred())
serverConnCh <- conn
}(serverConnCh, listener)
clientConn, err := net.Dial("tcp", address)
Expect(err).NotTo(HaveOccurred())
return <-serverConnCh, clientConn
}
func NewClient(clientNetConn net.Conn, clientConfig *ssh.ClientConfig) *ssh.Client {
if clientConfig == nil {
clientConfig = &ssh.ClientConfig{
User: "username",
Auth: []ssh.AuthMethod{
ssh.Password("secret"),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
}
clientConn, clientChannels, clientRequests, clientConnErr := ssh.NewClientConn(clientNetConn, "0.0.0.0", clientConfig)
Expect(clientConnErr).NotTo(HaveOccurred())
return ssh.NewClient(clientConn, clientChannels, clientRequests)
}
type TestNetError struct {
timeout bool
temporary bool
}
func NewTestNetError(timeout, temporary bool) *TestNetError {
return &TestNetError{
timeout: timeout,
temporary: temporary,
}
}
func (e *TestNetError) Error() string { return "test error" }
func (e *TestNetError) Timeout() bool { return e.timeout }
func (e *TestNetError) Temporary() bool { return e.temporary }