@@ -3,6 +3,7 @@ package gnmi
3
3
import (
4
4
"crypto/tls"
5
5
"fmt"
6
+ "io"
6
7
"path/filepath"
7
8
"reflect"
8
9
"strings"
@@ -18,11 +19,10 @@ import (
18
19
extnpb "github.com/openconfig/gnmi/proto/gnmi_ext"
19
20
"github.com/openconfig/ygot/ygot"
20
21
spb "github.com/sonic-net/sonic-gnmi/proto"
22
+ spb_gnoi "github.com/sonic-net/sonic-gnmi/proto/gnoi"
21
23
dbconfig "github.com/sonic-net/sonic-gnmi/sonic_db_config"
22
24
"golang.org/x/net/context"
23
- "google.golang.org/grpc"
24
25
"google.golang.org/grpc/codes"
25
- "google.golang.org/grpc/credentials"
26
26
"google.golang.org/grpc/status"
27
27
)
28
28
@@ -875,17 +875,11 @@ func doSet(t *testing.T, data ...interface{}) {
875
875
}
876
876
}
877
877
878
- cred := credentials .NewTLS (& tls.Config {InsecureSkipVerify : true })
879
- conn , err := grpc .Dial ("127.0.0.1:8081" , grpc .WithTransportCredentials (cred ))
880
- if err != nil {
881
- t .Fatalf ("Could not create client: %v" , err )
882
- }
883
-
878
+ client := gnmipb .NewGNMIClient (createClient (t , 8081 ))
884
879
ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
885
880
defer cancel ()
886
- defer conn .Close ()
887
881
888
- _ , err = gnmipb . NewGNMIClient ( conn ) .Set (ctx , req )
882
+ _ , err := client .Set (ctx , req )
889
883
if err != nil {
890
884
t .Fatalf ("Set failed: %v" , err )
891
885
}
@@ -935,3 +929,155 @@ func newBundleVersion(t *testing.T, version string) *extnpb.Extension {
935
929
ext := & extnpb.RegisteredExtension {Id : spb .BUNDLE_VERSION_EXT , Msg : v }
936
930
return & extnpb.Extension {Ext : & extnpb.Extension_RegisteredExt {RegisteredExt : ext }}
937
931
}
932
+
933
+ func TestDebugSubscribePreferences (t * testing.T ) {
934
+ s := createServer (t , 8081 )
935
+ go runServer (t , s )
936
+ defer s .s .Stop ()
937
+
938
+ ifTop := & spb_gnoi.SubscribePreference {
939
+ Path : strToPath ("/openconfig-interfaces:interfaces/interface[name=*]" ),
940
+ OnChangeSupported : false ,
941
+ TargetDefinedMode : ON_CHANGE ,
942
+ WildcardSupported : true ,
943
+ }
944
+ ifMtu := & spb_gnoi.SubscribePreference {
945
+ Path : strToPath ("/openconfig-interfaces:interfaces/interface[name=*]/config/mtu" ),
946
+ OnChangeSupported : true ,
947
+ TargetDefinedMode : ON_CHANGE ,
948
+ WildcardSupported : true ,
949
+ }
950
+ ifStat := & spb_gnoi.SubscribePreference {
951
+ Path : strToPath ("/openconfig-interfaces:interfaces/interface[name=*]/state/counters" ),
952
+ OnChangeSupported : false ,
953
+ TargetDefinedMode : SAMPLE ,
954
+ WildcardSupported : true ,
955
+ }
956
+ aclConfig := & spb_gnoi.SubscribePreference {
957
+ Path : strToPath ("/openconfig-acl:acl/acl-sets/acl-set[name=*][type=*]/config" ),
958
+ OnChangeSupported : true ,
959
+ TargetDefinedMode : ON_CHANGE ,
960
+ WildcardSupported : true ,
961
+ }
962
+ yanglib := & spb_gnoi.SubscribePreference {
963
+ Path : strToPath ("/ietf-yang-library:modules-state/module-set-id" ),
964
+ OnChangeSupported : false ,
965
+ TargetDefinedMode : SAMPLE ,
966
+ WildcardSupported : false ,
967
+ }
968
+
969
+ t .Run ("invalid_path" , func (t * testing.T ) {
970
+ _ , err := getSubscribePreferences (t , nil )
971
+ if res , _ := status .FromError (err ); res .Code () != codes .InvalidArgument {
972
+ t .Fatalf ("Expecting InvalidArgument error; got %v" , err )
973
+ }
974
+ })
975
+
976
+ t .Run ("unknown_path" , func (t * testing.T ) {
977
+ _ , err := getSubscribePreferences (t , strToPath ("/unknown" ))
978
+ if res , _ := status .FromError (err ); res .Code () != codes .InvalidArgument {
979
+ t .Fatalf ("Expecting InvalidArgument error; got %v" , err )
980
+ }
981
+ })
982
+
983
+ t .Run ("onchange_supported" , func (t * testing.T ) {
984
+ verifySubscribePreferences (t ,
985
+ []* gnmipb.Path {ifMtu .Path },
986
+ []* spb_gnoi.SubscribePreference {ifMtu })
987
+ })
988
+
989
+ t .Run ("onchange_unsupported" , func (t * testing.T ) {
990
+ verifySubscribePreferences (t ,
991
+ []* gnmipb.Path {ifStat .Path },
992
+ []* spb_gnoi.SubscribePreference {ifStat })
993
+ })
994
+
995
+ t .Run ("onchange_mixed" , func (t * testing.T ) {
996
+ verifySubscribePreferences (t ,
997
+ []* gnmipb.Path {ifTop .Path },
998
+ []* spb_gnoi.SubscribePreference {ifTop , ifStat })
999
+ })
1000
+
1001
+ t .Run ("nondb_path" , func (t * testing.T ) {
1002
+ verifySubscribePreferences (t ,
1003
+ []* gnmipb.Path {yanglib .Path },
1004
+ []* spb_gnoi.SubscribePreference {yanglib })
1005
+ })
1006
+
1007
+ t .Run ("unprefixed_path" , func (t * testing.T ) {
1008
+ verifySubscribePreferences (t ,
1009
+ []* gnmipb.Path {strToPath ("/acl/acl-sets/acl-set/config" )},
1010
+ []* spb_gnoi.SubscribePreference {aclConfig })
1011
+ })
1012
+
1013
+ t .Run ("multiple_paths" , func (t * testing.T ) {
1014
+ verifySubscribePreferences (t ,
1015
+ []* gnmipb.Path {yanglib .Path , ifTop .Path , aclConfig .Path },
1016
+ []* spb_gnoi.SubscribePreference {yanglib , ifTop , ifStat , aclConfig })
1017
+ })
1018
+ }
1019
+
1020
+ func TestDebugSubscribePreferences_dummy (t * testing.T ) {
1021
+ // Dummy testcase to increase code coverage !!!
1022
+ f := func (_ ... interface {}) {}
1023
+ for _ , m := range []* spb_gnoi.SubscribePreferencesReq {nil , {}} {
1024
+ f (m .String (), m .GetPath ())
1025
+ f (m .Descriptor ())
1026
+ }
1027
+ for _ , p := range []* spb_gnoi.SubscribePreference {nil , {}} {
1028
+ f (p .String (), p .GetPath (), p .GetOnChangeSupported (), p .GetTargetDefinedMode (), p .GetWildcardSupported (), p .GetMinSampleInterval ())
1029
+ f (p .Descriptor ())
1030
+ }
1031
+ }
1032
+
1033
+ func getSubscribePreferences (t * testing.T , paths ... * gnmipb.Path ) ([]* spb_gnoi.SubscribePreference , error ) {
1034
+ t .Helper ()
1035
+ client := spb_gnoi .NewDebugClient (createClient (t , 8081 ))
1036
+ stream , err := client .GetSubscribePreferences (
1037
+ context .Background (),
1038
+ & spb_gnoi.SubscribePreferencesReq {Path : paths },
1039
+ )
1040
+ if err != nil {
1041
+ t .Fatalf ("Could not invoke GetSubscribePreferences: %v" , err )
1042
+ }
1043
+
1044
+ var prefs []* spb_gnoi.SubscribePreference
1045
+ for {
1046
+ if p , err := stream .Recv (); err == nil {
1047
+ prefs = append (prefs , p )
1048
+ } else if err == io .EOF {
1049
+ break
1050
+ } else {
1051
+ return prefs , err
1052
+ }
1053
+ }
1054
+
1055
+ return prefs , nil
1056
+ }
1057
+
1058
+ func verifySubscribePreferences (t * testing.T , paths []* gnmipb.Path , exp []* spb_gnoi.SubscribePreference ) {
1059
+ t .Helper ()
1060
+ resp , err := getSubscribePreferences (t , paths ... )
1061
+ if err != nil {
1062
+ t .Fatalf ("GetSubscribePreferences returned error: %v" , err )
1063
+ }
1064
+ if len (resp ) != len (exp ) {
1065
+ t .Fatalf ("Expected: %s\n Received: %s" , prefsText (exp ), prefsText (resp ))
1066
+ }
1067
+ for i , ex := range exp {
1068
+ if ex .MinSampleInterval == 0 {
1069
+ resp [i ].MinSampleInterval = 0 // ignore MinSampleInterval for comparison
1070
+ }
1071
+ if ! proto .Equal (ex , resp [i ]) {
1072
+ t .Fatalf ("Expected: %s\n Received: %s" , prefsText (exp ), prefsText (resp ))
1073
+ }
1074
+ }
1075
+ }
1076
+
1077
+ func prefsText (prefs []* spb_gnoi.SubscribePreference ) string {
1078
+ var s []string
1079
+ for _ , p := range prefs {
1080
+ s = append (s , proto .MarshalTextString (p ))
1081
+ }
1082
+ return "[\n " + strings .Join (s , "\n " ) + "]"
1083
+ }
0 commit comments