Newer
Older
package store
import (
"reflect"
"sync"
"testing"
"code.fbi.h-da.de/danet/gosdn/interfaces/device"
"code.fbi.h-da.de/danet/gosdn/interfaces/store"
"code.fbi.h-da.de/danet/gosdn/mocks"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
"github.com/google/uuid"
)
func Test_deviceStore_get(t *testing.T) {
deviceMock := &mocks.Device{}
deviceMock.On("ID").Return(did)
deviceMock.On("Name").Return("did")
pndMock := &mocks.NetworkDomain{}
pndMock.On("ID").Return(did)
type fields struct {
genericStore *genericStore
}
type args struct {
id uuid.UUID
}
tests := []struct {
name string
fields fields
args args
want device.Device
wantErr bool
}{
{
name: "exists",
fields: fields{
&genericStore{
Store: map[uuid.UUID]store.Storable{
defaultPndID: deviceMock,
},
storeLock: sync.RWMutex{},
},
},
args: args{id: defaultPndID},
want: deviceMock,
wantErr: false,
},
{
name: "fails",
fields: fields{
&genericStore{
Store: map[uuid.UUID]store.Storable{
defaultPndID: deviceMock,
},
storeLock: sync.RWMutex{},
},
},
args: args{id: iid},
wantErr: true,
},
{
name: "fails empty",
fields: fields{
genericStore: &genericStore{},
},
args: args{id: defaultPndID},
wantErr: true,
},
{
name: "fails wrong type",
fields: fields{
&genericStore{
Store: map[uuid.UUID]store.Storable{
defaultPndID: pndMock,
},
storeLock: sync.RWMutex{},
},
},
args: args{id: defaultPndID},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := DeviceStore{genericStore: tt.fields.genericStore}
got, err := s.GetDevice(FromString(tt.args.id.String()))
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)
}
})
}
}
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
231
232
233
234
235
236
237
238
239
240
241
242
func Test_deviceStore_GetDevicesAsssociatedWithSbi(t *testing.T) {
mockSBI := &mocks.SouthboundInterface{}
mockSBI.On("ID").Return(defaultSbiID)
createDeviceMock := func(name string, sid uuid.UUID) *mocks.Device {
dm := &mocks.Device{}
dm.On("ID").Return(uuid.New())
dm.On("Name").Return("did")
dm.On("SBI").Return(mockSBI)
return dm
}
associatedDevices := []device.Device{
createDeviceMock("mockDevice1", defaultSbiID),
createDeviceMock("mockDevice2", defaultSbiID),
createDeviceMock("mockDevice3", defaultSbiID),
}
nonAssociatedDevice := createDeviceMock("mockDevice4", uuid.New())
pndMock := &mocks.NetworkDomain{}
pndMock.On("ID").Return(did)
// options to apply to cmp.Equal
opts := []cmp.Option{
// create custom comparer that simply checks if the device ID's are the
// same.
cmp.Comparer(func(x, y device.Device) bool {
return x.ID() == y.ID()
}),
// compare option to treat slices of length zero as equal
cmpopts.EquateEmpty(),
// sort the slices based on the ID
cmpopts.SortSlices(func(x, y device.Device) bool {
return x.ID().ID() < y.ID().ID()
}),
}
type fields struct {
genericStore *genericStore
}
type args struct {
sid uuid.UUID
}
tests := []struct {
name string
fields fields
args args
want []device.Device
wantErr bool
}{
{
name: "return devices associated with SBI",
fields: fields{
&genericStore{
Store: map[uuid.UUID]store.Storable{
associatedDevices[0].ID(): associatedDevices[0],
associatedDevices[1].ID(): associatedDevices[1],
associatedDevices[2].ID(): associatedDevices[2],
},
storeLock: sync.RWMutex{},
},
},
args: args{
sid: defaultSbiID,
},
want: associatedDevices,
wantErr: false,
},
{
name: "non associated devices should not be part of the returned slice",
fields: fields{
&genericStore{
Store: map[uuid.UUID]store.Storable{
associatedDevices[0].ID(): associatedDevices[0],
associatedDevices[1].ID(): associatedDevices[1],
associatedDevices[2].ID(): associatedDevices[2],
nonAssociatedDevice.ID(): nonAssociatedDevice,
},
storeLock: sync.RWMutex{},
},
},
args: args{
sid: defaultSbiID,
},
want: append(associatedDevices, nonAssociatedDevice),
wantErr: false,
},
{
name: "empty",
fields: fields{
&genericStore{
Store: map[uuid.UUID]store.Storable{},
storeLock: sync.RWMutex{},
},
},
want: []device.Device{},
wantErr: false,
},
{
name: "no device associated",
fields: fields{
&genericStore{
Store: map[uuid.UUID]store.Storable{
nonAssociatedDevice.ID(): nonAssociatedDevice,
},
storeLock: sync.RWMutex{},
},
},
want: []device.Device{},
wantErr: false,
},
{
name: "fails wrong type",
fields: fields{
&genericStore{
Store: map[uuid.UUID]store.Storable{
defaultPndID: pndMock,
},
storeLock: sync.RWMutex{},
},
},
args: args{sid: defaultSbiID},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := DeviceStore{genericStore: tt.fields.genericStore}
got, err := s.GetDevicesAssociatedWithSbi(tt.args.sid)
if (err != nil) != tt.wantErr {
t.Errorf("GetDevicesAssociatedWithSbi() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !cmp.Equal(got, tt.want, opts...) {
t.Errorf("GetDevicesAssociatedWithSbi() got = %v, want %v", got, tt.want)
}
})
}
}