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

fixed tests for new transport options

parent 6ead82b9
Branches
Tags
3 merge requests!116Resolve "Transport Tests",!102Resolve "Follow-up from "Resolve "PND handling via CLI and database""",!90Develop
Pipeline #66885 passed
...@@ -90,6 +90,7 @@ func TestNewDevice(t *testing.T) { ...@@ -90,6 +90,7 @@ func TestNewDevice(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
tt.want.Transport.(*Gnmi).client = got.Transport.(*Gnmi).client
tt.want.Uuid = got.Id() tt.want.Uuid = got.Id()
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("NewDevice() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("NewDevice() error = %v, wantErr %v", err, tt.wantErr)
......
...@@ -16,7 +16,6 @@ type Gnmi struct { ...@@ -16,7 +16,6 @@ type Gnmi struct {
RespChan chan *gpb.SubscribeResponse RespChan chan *gpb.SubscribeResponse
Unmarshal func([]byte, []string, interface{}, ...ytypes.UnmarshalOpt) error Unmarshal func([]byte, []string, interface{}, ...ytypes.UnmarshalOpt) error
Options *GnmiTransportOptions Options *GnmiTransportOptions
config *gnmi.Config
client gpb.GNMIClient client gpb.GNMIClient
} }
...@@ -28,22 +27,11 @@ func NewGnmiTransport(opts *GnmiTransportOptions) (*Gnmi, error) { ...@@ -28,22 +27,11 @@ func NewGnmiTransport(opts *GnmiTransportOptions) (*Gnmi, error) {
return &Gnmi{ return &Gnmi{
SetNode: opts.SetNode, SetNode: opts.SetNode,
RespChan: opts.RespChan, RespChan: opts.RespChan,
config: &opts.Config,
Options: opts, Options: opts,
client: c, client: c,
}, nil }, nil
} }
//SetConfig sets the config of gnmi
func (g *Gnmi) SetConfig(config *gnmi.Config) {
g.config = config
}
//GetConfig returns the gnmi config
func (g *Gnmi) GetConfig() *gnmi.Config {
return g.config
}
//SetConfig sets the config of gnmi //SetConfig sets the config of gnmi
func (g *Gnmi) SetOptions(to TransportOptions) { func (g *Gnmi) SetOptions(to TransportOptions) {
g.Options = to.(*GnmiTransportOptions) g.Options = to.(*GnmiTransportOptions)
...@@ -54,16 +42,6 @@ func (g *Gnmi) GetOptions() interface{} { ...@@ -54,16 +42,6 @@ func (g *Gnmi) GetOptions() interface{} {
return g.Options return g.Options
} }
//CreateConfig creates a new gnmi config based on the given parameters
func createConfig(opts *GnmiTransportOptions) *gnmi.Config {
return &gnmi.Config{
Addr: opts.Addr,
Username: opts.Username,
Password: opts.Password,
Encoding: opts.Encoding,
}
}
// interface satisfaction for now // interface satisfaction for now
// TODO: Convert to meaningfiul calls // TODO: Convert to meaningfiul calls
func (g *Gnmi) Get(ctx context.Context, params ...string) (interface{}, error) { func (g *Gnmi) Get(ctx context.Context, params ...string) (interface{}, error) {
...@@ -162,8 +140,8 @@ func extraxtPathElements(path *gpb.Path) []string { ...@@ -162,8 +140,8 @@ func extraxtPathElements(path *gpb.Path) []string {
// Capabilities calls GNMI capabilities // Capabilities calls GNMI capabilities
func (g *Gnmi) Capabilities(ctx context.Context) (interface{}, error) { func (g *Gnmi) Capabilities(ctx context.Context) (interface{}, error) {
ctx = gnmi.NewContext(ctx, g.config) ctx = gnmi.NewContext(ctx, &g.Options.Config)
ctx = context.WithValue(ctx, "config", g.config) ctx = context.WithValue(ctx, "config", &g.Options.Config)
resp, err := g.client.Capabilities(ctx, &gpb.CapabilityRequest{}) resp, err := g.client.Capabilities(ctx, &gpb.CapabilityRequest{})
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -173,8 +151,8 @@ func (g *Gnmi) Capabilities(ctx context.Context) (interface{}, error) { ...@@ -173,8 +151,8 @@ func (g *Gnmi) Capabilities(ctx context.Context) (interface{}, error) {
// 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.Options.Config)
ctx = context.WithValue(ctx, "config", g.config) ctx = context.WithValue(ctx, "config", &g.Options.Config)
req, err := gnmi.NewGetRequest(ctx, paths, origin) req, err := gnmi.NewGetRequest(ctx, paths, origin)
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -195,13 +173,13 @@ func (g *Gnmi) getWithRequest(ctx context.Context, req *gpb.GetRequest) (interfa ...@@ -195,13 +173,13 @@ 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) (*gpb.SetResponse, error) { exts ...*gnmi_ext.Extension) (*gpb.SetResponse, error) {
ctx = gnmi.NewContext(ctx, g.config) ctx = gnmi.NewContext(ctx, &g.Options.Config)
return gnmi.Set(ctx, g.client, setOps, exts...) return gnmi.Set(ctx, g.client, setOps, exts...)
} }
// Subscribe calls GNMI subscribe // Subscribe calls GNMI subscribe
func (g *Gnmi) subscribe(ctx context.Context) error { func (g *Gnmi) subscribe(ctx context.Context) error {
ctx = gnmi.NewContext(ctx, g.config) ctx = gnmi.NewContext(ctx, &g.Options.Config)
opts := ctx.Value("opts").(*gnmi.SubscribeOptions) opts := ctx.Value("opts").(*gnmi.SubscribeOptions)
go func() { go func() {
for { for {
......
...@@ -79,7 +79,6 @@ func mockTransport() Gnmi { ...@@ -79,7 +79,6 @@ func mockTransport() Gnmi {
SetNode: nil, SetNode: nil,
RespChan: make(chan *gpb.SubscribeResponse), RespChan: make(chan *gpb.SubscribeResponse),
Options: newGnmiTransportOptions(), Options: newGnmiTransportOptions(),
config: gnmiConfig,
client: &mocks.GNMIClient{}, client: &mocks.GNMIClient{},
} }
} }
...@@ -185,7 +184,6 @@ func TestGnmi_Close(t *testing.T) { ...@@ -185,7 +184,6 @@ func TestGnmi_Close(t *testing.T) {
g := &Gnmi{ g := &Gnmi{
SetNode: tt.fields.SetNode, SetNode: tt.fields.SetNode,
RespChan: tt.fields.RespChan, RespChan: tt.fields.RespChan,
config: tt.fields.config,
} }
if err := g.Close(); (err != nil) != tt.wantErr { if err := g.Close(); (err != nil) != tt.wantErr {
t.Errorf("Close() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("Close() error = %v, wantErr %v", err, tt.wantErr)
...@@ -264,7 +262,7 @@ func TestGnmi_Get(t *testing.T) { ...@@ -264,7 +262,7 @@ func TestGnmi_Get(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.args.runEndpoint { if tt.args.runEndpoint {
startGnmiTarget <- tt.fields.transport.config.Addr startGnmiTarget <- tt.fields.transport.Options.Addr
} }
got, err := tt.fields.transport.Get(context.Background(), tt.args.params...) got, err := tt.fields.transport.Get(context.Background(), tt.args.params...)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
...@@ -281,31 +279,6 @@ func TestGnmi_Get(t *testing.T) { ...@@ -281,31 +279,6 @@ func TestGnmi_Get(t *testing.T) {
} }
} }
func TestGnmi_GetConfig(t *testing.T) {
transport := mockTransport()
type fields struct {
transport *Gnmi
}
tests := []struct {
name string
fields fields
want *gnmi.Config
}{
{
name: "default",
fields: fields{transport: &transport},
want: gnmiConfig,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.fields.transport.GetConfig(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetConfig() = %v, want %v", got, tt.want)
}
})
}
}
func TestGnmi_ProcessResponse(t *testing.T) { func TestGnmi_ProcessResponse(t *testing.T) {
type fields struct { type fields struct {
Sbi SouthboundInterface Sbi SouthboundInterface
...@@ -403,47 +376,6 @@ func TestGnmi_Set(t *testing.T) { ...@@ -403,47 +376,6 @@ func TestGnmi_Set(t *testing.T) {
} }
} }
func TestGnmi_SetConfig(t *testing.T) {
type fields struct {
SetNode func(schema *yang.Entry, root interface{}, path *gpb.Path, val interface{}, opts ...ytypes.SetNodeOpt) error
RespChan chan *gpb.SubscribeResponse
config *gnmi.Config
}
type args struct {
config *gnmi.Config
}
tests := []struct {
name string
fields fields
args args
}{
{
name: "default",
fields: fields{
SetNode: nil,
RespChan: nil,
config: nil,
},
args: args{
config: &gnmi.Config{
Addr: "test:///",
Password: "test",
Username: "test",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := &Gnmi{}
g.SetConfig(tt.args.config)
if !reflect.DeepEqual(g.config, tt.args.config) {
t.Errorf("Type() = %v, want %v", g.config, tt.args.config)
}
})
}
}
func TestGnmi_Subscribe(t *testing.T) { func TestGnmi_Subscribe(t *testing.T) {
type fields struct { type fields struct {
SetNode func(schema *yang.Entry, root interface{}, path *gpb.Path, val interface{}, opts ...ytypes.SetNodeOpt) error SetNode func(schema *yang.Entry, root interface{}, path *gpb.Path, val interface{}, opts ...ytypes.SetNodeOpt) error
...@@ -490,7 +422,6 @@ func TestGnmi_Type(t *testing.T) { ...@@ -490,7 +422,6 @@ func TestGnmi_Type(t *testing.T) {
g := &Gnmi{ g := &Gnmi{
SetNode: tt.fields.SetNode, SetNode: tt.fields.SetNode,
RespChan: tt.fields.RespChan, RespChan: tt.fields.RespChan,
config: tt.fields.config,
} }
if got := g.Type(); got != tt.want { if got := g.Type(); got != tt.want {
t.Errorf("Type() = %v, want %v", got, tt.want) t.Errorf("Type() = %v, want %v", got, tt.want)
...@@ -596,11 +527,13 @@ func TestNewGnmiTransport(t *testing.T) { ...@@ -596,11 +527,13 @@ func TestNewGnmiTransport(t *testing.T) {
}, },
}}, }},
want: &Gnmi{ want: &Gnmi{
config: &gnmi.Config{ Options: &GnmiTransportOptions{
Username: "test", Config: gnmi.Config{
Password: "test", Username: "test",
Addr: "localhost:13371", Password: "test",
Encoding: gpb.Encoding_PROTO, Addr: "localhost:13371",
Encoding: gpb.Encoding_PROTO,
},
}, },
client: nil, client: nil,
}, },
...@@ -733,7 +666,6 @@ func TestGnmi_subscribe(t *testing.T) { ...@@ -733,7 +666,6 @@ func TestGnmi_subscribe(t *testing.T) {
SetNode: tt.fields.SetNode, SetNode: tt.fields.SetNode,
Unmarshal: tt.fields.Unmarshal, Unmarshal: tt.fields.Unmarshal,
RespChan: tt.fields.RespChan, RespChan: tt.fields.RespChan,
config: tt.fields.config,
client: tt.fields.client, client: tt.fields.client,
} }
if err := g.subscribe(tt.args.ctx); (err != nil) != tt.wantErr { if err := g.subscribe(tt.args.ctx); (err != nil) != tt.wantErr {
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
gpb "github.com/openconfig/gnmi/proto/gnmi" gpb "github.com/openconfig/gnmi/proto/gnmi"
"os" "os"
"reflect" "reflect"
"sort"
"testing" "testing"
"time" "time"
) )
...@@ -238,6 +239,12 @@ func TestGnmi_CapabilitiesIntegration(t *testing.T) { ...@@ -238,6 +239,12 @@ func TestGnmi_CapabilitiesIntegration(t *testing.T) {
got = resp.(*gpb.CapabilityResponse).SupportedEncodings got = resp.(*gpb.CapabilityResponse).SupportedEncodings
case "supported models": case "supported models":
got = resp.(*gpb.CapabilityResponse).SupportedModels got = resp.(*gpb.CapabilityResponse).SupportedModels
sort.Slice(got.([]*gpb.ModelData), func(i, j int) bool {
return got.([]*gpb.ModelData)[i].Name < got.([]*gpb.ModelData)[j].Name
})
sort.Slice(tt.want.([]*gpb.ModelData), func(i, j int) bool {
return tt.want.([]*gpb.ModelData)[i].Name < tt.want.([]*gpb.ModelData)[j].Name
})
case "gnmi version": case "gnmi version":
got = resp.(*gpb.CapabilityResponse).GNMIVersion got = resp.(*gpb.CapabilityResponse).GNMIVersion
default: default:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment