Commit 57fce2e7 authored by Manuel Max Kieweg's avatar Manuel Max Kieweg
Browse files

merge commit

parent 93cb6608
......@@ -23,7 +23,6 @@ import (
pb "github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/gnmi/proto/gnmi_ext"
"google.golang.org/grpc/codes"
)
// GetWithRequest takes a fully formed GetRequest, performs the Get,
......@@ -415,24 +414,17 @@ func newSetRequest(setOps []*Operation, exts ...*gnmi_ext.Extension) (*pb.SetReq
return req, nil
}
// Set sends a SetRequest to the given ciena.
// Set sends a SetRequest to the given client.
func Set(ctx context.Context, client pb.GNMIClient, setOps []*Operation,
exts ...*gnmi_ext.Extension) error {
exts ...*gnmi_ext.Extension) (*pb.SetResponse, error) {
req, err := newSetRequest(setOps, exts...)
if err != nil {
return err
}
resp, err := client.Set(ctx, req)
if err != nil {
return err
}
if resp.Message != nil && codes.Code(resp.Message.Code) != codes.OK {
return errors.New(resp.Message.Message)
return nil, err
}
return nil
return client.Set(ctx, req)
}
// Subscribe sends a SubscribeRequest to the given ciena.
// Subscribe sends a SubscribeRequest to the given client.
// Deprecated: Use SubscribeErr instead.
func Subscribe(ctx context.Context, client pb.GNMIClient, subscribeOptions *SubscribeOptions,
respChan chan<- *pb.SubscribeResponse, errChan chan<- error) {
......
......@@ -2,7 +2,6 @@ package nucleus
import (
"code.fbi.h-da.de/cocsn/gosdn/forks/goarista/gnmi"
"code.fbi.h-da.de/cocsn/gosdn/nucleus/util"
"context"
gpb "github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/gnmi/proto/gnmi_ext"
......@@ -10,18 +9,8 @@ import (
"github.com/openconfig/ygot/ytypes"
log "github.com/sirupsen/logrus"
"strings"
"time"
)
var tapProto bool
func init() {
// tapProto taps gpb.getResponse and gpb.Getrequests
// to binary file
// CAUTION only set true if you know what you're doing
tapProto = false
}
func NewGnmiTransport(config *gnmi.Config) (*Gnmi, error) {
c, err := gnmi.Dial(config)
if err != nil {
......@@ -123,7 +112,7 @@ func (g *Gnmi) Capabilities(ctx context.Context) (interface{}, error) {
return resp, nil
}
// Get calls GNMI get
// get calls GNMI get
func (g *Gnmi) get(ctx context.Context, paths [][]string, origin string) (interface{}, error) {
ctx = gnmi.NewContext(ctx, g.config)
ctx = context.WithValue(ctx, "config", g.config)
......@@ -138,14 +127,6 @@ func (g *Gnmi) get(ctx context.Context, paths [][]string, origin string) (interf
// and returns any response.
func (g *Gnmi) getWithRequest(ctx context.Context, req *gpb.GetRequest) (interface{}, error) {
resp, err := g.client.Get(ctx, req)
if tapProto {
if err := util.Write(req, "get-req-"+time.Now().String()); err != nil {
log.Errorf("error while writing request: %v", err)
}
if err := util.Write(resp, "get-resp-"+time.Now().String()); err != nil {
log.Errorf("error while writing request: %v", err)
}
}
if err != nil {
return nil, err
}
......@@ -154,7 +135,7 @@ func (g *Gnmi) getWithRequest(ctx context.Context, req *gpb.GetRequest) (interfa
// Set calls GNMI set
func (g *Gnmi) set(ctx context.Context, setOps []*gnmi.Operation,
exts ...*gnmi_ext.Extension) error {
exts ...*gnmi_ext.Extension) (*gpb.SetResponse, error) {
ctx = gnmi.NewContext(ctx, g.config)
return gnmi.Set(ctx, g.client, setOps, exts...)
}
......
......@@ -7,9 +7,11 @@ import (
"code.fbi.h-da.de/cocsn/gosdn/test"
"code.fbi.h-da.de/cocsn/yang-models/generated/openconfig"
"context"
"errors"
log "github.com/golang/glog"
"github.com/golang/protobuf/proto"
gpb "github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/gnmi/proto/gnmi_ext"
"github.com/openconfig/goyang/pkg/yang"
"github.com/openconfig/ygot/ytypes"
"github.com/stretchr/testify/mock"
......@@ -482,10 +484,6 @@ func TestGnmi_Type(t *testing.T) {
}
}
func TestGnmi_get(t *testing.T) {
// TODO: Design integration test for this one
}
func TestGnmi_getWithRequest(t *testing.T) {
transport := mockTransport()
reqFullNode := gnmiMessages["../test/req-full-node"].(*gpb.GetRequest)
......@@ -501,6 +499,10 @@ func TestGnmi_getWithRequest(t *testing.T) {
On("Get", mockContext, reqInterfacesWildcard).
Return(respInterfacesWildcard, nil)
transport.client.(*mocks.GNMIClient).
On("Get", mockContext, mock.Anything).
Return(nil, errors.New("expected mock gnmi error"))
type fields struct {
transport *Gnmi
}
......@@ -533,6 +535,16 @@ func TestGnmi_getWithRequest(t *testing.T) {
want: respInterfacesWildcard,
wantErr: false,
},
{
name: "invalid request",
fields: fields{transport: &transport},
args: args{
request: nil,
runEndpoint: false,
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
......@@ -548,14 +560,6 @@ func TestGnmi_getWithRequest(t *testing.T) {
}
}
func TestGnmi_set(t *testing.T) {
// Does not test well
}
func TestGnmi_subscribe(t *testing.T) {
// TODO: Design integration test for this one
}
func TestNewGnmiTransport(t *testing.T) {
type args struct {
config *gnmi.Config
......@@ -614,7 +618,7 @@ func TestNewGnmiTransport(t *testing.T) {
t.Errorf("NewGnmiTransport() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.name == "default" && got != nil{
if tt.name == "default" && got != nil {
tt.want.client = got.client
}
if !reflect.DeepEqual(got, tt.want) {
......@@ -626,3 +630,98 @@ func TestNewGnmiTransport(t *testing.T) {
})
}
}
func TestGnmi_set(t *testing.T) {
transport := mockTransport()
mockResponse := &gpb.SetResponse{}
transport.client.(*mocks.GNMIClient).
On("NewContext", mockContext, mock.Anything).
Return(mockContext)
transport.client.(*mocks.GNMIClient).
On("Set", mockContext, mock.Anything,mock.Anything).
Return(mockResponse, nil)
type fields struct {
transport *Gnmi
}
type args struct {
ctx context.Context
setOps []*gnmi.Operation
exts []*gnmi_ext.Extension
}
tests := []struct {
name string
fields fields
args args
want *gpb.SetResponse
wantErr bool
}{
{
name: "default",
fields: fields{transport: &transport},
args: args{
ctx: context.Background(),
setOps: []*gnmi.Operation{
{
Type: "update",
Path: []string{"interfaces", "interface", "name"},
Val: "test0",
},
},
exts: nil,
},
want: &gpb.SetResponse{},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.fields.transport.set(tt.args.ctx, tt.args.setOps, tt.args.exts...)
if (err != nil) != tt.wantErr {
t.Errorf("set() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("set() got = %v, want %v", got, tt.want)
}
})
}
}
func TestGnmi_subscribe(t *testing.T) {
type fields struct {
SetNode func(schema *yang.Entry, root interface{}, path *gpb.Path, val interface{}, opts ...ytypes.SetNodeOpt) error
Unmarshal func([]byte, []string, interface{}, ...ytypes.UnmarshalOpt) error
RespChan chan *gpb.SubscribeResponse
config *gnmi.Config
client gpb.GNMIClient
}
type args struct {
ctx context.Context
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := &Gnmi{
SetNode: tt.fields.SetNode,
Unmarshal: tt.fields.Unmarshal,
RespChan: tt.fields.RespChan,
config: tt.fields.config,
client: tt.fields.client,
}
if err := g.subscribe(tt.args.ctx); (err != nil) != tt.wantErr {
t.Errorf("subscribe() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment