diff --git a/nucleus/gnmi_transport_test.go b/nucleus/gnmi_transport_test.go index 888a01a7b49f1274c87dfad854fc70b2cd1977a3..246f46ecc024819457f65429ba55185876f7bfd4 100644 --- a/nucleus/gnmi_transport_test.go +++ b/nucleus/gnmi_transport_test.go @@ -641,36 +641,3 @@ func TestGnmi_set(t *testing.T) { } } -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, - 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) - } - }) - } -} diff --git a/nucleus/integration_test.go b/nucleus/integration_test.go index 26e543bb1bb7b8e61d61c3b5b6179b7b28420128..a4415afceab6db2abba979a0cece8f3c828a3f9c 100644 --- a/nucleus/integration_test.go +++ b/nucleus/integration_test.go @@ -27,6 +27,7 @@ func testSetupIntegration() { Password: "arista", Encoding: gpb.Encoding_JSON_IETF, }, + RespChan: make(chan *gpb.SubscribeResponse), } } @@ -94,13 +95,15 @@ func TestGnmi_SetIntegration(t *testing.T) { return } got, err := g.Set(tt.args.ctx, tt.args.params...) - if tt.want != nil { - tt.want.(*gpb.SetResponse).Timestamp = got.(*gpb.SetResponse).Timestamp - } if (err != nil) != tt.wantErr { t.Errorf("Set() error = %v, wantErr %v", err, tt.wantErr) return } + if got != nil { + if tt.want != nil { + tt.want.(*gpb.SetResponse).Timestamp = got.(*gpb.SetResponse).Timestamp + } + } if err != nil && tt.wantErr { return } else if !reflect.DeepEqual(got, tt.want) { @@ -109,65 +112,179 @@ func TestGnmi_SetIntegration(t *testing.T) { }) } } + func TestGnmi_GetIntegration(t *testing.T) { if testing.Short() { t.Skip("skipping integration test") } - t.Run("Test GNMI Get", func(t *testing.T) { - transport, err := NewGnmiTransport(opt) - if err != nil { - t.Error(err) - } - p := []string{"/interfaces/interface"} - resp, err := transport.Get(context.Background(), p...) - if err != nil { - t.Error(err) - } - if resp == nil { - t.Error("resp is nil") - } - }) -} + paths := []string{ + "/interfaces/interface", + "system/config/hostname", + } + type fields struct { + opt *GnmiTransportOptions + } + type args struct { + ctx context.Context + params []string + } + tests := []struct { + name string + fields fields + args args + want interface{} + wantErr bool + }{ + { + name: "default", + fields: fields{opt: opt}, + args: args{ + ctx: context.Background(), + params: paths[:1], + }, + want: gnmiMessages["../test/proto/resp-interfaces-arista-ceos"], + wantErr: false, + }, + { + name: "destination unreachable", + fields: fields{opt: &GnmiTransportOptions{ + Config: gnmi.Config{ + Addr: "203.0.113.10:6030", + }, + }, + }, + args: args{ + ctx: context.Background(), + params: paths, + }, + want: nil, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g, err := NewGnmiTransport(tt.fields.opt) + if err != nil { + t.Error(err) + return + } + got, err := g.Get(tt.args.ctx, tt.args.params...) + if (err != nil) != tt.wantErr { + t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("Get() got = %v, want %v", got, tt.want) + } + }) + } +} func TestGnmi_SubscribeIntegration(t *testing.T) { if testing.Short() { t.Skip("skipping integration test") } - if os.Getenv("GOSDN_TEST_ENDPOINT") != "" { - t.Skip("skipping locally running integration test") - } - - t.Run("Test GNMI Subscribe", func(t *testing.T) { - transport, err := NewGnmiTransport(opt) - if err != nil { - t.Error(err) - } - transport.RespChan = make(chan *gpb.SubscribeResponse) - paths := []string{"/interfaces/interface/name"} - - opts := &gnmi.SubscribeOptions{ - UpdatesOnly: false, - Prefix: "", - Mode: "stream", - StreamMode: "sample", - SampleInterval: uint64(10 * time.Second.Nanoseconds()), - SuppressRedundant: false, - HeartbeatInterval: uint64(time.Second.Nanoseconds()), - Paths: gnmi.SplitPaths(paths), - Origin: "", - Target: address, - } - ctx := context.WithValue(context.Background(), "opts", opts) - go func() { - if err := transport.Subscribe(ctx); err != nil { + type fields struct { + opt *GnmiTransportOptions + } + type args struct { + ctx context.Context + opts *gnmi.SubscribeOptions + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + { + name: "default", + fields: fields{ + opt: &GnmiTransportOptions{ + Config: opt.Config, + RespChan: make(chan *gpb.SubscribeResponse), + }, + }, + args: args{ + ctx: context.Background(), + opts: &gnmi.SubscribeOptions{ + Mode: "stream", + StreamMode: "sample", + SampleInterval: uint64(1 * time.Second), + HeartbeatInterval: uint64(100 * time.Millisecond), + Paths: gnmi.SplitPaths([]string{ + "/interfaces/interface/name", + "/system/config/hostname", + }), + Target: address, + }, + }, + wantErr: false, + }, + { + name: "wrong path", + fields: fields{ + opt: &GnmiTransportOptions{ + Config: opt.Config, + RespChan: make(chan *gpb.SubscribeResponse), + }, + }, + args: args{ + opts: &gnmi.SubscribeOptions{ + Mode: "stream", + StreamMode: "sample", + SampleInterval: uint64(1 * time.Second), + HeartbeatInterval: uint64(100 * time.Millisecond), + Paths: gnmi.SplitPaths([]string{ + "interfaces/interface/name", + "ystem/config/hostname", + }), + Target: address, + }, + }, + wantErr: true, + }, + { + name: "destination unreachable", + fields: fields{ + opt: &GnmiTransportOptions{ + Config: gnmi.Config{ + Addr: "203.0.113.10:6030", + }, + RespChan: make(chan *gpb.SubscribeResponse), + }, + }, + args: args{ + opts: &gnmi.SubscribeOptions{}, + }, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g, err := NewGnmiTransport(tt.fields.opt) + if err != nil { t.Error(err) + return } - }() - time.Sleep(time.Second * 30) - ctx.Done() - time.Sleep(time.Second * 5) - }) + ctx := context.WithValue(context.Background(), "opts", tt.args.opts) + ctx, cancel := context.WithCancel(ctx) + go func() { + err = g.Subscribe(ctx) + if (err != nil) != tt.wantErr { + if !tt.wantErr{ + if err.Error() != "rpc error: code = Canceled desc = context canceled"{ + t.Errorf("Subscribe() error = %v, wantErr %v", err, tt.wantErr) + } + } + } + }() + time.Sleep(time.Second * 3) + cancel() + time.Sleep(time.Second * 1) + }) + } } func TestGnmi_CapabilitiesIntegration(t *testing.T) { @@ -255,17 +372,3 @@ func TestGnmi_CapabilitiesIntegration(t *testing.T) { }) } } - -func TestPndImplementation_RequestAllIntegration(t *testing.T) { - if testing.Short() { - t.Skip("skipping integration test") - } - t.Fail() -} - -func TestPndImplementation_RequestIntegration(t *testing.T) { - if testing.Short() { - t.Skip("skipping integration test") - } - t.Fail() -}