-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh_pool_test.go
More file actions
72 lines (59 loc) · 1.52 KB
/
ssh_pool_test.go
File metadata and controls
72 lines (59 loc) · 1.52 KB
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
package shaw
import (
"bufio"
"bytes"
"os/user"
"testing"
"github.com/emptyinterface/extio"
"github.com/emptyinterface/shaw/test"
"golang.org/x/crypto/ssh"
)
func TestPoolExec(t *testing.T) {
const poolsize = 10
var servers []*test.SSHExecServer
for i := 0; i < poolsize; i++ {
server := test.NewTestSSHExecServer(t, test.ExecFunc)
defer server.Close()
servers = append(servers, server)
}
u, _ := user.Current()
pool := NewSSHPool()
for i := 0; i < poolsize; i++ {
config := &ssh.ClientConfig{
User: u.Username,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
pool.AddClient(NewSSHClient(servers[i].Host, config))
}
var stdout []string
for _, report := range pool.NewCommandSession(&Command{
Executor: BinBashExecutor,
Command: "whoami",
Stdout: extio.NewScannerWriter(bufio.ScanLines, 1<<10, func(token []byte) error {
stdout = append(stdout, string(token))
return nil
}),
Stderr: &bytes.Buffer{},
}).Run() {
if report.err != nil {
t.Error(report.err)
}
if report.start.IsZero() {
t.Errorf("Expected non-zero start time")
}
if report.end.IsZero() {
t.Errorf("Expected non-zero end time")
}
if stderr := report.cmd.Stderr.(*bytes.Buffer); stderr.Len() > 0 {
t.Errorf("Expected 0 bytes from stderr, got %q", stderr.String())
}
}
if len(stdout) != poolsize {
t.Errorf("Expected %d lines back, got %d", poolsize, len(stdout))
}
for _, line := range stdout {
if line != u.Username {
t.Errorf("Expected %q, got %q", u.Username, line)
}
}
}