Skip to content
This repository has been archived by the owner on Aug 31, 2022. It is now read-only.

Commit

Permalink
Separate path module from db_client and add testcase
Browse files Browse the repository at this point in the history
- Move the code that get DB table/key/field from gNMI path to path.go
- Add testcase for path module
- Add permit path list for SET RPC, and add testcase to cover it
  • Loading branch information
Liuqu committed Aug 22, 2018
1 parent b99bc6b commit dcb76f8
Show file tree
Hide file tree
Showing 7 changed files with 631 additions and 259 deletions.
6 changes: 3 additions & 3 deletions gnmi_server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func (srv *Server) Set(ctx context.Context, req *gnmipb.SetRequest) (*gnmipb.Set
}

for _, path := range req.GetDelete() {
log.V(6).Infof("Delete path: %v", path)
log.V(2).Infof("Delete path: %v", path)
err := dc.Set(path, "")
if err != nil {
return nil, err
Expand All @@ -283,7 +283,7 @@ func (srv *Server) Set(ctx context.Context, req *gnmipb.SetRequest) (*gnmipb.Set
if err != nil {
return nil, err
}
log.V(5).Infof("Replace path: %v val: %v", path, val)
log.V(2).Infof("Replace path: %v val: %v", path, val)
err = dc.Set(path.GetPath(), val)
if err != nil {
return nil, err
Expand All @@ -300,7 +300,7 @@ func (srv *Server) Set(ctx context.Context, req *gnmipb.SetRequest) (*gnmipb.Set
if err != nil {
return nil, err
}
log.V(5).Infof("Update path: %v val: %v", path, val)
log.V(2).Infof("Update path: %v val: %v", path, val)
err = dc.Set(path.GetPath(), val)
if err != nil {
return nil, err
Expand Down
91 changes: 59 additions & 32 deletions gnmi_server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/go-redis/redis"
"github.com/golang/protobuf/proto"

//"flag"
"flag"
"github.com/kylelemons/godebug/pretty"
"github.com/openconfig/gnmi/client"
pb "github.com/openconfig/gnmi/proto/gnmi"
Expand Down Expand Up @@ -237,6 +237,9 @@ func prepareDb(t *testing.T) {
}

func TestGnmiGet(t *testing.T) {
flag.Set("alsologtostderr", "true")
//flag.Set("v", "6")
flag.Parse()
//t.Log("Start server")
s := createServer(t)
go runServer(t, s)
Expand Down Expand Up @@ -1058,10 +1061,11 @@ func TestGnmiSet(t *testing.T) {
jByte := []byte(jVal)

tests := []struct {
desc string
in SimpleSRequest
want SimpleSResponse
wFV tblFV
desc string
in SimpleSRequest
want SimpleSResponse
wantFV tblFV
wantErr string
}{
{
desc: "Delete path",
Expand All @@ -1074,7 +1078,7 @@ func TestGnmiSet(t *testing.T) {
path: []string{"TELEMETRY_CLIENT", "Global", "retry_interval"},
op: int32(pb.UpdateResult_DELETE),
},
wFV: tblFV{
wantFV: tblFV{
tbl: "TELEMETRY_CLIENT|Global",
f: "retry_interval",
v: "",
Expand All @@ -1094,7 +1098,7 @@ func TestGnmiSet(t *testing.T) {
path: []string{"TELEMETRY_CLIENT", "Global", "retry_interval"},
op: int32(pb.UpdateResult_UPDATE),
},
wFV: tblFV{
wantFV: tblFV{
tbl: "TELEMETRY_CLIENT|Global",
f: "retry_interval",
v: "5",
Expand All @@ -1114,7 +1118,7 @@ func TestGnmiSet(t *testing.T) {
path: []string{"TELEMETRY_CLIENT", "DestinationGroup_TEST", "dst_addr"},
op: int32(pb.UpdateResult_UPDATE),
},
wFV: tblFV{
wantFV: tblFV{
tbl: "TELEMETRY_CLIENT|DestinationGroup_TEST",
f: "dst_addr",
v: "20.20.20.20:8081",
Expand All @@ -1134,7 +1138,7 @@ func TestGnmiSet(t *testing.T) {
path: []string{"TELEMETRY_CLIENT", "Global"},
op: int32(pb.UpdateResult_UPDATE),
},
wFV: tblFV{
wantFV: tblFV{
tbl: "TELEMETRY_CLIENT|Global",
f: "",
v: map[string]string{
Expand All @@ -1158,7 +1162,7 @@ func TestGnmiSet(t *testing.T) {
path: []string{"TELEMETRY_CLIENT", "Global", "retry_interval"},
op: int32(pb.UpdateResult_REPLACE),
},
wFV: tblFV{
wantFV: tblFV{
tbl: "TELEMETRY_CLIENT|Global",
f: "retry_interval",
v: "5",
Expand All @@ -1178,7 +1182,7 @@ func TestGnmiSet(t *testing.T) {
path: []string{"TELEMETRY_CLIENT", "DestinationGroup_TEST", "dst_addr"},
op: int32(pb.UpdateResult_REPLACE),
},
wFV: tblFV{
wantFV: tblFV{
tbl: "TELEMETRY_CLIENT|DestinationGroup_TEST",
f: "dst_addr",
v: "20.20.20.20:8081",
Expand All @@ -1198,7 +1202,7 @@ func TestGnmiSet(t *testing.T) {
path: []string{"TELEMETRY_CLIENT", "Global"},
op: int32(pb.UpdateResult_REPLACE),
},
wFV: tblFV{
wantFV: tblFV{
tbl: "TELEMETRY_CLIENT|Global",
f: "",
v: map[string]string{
Expand All @@ -1208,9 +1212,29 @@ func TestGnmiSet(t *testing.T) {
},
},
},
{
desc: "Update path not supported",
in: SimpleSRequest{
target: "CONFIG_DB",
updatePath: []string{"PORT", "Ethernet1", "alias"},
updateVal: pb.TypedValue{
Value: &pb.TypedValue_StringVal{"1"},
},
},
want: SimpleSResponse{
target: "CONFIG_DB",
path: []string{"TELEMETRY_CLIENT", "DestinationGroup_TEST", "dst_addr"},
op: int32(pb.UpdateResult_UPDATE),
},
wantFV: tblFV{
tbl: "TELEMETRY_CLIENT|DestinationGroup_TEST",
f: "dst_addr",
v: "20.20.20.20:8081",
},
wantErr: "config [CONFIG_DB PORT Ethernet1 alias] not supported",
},
}

wantRetCode := codes.OK
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
rclient.FlushDb()
Expand All @@ -1225,29 +1249,32 @@ func TestGnmiSet(t *testing.T) {
if !ok {
t.Fatal("got a non-grpc error from grpc call")
}
if gotRetStatus.Code() != wantRetCode {
if (len(test.wantErr) == 0 && gotRetStatus.Code() != codes.OK) ||
(len(test.wantErr) != 0 && gotRetStatus.Message() != test.wantErr) {
t.Log("err: ", err)
t.Fatalf("got return code %v, want %v", gotRetStatus.Code(), wantRetCode)
}

if resp == nil {
t.Fatal("got SetResponse nil")
}
if !compareSetResponse(resp, &test.want) {
t.Errorf("SetResponse is not expected: %v", resp)
t.Logf("want: %v", test.want)
t.Fatalf("got return code %v, want %v", gotRetStatus.Message(), test.wantErr)
}

// Check field value in DB
if test.wFV.f != "" {
val, _ := rclient.HGet(test.wFV.tbl, test.wFV.f).Result()
if val != test.wFV.v {
t.Errorf("got (%v) != wFV.v (%v)", val, test.wFV.v)
if len(test.wantErr) == 0 {
if resp == nil {
t.Fatal("got SetResponse nil")
}
if !compareSetResponse(resp, &test.want) {
t.Errorf("SetResponse is not expected: %v", resp)
t.Logf("want: %v", test.want)
}
} else {
val, _ := rclient.HGetAll(test.wFV.tbl).Result()
if !reflect.DeepEqual(val, test.wFV.v) {
t.Errorf("got (%v) != wFV.v (%v)", val, test.wFV.v)

// Check field value in DB
if test.wantFV.f != "" {
val, _ := rclient.HGet(test.wantFV.tbl, test.wantFV.f).Result()
if val != test.wantFV.v {
t.Errorf("got (%v) != wantFV.v (%v)", val, test.wantFV.v)
}
} else {
val, _ := rclient.HGetAll(test.wantFV.tbl).Result()
if !reflect.DeepEqual(val, test.wantFV.v) {
t.Errorf("got (%v) != wantFV.v (%v)", val, test.wantFV.v)
}
}
}
})
Expand Down
Loading

0 comments on commit dcb76f8

Please sign in to comment.