forked from mobiledgex/golang-ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
105 lines (95 loc) · 3.03 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
package ssh
import (
"fmt"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
)
// these servers need to be modified when running to have working SSH servers
var testServers []string
var key string
var timeout time.Duration
var vers string
var nonDefaultTimeout time.Duration
var nullServer string
// this data is subject to change; it needs to be populated with 2 working servers
// that can reach each other via the key provided
func initTestData() {
testServers = []string{"35.199.188.102", "35.199.188.102", "80.187.128.13"} // twice thru the first svr and then to another
key = os.Getenv("HOME") + "/.mobiledgex/id_rsa_mex"
timeout = time.Second * 10
vers = "SSH-2.0-mobiledgex-ssh-client-1.0"
nonDefaultTimeout = timeout / 4
nullServer = "192.0.2.1" // 192.0.2.1/24 (TEST-NET-1)[RFC5737]
}
type Result struct {
expected string
out string
err error
}
func TestNativeClient(t *testing.T) {
initTestData()
auth := Auth{Keys: []string{key}}
client, err := NewNativeClient("ubuntu", vers, testServers[0], 22, &auth, timeout, nil)
//client.AddHop(testServers[1])
require.Nil(t, err, "NewNativeClient")
// run 3 tests concurrently 4 times
numThread := 5
results := make(chan Result, numThread)
// test cached connection api with multiple threads
client.StartPersistentConn(timeout)
for i := 0; i < numThread; i++ {
go func(i int) {
expected := fmt.Sprintf("testing: %d", i)
out, err := client.Output("echo " + expected)
results <- Result{expected, out, err}
}(i)
}
for i := 0; i < numThread; i++ {
result := <-results
require.Nil(t, result.err, "result")
require.Equal(t, result.expected, result.out, result.expected)
}
client.StopPersistentConn()
for hopTest := 0; hopTest < len(testServers); hopTest++ {
if hopTest != 0 {
newclient, err := client.AddHop(testServers[hopTest], 22)
client = newclient.(Client)
require.Nil(t, err, "addhop")
}
for i := 0; i < numThread; i++ {
go func(i int) {
expected := fmt.Sprintf("testing: %d", i)
out, err := client.Output("echo " + expected)
results <- Result{expected, out, err}
}(i)
}
for i := 0; i < numThread; i++ {
result := <-results
require.Nil(t, result.err, "result")
require.Equal(t, result.expected, result.out, result.expected)
}
}
//now add bad hop and check for error
badClient, _ := client.AddHop("10.10.10.10", 22)
_, err = badClient.Output("echo hello")
require.NotNil(t, err)
require.Contains(t, err.Error(), "ssh client timeout")
// test non-default timeout - connect to
client, err = NewNativeClient("ubuntu", vers, nullServer, 22, &auth, timeout, nil)
require.Nil(t, err, "NewNativeClient")
ch := make(chan Result, 1)
go func() {
out, err := client.OutputWithTimeout("echo abc", nonDefaultTimeout)
ch <- Result{"", out, err}
}()
// we should make sure that a non-default timeout is used
select {
case result := <-ch:
require.NotNil(t, result.err)
require.Contains(t, result.err.Error(), "timeout")
case <-time.After(timeout / 2):
require.Fail(t, "Error - Non-default timeout was ignored")
}
}