-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtmsh_test.go
75 lines (63 loc) · 1.7 KB
/
tmsh_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
package tmsh
import (
"fmt"
"reflect"
"strings"
"testing"
)
var validCmds = []string{
"show sys clock",
"show ltm node",
"show ltm pool",
"list ltm pool",
"list ltm virtual",
"list ltm profile",
}
type TestSSHConnection struct {
validCmd bool
ret []byte
}
func (c *TestSSHConnection) Send(cmd string) (int, error) {
for _, vc := range validCmds {
if strings.HasPrefix(cmd, vc) {
c.validCmd = true
return 0, nil
}
}
c.validCmd = false
return 1, nil
}
func (c *TestSSHConnection) Recv(suffix string) ([]byte, error) {
if !c.validCmd {
return nil, fmt.Errorf("Syntax Error: unexpected argument \"foo\"")
}
return c.ret, nil
}
func (c *TestSSHConnection) Close() error {
return nil
}
func TestExecuteCommand(t *testing.T) {
bigip := &BigIP{
host: "example.com",
user: "admin.prd",
sshconn: &TestSSHConnection{
ret: []byte("Last login: Mon Jul 1 10:20:34 2017 from 192.0.2.10\nadmin.prd@(LB000)(cfg-sync In Sync)(/S1-green-P:Active)(/admin.prd)(tmos)#\nshow sys clock\n------------------------\nSys::Clock\n------------------------\nMon Jul 03 14:25:02 JST 2017\n\nn.prd)(tmos)# ve)(/admi\n"),
},
}
expect := "------------------------\nSys::Clock\n------------------------\nMon Jul 03 14:25:02 JST 2017\n"
actual, err := bigip.ExecuteCommand("show sys clock")
if err != nil {
t.Errorf("%v", err)
}
if !reflect.DeepEqual(actual, expect) {
t.Errorf("got %v\nwant %v", actual, expect)
}
expectErr := fmt.Errorf("Syntax Error: unexpected argument \"foo\"")
ret, actualErr := bigip.ExecuteCommand("show foo")
if ret != "" {
t.Errorf("got %v\nwant %v", ret, "")
}
if !reflect.DeepEqual(actualErr, expectErr) {
t.Errorf("got %v\nwant %v", actualErr, expectErr)
}
}