Skip to content

Commit a6279fe

Browse files
committed
[#1425] Avoid panic in Kea commands creation
1 parent c717b22 commit a6279fe

File tree

2 files changed

+16
-35
lines changed

2 files changed

+16
-35
lines changed

backend/appctrl/kea/subnetcmds.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package keactrl
22

33
import (
4-
"fmt"
5-
64
keaconfig "isc.org/stork/appcfg/kea"
75
)
86

@@ -71,16 +69,13 @@ func NewCommandNetwork6SubnetDel(sharedNetworkName string, localSubnetID int64,
7169
}
7270

7371
// Creates network4-subnet-del or network6-subnet-del depending on the family.
74-
// It panics if the family is neither 4 nor 6.
7572
func NewCommandNetworkSubnetDel(family int, sharedNetworkName string, localSubnetID int64, daemonNames ...DaemonName) *Command {
7673
var commandName CommandName
7774
switch family {
7875
case 4:
7976
commandName = Network4SubnetDel
80-
case 6:
81-
commandName = Network6SubnetDel
8277
default:
83-
panic(fmt.Sprintf("invalid family %d", family))
78+
commandName = Network6SubnetDel
8479
}
8580
return NewCommandBase(commandName, daemonNames...).
8681
WithArgument("id", localSubnetID).
@@ -113,10 +108,8 @@ func NewCommandSubnetDel(family int, subnet *keaconfig.SubnetCmdsDeletedSubnet,
113108
switch family {
114109
case 4:
115110
commandName = Subnet4Del
116-
case 6:
117-
commandName = Subnet6Del
118111
default:
119-
panic(fmt.Sprintf("invalid family %d", family))
112+
commandName = Subnet6Del
120113
}
121114
return NewCommandBase(commandName, daemonNames...).
122115
WithArgument("id", subnet.ID)

backend/appctrl/kea/subnetcmds_test.go

+14-26
Original file line numberDiff line numberDiff line change
@@ -156,24 +156,19 @@ func TestNewCommandNetworkSubnetDelFamily4(t *testing.T) {
156156

157157
// Tests network6-subnet-del command.
158158
func TestNewCommandNetworkSubnetDelFamily6(t *testing.T) {
159-
command := NewCommandNetworkSubnetDel(6, "foo", 123, DHCPv6)
160-
require.NotNil(t, command)
161-
require.JSONEq(t, `{
159+
// The network6-subnet-del command should be returned for different families.
160+
for family := range []int64{6, 1, 0} {
161+
command := NewCommandNetworkSubnetDel(family, "foo", 123, DHCPv6)
162+
require.NotNil(t, command)
163+
require.JSONEq(t, `{
162164
"command": "network6-subnet-del",
163165
"service": ["dhcp6"],
164166
"arguments": {
165167
"id": 123,
166168
"name": "foo"
167169
}
168170
}`, command.Marshal())
169-
}
170-
171-
// Test that the function creating network4-subnet-del or network6-subnet-del
172-
// panics when the family is invalid.
173-
func TestNewCommandNetworkSubnetDelInvalidFamily(t *testing.T) {
174-
require.Panics(t, func() {
175-
_ = NewCommandNetworkSubnetDel(0, "foo", 123, DHCPv4)
176-
})
171+
}
177172
}
178173

179174
// Tests subnet4-add command.
@@ -269,27 +264,20 @@ func TestNewCommandSubnetDelFamily4(t *testing.T) {
269264

270265
// Tests subnet6-del command.
271266
func TestNewCommandSubnetDelFamily6(t *testing.T) {
272-
command := NewCommandSubnetDel(6, &keaconfig.SubnetCmdsDeletedSubnet{
273-
ID: 4,
274-
}, "dhcp6")
275-
require.NotNil(t, command)
276-
require.JSONEq(t, `{
267+
// The subnet6-del command should be returned for different families.
268+
for family := range []int64{6, 1, 0} {
269+
command := NewCommandSubnetDel(family, &keaconfig.SubnetCmdsDeletedSubnet{
270+
ID: 4,
271+
}, "dhcp6")
272+
require.NotNil(t, command)
273+
require.JSONEq(t, `{
277274
"command": "subnet6-del",
278275
"service": ["dhcp6"],
279276
"arguments": {
280277
"id": 4
281278
}
282279
}`, command.Marshal())
283-
}
284-
285-
// Test that the function returning subnet4-del or subnet6-del command
286-
// panics when the family is invalid.
287-
func TestNewCommandSubnetDelInvalidFamily(t *testing.T) {
288-
require.Panics(t, func() {
289-
_ = NewCommandSubnetDel(123, &keaconfig.SubnetCmdsDeletedSubnet{
290-
ID: 2,
291-
}, DHCPv4)
292-
})
280+
}
293281
}
294282

295283
// Tests subnet4-update command.

0 commit comments

Comments
 (0)