From fd8c1ba11884876b49b4d8145f43592f1eb4c0d1 Mon Sep 17 00:00:00 2001 From: Malte Bauch <malte.bauch@stud.h-da.de> Date: Tue, 29 Mar 2022 12:18:16 +0000 Subject: [PATCH] Resolve "GetPath() test case for path `system` can fail if slice has the wrong order" See merge request danet/gosdn!269 --- controller/northbound/server/pnd_test.go | 28 +++++++++++++----------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/controller/northbound/server/pnd_test.go b/controller/northbound/server/pnd_test.go index 3bd0af7e1..243ebc23e 100644 --- a/controller/northbound/server/pnd_test.go +++ b/controller/northbound/server/pnd_test.go @@ -19,6 +19,7 @@ import ( "code.fbi.h-da.de/danet/yang-models/generated/openconfig" "github.com/golang/protobuf/proto" "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" "github.com/openconfig/gnmi/proto/gnmi" log "github.com/sirupsen/logrus" @@ -236,7 +237,16 @@ func Test_pnd_Get(t *testing.T) { func Test_pnd_GetPath(t *testing.T) { removeExistingStores() - // options to apply to cmp.Equal + + opts := cmp.Options{ + cmpopts.SortSlices( + func(x, y *gnmi.Update) bool { + return x.GetVal().String() < y.GetVal().String() + }, + ), + cmp.Comparer(proto.Equal), + } + type args struct { ctx context.Context request *ppb.GetPathRequest @@ -252,7 +262,7 @@ func Test_pnd_GetPath(t *testing.T) { args: args{ ctx: context.Background(), request: &ppb.GetPathRequest{ - Timestamp: 1648488947324854250, + Timestamp: time.Now().UnixNano(), Did: mockDevice.ID().String(), Path: "system/config/hostname", Pid: mockPnd.ID().String(), @@ -278,7 +288,7 @@ func Test_pnd_GetPath(t *testing.T) { args: args{ ctx: context.Background(), request: &ppb.GetPathRequest{ - Timestamp: 1648488947324854250, + Timestamp: time.Now().UnixNano(), Did: mockDevice.ID().String(), Path: "system", Pid: mockPnd.ID().String(), @@ -312,7 +322,7 @@ func Test_pnd_GetPath(t *testing.T) { args: args{ ctx: context.Background(), request: &ppb.GetPathRequest{ - Timestamp: 1648488947324854250, + Timestamp: time.Now().UnixNano(), Did: mockDevice.ID().String(), Path: "this/path/is/not/valid", Pid: mockPnd.ID().String(), @@ -335,16 +345,8 @@ func Test_pnd_GetPath(t *testing.T) { got := resp.GetDevice() - // with the current implementation we should only get a - // notification set with length one. For the case that we do not - // get anything this makes sure we do not trigger a panic. - if len(got) >= 1 { - // set timestamp for equality; not the prettiest solution - tt.want[0].Timestamp = got[0].Timestamp - } - for i, n := range got { - if diff := cmp.Diff(n, tt.want[i], cmp.Comparer(proto.Equal)); diff != "" { + if diff := cmp.Diff(n.GetUpdate(), tt.want[i].GetUpdate(), opts...); diff != "" { t.Errorf("GetPath() diff in the received notification %d: \n%s", i+1, diff) } } -- GitLab