-
Notifications
You must be signed in to change notification settings - Fork 4
/
connector.go
44 lines (36 loc) · 1.43 KB
/
connector.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
package server
import (
"golang.org/x/crypto/ssh"
)
// Connector is an interface to be able to mock SSH connections
type Connector interface {
NewClient(remoteServer string, clientConfig *ssh.ClientConfig) (*ssh.Client, error)
NewSession(client *ssh.Client) (*ssh.Session, error)
Run(session *ssh.Session, command string) error
CombinedOutput(session *ssh.Session, command string) ([]byte, error)
CloseSession(session *ssh.Session) error
}
// SSHConnector is used tp replace the standard SSH library functions
type SSHConnector struct {
Description string
}
// NewClient makes it possible to mock SSH dial
func (c SSHConnector) NewClient(remoteServer string, clientConfig *ssh.ClientConfig) (*ssh.Client, error) {
return ssh.Dial("tcp", remoteServer, clientConfig)
}
// NewSession makes it possible to mock a SSH session
func (c SSHConnector) NewSession(client *ssh.Client) (*ssh.Session, error) {
return client.NewSession()
}
// Run makes it possible to mock a remote Run command
func (c SSHConnector) Run(session *ssh.Session, command string) error {
return session.Run(command)
}
// CombinedOutput makes it possible to mock a local CombinedOutput
func (c SSHConnector) CombinedOutput(session *ssh.Session, command string) ([]byte, error) {
return session.CombinedOutput(command)
}
// CloseSession makes it possible to mock the closing of a session
func (c SSHConnector) CloseSession(session *ssh.Session) error {
return session.Close()
}