Skip to content

Commit

Permalink
fix CreateSubscription test (#55)
Browse files Browse the repository at this point in the history
Since the xml namespace for `<create-subscription>` is different from
the base namespace it need to be explicit and it was missing from the
match side of the tests.

Also moved to `time.Date` from `time.Parse` to get rid of some unneeded
error handling and migrated the tests to use testify to make the output
more clear.
  • Loading branch information
nemith authored Jun 16, 2023
1 parent f2ecb06 commit dd5a04d
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,14 +539,9 @@ func TestCancelCommit(t *testing.T) {
}

func TestCreateSubscription(t *testing.T) {
st, err := time.Parse(time.RFC3339, "2023-06-07T18:31:48+02:00")
if err != nil {
t.Fatalf("invalid startTime: %s", st)
}
et, err := time.Parse(time.RFC3339, "2023-06-07T18:33:48+02:00")
if err != nil {
t.Fatalf("invalid endTime: %s", et)
}
start := time.Date(2023, time.June, 07, 18, 31, 48, 00, time.UTC)
end := time.Date(2023, time.June, 07, 18, 33, 48, 00, time.UTC)

tt := []struct {
name string
options []CreateSubscriptionOption
Expand All @@ -555,28 +550,28 @@ func TestCreateSubscription(t *testing.T) {
{
name: "noOptions",
matches: []*regexp.Regexp{
regexp.MustCompile(`<create-subscription></create-subscription>`),
regexp.MustCompile(`<create-subscription xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0"></create-subscription>`),
},
},
{
name: "startTime option",
options: []CreateSubscriptionOption{WithStartTimeOption(st)},
options: []CreateSubscriptionOption{WithStartTimeOption(start)},
matches: []*regexp.Regexp{
regexp.MustCompile(`<create-subscription><startTime>` + regexp.QuoteMeta(st.Format(time.RFC3339)) + `</startTime></create-subscription>`),
regexp.MustCompile(`<create-subscription xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0"><startTime>` + regexp.QuoteMeta(start.Format(time.RFC3339)) + `</startTime></create-subscription>`),
},
},
{
name: "endTime option",
options: []CreateSubscriptionOption{WithEndTimeOption(et)},
options: []CreateSubscriptionOption{WithEndTimeOption(end)},
matches: []*regexp.Regexp{
regexp.MustCompile(`<create-subscription><endTime>` + regexp.QuoteMeta(et.Format(time.RFC3339)) + `</endTime></create-subscription>`),
regexp.MustCompile(`<create-subscription xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0"><endTime>` + regexp.QuoteMeta(end.Format(time.RFC3339)) + `</endTime></create-subscription>`),
},
},
{
name: "stream option",
options: []CreateSubscriptionOption{WithStreamOption("thestream")},
matches: []*regexp.Regexp{
regexp.MustCompile(`<create-subscription><stream>thestream</stream></create-subscription>`),
regexp.MustCompile(`<create-subscription xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0"><stream>thestream</stream></create-subscription>`),
},
},
}
Expand All @@ -590,19 +585,13 @@ func TestCreateSubscription(t *testing.T) {
ts.queueRespString(`<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1"><ok/></rpc-reply>`)

err := sess.CreateSubscription(context.Background(), tc.options...)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
assert.NoError(t, err)

sentMsg, err := ts.popReq()
if err != nil {
t.Errorf("failed to read message sent to sever: %v", err)
}
assert.NoError(t, err)

for _, match := range tc.matches {
if !match.Match(sentMsg) {
t.Errorf("sent message didn't match `%s`", match.String())
}
assert.Regexp(t, match, string(sentMsg))
}
})
}
Expand Down

0 comments on commit dd5a04d

Please sign in to comment.