-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy patheasyssh_test.go
60 lines (56 loc) · 1.24 KB
/
easyssh_test.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
package easyssh
import (
"testing"
)
var sshConfig = &MakeConfig{
User: "root",
Server: "172.30.19.2",
Password: "password",
//Key: "/.ssh/id_rsa",
Port: "22",
}
func TestStream(t *testing.T) {
t.Parallel()
// input command/output string pairs
testCases := [][]string{
{`for i in $(seq 1 5); do echo "$i"; done`, "12345"},
{`echo "test"`, "test"},
}
for _, testCase := range testCases {
outChannel, errChannel, done, err := sshConfig.Stream(testCase[0], 10)
if err != nil {
t.Errorf("Stream failed: %s", err)
}
stillGoing := true
stdout := ""
stderr := ""
for stillGoing {
select {
case <-done:
stillGoing = false
case line := <-outChannel:
stdout += line
case line := <-errChannel:
stderr += line
}
}
if stdout != testCase[1] {
t.Error("Output didn't match expected: %s,%s", stdout, stderr)
}
}
}
func TestRun(t *testing.T) {
t.Parallel()
commands := []string{
"echo test", `for i in $(ls); do echo "$i"; done`, "ls",
}
for _, cmd := range commands {
stdout, stderr, istimeout, err := sshConfig.Run(cmd, 10)
if err != nil {
t.Errorf("Run failed: %s", err)
}
if stdout == "" {
t.Errorf("Output was empty for command: %s,%s,%s", cmd, stdout, stderr, istimeout)
}
}
}