forked from hypersleep/easyssh
-
Notifications
You must be signed in to change notification settings - Fork 5
/
easyssh_test.go
57 lines (53 loc) · 1.09 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
package easyssh
import (
"testing"
)
var sshConfig = &MakeConfig{
User: "username",
Server: "example.com",
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 {
channel, done, err := sshConfig.Stream(testCase[0])
if err != nil {
t.Errorf("Stream failed: %s", err)
}
stillGoing := true
output := ""
for stillGoing {
select {
case <-done:
stillGoing = false
case line := <-channel:
output += line
}
}
if output != testCase[1] {
t.Error("Output didn't match expected: %s", output)
}
}
}
func TestRun(t *testing.T) {
t.Parallel()
commands := []string{
"echo test", `for i in $(ls); do echo "$i"; done`, "ls",
}
for _, cmd := range commands {
out, err := sshConfig.Run(cmd)
if err != nil {
t.Errorf("Run failed: %s", err)
}
if out == "" {
t.Errorf("Output was empty for command: %s", cmd)
}
}
}