forked from helloyi/go-sshclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshclient_test.go
170 lines (149 loc) · 3.71 KB
/
sshclient_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package sshclient
import (
"bytes"
"flag"
"os"
"testing"
)
var (
addr string
user string
passwd string
prikey string
)
func TestDailWithPasswd(t *testing.T) {
client, err := DialWithPasswd(addr, user, passwd)
if err != nil {
t.Fatal("DialWithPasswd err: ", err)
}
if err := client.Close(); err != nil {
t.Fatal("client.Close err: ", err)
}
}
func TestDailWithKey(t *testing.T) {
client, err := DialWithKey(addr, user, prikey)
if err != nil {
t.Fatal("DialWithPasswd err: ", err)
}
if err := client.Close(); err != nil {
t.Fatal("client.Close err: ", err)
}
}
func TestCmdRun(t *testing.T) {
client, err := DialWithPasswd(addr, user, passwd)
if err != nil {
t.Fatal("DialWithPasswd err: ", err)
}
defer client.Close()
var stdout bytes.Buffer
var stderr bytes.Buffer
err = client.Cmd("echo stdout").Cmd(">&2 echo stderr").SetStdio(&stdout, &stderr).Run()
if err != nil {
t.Fatal("Run command err: ", err)
}
if stdout.String() != "stdout\n" {
t.Fatal("Command output mismatching on stdout")
}
if stderr.String() != "stderr\n" {
t.Fatal("Command output mismatching on stderr")
}
}
func TestScriptRun(t *testing.T) {
client, err := DialWithPasswd(addr, user, passwd)
if err != nil {
t.Fatal("DialWithPasswd err: ", err)
}
defer client.Close()
script := `
echo stdout
>&2 echo stderr
`
var stdout bytes.Buffer
var stderr bytes.Buffer
err = client.Script(script).SetStdio(&stdout, &stderr).Run()
if err != nil {
t.Fatal("Run command err: ", err)
}
if stdout.String() != "stdout\n" {
t.Fatal("Command output mismatching on stdout")
}
if stderr.String() != "stderr\n" {
t.Fatal("Command output mismatching on stderr")
}
}
func TestScriptFileRun(t *testing.T) {
client, err := DialWithPasswd(addr, user, passwd)
if err != nil {
t.Fatal("DialWithPasswd err: ", err)
}
defer client.Close()
}
func TestRemoteScriptOutput(t *testing.T) {
client, err := DialWithPasswd(addr, user, passwd)
if err != nil {
t.Fatal("DialWithPasswd err: ", err)
}
defer client.Close()
out, err := client.Cmd("echo a").Output()
if err != nil {
t.Fatal("Run command err: ", err)
}
if string(out) != "a\n" {
t.Fatal("Command output mismatching on stdout")
}
}
func TestRemoteScriptSmartOutput(t *testing.T) {
client, err := DialWithPasswd(addr, user, passwd)
if err != nil {
t.Fatal("DialWithPasswd err: ", err)
}
defer client.Close()
out, err := client.Cmd("echo a").SmartOutput()
if err != nil {
t.Fatal("Run command err: ", err)
}
if string(out) != "a\n" {
t.Fatal("Command output mismatching on stdout")
}
script := `
>&2 echo error
exit 123
`
out, err = client.Script(script).SmartOutput()
if err == nil {
t.Fatal("Run script err")
}
if string(out) != "error\n" {
t.Fatal("Command output mismatching on stderr")
}
}
func TestShell(t *testing.T) {
client, err := DialWithPasswd(addr, user, passwd)
if err != nil {
t.Fatal("DialWithPasswd err: ", err)
}
defer client.Close()
script := bytes.NewBufferString("echo stdout\n >&2 echo stderr")
var (
stdout bytes.Buffer
stderr bytes.Buffer
)
err = client.Shell().SetStdio(script, &stdout, &stderr).Start()
if err != nil {
t.Fatal("Start shell faield: ", err)
}
if stdout.String() != "stdout" == false {
t.Fatal("Command output mismatching on stdout")
}
if stderr.String() != "stderr" == false {
t.Fatal("Command output mismatching on stderr")
}
}
func TestMain(m *testing.M) {
flag.StringVar(&addr, "addr", "localhost:22", "The host of ssh")
flag.StringVar(&user, "user", "username", "The user of login")
flag.StringVar(&passwd, "passwd", "yourpasswd", "The passwd of user")
flag.StringVar(&prikey, "privatekey", "/path/to/your/key", "The privatekey of user")
flag.Parse()
os.Exit(m.Run())
}