diff --git a/nucleus/device_test.go b/nucleus/device_test.go index 900855346f0d7d4beb2bc39106fc9790af2cb761..d6391e830b584836b8d5d6a8928d5320d05e2e36 100644 --- a/nucleus/device_test.go +++ b/nucleus/device_test.go @@ -90,6 +90,7 @@ func TestNewDevice(t *testing.T) { if err != nil { t.Error(err) } + tt.want.Transport.(*Gnmi).client = got.Transport.(*Gnmi).client tt.want.Uuid = got.Id() if (err != nil) != tt.wantErr { t.Errorf("NewDevice() error = %v, wantErr %v", err, tt.wantErr) diff --git a/nucleus/gnmi_transport.go b/nucleus/gnmi_transport.go index 500dd66391cb4ff0207280b395094881303f0e9e..30af29f2320e673a0f013e5738fa00ff63cfdbe4 100644 --- a/nucleus/gnmi_transport.go +++ b/nucleus/gnmi_transport.go @@ -16,7 +16,6 @@ type Gnmi struct { RespChan chan *gpb.SubscribeResponse Unmarshal func([]byte, []string, interface{}, ...ytypes.UnmarshalOpt) error Options *GnmiTransportOptions - config *gnmi.Config client gpb.GNMIClient } @@ -28,22 +27,11 @@ func NewGnmiTransport(opts *GnmiTransportOptions) (*Gnmi, error) { return &Gnmi{ SetNode: opts.SetNode, RespChan: opts.RespChan, - config: &opts.Config, Options: opts, client: c, }, 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 func (g *Gnmi) SetOptions(to TransportOptions) { g.Options = to.(*GnmiTransportOptions) @@ -54,16 +42,6 @@ func (g *Gnmi) GetOptions() interface{} { 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 // TODO: Convert to meaningfiul calls func (g *Gnmi) Get(ctx context.Context, params ...string) (interface{}, error) { @@ -162,8 +140,8 @@ func extraxtPathElements(path *gpb.Path) []string { // Capabilities calls GNMI capabilities func (g *Gnmi) Capabilities(ctx context.Context) (interface{}, error) { - ctx = gnmi.NewContext(ctx, g.config) - ctx = context.WithValue(ctx, "config", g.config) + ctx = gnmi.NewContext(ctx, &g.Options.Config) + ctx = context.WithValue(ctx, "config", &g.Options.Config) resp, err := g.client.Capabilities(ctx, &gpb.CapabilityRequest{}) if err != nil { return nil, err @@ -173,8 +151,8 @@ func (g *Gnmi) Capabilities(ctx context.Context) (interface{}, error) { // 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) + ctx = gnmi.NewContext(ctx, &g.Options.Config) + ctx = context.WithValue(ctx, "config", &g.Options.Config) req, err := gnmi.NewGetRequest(ctx, paths, origin) if err != nil { return nil, err @@ -195,13 +173,13 @@ 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) (*gpb.SetResponse, error) { - ctx = gnmi.NewContext(ctx, g.config) + ctx = gnmi.NewContext(ctx, &g.Options.Config) return gnmi.Set(ctx, g.client, setOps, exts...) } // Subscribe calls GNMI subscribe 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) go func() { for { diff --git a/nucleus/gnmi_transport_test.go b/nucleus/gnmi_transport_test.go index 54fcf554d79577fda7ecb7e0c7a90ea1d0564f24..206e231a9d1666022f7ef6fa83b2b43ded9ac858 100644 --- a/nucleus/gnmi_transport_test.go +++ b/nucleus/gnmi_transport_test.go @@ -79,7 +79,6 @@ func mockTransport() Gnmi { SetNode: nil, RespChan: make(chan *gpb.SubscribeResponse), Options: newGnmiTransportOptions(), - config: gnmiConfig, client: &mocks.GNMIClient{}, } } @@ -185,7 +184,6 @@ func TestGnmi_Close(t *testing.T) { g := &Gnmi{ SetNode: tt.fields.SetNode, RespChan: tt.fields.RespChan, - config: tt.fields.config, } if err := g.Close(); (err != nil) != tt.wantErr { t.Errorf("Close() error = %v, wantErr %v", err, tt.wantErr) @@ -264,7 +262,7 @@ func TestGnmi_Get(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { 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...) if (err != nil) != tt.wantErr { @@ -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) { type fields struct { Sbi SouthboundInterface @@ -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) { type fields struct { 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) { g := &Gnmi{ SetNode: tt.fields.SetNode, RespChan: tt.fields.RespChan, - config: tt.fields.config, } if got := g.Type(); got != tt.want { t.Errorf("Type() = %v, want %v", got, tt.want) @@ -596,11 +527,13 @@ func TestNewGnmiTransport(t *testing.T) { }, }}, want: &Gnmi{ - config: &gnmi.Config{ - Username: "test", - Password: "test", - Addr: "localhost:13371", - Encoding: gpb.Encoding_PROTO, + Options: &GnmiTransportOptions{ + Config: gnmi.Config{ + Username: "test", + Password: "test", + Addr: "localhost:13371", + Encoding: gpb.Encoding_PROTO, + }, }, client: nil, }, @@ -733,7 +666,6 @@ func TestGnmi_subscribe(t *testing.T) { 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 { diff --git a/nucleus/integration_test.go b/nucleus/integration_test.go index 1f73e3f7ce8c0365729e31cb40398471f2f71106..c57e2827f36abb7541cf4ec9b5b0df967e91cd62 100644 --- a/nucleus/integration_test.go +++ b/nucleus/integration_test.go @@ -6,6 +6,7 @@ import ( gpb "github.com/openconfig/gnmi/proto/gnmi" "os" "reflect" + "sort" "testing" "time" ) @@ -238,6 +239,12 @@ func TestGnmi_CapabilitiesIntegration(t *testing.T) { got = resp.(*gpb.CapabilityResponse).SupportedEncodings case "supported models": 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": got = resp.(*gpb.CapabilityResponse).GNMIVersion default: