Skip to content

Commit

Permalink
Merge pull request #78 from openconfig/notfound
Browse files Browse the repository at this point in the history
Hide not found errors when doing gnmi.Get
  • Loading branch information
DanG100 authored Dec 21, 2022
2 parents 4393e98 + 9c503ad commit 66e9876
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 35 deletions.
8 changes: 6 additions & 2 deletions internal/testutil/gnmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,16 @@ func (g *clientWithGetter) Get(ctx context.Context, req *gpb.GetRequest, _ ...gr
}
resp := g.stub.getResponses[0]
g.stub.getResponses = g.stub.getResponses[1:]
return resp, nil
err := g.stub.getErrs[0]
g.stub.getErrs = g.stub.getErrs[1:]
return resp, err
}

// Stubber is a handle to add stubbed responses.
type Stubber struct {
gen *fpb.FixedGenerator
getResponses []*gpb.GetResponse
getErrs []error
}

// Notification appends the given notification as a stub response.
Expand All @@ -117,8 +120,9 @@ func (s *Stubber) Notification(n *gpb.Notification) *Stubber {
}

// GetResponse appends the given GetResponse as a stub response.
func (s *Stubber) GetResponse(gr *gpb.GetResponse) *Stubber {
func (s *Stubber) GetResponse(gr *gpb.GetResponse, err error) *Stubber {
s.getResponses = append(s.getResponses, gr)
s.getErrs = append(s.getErrs, err)
return s
}

Expand Down
4 changes: 3 additions & 1 deletion ygnmi/gnmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ func (gs *getSubscriber) Send(req *gpb.SubscribeRequest) error {
}
log.V(gs.client.requestLogLevel).Info(prototext.Format(getReq))
resp, err := gs.client.gnmiC.Get(gs.ctx, getReq)
if err != nil {
if st, ok := status.FromError(err); ok && st.Code() == codes.NotFound { // Make this behave like Subscribe, where non-existent paths don't return values.
return nil
} else if err != nil {
return err
}
gs.notifs = resp.GetNotification()
Expand Down
96 changes: 64 additions & 32 deletions ygnmi/ygnmi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
"github.com/openconfig/ygnmi/ygnmi"
"github.com/openconfig/ygot/util"
"github.com/openconfig/ygot/ygot"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/known/anypb"
Expand Down Expand Up @@ -425,36 +427,66 @@ func TestLookup(t *testing.T) {
}
})
}
t.Run("use get", func(t *testing.T) {
fakeGNMI.Stub().GetResponse(&gpb.GetResponse{
Notification: []*gpb.Notification{{
Timestamp: 100,
Update: []*gpb.Update{{
Path: leafPath,
Val: &gpb.TypedValue{Value: &gpb.TypedValue_JsonIetfVal{JsonIetfVal: []byte(`"foo"`)}},
}},
}},
})
wantGetRequest := &gpb.GetRequest{
Encoding: gpb.Encoding_JSON_IETF,
Type: gpb.GetRequest_STATE,
Prefix: &gpb.Path{},
Path: []*gpb.Path{leafPath},
}
wantVal := (&ygnmi.Value[string]{
Path: leafPath,
Timestamp: time.Unix(0, 100),
}).SetVal("foo")

got, err := ygnmi.Lookup(context.Background(), c, exampleocpath.Root().RemoteContainer().ALeaf().State(), ygnmi.WithUseGet())
if err != nil {
t.Fatalf("Lookup() returned unexpected error: %v", err)
}
if diff := cmp.Diff(wantVal, got, cmp.AllowUnexported(ygnmi.Value[string]{}), cmpopts.IgnoreFields(ygnmi.Value[string]{}, "RecvTimestamp"), protocmp.Transform()); diff != "" {
t.Errorf("Lookup() returned unexpected diff: %s", diff)
}
if diff := cmp.Diff(wantGetRequest, fakeGNMI.GetRequests()[0], protocmp.Transform()); diff != "" {
t.Errorf("Lookup() GetRequest different from expected: %s", diff)
t.Run("use get", func(t *testing.T) {
tests := []struct {
desc string
stub func(s *testutil.Stubber)
wantVal *ygnmi.Value[string]
wantRequest *gpb.GetRequest
wantErr string
}{{
desc: "success",
stub: func(s *testutil.Stubber) {
s.GetResponse(&gpb.GetResponse{
Notification: []*gpb.Notification{{
Timestamp: 100,
Update: []*gpb.Update{{
Path: leafPath,
Val: &gpb.TypedValue{Value: &gpb.TypedValue_JsonIetfVal{JsonIetfVal: []byte(`"foo"`)}},
}},
}},
}, nil)
},
wantVal: (&ygnmi.Value[string]{
Path: leafPath,
Timestamp: time.Unix(0, 100),
}).SetVal("foo"),
wantRequest: &gpb.GetRequest{
Encoding: gpb.Encoding_JSON_IETF,
Type: gpb.GetRequest_STATE,
Prefix: &gpb.Path{},
Path: []*gpb.Path{leafPath},
},
}, {
desc: "not found error",
stub: func(s *testutil.Stubber) {
s.GetResponse(nil, status.Error(codes.NotFound, "test"))
},
wantVal: (&ygnmi.Value[string]{
Path: leafPath,
}),
wantRequest: &gpb.GetRequest{
Encoding: gpb.Encoding_JSON_IETF,
Type: gpb.GetRequest_STATE,
Prefix: &gpb.Path{},
Path: []*gpb.Path{leafPath},
},
}}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
tt.stub(fakeGNMI.Stub())
got, err := ygnmi.Lookup(context.Background(), c, exampleocpath.Root().RemoteContainer().ALeaf().State(), ygnmi.WithUseGet())
if err != nil {
t.Fatalf("Lookup() returned unexpected error: %v", err)
}
if diff := cmp.Diff(tt.wantVal, got, cmp.AllowUnexported(ygnmi.Value[string]{}), cmpopts.IgnoreFields(ygnmi.Value[string]{}, "RecvTimestamp"), protocmp.Transform()); diff != "" {
t.Errorf("Lookup() returned unexpected diff: %s", diff)
}
if diff := cmp.Diff(tt.wantRequest, fakeGNMI.GetRequests()[0], protocmp.Transform()); diff != "" {
t.Errorf("Lookup() GetRequest different from expected: %s", diff)
}
})
}
})
}
Expand Down Expand Up @@ -531,7 +563,7 @@ func TestGet(t *testing.T) {
Val: &gpb.TypedValue{Value: &gpb.TypedValue_JsonIetfVal{JsonIetfVal: []byte(`"foo"`)}},
}},
}},
})
}, nil)
wantGetRequest := &gpb.GetRequest{
Encoding: gpb.Encoding_JSON_IETF,
Type: gpb.GetRequest_CONFIG,
Expand Down Expand Up @@ -1578,7 +1610,7 @@ func TestLookupAll(t *testing.T) {
Val: &gpb.TypedValue{Value: &gpb.TypedValue_JsonIetfVal{JsonIetfVal: []byte(`"1"`)}},
}},
}},
})
}, nil)
wantGetRequest := &gpb.GetRequest{
Encoding: gpb.Encoding_JSON_IETF,
Type: gpb.GetRequest_STATE,
Expand Down Expand Up @@ -1661,7 +1693,7 @@ func TestGetAll(t *testing.T) {
Val: &gpb.TypedValue{Value: &gpb.TypedValue_JsonIetfVal{JsonIetfVal: []byte(`"1"`)}},
}},
}},
})
}, nil)
wantGetRequest := &gpb.GetRequest{
Encoding: gpb.Encoding_JSON_IETF,
Type: gpb.GetRequest_STATE,
Expand Down

0 comments on commit 66e9876

Please sign in to comment.