-
Notifications
You must be signed in to change notification settings - Fork 5
/
server_test.go
159 lines (145 loc) · 3.57 KB
/
server_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
package sshclient
import (
"fmt"
"os"
"strings"
"testing"
"golang.org/x/crypto/ssh"
)
const (
testUsername = "joebob"
testPassword = "howdy!"
)
var (
testPort int
)
func testOptions(t *testing.T) *ServerOptions {
return &ServerOptions{
Username: testUsername,
Password: testPassword,
Port: &testPort,
Logger: t,
KeyFile: "~/.ssh/id_rsa",
}
}
func testServer(t *testing.T, options *ServerOptions) {
t.Helper()
if options == nil {
options = testOptions(t)
}
close, err := Server(options)
if err != nil {
t.Fatal(err)
}
t.Cleanup(close)
t.Logf("test server running")
}
func TestLocal(t *testing.T) {
testServer(t, nil)
cmd := "hostname"
timeout := 5
t.Logf("server port is: %d\n", testPort)
host := fmt.Sprintf("localhost:%d", testPort)
r, err := ExecPassword(host, testUsername, testPassword, cmd, timeout)
if err != nil {
t.Fatal("ssh connect error:", err)
}
if r.RC > 0 {
t.Error("ssh execution error:", r.Stderr)
} else if len(r.Stderr) > 0 {
t.Error("ssh execution error:", r.Stderr)
} else {
t.Log("client returned:", r.Stdout)
}
}
func TestLocalError(t *testing.T) {
cmd := "foo" // command is ignored, so what ev er
stdout := "meh"
stderr := "we have a failure to communicate"
rc := 23
options := testOptions(t)
options.Exec = &MockHandler{RC: rc, Stdout: stdout, Stderr: stderr}
testServer(t, options)
timeout := 1
host := fmt.Sprintf("localhost:%d", testPort)
r, err := ExecPassword(host, testUsername, testPassword, cmd, timeout)
if err != nil {
if err, ok := err.(*ssh.ExitError); ok {
t.Logf("got expected error: %+v", err)
} else {
t.Errorf("ssh connect error (%T): %+v", err, err)
}
}
if r.RC != rc {
t.Errorf("rc want: %d -- got: %d\n", rc, r.RC)
}
if r.Stdout != stdout {
t.Errorf("stdout want: %q -- got: %q\n", stdout, r.Stdout)
}
if r.Stderr != stderr {
t.Errorf("stderr want: %q -- got: %q\n", stderr, r.Stderr)
}
}
func TestLocalBash(t *testing.T) {
cmd := "hostname"
stdout, err := os.Hostname()
if err != nil {
t.Fatalf("error getting hostname: %+v\n", err)
}
stderr := ""
rc := 0
options := testOptions(t)
options.Exec = &BashHandler{}
testServer(t, options)
timeout := 1
host := fmt.Sprintf("localhost:%d", testPort)
r, err := ExecPassword(host, testUsername, testPassword, cmd, timeout)
if err != nil {
if err, ok := err.(*ssh.ExitError); ok {
t.Logf("got expected error: %+v", err)
} else {
t.Errorf("ssh connect error (%T): %+v", err, err)
}
}
t.Logf("REPLY: %+v\n", r)
if r.RC != rc {
t.Errorf("rc want: %d -- got: %d\n", rc, r.RC)
}
out := strings.TrimSpace(r.Stdout)
if out != stdout {
t.Errorf("stdout want: %q -- got: %q\n", stdout, out)
}
if r.Stderr != stderr {
t.Errorf("stderr want: %q -- got: %q\n", stderr, r.Stderr)
}
}
func TestLocalBashError(t *testing.T) {
cmd := "foo" // this should be an invalid command
stdout := ""
stderr := "bash: foo: command not found\n"
rc := 127
options := testOptions(t)
options.Exec = &BashHandler{}
testServer(t, options)
timeout := 1
host := fmt.Sprintf("localhost:%d", testPort)
r, err := ExecPassword(host, testUsername, testPassword, cmd, timeout)
if err != nil {
if err, ok := err.(*ssh.ExitError); ok {
t.Logf("got expected error: %+v", err)
} else {
t.Errorf("ssh connect error (%T): %+v", err, err)
}
}
t.Logf("REPLY: %+v\n", r)
if r.RC != rc {
t.Errorf("rc want: %d -- got: %d\n", rc, r.RC)
}
out := strings.TrimSpace(r.Stdout)
if out != stdout {
t.Errorf("stdout want: %q -- got: %q\n", stdout, out)
}
if r.Stderr != stderr {
t.Errorf("stderr want: %q -- got: %q\n", stderr, r.Stderr)
}
}