-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve
RPCMsg
xml encoding testing (#36)
Add unittest for RPCMsg encoding and ensure that operation is supplied when encoding `rpc` request messages (i.e `ReqMSG`) via custom MarshalXML method. Future work to make sure structs passed in are named. **Test Plan:** ``` $ go test . -v -run=MarshalRPCMsg === RUN TestMarshalRPCMsg === RUN TestMarshalRPCMsg/nil msg_test.go:55: out: === RUN TestMarshalRPCMsg/string msg_test.go:55: out: <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1"><foo><bar/></foo></rpc> === RUN TestMarshalRPCMsg/byteslice msg_test.go:55: out: <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1"><baz><qux/></baz></rpc> === RUN TestMarshalRPCMsg/validate msg_test.go:55: out: <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1"><validate><source><running/></source></validate></rpc> === RUN TestMarshalRPCMsg/customStruct msg_test.go:55: out: <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1"><command xmlns="http://xml.juniper.net/junos/22.4R0/junos">show bgp neighbors</command></rpc> --- PASS: TestMarshalRPCMsg (0.00s) --- PASS: TestMarshalRPCMsg/nil (0.00s) --- PASS: TestMarshalRPCMsg/string (0.00s) --- PASS: TestMarshalRPCMsg/byteslice (0.00s) --- PASS: TestMarshalRPCMsg/validate (0.00s) --- PASS: TestMarshalRPCMsg/customStruct (0.00s) PASS ok github.com/nemith/netconf (cached) ```
- Loading branch information
Showing
4 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
{ | ||
"cSpell.words": ["netconf"] | ||
"cSpell.words": [ | ||
"byteslice", | ||
"innerxml", | ||
"inttest", | ||
"Junos", | ||
"nemith", | ||
"netconf" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package netconf | ||
|
||
import ( | ||
"encoding/xml" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMarshalRPCMsg(t *testing.T) { | ||
tt := []struct { | ||
name string | ||
operation interface{} | ||
err bool | ||
want []byte | ||
}{ | ||
{ | ||
name: "nil", | ||
operation: nil, | ||
err: true, | ||
}, | ||
{ | ||
name: "string", | ||
operation: "<foo><bar/></foo>", | ||
want: []byte(`<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1"><foo><bar/></foo></rpc>`), | ||
}, | ||
{ | ||
name: "byteslice", | ||
operation: []byte("<baz><qux/></baz>"), | ||
want: []byte(`<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1"><baz><qux/></baz></rpc>`), | ||
}, | ||
{ | ||
name: "validate", | ||
operation: validateReq{Source: Running}, | ||
want: []byte(`<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1"><validate><source><running/></source></validate></rpc>`), | ||
}, | ||
{ | ||
name: "namedStruct", | ||
operation: struct { | ||
XMLName xml.Name `xml:"http://xml.juniper.net/junos/22.4R0/junos command"` | ||
Command string `xml:",innerxml"` | ||
}{ | ||
Command: "show bgp neighbors", | ||
}, | ||
want: []byte(`<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1"><command xmlns="http://xml.juniper.net/junos/22.4R0/junos">show bgp neighbors</command></rpc>`), | ||
}, | ||
/* | ||
{ | ||
name: "unnamedStruct", | ||
operation: struct { | ||
Command string `xml:"command"` | ||
}{ | ||
Command: "show version", | ||
}, | ||
},*/ | ||
} | ||
|
||
for _, tc := range tt { | ||
t.Run(tc.name, func(t *testing.T) { | ||
out, err := xml.Marshal(&RPCMsg{ | ||
MessageID: 1, | ||
Operation: tc.operation, | ||
}) | ||
t.Logf("out: %s", out) | ||
|
||
if tc.err { | ||
assert.Error(t, err) | ||
return | ||
} | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, out, tc.want) | ||
}) | ||
} | ||
} |