Newer
Older
import (
"code.fbi.h-da.de/cocsn/gosdn/mocks"
"code.fbi.h-da.de/cocsn/yang-models/generated/openconfig"
"errors"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/mock"
"reflect"
"testing"
)
func testSetupPnd() {
var err error
did, err = uuid.Parse("4d8246f8-e884-41d6-87f5-c2c784df9e44")
if err != nil {
log.Fatal(err)
}
mdid, err = uuid.Parse("688a264e-5f85-40f8-bd13-afc42fcd5c7a")
if err != nil {
log.Fatal(err)
}
func mockDevice() Device {
return Device{
GoStruct: nil,
SBI: &OpenConfig{},
Config: DeviceConfig{
Uuid: mdid,
Address: "mock://localhost",
Username: "mock",
Password: "mock",
},
Transport: &mocks.Transport{},
}
}
func freshPnd() pndImplementation {
return pndImplementation{
name: "default",
description: "default test pnd",
sbi: map[string]SouthboundInterface{"default": &OpenConfig{}},
devices: map[uuid.UUID]*Device{},
}
}
var did uuid.UUID
var mdid uuid.UUID
func TestNewPND(t *testing.T) {
type args struct {
name string
description string
sbi SouthboundInterface
}
tests := []struct {
name string
args args
want PrincipalNetworkDomain
}{
{
name: "default",
args: args{
name: "default",
description: "default test pnd",
sbi: &OpenConfig{},
},
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewPND(tt.args.name, tt.args.description, tt.args.sbi); !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 {
device *Device
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "default",
args: args{device: &Device{
Config: DeviceConfig{
Uuid: did,
},
}},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pnd := freshPnd()
if err := pnd.AddDevice(tt.args.device); (err != nil) != tt.wantErr {
t.Errorf("AddDevice() error = %v, wantErr %v", err, tt.wantErr)
}
if !ok {
t.Errorf("AddDevice() Device %v not in device store %v",
})
}
}
func Test_pndImplementation_AddSbi(t *testing.T) {
type args struct {
sbi SouthboundInterface
}
tests := []struct {
name string
args args
wantErr bool
}{
{name: "default", args: args{sbi: &OpenConfig{}}, wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pnd := freshPnd()
if err := pnd.AddSbi(tt.args.sbi); (err != nil) != tt.wantErr {
t.Errorf("AddSbi() error = %v, wantErr %v", err, tt.wantErr)
}
_, ok := pnd.GetSBIs()[tt.args.sbi.SbiIdentifier()]
if !ok {
t.Errorf("AddSbi() SBI %v not in device store %v",
delete(pnd.sbi, tt.args.sbi.SbiIdentifier())
})
}
}
func Test_pndImplementation_ContainsDevice(t *testing.T) {
type args struct {
uuid uuid.UUID
device *Device
}
tests := []struct {
name string
args args
want bool
}{
{name: "default", args: args{
uuid: did,
device: &Device{Config: DeviceConfig{Uuid: did}},
}, want: true},
{name: "fails", args: args{
uuid: uuid.New(),
device: &Device{Config: DeviceConfig{Uuid: did}},
}, want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pnd := freshPnd()
pnd.devices[did] = tt.args.device
if got := pnd.ContainsDevice(tt.args.uuid); got != tt.want {
t.Errorf("ContainsDevice() = %v, want %v", got, tt.want)
}
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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
})
}
}
func Test_pndImplementation_Destroy(t *testing.T) {
type fields struct {
name string
description string
sbi map[string]SouthboundInterface
devices map[uuid.UUID]*Device
}
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,
sbi: tt.fields.sbi,
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) {
pnd := freshPnd()
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) {
pnd := freshPnd()
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
want map[string]SouthboundInterface
}{
}
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) {
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) {
d := &Device{
GoStruct: &openconfig.Device{},
SBI: nil,
Config: DeviceConfig{
Uuid: tt.args.uuid,
Address: "localhost",
Username: "test",
Password: "test",
},
Transport: nil,
}
_ = pnd.addDevice(d)
got, err := pnd.MarshalDevice(tt.args.uuid)
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)
}
})
}
}
func Test_pndImplementation_RemoveDevice(t *testing.T) {
type args struct {
uuid uuid.UUID
}
tests := []struct {
name string
args args
wantErr bool
}{
{name: "default", args: args{uuid: did}, wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &Device{Config: DeviceConfig{Uuid: did}}
_ = pnd.addDevice(d)
if err := pnd.RemoveDevice(tt.args.uuid); (err != nil) != tt.wantErr {
t.Errorf("RemoveDevice() error = %v, wantErr %v", err, tt.wantErr)
}
t.Errorf("RemoveDevice() device %v still in device store %v", d, pnd.devices)
}
})
}
}
func Test_pndImplementation_RemoveSbi(t *testing.T) {
type args struct {
sbiIdentifier string
}
tests := []struct {
name string
args args
wantErr bool
}{
{name: "default", args: args{sbiIdentifier: "openconfig"}, wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pnd := freshPnd()
_ = pnd.addSbi(&OpenConfig{})
if err := pnd.RemoveSbi(tt.args.sbiIdentifier); (err != nil) != tt.wantErr {
t.Errorf("RemoveSbi() error = %v, wantErr %v", err, tt.wantErr)
}
sbi, ok := pnd.sbi[tt.args.sbiIdentifier]
t.Errorf("RemoveDevice() SBI %v still in SBI store %v", sbi, pnd.sbi)
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
}
})
}
}
func Test_pndImplementation_Request(t *testing.T) {
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()
pnd := freshPnd()
tr := deviceWithMockTransport.Transport.(*mocks.Transport)
tr.On("Get", mockContext, mock.Anything).Return(mock.Anything, tt.args.rErr)
tr.On("ProcessResponse", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.rErr)
_ = pnd.addDevice(&deviceWithMockTransport)
if err := pnd.Request(tt.args.uuid, tt.args.path); (err != nil) != tt.wantErr {
t.Errorf("Request() error = %v, wantErr %v", err, tt.wantErr)
}
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
})
}
}
func Test_pndImplementation_RequestAll(t *testing.T) {
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()
pnd := freshPnd()
tr := deviceWithMockTransport.Transport.(*mocks.Transport)
tr.On("Get", mockContext, mock.Anything).Return(mock.Anything, tt.args.rErr)
tr.On("ProcessResponse", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.rErr)
_ = pnd.addDevice(&deviceWithMockTransport)
if err := pnd.RequestAll(tt.args.path); (err != nil) != tt.wantErr {
t.Errorf("RequestAll() error = %v, wantErr %v", err, tt.wantErr)
}