-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmodel_cisco_iosxr_test.go
262 lines (208 loc) · 7.07 KB
/
model_cisco_iosxr_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package dev
import (
"fmt"
"io"
"net"
"path/filepath"
"strings"
"testing"
"github.com/udhos/jazigo/conf"
"github.com/udhos/jazigo/temp"
)
type optionsCiscoIOSXR struct {
sendUsername bool
sendDisable bool
requestEnablePass bool
breakConn bool
}
func TestCiscoIOSXR1(t *testing.T) {
// launch bogus test server
addr := ":2001"
s, listenErr := spawnServerCiscoIOSXR(t, addr, optionsCiscoIOSXR{sendUsername: true, sendDisable: true, requestEnablePass: true})
if listenErr != nil {
t.Errorf("could not spawn bogus CiscoIOSXR server: %v", listenErr)
}
t.Logf("TestCiscoIOSXR: server running on %s", addr)
// run client test
logger := &testLogger{t}
tab := NewDeviceTable()
opt := conf.NewOptions()
opt.Set(&conf.AppConfig{MaxConcurrency: 3, MaxConfigFiles: 10})
RegisterModels(logger, tab)
CreateDevice(tab, logger, "cisco-iosxr", "lab1", "localhost"+addr, "telnet", "lab", "pass", "en", false, nil)
repo := temp.MakeTempRepo()
defer temp.CleanupTempRepo()
requestCh := make(chan FetchRequest)
errlogPrefix := filepath.Join(repo, "errlog_test.")
go Spawner(tab, logger, requestCh, repo, errlogPrefix, opt, NewFilterTable(logger))
good, bad, skip := Scan(tab, tab.ListDevices(), logger, opt.Get(), requestCh)
if good != 1 || bad != 0 || skip != 0 {
t.Errorf("good=%d bad=%d skip=%d", good, bad, skip)
}
close(requestCh) // shutdown Spawner - we might exit first though
s.close() // shutdown server
<-s.done // wait termination of accept loop goroutine
}
func TestCiscoIOSXR2(t *testing.T) {
// launch bogus test server
addr := ":2002"
s, listenErr := spawnServerCiscoIOSXR(t, addr, optionsCiscoIOSXR{sendUsername: false})
if listenErr != nil {
t.Errorf("could not spawn bogus CiscoIOSXR server: %v", listenErr)
}
t.Logf("TestCiscoIOSXR: server running on %s", addr)
// run client test
logger := &testLogger{t}
tab := NewDeviceTable()
opt := conf.NewOptions()
opt.Set(&conf.AppConfig{MaxConcurrency: 3, MaxConfigFiles: 10})
RegisterModels(logger, tab)
CreateDevice(tab, logger, "cisco-iosxr", "lab1", "localhost"+addr, "telnet", "lab", "pass", "en", false, nil)
repo := temp.MakeTempRepo()
defer temp.CleanupTempRepo()
requestCh := make(chan FetchRequest)
errlogPrefix := filepath.Join(repo, "errlog_test.")
go Spawner(tab, logger, requestCh, repo, errlogPrefix, opt, NewFilterTable(logger))
good, bad, skip := Scan(tab, tab.ListDevices(), logger, opt.Get(), requestCh)
if good != 1 || bad != 0 || skip != 0 {
t.Errorf("good=%d bad=%d skip=%d", good, bad, skip)
}
close(requestCh) // shutdown Spawner - we might exit first though
s.close() // shutdown server
<-s.done // wait termination of accept loop goroutine
}
func TestCiscoIOSXR3(t *testing.T) {
// launch bogus test server
addr := ":2003"
s, listenErr := spawnServerCiscoIOSXR(t, addr, optionsCiscoIOSXR{sendUsername: true, sendDisable: true, requestEnablePass: true, breakConn: true})
if listenErr != nil {
t.Errorf("could not spawn bogus CiscoIOSXR server: %v", listenErr)
}
// run client test
logger := &testLogger{t}
tab := NewDeviceTable()
opt := conf.NewOptions()
opt.Set(&conf.AppConfig{MaxConcurrency: 3, MaxConfigFiles: 10})
RegisterModels(logger, tab)
CreateDevice(tab, logger, "cisco-iosxr", "lab1", "localhost"+addr, "telnet", "lab", "pass", "en", false, nil)
repo := temp.MakeTempRepo()
defer temp.CleanupTempRepo()
requestCh := make(chan FetchRequest)
errlogPrefix := filepath.Join(repo, "errlog_test.")
go Spawner(tab, logger, requestCh, repo, errlogPrefix, opt, NewFilterTable(logger))
good, bad, skip := Scan(tab, tab.ListDevices(), logger, opt.Get(), requestCh)
if good != 0 || bad != 1 || skip != 0 {
t.Errorf("good=%d bad=%d skip=%d", good, bad, skip)
}
close(requestCh) // shutdown Spawner - we might exit first though
s.close() // shutdown server
<-s.done // wait termination of accept loop goroutine
}
func spawnServerCiscoIOSXR(t *testing.T, addr string, options optionsCiscoIOSXR) (*testServer, error) {
ln, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
s := &testServer{listener: ln, done: make(chan int)}
go acceptLoopIOSXR(t, s, handleConnectionCiscoIOSXR, options)
return s, nil
}
func acceptLoopIOSXR(t *testing.T, s *testServer, handler func(*testing.T, net.Conn, optionsCiscoIOSXR), options optionsCiscoIOSXR) {
for {
conn, err := s.listener.Accept()
if err != nil {
t.Logf("acceptLoopIOSXR: accept failure, exiting: %v", err)
break
}
go handler(t, conn, options)
}
close(s.done)
}
func handleConnectionCiscoIOSXR(t *testing.T, c net.Conn, options optionsCiscoIOSXR) {
defer c.Close()
buf := make([]byte, 1000)
if options.sendUsername {
// send username prompt
if _, err := c.Write([]byte("Bogus CiscoIOSXR server\n\nUser Access Verification\n\nUsername: ")); err != nil {
t.Logf("handleConnectionCiscoIOSXR: send username prompt error: %v", err)
return
}
// consume username
if _, err := c.Read(buf); err != nil {
t.Logf("handleConnectionCiscoIOSXR: read username error: %v", err)
return
}
}
// send password prompt
if _, err := c.Write([]byte("\nPassword: ")); err != nil {
t.Logf("handleConnectionCiscoIOSXR: send password prompt error: %v", err)
return
}
// consume password
if _, err := c.Read(buf); err != nil {
t.Logf("handleConnectionCiscoIOSXR: read password error: %v", err)
return
}
enabled := !options.sendDisable
LOOP:
for {
prompt := ">"
if enabled {
prompt = "#"
}
// send command prompt
if _, err := c.Write([]byte(fmt.Sprintf("\nRP/0/RSP0/CPU0:asr9k%s", prompt))); err != nil {
t.Logf("handleConnectionCiscoIOSXR: send command prompt error: %v", err)
return
}
// consume command
if _, err := c.Read(buf); err != nil {
if err == io.EOF {
return // peer closed connection
}
t.Logf("handleConnectionCiscoIOSXR: read command error: %v", err)
return
}
str := string(buf)
switch {
case strings.HasPrefix(str, "q"): //quit
break LOOP
case strings.HasPrefix(str, "ex"): //exit
break LOOP
case strings.HasPrefix(str, "term"): //term len 0
case strings.HasPrefix(str, "sh"): //sh run
if options.breakConn {
// break connection (on defer/exit)
return
}
if _, err := c.Write([]byte("\nshow running-configuration\nthis is the IOS XR config\n")); err != nil {
t.Logf("handleConnectionCiscoIOSXR: send sh run error: %v", err)
return
}
case strings.HasPrefix(str, "en"): //enable
if !enabled {
// send password prompt
if _, err := c.Write([]byte("\nPassword: ")); err != nil {
t.Logf("handleConnectionCiscoIOSXR: send enable password prompt error: %v", err)
return
}
// consume password
if _, err := c.Read(buf); err != nil {
t.Logf("handleConnectionCiscoIOSXR: read enable password error: %v", err)
return
}
enabled = true
}
default:
if _, err := c.Write([]byte("\nIgnoring unknown command")); err != nil {
t.Logf("handleConnectionCiscoIOSXR: send unknown command error: %v", err)
return
}
}
}
// send bye
if _, err := c.Write([]byte("\nbye\n")); err != nil {
t.Logf("handleConnectionCiscoIOSXR: send bye error: %v", err)
return
}
}