Skip to content

Commit

Permalink
Move RPCReply construction into rpc and add basic tests (Juniper#50)
Browse files Browse the repository at this point in the history
* Move RPCReply construction into rpc and add basic tests
* Hide newRPCReply and get rid of RaiseErrors method
* Fixes to make Travis CI happy
  • Loading branch information
wtucker authored and nemith committed Feb 18, 2018
1 parent cc6ed39 commit 3d394c5
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 14 deletions.
19 changes: 19 additions & 0 deletions netconf/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ type RPCReply struct {
RawReply string `xml:"-"`
}

func newRPCReply(rawXML []byte, ErrOnWarning bool) (*RPCReply, error) {
reply := &RPCReply{}
reply.RawReply = string(rawXML)

if err := xml.Unmarshal(rawXML, reply); err != nil {
return nil, err
}

if reply.Errors != nil {
for _, rpcErr := range reply.Errors {
if rpcErr.Severity == "error" || ErrOnWarning {
return reply, &rpcErr
}
}
}

return reply, nil
}

// RPCError defines an error reply to a RPC request
type RPCError struct {
Type string `xml:"error-type"`
Expand Down
83 changes: 83 additions & 0 deletions netconf/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,89 @@ func TestUUIDChar(t *testing.T) {
for _, v := range u {
if valid(int(v)) == false {
t.Errorf("invalid char %s", string(v))

}
}
}

var RPCReplytests = []struct {
rawXML string
replyOk bool
}{
{
`
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/15.1F4/junos">
<commit-results>
</commit-results>
<ok/>
</rpc-reply>`,
false,
},
{
`
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/15.1F4/junos">
<commit-results>
<rpc-error>
<error-type>application</error-type>
<error-tag>invalid-value</error-tag>
<error-severity>error</error-severity>
<error-path>[edit]</error-path>
<error-message>mgd: Missing mandatory statement: 'root-authentication'</error-message>
<error-info>
<bad-element>system</bad-element>
</error-info>
</rpc-error>
<rpc-error>
<error-type>protocol</error-type>
<error-tag>operation-failed</error-tag>
<error-severity>error</error-severity>
<error-message>
configuration check-out failed: (missing mandatory statements)
</error-message>
</rpc-error>
</commit-results>
</rpc-reply>`,
false,
},
{
`
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/16.1R3/junos">
<commit-results>
<rpc-error>
<error-severity>warning</error-severity>
<error-path>[edit protocols]</error-path>
<error-message>mgd: requires 'mpls' license</error-message>
<error-info>
<bad-element>mpls</bad-element>
</error-info>
</rpc-error>
<rpc-error>
<error-severity>warning</error-severity>
<error-path>[edit protocols]</error-path>
<error-message>mgd: requires 'bgp' license</error-message>
<error-info>
<bad-element>bgp</bad-element>
</error-info>
</rpc-error>
<routing-engine junos:style="normal">
<name>fpc0</name>
<commit-check-success/>
</routing-engine>
</commit-results>
<ok/>
</rpc-reply>`,
false,
},
}

func TestNewRPCReply(t *testing.T) {
for _, tc := range RPCReplytests {
reply, err := newRPCReply([]byte(tc.rawXML), false)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if reply.RawReply != tc.rawXML {
t.Errorf("newRPCReply(%q) did not set RawReply to input, got %q", tc.rawXML, reply.RawReply)
}
}
}
16 changes: 2 additions & 14 deletions netconf/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,11 @@ func (s *Session) Exec(methods ...RPCMethod) (*RPCReply, error) {
return nil, err
}

reply := &RPCReply{}
reply.RawReply = string(rawXML)

if err := xml.Unmarshal(rawXML, reply); err != nil {
reply, err := newRPCReply(rawXML, s.ErrOnWarning)
if err != nil {
return nil, err
}

if reply.Errors != nil {
// We have errors, lets see if it's a warning or an error.
for _, rpcErr := range reply.Errors {
if rpcErr.Severity == "error" || s.ErrOnWarning {
return reply, &rpcErr
}
}

}

return reply, nil
}

Expand Down

0 comments on commit 3d394c5

Please sign in to comment.