Skip to content

Commit 3c8a60f

Browse files
kopcsoLevovar
kopcso
authored andcommitted
correct PopDevice (panic case)
update TestPopDevice
1 parent 2645180 commit 3c8a60f

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

Diff for: pkg/danm/danm.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,7 @@ func extractConnections(args *cniArgs) error {
188188
return nil
189189
}
190190

191-
func getAllocatedDevices(args *cniArgs, devicePool string)(*[]string, error){
192-
checkpoint, err := checkpoint_utils.GetCheckpoint()
193-
if err != nil {
194-
return nil, errors.New("failed to instantiate checkpoint object due to:" + err.Error())
195-
}
191+
func getAllocatedDevices(args *cniArgs, checkpoint checkpoint_utils.Checkpoint, devicePool string)(*[]string, error){
196192
resourceMap, err := checkpoint.GetComputeDeviceMap(string(args.podUid))
197193
if err != nil || len(resourceMap) == 0 {
198194
return nil, errors.New("failed to retrieve Pod info from checkpoint object due to:" + err.Error())
@@ -204,6 +200,7 @@ func getAllocatedDevices(args *cniArgs, devicePool string)(*[]string, error){
204200
}
205201

206202
func popDevice(devicePool string, allocatedDevices map[string]*[]string)(string, error) {
203+
if len(allocatedDevices) == 0 { return "", errors.New("allocatedDevices is empty") }
207204
devices := (*allocatedDevices[devicePool])
208205
if len(devices) == 0 { return "", errors.New("devicePool is empty") }
209206
device, devices := devices[len(devices)-1], devices[:len(devices)-1]
@@ -218,6 +215,11 @@ func setupNetworking(args *cniArgs) (*current.Result, error) {
218215
}
219216
syncher := syncher.NewSyncher(len(args.interfaces))
220217
allocatedDevices := make(map[string]*[]string)
218+
219+
checkpoint, err := checkpoint_utils.GetCheckpoint()
220+
if err != nil {
221+
return nil, errors.New("failed to instantiate checkpoint object due to:" + err.Error())
222+
}
221223
var cniRes *current.Result
222224
for nicID, nicParams := range args.interfaces {
223225
isDelegationRequired, netInfo, err := cnidel.IsDelegationRequired(danmClient, nicParams.Network, args.nameSpace)
@@ -228,7 +230,7 @@ func setupNetworking(args *cniArgs) (*current.Result, error) {
228230
if isDelegationRequired {
229231
if cnidel.IsDeviceNeeded(netInfo.Spec.NetworkType) {
230232
if _, ok := allocatedDevices[netInfo.Spec.Options.DevicePool]; !ok {
231-
allocatedDevices[netInfo.Spec.Options.DevicePool], err = getAllocatedDevices(args, netInfo.Spec.Options.DevicePool)
233+
allocatedDevices[netInfo.Spec.Options.DevicePool], err = getAllocatedDevices(args, checkpoint, netInfo.Spec.Options.DevicePool)
232234
if err != nil {
233235
return cniRes, errors.New("failed to get allocated devices due to:" + err.Error())
234236
}

Diff for: pkg/danm/danm_test.go

+9-14
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,18 @@ import (
77
var devicePool0 = "pool0"
88
var devicePool1 = "pool1"
99

10-
var allocatedDevices = make(map[string]*[]string)
10+
func TestPopDevice(t *testing.T) {
11+
allocatedDevices := make(map[string]*[]string)
1112

12-
func TestPopDevicePanic(t *testing.T) {
13-
defer func() {
14-
if r := recover(); r == nil {
15-
t.Errorf("Uninitialized map should expect error.")
16-
}
17-
}()
18-
19-
popDevice(devicePool0, allocatedDevices)
20-
}
13+
device,err := popDevice(devicePool0, allocatedDevices)
14+
if err == nil {
15+
t.Errorf("Uninitialized map should expect error.")
16+
}
2117

22-
func TestPopDevice(t *testing.T) {
2318
allocatedDevices[devicePool0] = &[]string{"device0"}
2419
allocatedDevices[devicePool1] = &[]string{"device1", "device2"}
25-
26-
device,err := popDevice(devicePool1, allocatedDevices)
20+
21+
device,err = popDevice(devicePool1, allocatedDevices)
2722
if device != "device2" || err != nil {
2823
t.Errorf("Received device or error does not match with expectation.")
2924
}
@@ -32,7 +27,7 @@ func TestPopDevice(t *testing.T) {
3227
t.Errorf("Received device or error does not match with expectation.")
3328
}
3429
device,err = popDevice(devicePool1, allocatedDevices)
35-
if device == "" && err == nil {
30+
if err == nil {
3631
t.Errorf("Empty pool should expect error.")
3732
}
3833
}

0 commit comments

Comments
 (0)