Newer
Older
"io"
"code.fbi.h-da.de/danet/gosdn/api/go/gosdn/csbi"
ppb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/pnd"
spb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/southbound"
tpb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/transport"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/device"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/networkdomain"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/southbound"
"code.fbi.h-da.de/danet/gosdn/controller/mocks"
"code.fbi.h-da.de/danet/gosdn/controller/store"
"code.fbi.h-da.de/danet/yang-models/generated/openconfig"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/mock"
)
func removeExistingPNDStore() {
os.Remove(fmt.Sprintf("stores/device-store-%s.json", defaultPndID))
}
func TestNewPND(t *testing.T) {
removeExistingPNDStore()
p := newPnd()
if err := p.addSbi(&OpenConfig{id: defaultSbiID}); err != nil {
type args struct {
name string
description string
}{
{
name: "default",
args: args{
name: "default",
description: "default test pnd",
sbi: &OpenConfig{id: defaultSbiID},
pid: defaultPndID,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewPND(tt.args.name, tt.args.description, tt.args.pid, tt.args.sbi, nil, nil)
if (err != nil) != tt.wantErr {
t.Errorf("NewPND() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewPND() = %v, want %v", got, tt.want)
}
})
}
}
func Test_destroy(t *testing.T) {
tests := []struct {
name string
wantErr bool
}{
{name: "dummy", wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := destroy(); (err != nil) != tt.wantErr {
t.Errorf("destroy() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func Test_pndImplementation_AddDevice(t *testing.T) {
type args struct {
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "default",
name: "fridolin",
opts: &tpb.TransportOption{
TransportOption: &tpb.TransportOption_GnmiTransportOption{
GnmiTransportOption: &tpb.GnmiTransportOption{},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := pnd.addSbi(&OpenConfig{id: defaultSbiID}); err != nil {
t.Error(err)
}
if tt.name == "already exists" {
pnd.devices.Store[did] = tt.args.device.(device.Device)
err := pnd.AddDevice(tt.args.name, tt.args.opts, defaultSbiID)
if (err != nil) != tt.wantErr {
t.Errorf("AddDevice() error = %v, wantErr %v", err, tt.wantErr)
if tt.name != "fails wrong type" {
if err == nil {
d, err := pnd.devices.GetDevice(store.FromString(tt.args.name))
if err != nil {
t.Errorf("AddDevice() error = %v", err)
return
if d.Name() != tt.args.name {
t.Errorf("AddDevice() got = %v, want %v", d.Name(), tt.args.name)
}
if err := pnd.devices.Delete(d.ID()); err != nil {
// TODO: refactor test to use store interface instead if direct access.
func Test_pndImplementation_AddSbi(t *testing.T) {
type args struct {
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "default",
args: args{
sbi: &OpenConfig{
},
},
wantErr: false,
},
{
name: "already exists",
args: args{
sbi: &OpenConfig{
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.name == "already exists" {
pnd.sbic.Store[defaultSbiID] = tt.args.sbi
err := pnd.AddSbi(tt.args.sbi)
if (err != nil) != tt.wantErr {
t.Errorf("AddSbi() error = %v, wantErr %v", err, tt.wantErr)
if tt.name != "fails wrong type" {
if err == nil {
if !ok {
t.Errorf("AddSbi() SBI %v not in device store %v",
tt.args.sbi, pnd.GetSBIs())
}
if err := pnd.sbic.Delete(defaultSbiID); err != nil {
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
func Test_pndImplementation_AddSbiFromStore(t *testing.T) {
type args struct {
uuid uuid.UUID
ttype spb.Type
sbi southbound.SouthboundInterface
path string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "default",
args: args{
uuid: defaultSbiID,
ttype: spb.Type_TYPE_OPENCONFIG,
path: "",
},
wantErr: false,
},
{
name: "already exists",
args: args{
uuid: defaultSbiID,
ttype: spb.Type_TYPE_OPENCONFIG,
path: "",
sbi: &OpenConfig{
id: defaultSbiID,
schema: nil,
path: "",
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pnd := newPnd()
if tt.name == "already exists" {
pnd.sbic.Store[defaultSbiID] = tt.args.sbi.(southbound.SouthboundInterface)
}
err := pnd.AddSbiFromStore(tt.args.uuid, tt.args.ttype.String(), tt.args.path)
if (err != nil) != tt.wantErr {
t.Errorf("AddSbiFromStore() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func Test_pndImplementation_ContainsDevice(t *testing.T) {
removeExistingPNDStore()
type args struct {
uuid uuid.UUID
}
tests := []struct {
name string
args args
want bool
}{
{name: "default", args: args{
uuid: did,
}, want: true},
{name: "fails", args: args{
uuid: uuid.New(),
{name: "fails empty", args: args{
uuid: uuid.New(),
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pnd := newPnd()
if tt.name != "fails empty" {
if err := pnd.devices.Add(tt.args.device, "test"); err != nil {
if got := pnd.ContainsDevice(tt.args.uuid); got != tt.want {
t.Errorf("ContainsDevice() = %v, want %v", got, tt.want)
}
if err := pnd.devices.Delete(did); err != nil && tt.name != "fails empty" {
})
}
}
func Test_pndImplementation_Destroy(t *testing.T) {
type fields struct {
name string
description string
sbi *store.SbiStore
devices *store.DeviceStore
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{name: "dummy", fields: fields{}, wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pnd := &pndImplementation{
Name: tt.fields.name,
Description: tt.fields.description,
devices: tt.fields.devices,
}
if err := pnd.Destroy(); (err != nil) != tt.wantErr {
t.Errorf("Destroy() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func Test_pndImplementation_GetDescription(t *testing.T) {
tests := []struct {
name string
want string
}{
{name: "default", want: "default test pnd"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := pnd.GetDescription(); got != tt.want {
t.Errorf("GetDescription() = %v, want %v", got, tt.want)
}
})
}
}
func Test_pndImplementation_GetName(t *testing.T) {
tests := []struct {
name string
want string
}{
{name: "default", want: "default"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := pnd.GetName(); got != tt.want {
t.Errorf("GetName() = %v, want %v", got, tt.want)
}
})
}
}
func Test_pndImplementation_GetSBIs(t *testing.T) {
tests := []struct {
name string
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := pnd.GetSBIs(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetSBIs() = %v, want %v", got, tt.want)
}
})
}
}
func Test_pndImplementation_MarshalDevice(t *testing.T) {
removeExistingPNDStore()
type args struct {
uuid uuid.UUID
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "default",
args: args{did},
want: "{\n\t\"Acl\": null,\n\t\"Bgp\": null,\n\t\"Components\": null,\n\t\"Interfaces\": null,\n\t\"LocalRoutes\": null,\n\t\"Messages\": null,\n\t\"NetworkInstances\": null,\n\t\"RoutingPolicy\": null,\n\t\"System\": null\n}",
wantErr: false,
},
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := pnd.addDevice(d); err != nil {
t.Error(err)
}
got, err := pnd.MarshalDevice(tt.args.uuid.String())
if (err != nil) != tt.wantErr {
t.Errorf("MarshalDevice() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("MarshalDevice() got = %v, want %v", got, tt.want)
}
if err := pnd.devices.Delete(did); err != nil {
})
}
}
func Test_pndImplementation_RemoveDevice(t *testing.T) {
removeExistingPNDStore()
type args struct {
uuid uuid.UUID
}
tests := []struct {
name string
args args
wantErr bool
}{
{name: "default", args: args{uuid: did}, wantErr: false},
{name: "fails", args: args{uuid: uuid.New()}, wantErr: true},
{name: "fails empty", args: args{uuid: did}, wantErr: true},
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pnd := newPnd()
if tt.name != "fails empty" {
sbi, err := NewSBI(spb.Type_TYPE_OPENCONFIG)
if err != nil {
t.Error(err)
return
}
if err := pnd.addDevice(d); err != nil {
t.Error(err)
}
}
if err := pnd.RemoveDevice(tt.args.uuid); (err != nil) != tt.wantErr {
t.Errorf("RemoveDevice() error = %v, wantErr %v", err, tt.wantErr)
}
if pnd.devices.Exists(did) && tt.name == "default" {
t.Errorf("RemoveDevice() device still in device store %v", pnd.devices)
}
})
}
}
func Test_pndImplementation_RemoveSbi(t *testing.T) {
opts := &tpb.TransportOption{
TransportOption: &tpb.TransportOption_GnmiTransportOption{
GnmiTransportOption: &tpb.GnmiTransportOption{},
},
}
}
tests := []struct {
name string
args args
wantErr bool
}{
{name: "default", args: args{id: defaultSbiID}, wantErr: false},
{name: "fails", args: args{id: uuid.New()}, wantErr: true},
{name: "fails empty", args: args{id: defaultSbiID}, wantErr: true},
{name: "exclusively remove associated devices", args: args{id: defaultSbiID}, wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Name: "test-remove-sbi",
Description: "test-remove-sbi",
sbic: store.NewSbiStore(defaultPndID),
devices: store.NewDeviceStore(defaultPndID),
Id: defaultPndID,
if tt.name != "fails empty" && tt.name != "fails" {
if err := pnd.addSbi(&OpenConfig{id: defaultSbiID}); err != nil {
if err := pnd.AddDevice("associatedDevice", opts, tt.args.id); err != nil {
t.Error(err)
}
if err := pnd.AddDevice("associatedDevice2", opts, tt.args.id); err != nil {
t.Error(err)
}
if tt.name == "exclusively remove associated devices" {
newID := uuid.New()
if err := pnd.addSbi(&OpenConfig{id: newID}); err != nil {
t.Error(err)
}
if err := pnd.AddDevice("associatedDevice2", opts, newID); err != nil {
t.Error(err)
}
}
if err := pnd.RemoveSbi(tt.args.id); (err != nil) != tt.wantErr {
t.Errorf("RemoveSbi() error = %v, wantErr %v", err, tt.wantErr)
}
t.Errorf("RemoveSbi() SBI still in SBI store %v", pnd.sbic)
}
if tt.name == "exclusively remove associated devices" {
if len(pnd.devices.Store) != 1 {
t.Errorf("RemoveSbi() non associated devices should remain in the storage %v", pnd.devices)
}
} else {
if len(pnd.devices.Store) != 0 {
t.Errorf("RemoveSbi() associated devices have not been removed correctly %v", len(pnd.devices.Store))
}
}
})
}
}
func Test_pndImplementation_Request(t *testing.T) {
removeExistingPNDStore()
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
type args struct {
uuid uuid.UUID
path string
rErr error
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "default",
args: args{
uuid: mdid,
path: "",
rErr: nil,
},
wantErr: false,
},
{
name: "error",
args: args{
uuid: did,
path: "",
rErr: errors.New("deliberate test fail"),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
deviceWithMockTransport := mockDevice()
tr := deviceWithMockTransport.Transport().(*mocks.Transport)
tr.On("Get", mockContext, mock.Anything).Return(&gpb.GetResponse{}, tt.args.rErr)
tr.On("ProcessResponse", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.rErr)
_, err := pnd.Request(tt.args.uuid, tt.args.path)
if (err != nil) != tt.wantErr {
t.Errorf("Request() error = %v, wantErr %v", err, tt.wantErr)
}
if err := pnd.devices.Delete(mdid); err != nil {
})
}
}
func Test_pndImplementation_RequestAll(t *testing.T) {
removeExistingPNDStore()
type args struct {
uuid uuid.UUID
path string
rErr error
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "default",
args: args{
uuid: mdid,
path: "",
rErr: nil,
},
wantErr: false,
},
{
name: "error",
args: args{
path: "",
rErr: errors.New("deliberate test fail"),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
deviceWithMockTransport := mockDevice()
tr := deviceWithMockTransport.Transport().(*mocks.Transport)
tr.On("Get", mockContext, mock.Anything).Return(&gpb.GetResponse{}, tt.args.rErr)
tr.On("ProcessResponse", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.rErr)
if err := pnd.RequestAll(tt.args.path); (err != nil) != tt.wantErr {
t.Errorf("RequestAll() error = %v, wantErr %v", err, tt.wantErr)
}
if err := pnd.devices.Delete(mdid); err != nil {
func Test_pndImplementation_ChangeOND(t *testing.T) {
opts := &tpb.TransportOption{
TransportOption: &tpb.TransportOption_GnmiTransportOption{
GnmiTransportOption: &tpb.GnmiTransportOption{},
},
path string
value []string
}
tests := []struct {
name string
args args
wantErr bool
}{
path: "/system/config/hostname",
value: []string{"ceos3000"},
},
wantErr: false,
},
{
operation: ppb.ApiOperation_API_OPERATION_REPLACE,
path: "/system/config/hostname",
value: []string{"ceos3000"},
},
wantErr: false,
},
{
path: "/system/config/hostname",
},
wantErr: false,
},
{
path: "/system/config/hostname",
value: []string{"ceos3000"},
},
wantErr: false,
},
// Negative test cases
{
},
wantErr: true,
},
{
path: "/system/config/hostname",
value: []string{"ceos3000", "ceos3001"},
},
wantErr: true,
},
path: "/system/config/hostname",
},
wantErr: true,
},
{
path: "/system/config/hostname",
},
wantErr: true,
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pnd := newPnd()
if err := pnd.addSbi(&OpenConfig{id: defaultSbiID}); err != nil {
t.Error(err)
}
if err := pnd.AddDevice("testdevice", opts, defaultSbiID); err != nil {
t.Error(err)
return
}
did, ok := pnd.devices.DeviceNameToUUIDLookup["testdevice"]
if !ok {
err := errors.New("error fetching device")
t.Error(err)
return
}
_, err := pnd.ChangeOND(did, tt.args.operation, tt.args.path, tt.args.value...)
if (err != nil) != tt.wantErr {
t.Errorf("ChangeOND() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
if len(pnd.changes.Store) != 1 {
t.Errorf("ChangeOND() unexpected change count. got %v, want 1", len(pnd.changes.Store))
func Test_pndImplementation_GetDevice(t *testing.T) {
sbi, err := NewSBI(spb.Type_TYPE_OPENCONFIG)
if err != nil {
t.Errorf("NewSBI() error = %v", err)
return
Martin Stiemerling
committed
d, err := NewDevice("", uuid.Nil, newGnmiTransportOptions(), sbi)
if err != nil {
t.Error(err)
return
}
if err = pnd.addDevice(d); err != nil {
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
t.Error(err)
return
}
type args struct {
uuid uuid.UUID
}
tests := []struct {
name string
args args
want ygot.GoStruct
wantErr bool
}{
{
name: "default",
args: args{uuid: d.ID()},
want: sbi.Schema().Root,
wantErr: false,
},
{
name: "device not found",
args: args{uuid: mdid},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
foundDevice, err := pnd.GetDevice(tt.args.uuid.String())
if (err != nil) != tt.wantErr {
t.Errorf("GetDevice() error = %v, wantErr %v", err, tt.wantErr)
return
}
if foundDevice != nil {
if !reflect.DeepEqual(foundDevice.(device.Device).Model(), tt.want) {
t.Errorf("GetDevice() got = %v, want %v", foundDevice.(device.Device).Model(), tt.want)
})
}
}
func Test_pndImplementation_GetDeviceByName(t *testing.T) {
p := newPnd()
sbi, err := NewSBI(spb.Type_TYPE_OPENCONFIG)
if err != nil {
t.Errorf("NewSBI() error = %v", err)
return
Martin Stiemerling
committed
d, err := NewDevice("my-device", uuid.Nil, newGnmiTransportOptions(), sbi)
if err != nil {
t.Error(err)
return
}
if err = p.addDevice(d); err != nil {
t.Error(err)
return
}
type args struct {
name string
}
tests := []struct {
name string
args args
want ygot.GoStruct
wantErr bool
}{
{
name: "default",
want: sbi.Schema().Root,
wantErr: false,
},
{
name: "device not found",
args: args{name: "test-device"},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
foundDevice, err := p.GetDevice(tt.args.name)
if (err != nil) != tt.wantErr {
t.Errorf("GetDeviceByName() error = %v, wantErr %v", err, tt.wantErr)
return
}
if foundDevice != nil {
if !reflect.DeepEqual(foundDevice.(device.Device).Model(), tt.want) {
t.Errorf("GetDeviceByName() got = %v, want %v", foundDevice.(device.Device).Model(), tt.want)
})
}
}
func Test_pndImplementation_Confirm(t *testing.T) {
removeExistingPNDStore()
tests := []struct {
name string
wantErr bool
}{
{
name: "default",
wantErr: false,
},
{
name: "uncommitted",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pnd := newPnd()
d := mockDevice()
tr.On("Set", mockContext, mock.Anything, mock.Anything).Return(nil)
_, err := pnd.ChangeOND(d.ID(), ppb.ApiOperation_API_OPERATION_UPDATE, "system/config/hostname", "ceos3000")
if tt.name != "uncommitted" {
if err := pnd.Commit(u); (err != nil) != tt.wantErr {
t.Errorf("Confirm() error = %v, wantErr %v", err, tt.wantErr)
return
}
}
if err := pnd.Confirm(u); (err != nil) != tt.wantErr {
t.Errorf("Confirm() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func Test_pndImplementation_PendingChanges(t *testing.T) {
testName := t.Name()
callback := func(first ygot.GoStruct, second ygot.GoStruct) error {
log.Infof("callback in test %v", testName)
return nil
}
pending := NewChange(did, &openconfig.Device{}, &openconfig.Device{}, callback)
if err := store.Add(pending); err != nil {
t.Error(err)
return
}
tests := []struct {
name string
want []uuid.UUID
}{
{
name: "default",
want: []uuid.UUID{pending.cuid},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pnd := newPnd()
if got := pnd.PendingChanges(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("pndImplementation.PendingChanges() = %v, want %v", got, tt.want)
}
})
}
}
func Test_pndImplementation_CommittedChanges(t *testing.T) {
testName := t.Name()
callback := func(first ygot.GoStruct, second ygot.GoStruct) error {
log.Infof("callback in test %v", testName)
return nil
}
committed := NewChange(did, &openconfig.Device{}, &openconfig.Device{}, callback)
if err := committed.Commit(); err != nil {
t.Error(err)
return
}
if err := store.Add(committed); err != nil {
t.Error(err)
return
}
tests := []struct {
name string
want []uuid.UUID
}{
{
name: "default",
want: []uuid.UUID{committed.cuid},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pnd := newPnd()
if got := pnd.CommittedChanges(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("pndImplementation.CommittedChanges() = %v, want %v", got, tt.want)
}
})
}
}
func Test_pndImplementation_ConfirmedChanges(t *testing.T) {
testName := t.Name()
callback := func(first ygot.GoStruct, second ygot.GoStruct) error {
log.Infof("callback in test %v", testName)
return nil
}
confirmed := NewChange(did, &openconfig.Device{}, &openconfig.Device{}, callback)
if err := confirmed.Commit(); err != nil {
t.Error(err)
return
}
if err := confirmed.Confirm(); err != nil {
t.Error(err)
return
}
if err := store.Add(confirmed); err != nil {
t.Error(err)
return
}
tests := []struct {
name string
want []uuid.UUID
}{
{
name: "default",
want: []uuid.UUID{confirmed.cuid},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pnd := newPnd()
if got := pnd.ConfirmedChanges(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("pndImplementation.ConfirmedChanges() = %v, want %v", got, tt.want)
}
})
}
}
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
func Test_pndImplementation_LoadStoredDevices(t *testing.T) {
type args struct {
device interface{}
name string
opts *tpb.TransportOption
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "default",
args: args{
name: "fridolin",
opts: &tpb.TransportOption{
TransportOption: &tpb.TransportOption_GnmiTransportOption{
GnmiTransportOption: &tpb.GnmiTransportOption{},
},
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pnd := newPnd()
if err := pnd.addSbi(&OpenConfig{id: defaultSbiID}); err != nil {
t.Error(err)
}
if tt.name == "already exists" {
pnd.devices.Store[did] = tt.args.device.(device.Device)
}
err := pnd.AddDevice(tt.args.name, tt.args.opts, defaultSbiID)
if (err != nil) != tt.wantErr {
t.Errorf("AddDevice() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pnd := newPnd()
if err := pnd.addSbi(&OpenConfig{id: defaultSbiID}); err != nil {
t.Error(err)
}
err := pnd.loadStoredDevices()
if err != nil {
t.Error(err)
}
dev, err := pnd.GetDevice(tt.args.name)
if err != nil {
t.Errorf("GetDevice() error = %v, want no err", err)
}
if dev.Name() != tt.args.name {
t.Errorf("Device name is = %s, want %s", dev.Name(), tt.args.name)
}
})
}
}
// TODO(mbauch): This test case looks unfinished. For example there are no
// cases for 'already exists' and 'fails wrong type'.
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
func Test_pndImplementation_AddDeviceWithUUID(t *testing.T) {
type args struct {
uuid uuid.UUID
device interface{}
name string
opts *tpb.TransportOption
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "default",
args: args{
uuid: did,
name: "fridolin",
opts: &tpb.TransportOption{
TransportOption: &tpb.TransportOption_GnmiTransportOption{
GnmiTransportOption: &tpb.GnmiTransportOption{},
},
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pnd := newPnd()
if err := pnd.addSbi(&OpenConfig{id: defaultSbiID}); err != nil {
t.Error(err)
}
if tt.name == "already exists" {
pnd.devices.Store[did] = tt.args.device.(device.Device)
}
err := pnd.AddDeviceFromStore(tt.args.name, tt.args.uuid, tt.args.opts, defaultSbiID)
if (err != nil) != tt.wantErr {
t.Errorf("AddDevice() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.name != "fails wrong type" {
if err == nil {
d, err := pnd.devices.GetDevice(store.FromString(tt.args.name))
if err != nil {
t.Errorf("AddDevice() error = %v", err)
return
}
if d.Name() != tt.args.name {
t.Errorf("AddDevice() got = %v, want %v", d.Name(), tt.args.name)
}
if d.ID() != tt.args.uuid {
t.Errorf("AddDevice() got = %v, want %v", d.ID(), tt.args.uuid)
}
if err := pnd.devices.Delete(d.ID()); err != nil {
t.Error(err)
}
}
}
})
}
}
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
func Test_pndImplementation_saveGoStructsToFile(t *testing.T) {
type genericGoStructClientArg struct {
fn string
rtrn []interface{}
times int
}
// Create a new mock for GenericGoStructClient. With
// genericGoStructClientArg it is possible to set the Return values of the
// mocks methods.
newGenericGoStructClient := func(args ...genericGoStructClientArg) *mocks.GenericGoStructClient {
ggsc := &mocks.GenericGoStructClient{}
for _, arg := range args {
ggsc.On(arg.fn).Return(arg.rtrn...).Times(arg.times)
}
ggsc.On("CloseSend").Return(nil)
return ggsc
}
type args struct {
id uuid.UUID
client GenericGrpcClient
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "default",
args: args{
id: uuid.New(),
client: newGenericGoStructClient(
[]genericGoStructClientArg{
{
fn: "Recv",
rtrn: []interface{}{
&csbi.Payload{Chunk: []byte("test")},
nil,
},
times: 3,
},
{
fn: "Recv",
rtrn: []interface{}{
&csbi.Payload{Chunk: nil},
io.EOF,
},
times: 1,
},
}...),
},
wantErr: false,
},
{
name: "unexpected EOF error",
args: args{
id: uuid.New(),
client: newGenericGoStructClient(
[]genericGoStructClientArg{
{
fn: "Recv",
rtrn: []interface{}{
&csbi.Payload{Chunk: nil},
io.ErrUnexpectedEOF,
},
times: 1,
},
{
fn: "CloseSend",
rtrn: []interface{}{
nil,
},
times: 1,
},
}...),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := saveGenericClientStreamToFile(tt.args.client, "gostructs.go", tt.args.id)
if (err != nil) != tt.wantErr {
t.Errorf("saveGoStructsToFile() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}