-
Notifications
You must be signed in to change notification settings - Fork 5
/
client_test.go
186 lines (165 loc) · 4.01 KB
/
client_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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2016-2020 Paul Stuart. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sshclient
import (
"os"
"strings"
"testing"
"github.com/joho/godotenv"
)
const (
DefaultEnvFile = "testing.env"
EnvFileEnv = "ENV_FILE" // to override DefaultEnvFile
)
var (
host, username, password, keyfile string
keybytes []byte
)
// Using envs to avoid accidentally baking creds into code
func init() {
envFile := os.Getenv(EnvFileEnv)
if envFile == "" {
envFile = DefaultEnvFile
}
_ = godotenv.Load(envFile)
host = os.Getenv("SSH_HOST")
if host == "" {
panic("Error: SSH_HOST not set")
}
username = os.Getenv("SSH_USERNAME")
if username == "" {
panic("Error: SSH_USERNAME not set")
}
password = os.Getenv("SSH_PASSWORD")
if password == "" {
panic("Error: SSH_PASSWORD not set")
}
keyfile = os.Getenv("SSH_PRIVATE")
if keyfile == "" {
panic("Error: SSH_PRIVATE not set (private keyfile)")
}
keybytes = []byte(os.Getenv("SSH_KEY"))
if keyfile == "" {
panic("Error: SSH_KEY not set (private key variable)")
}
}
func TestSSHKey(t *testing.T) {
keyauth, err := AuthKeyFile(keyfile)
if err != nil {
t.Fatal("keyauth error:", err)
}
client, err := DialSSH(host, username, 5, keyauth)
if err != nil {
t.Fatal("keyauth dial error:", err)
}
cmd := "uptime"
_, err = Run(client, cmd)
if err != nil {
t.Fatal("keyauth run error:", err)
}
}
func TestSSHKeyAuth(t *testing.T) {
t.Skip("bad envs?")
client, err := DialKey(host, username, keybytes, 5)
if err != nil {
t.Fatal("key auth dial error:", err)
}
cmd := "logname"
r, err := Run(client, cmd)
if err != nil {
t.Fatal("key auth run error:", err)
}
if strings.TrimSpace(r.Stdout) != username {
t.Fatal("keyauth command failed. expected", username, "got", r.Stdout)
}
}
func TestSSHKeyFileAuth(t *testing.T) {
client, err := DialKeyFile(host, username, keyfile, 5)
if err != nil {
t.Fatal("keyfile auth dial error:", err)
}
client.Buffered()
_ = client.Terminal()
cmd := "logname"
r, err := Run(client, cmd)
if err != nil {
t.Fatal("keyfile auth run error:", err)
}
if r.Stderr != "" {
t.Log(r.Stderr)
}
if strings.TrimSpace(r.Stdout) != username {
t.Fatalf("want: %q -- got: %q", username, r.Stdout)
}
}
func TestSSHAgent(t *testing.T) {
client, err := DialAgent(host, username, 5)
if err != nil {
t.Fatalf("agent dial error for host %q: %v", host, err)
}
client.Buffered()
cmd := "logname"
r, err := Run(client, cmd)
if err != nil {
t.Fatal("key auth run error:", err)
}
if strings.TrimSpace(r.Stdout) != username {
t.Fatal("ssh-agent command failed. expected", username, "got", r.Stdout)
}
}
func TestSSHClient(t *testing.T) {
t.Skip("need test ssh server")
cmd := "hostname"
timeout := 5
r, err := ExecPassword(host, username, password, 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 TestSSHStderr(t *testing.T) {
cmd := "lsX"
timeout := 5
r, _ := ExecPassword(host, username, password, cmd, timeout)
if len(r.Stdout) > 0 {
t.Log("ssh stdout", r.Stdout)
}
if len(r.Stderr) > 0 {
t.Log("ssh stderr:", r.Stderr)
}
}
func TestSSHTimeout(t *testing.T) {
cmd := "sleep 10"
timeout := 5
r, err := ExecPassword(host, username, password, cmd, timeout)
if err == nil {
t.Error("ssh timeout failed")
}
if r.RC > 0 {
t.Error("ssh execution error:", r.Stderr)
} else if len(r.Stderr) > 0 {
t.Error("ssh execution error:", r.Stderr)
}
}
// TODO: makr this test (and others) self contained with a test ssh server
const (
scpTestFile = "testdata/arewethereyet.txt"
scpTestDir = "/tmp"
)
func TestSCP(t *testing.T) {
s, err := DialKeyFile(host, username, keyfile, 5)
if err != nil {
t.Fatal(err)
}
err = s.CopyFile(scpTestFile, scpTestDir)
if err != nil {
t.Fatal("copy error:", err)
}
}