-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_operations_test.go
118 lines (104 loc) · 3.23 KB
/
get_operations_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
package netconf
import (
"context"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetConfig(t *testing.T) {
tt := []struct {
name string
source Datastore
options []GetOption
matches []*regexp.Regexp
}{
{
name: "get-config startup with-defaults",
source: Startup,
options: []GetOption{
WithDefaultMode(DefaultsModeTrim),
},
matches: []*regexp.Regexp{
regexp.MustCompile(`<source>\S*<startup/>\S*</source>`),
regexp.MustCompile(`<with-defaults xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults">trim</with-defaults>`),
},
},
{
name: "get-config running filter",
source: Running,
options: []GetOption{
WithSubtreeFilter(`<interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"/>`),
},
matches: []*regexp.Regexp{
regexp.MustCompile(`<source>\S*<running/>\S*</source>`),
regexp.MustCompile(`<filter type="subtree"><interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"/></filter>`),
},
},
{
name: "get-config running no options",
source: Running,
matches: []*regexp.Regexp{
regexp.MustCompile(`<get-config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"><source><running/></source></get-config>`),
},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
ts := newTestServer(t)
sess := newSession(ts.transport())
go sess.recv()
ts.queueRespString(`<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1"><ok/></rpc-reply>`)
reply, err := sess.GetConfig(context.Background(), tc.source, tc.options...)
assert.NoError(t, err)
assert.NotNil(t, reply)
sentMsg, err := ts.popReq()
assert.NoError(t, err)
for _, match := range tc.matches {
assert.Regexp(t, match, string(sentMsg))
}
})
}
}
func TestGet(t *testing.T) {
tt := []struct {
name string
options []GetOption
matches []*regexp.Regexp
}{
{
name: "get ifm",
options: []GetOption{
WithSubtreeFilter(`<ifm xmlns="urn:huawei:yang:huawei-ifm"/>`),
},
matches: []*regexp.Regexp{
regexp.MustCompile(`<get xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">\S*<filter type="subtree">\S*<ifm xmlns="urn:huawei:yang:huawei-ifm"/>\S*</filter>\S*</get>`),
},
},
{
name: "get devm",
options: []GetOption{
WithDefaultMode("report-all"),
WithSubtreeFilter(`<devm xmlns="urn:huawei:yang:huawei-devm"/>`),
},
matches: []*regexp.Regexp{
regexp.MustCompile(`<get xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">\S*<filter type="subtree">\S*<devm xmlns="urn:huawei:yang:huawei-devm"/>\S*</filter>\S*<with-defaults xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults">report-all</with-defaults>\S*</get>`),
},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
ts := newTestServer(t)
sess := newSession(ts.transport())
go sess.recv()
ts.queueRespString(`<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1"><data>daa</data></rpc-reply>`)
reply, err := sess.Get(context.Background(), tc.options...)
assert.NoError(t, err)
assert.NotNil(t, reply)
sentMsg, err := ts.popReq()
assert.NoError(t, err)
for _, match := range tc.matches {
assert.Regexp(t, match, string(sentMsg))
}
})
}
}