Skip to content

Commit

Permalink
Add check for junos specific tags and test
Browse files Browse the repository at this point in the history
  • Loading branch information
netixx committed Jun 19, 2024
1 parent d244276 commit a0a10fb
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 0 deletions.
90 changes: 90 additions & 0 deletions driver/netconf/operational_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package netconf_test

import (
"bytes"
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/scrapli/scrapligo/util"
)

func testGetOperational(testName string, testCase *operationalTest) func(t *testing.T) {
return func(t *testing.T) {
t.Logf("%s: starting", testName)

d, fileTransportObj := prepareDriver(t, testName, testCase.payload.PayloadFile)

r, err := d.Get(testCase.filter)
if err != nil {
t.Fatalf(
"%s: encountered error running netconf Driver Validate, error: %s",
testName,
err,
)
}

if r.Failed != nil && !testCase.shouldFail {
t.Fatalf("%s: response object indicates failure",
testName)
}

actualOut := r.Result
actualIn := bytes.Join(fileTransportObj.Writes, []byte("\n"))

if *update {
writeGolden(t, testName, actualIn, actualOut)
}

expectedIn := readFile(t, fmt.Sprintf("golden/%s-in.txt", testName))
expectedOut := readFile(t, fmt.Sprintf("golden/%s-out.txt", testName))

if !cmp.Equal(actualIn, expectedIn) {
t.Errorf(
"%s: actual and expected inputs do not match\nactual: %s\nexpected:%s",
testName,
actualIn,
expectedIn,
)
}

if !cmp.Equal(actualOut, string(expectedOut)) {
t.Fatalf(
"%s: actual and expected outputs do not match\nactual: %s\nexpected:%s",
testName,
actualOut,
expectedOut,
)
}
}
}

type operationalTest struct {
payload *util.PayloadTestCase
filter string
shouldFail bool
}

func TestGetOperational(t *testing.T) {
cases := map[string]*operationalTest{
"junos-getoperational-components-fail": {
payload: &util.PayloadTestCase{
Description: "operational show components",
PayloadFile: "junos-getoperational-components-fail.txt",
},
filter: `<components xmlns="http://openconfig.net/yang/platform">
<component>
<state>
<type xmlns:idx="http://openconfig.net/yang/platform-types">idx:CHASSIS</type>
</state>
</component>
</components>`,
shouldFail: true,
},
}

for testName, testCase := range cases {
f := testGetOperational(testName, testCase)
t.Run(testName, f)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<capabilities>
<capability>urn:ietf:params:netconf:base:1.0</capability>
</capabilities>
</hello>]]>]]>


<?xml version="1.0" encoding="UTF-8"?><rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="101"><get><filter type="subtree"><components xmlns="http://openconfig.net/yang/platform"><component><state><type xmlns:idx="http://openconfig.net/yang/platform-types">idx:CHASSIS</type></state></component></components></filter></get></rpc>]]>]]>

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<nc:rpc-reply xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/23.2R2.21-EVO/junos" message-id="101">
<nc:rpc-error>
<nc:error-type>protocol</nc:error-type>
<nc:error-tag>operation-failed</nc:error-tag>
<nc:error-severity>error</nc:error-severity>
<nc:error-message>syntax error</nc:error-message>
<nc:error-info>
<nc:bad-element>components</nc:bad-element>
</nc:error-info>
</nc:rpc-error>
<nc:rpc-error>
<nc:error-type>protocol</nc:error-type>
<nc:error-tag>operation-failed</nc:error-tag>
<nc:error-severity>error</nc:error-severity>
<nc:error-message>syntax error, expecting &lt;/filter&gt;</nc:error-message>
<nc:error-info>
<nc:bad-element>components</nc:bad-element>
</nc:error-info>
</nc:rpc-error>
</nc:rpc-reply>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<nc:hello xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<nc:capabilities>
<nc:capability>urn:ietf:params:netconf:base:1.0</nc:capability>
<nc:capability>urn:ietf:params:netconf:capability:candidate:1.0</nc:capability>
<nc:capability>urn:ietf:params:netconf:capability:confirmed-commit:1.0</nc:capability>
<nc:capability>urn:ietf:params:netconf:capability:validate:1.0</nc:capability>
<nc:capability>urn:ietf:params:netconf:capability:url:1.0?scheme=http,ftp,file</nc:capability>
<nc:capability>urn:ietf:params:xml:ns:netconf:base:1.0?module=ietf-netconf&amp;revision=2011-06-01</nc:capability>
<nc:capability>urn:ietf:params:xml:ns:netconf:capability:candidate:1.0</nc:capability>
<nc:capability>urn:ietf:params:xml:ns:netconf:capability:confirmed-commit:1.0</nc:capability>
<nc:capability>urn:ietf:params:xml:ns:netconf:capability:validate:1.0</nc:capability>
<nc:capability>urn:ietf:params:xml:ns:netconf:capability:url:1.0?scheme=http,ftp,file</nc:capability>
<nc:capability>urn:ietf:params:xml:ns:yang:ietf-inet-types?module=ietf-inet-types&amp;revision=2013-07-15</nc:capability>
<nc:capability>urn:ietf:params:xml:ns:yang:ietf-yang-metadata?module=ietf-yang-metadata&amp;revision=2016-08-05</nc:capability>
<nc:capability>urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring</nc:capability>
<nc:capability>http://xml.juniper.net/netconf/junos/1.0</nc:capability>
<nc:capability>http://xml.juniper.net/dmi/system/1.0</nc:capability>
<nc:capability>http://yang.juniper.net/junos/jcmd?module=junos-configuration-metadata&amp;revision=2021-09-01</nc:capability>
</nc:capabilities>
<nc:session-id>5026</nc:session-id>
</nc:hello>
]]>]]>
<?xml version="1.0" encoding="utf-8"?>
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<capabilities>
<capability>urn:ietf:params:netconf:base:1.0</capability>
</capabilities>
</hello>]]>]]>
<?xml version="1.0" encoding="UTF-8"?><rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="101"><get><filter type="subtree"><components xmlns="http://openconfig.net/yang/platform"><component><state><type xmlns:idx="http://openconfig.net/yang/platform-types">idx:CHASSIS</type></state></component></components></filter></get></rpc>]]>]]>
<nc:rpc-reply xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/23.2R2.21-EVO/junos" message-id="101">
<nc:rpc-error>
<nc:error-type>protocol</nc:error-type>
<nc:error-tag>operation-failed</nc:error-tag>
<nc:error-severity>error</nc:error-severity>
<nc:error-message>syntax error</nc:error-message>
<nc:error-info>
<nc:bad-element>components</nc:bad-element>
</nc:error-info>
</nc:rpc-error>
<nc:rpc-error>
<nc:error-type>protocol</nc:error-type>
<nc:error-tag>operation-failed</nc:error-tag>
<nc:error-severity>error</nc:error-severity>
<nc:error-message>syntax error, expecting &lt;/filter&gt;</nc:error-message>
<nc:error-info>
<nc:bad-element>components</nc:bad-element>
</nc:error-info>
</nc:rpc-error>
</nc:rpc-reply>
]]>]]>
3 changes: 3 additions & 0 deletions response/netconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ func NewNetconfResponse(
[]byte("<rpc-errors>"),
[]byte("</rpc-error>"),
[]byte("</rpc-errors>"),
// for juniper, with set system services netconf rfc-compliant
[]byte("<nc:rpc-error>"),
[]byte("</nc:rpc-error>"),
},
NetconfVersion: version,
}
Expand Down

0 comments on commit a0a10fb

Please sign in to comment.