Skip to content
Snippets Groups Projects
Commit f21a55d7 authored by Manuel Kieweg's avatar Manuel Kieweg
Browse files

Merge branch '43-increase-test-coverage' into 80-transport-tests

parents c33d5bbd 57fce2e7
No related branches found
No related tags found
2 merge requests!114WIP: Complete testing,!90Develop
Pipeline #66697 passed with warnings
...@@ -23,7 +23,6 @@ import ( ...@@ -23,7 +23,6 @@ import (
pb "github.com/openconfig/gnmi/proto/gnmi" pb "github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/gnmi/proto/gnmi_ext" "github.com/openconfig/gnmi/proto/gnmi_ext"
"google.golang.org/grpc/codes"
) )
// GetWithRequest takes a fully formed GetRequest, performs the Get, // GetWithRequest takes a fully formed GetRequest, performs the Get,
...@@ -415,24 +414,17 @@ func newSetRequest(setOps []*Operation, exts ...*gnmi_ext.Extension) (*pb.SetReq ...@@ -415,24 +414,17 @@ func newSetRequest(setOps []*Operation, exts ...*gnmi_ext.Extension) (*pb.SetReq
return req, nil 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, 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...) req, err := newSetRequest(setOps, exts...)
if err != nil { if err != nil {
return err return nil, 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 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. // Deprecated: Use SubscribeErr instead.
func Subscribe(ctx context.Context, client pb.GNMIClient, subscribeOptions *SubscribeOptions, func Subscribe(ctx context.Context, client pb.GNMIClient, subscribeOptions *SubscribeOptions,
respChan chan<- *pb.SubscribeResponse, errChan chan<- error) { respChan chan<- *pb.SubscribeResponse, errChan chan<- error) {
......
...@@ -2,7 +2,6 @@ package nucleus ...@@ -2,7 +2,6 @@ package nucleus
import ( import (
"code.fbi.h-da.de/cocsn/gosdn/forks/goarista/gnmi" "code.fbi.h-da.de/cocsn/gosdn/forks/goarista/gnmi"
"code.fbi.h-da.de/cocsn/gosdn/nucleus/util"
"context" "context"
gpb "github.com/openconfig/gnmi/proto/gnmi" gpb "github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/gnmi/proto/gnmi_ext" "github.com/openconfig/gnmi/proto/gnmi_ext"
...@@ -10,18 +9,8 @@ import ( ...@@ -10,18 +9,8 @@ import (
"github.com/openconfig/ygot/ytypes" "github.com/openconfig/ygot/ytypes"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"strings" "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) { func NewGnmiTransport(config *gnmi.Config) (*Gnmi, error) {
c, err := gnmi.Dial(config) c, err := gnmi.Dial(config)
if err != nil { if err != nil {
...@@ -123,7 +112,7 @@ func (g *Gnmi) Capabilities(ctx context.Context) (interface{}, error) { ...@@ -123,7 +112,7 @@ func (g *Gnmi) Capabilities(ctx context.Context) (interface{}, error) {
return resp, nil return resp, nil
} }
// Get calls GNMI get // get calls GNMI get
func (g *Gnmi) get(ctx context.Context, paths [][]string, origin string) (interface{}, error) { func (g *Gnmi) get(ctx context.Context, paths [][]string, origin string) (interface{}, error) {
ctx = gnmi.NewContext(ctx, g.config) ctx = gnmi.NewContext(ctx, g.config)
ctx = context.WithValue(ctx, "config", 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 ...@@ -138,14 +127,6 @@ func (g *Gnmi) get(ctx context.Context, paths [][]string, origin string) (interf
// and returns any response. // and returns any response.
func (g *Gnmi) getWithRequest(ctx context.Context, req *gpb.GetRequest) (interface{}, error) { func (g *Gnmi) getWithRequest(ctx context.Context, req *gpb.GetRequest) (interface{}, error) {
resp, err := g.client.Get(ctx, req) 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 { if err != nil {
return nil, err return nil, err
} }
...@@ -154,7 +135,7 @@ func (g *Gnmi) getWithRequest(ctx context.Context, req *gpb.GetRequest) (interfa ...@@ -154,7 +135,7 @@ func (g *Gnmi) getWithRequest(ctx context.Context, req *gpb.GetRequest) (interfa
// Set calls GNMI set // Set calls GNMI set
func (g *Gnmi) set(ctx context.Context, setOps []*gnmi.Operation, 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) ctx = gnmi.NewContext(ctx, g.config)
return gnmi.Set(ctx, g.client, setOps, exts...) return gnmi.Set(ctx, g.client, setOps, exts...)
} }
......
...@@ -7,9 +7,11 @@ import ( ...@@ -7,9 +7,11 @@ import (
"code.fbi.h-da.de/cocsn/gosdn/test" "code.fbi.h-da.de/cocsn/gosdn/test"
"code.fbi.h-da.de/cocsn/yang-models/generated/openconfig" "code.fbi.h-da.de/cocsn/yang-models/generated/openconfig"
"context" "context"
"errors"
log "github.com/golang/glog" log "github.com/golang/glog"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
gpb "github.com/openconfig/gnmi/proto/gnmi" gpb "github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/gnmi/proto/gnmi_ext"
"github.com/openconfig/goyang/pkg/yang" "github.com/openconfig/goyang/pkg/yang"
"github.com/openconfig/ygot/ytypes" "github.com/openconfig/ygot/ytypes"
"github.com/stretchr/testify/mock" "github.com/stretchr/testify/mock"
...@@ -482,10 +484,6 @@ func TestGnmi_Type(t *testing.T) { ...@@ -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) { func TestGnmi_getWithRequest(t *testing.T) {
transport := mockTransport() transport := mockTransport()
reqFullNode := gnmiMessages["../test/req-full-node"].(*gpb.GetRequest) reqFullNode := gnmiMessages["../test/req-full-node"].(*gpb.GetRequest)
...@@ -501,6 +499,10 @@ func TestGnmi_getWithRequest(t *testing.T) { ...@@ -501,6 +499,10 @@ func TestGnmi_getWithRequest(t *testing.T) {
On("Get", mockContext, reqInterfacesWildcard). On("Get", mockContext, reqInterfacesWildcard).
Return(respInterfacesWildcard, nil) Return(respInterfacesWildcard, nil)
transport.client.(*mocks.GNMIClient).
On("Get", mockContext, mock.Anything).
Return(nil, errors.New("expected mock gnmi error"))
type fields struct { type fields struct {
transport *Gnmi transport *Gnmi
} }
...@@ -533,6 +535,16 @@ func TestGnmi_getWithRequest(t *testing.T) { ...@@ -533,6 +535,16 @@ func TestGnmi_getWithRequest(t *testing.T) {
want: respInterfacesWildcard, want: respInterfacesWildcard,
wantErr: false, wantErr: false,
}, },
{
name: "invalid request",
fields: fields{transport: &transport},
args: args{
request: nil,
runEndpoint: false,
},
want: nil,
wantErr: true,
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
...@@ -548,17 +560,7 @@ func TestGnmi_getWithRequest(t *testing.T) { ...@@ -548,17 +560,7 @@ 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) { func TestNewGnmiTransport(t *testing.T) {
oc := &OpenConfig{}
respChan := make(chan *gpb.SubscribeResponse)
type args struct { type args struct {
config *gnmi.Config config *gnmi.Config
} }
...@@ -577,9 +579,6 @@ func TestNewGnmiTransport(t *testing.T) { ...@@ -577,9 +579,6 @@ func TestNewGnmiTransport(t *testing.T) {
Encoding: gpb.Encoding_PROTO, Encoding: gpb.Encoding_PROTO,
}}, }},
want: &Gnmi{ want: &Gnmi{
SetNode: oc.SetNode(),
Unmarshal: oc.Unmarshal(),
RespChan: respChan,
config: &gnmi.Config{ config: &gnmi.Config{
Username: "test", Username: "test",
Password: "test", Password: "test",
...@@ -611,7 +610,7 @@ func TestNewGnmiTransport(t *testing.T) { ...@@ -611,7 +610,7 @@ func TestNewGnmiTransport(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if tt.name == "defaul" { if tt.name == "default" {
startGnmiTarget <- gnmiConfig.Addr startGnmiTarget <- gnmiConfig.Addr
} }
got, err := NewGnmiTransport(tt.args.config) got, err := NewGnmiTransport(tt.args.config)
...@@ -619,12 +618,110 @@ func TestNewGnmiTransport(t *testing.T) { ...@@ -619,12 +618,110 @@ func TestNewGnmiTransport(t *testing.T) {
t.Errorf("NewGnmiTransport() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("NewGnmiTransport() error = %v, wantErr %v", err, tt.wantErr)
return return
} }
if tt.name == "default" && got != nil {
tt.want.client = got.client
}
if !reflect.DeepEqual(got, tt.want) { if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewGnmiTransport() got = %v, want %v", got, tt.want) t.Errorf("NewGnmiTransport() got = %v, want %v", got, tt.want)
} }
if tt.name == "defaul" { if tt.name == "default" {
stopGnmiTarget <- true stopGnmiTarget <- true
} }
}) })
} }
} }
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)
}
})
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment