forked from yahoo/vssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvssh_test.go
142 lines (118 loc) · 2.76 KB
/
vssh_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
package vssh
import (
"log"
"testing"
"time"
"golang.org/x/crypto/ssh"
)
func TestForceReConn(t *testing.T) {
vs := New()
cfg := GetConfigUserPass("vssh", "vssh")
err := vs.AddClient("127.0.0.1:22", cfg)
if err != nil {
t.Error(err)
}
vs.ForceReConn("127.0.0.1:22")
if len(vs.actionQ) != 2 {
t.Fatal("expect to have two tasks but got", len(vs.actionQ))
}
if err := vs.ForceReConn("127.0.0.2:22"); err == nil {
t.Fatal("expect to have not exist error")
}
}
func TestIncreaseProc(t *testing.T) {
vs := New().Start()
time.Sleep(time.Millisecond * 500)
p1 := vs.stats.processes
vs.IncreaseProc(100)
time.Sleep(time.Millisecond * 500)
p2 := vs.stats.processes
if p2-p1 != 100 {
t.Error("unexpect increase proc")
}
}
func TestDecreaseProc(t *testing.T) {
vs := New().Start()
time.Sleep(time.Millisecond * 500)
p1 := vs.stats.processes
vs.DecreaseProc(100)
time.Sleep(time.Millisecond * 500)
p2 := vs.stats.processes
if p1-p2 != 100 {
t.Error("unexpect decrease proc")
}
}
func TestCurrentProc(t *testing.T) {
vs := New()
vs.SetInitNumProc(100)
vs.Start()
time.Sleep(time.Millisecond * 500)
n := vs.CurrentProc()
if n != 100 {
t.Error("expect 100 current processes but got", n)
}
}
func TestSetInitNumProc(t *testing.T) {
vs := New()
vs.SetInitNumProc(100)
if initNumProc != 100 {
t.Error("expect initNumProcess 100 but got", initNumProc)
}
}
func TestSetClientsShardNumber(t *testing.T) {
SetClientsShardNumber(20)
if clientsShardNum != 20 {
t.Error("expect clientsShardNum 20 but got", clientsShardNum)
}
}
func TestSetLogger(t *testing.T) {
vs := New()
logger := &log.Logger{}
vs.SetLogger(logger)
if vs.logger != logger {
t.Error("setlogger failed")
}
}
func TestRequestPty(t *testing.T) {
f := RequestPty("xterm", 40, 80, ssh.TerminalModes{ssh.ECHO: 0})
c := clientAttr{}
f(&c)
if !c.pty.enabled {
t.Error("expect pty enabled but disabled")
}
if c.pty.term != "xterm" {
t.Error("expect terminal xterm but got", c.pty.term)
}
if c.pty.height != 40 {
t.Error("expect to have height 40 but got", c.pty.height)
}
if c.pty.wide != 80 {
t.Error("expect to have wide 80 but got", c.pty.wide)
}
}
func TestSetLimitReaderStdout(t *testing.T) {
f := SetLimitReaderStdout(1024)
q := &query{}
f(q)
if q.limitReadOut != 1024 {
t.Error("expect limitReadOut 1024 but got", q.limitReadOut)
}
}
func TestSetLimitReaderStderr(t *testing.T) {
f := SetLimitReaderStderr(1024)
q := &query{}
f(q)
if q.limitReadErr != 1024 {
t.Error("expect limitReadErr 1024 but got", q.limitReadErr)
}
}
func TestGetConfigPEM(t *testing.T) {
_, err := GetConfigPEM("vssh", "notexitfile")
if err == nil {
t.Error("expect error but nil")
}
_, err = GetConfigPEM("vssh", "vssh.go")
if err == nil {
t.Error("expect error but nil")
}
}