Skip to content

Commit

Permalink
Pluralize also key for all rx-modes of an interface
Browse files Browse the repository at this point in the history
Signed-off-by: Milan Lenco <[email protected]>
  • Loading branch information
Milan Lenco committed May 17, 2019
1 parent 36a4186 commit d06757f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 52 deletions.
21 changes: 10 additions & 11 deletions api/models/vpp/interfaces/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ const (
rxPlacementKeyTemplate = "vpp/interface/{iface}/rx-placement/queue/{queue}"
)

/* Interface Rx-mode (derived) */
/* Interface Rx-modes (derived) */
const (
// rxModeKeyTemplate is a template for (derived) key representing
// rx-mode configuration for all queues of a given interface.
rxModeKeyTemplate = "vpp/interface/{iface}/rx-mode"
rxModesKeyTemplate = "vpp/interface/{iface}/rx-modes"
)

const (
Expand Down Expand Up @@ -527,24 +527,23 @@ func ParseRxPlacementKey(key string) (ifaceName string, queue uint32, isRxPlacem
return
}

/* Rx mode (derived) */
/* Rx modes (derived) */

// RxModeKey returns a key representing rx-mode configuration for all queues
// RxModesKey returns a key representing rx-mode configuration for all queues
// of a given interface.
func RxModeKey(ifaceName string) string {
func RxModesKey(ifaceName string) string {
if ifaceName == "" {
ifaceName = InvalidKeyPart
}
return strings.Replace(rxModeKeyTemplate, "{iface}", ifaceName, 1)
return strings.Replace(rxModesKeyTemplate, "{iface}", ifaceName, 1)
}

// ParseRxModeKey parses key representing rx-mode configuration for all queues
// ParseRxModesKey parses key representing rx-mode configuration for all queues
// of a given interface.
func ParseRxModeKey(key string) (ifaceName string, isRxModeKey bool) {
func ParseRxModesKey(key string) (ifaceName string, isRxModesKey bool) {
if suffix := strings.TrimPrefix(key, "vpp/interface/"); suffix != key {
parts := strings.Split(suffix, "/")
if len(parts) == 0 || parts[len(parts)-1] != "rx-mode" {
isRxModeKey = false
if len(parts) == 0 || parts[len(parts)-1] != "rx-modes" {
return
}

Expand All @@ -555,7 +554,7 @@ func ParseRxModeKey(key string) (ifaceName string, isRxModeKey bool) {
return
}

isRxModeKey = true
isRxModesKey = true
}
return
}
Expand Down
76 changes: 37 additions & 39 deletions api/models/vpp/interfaces/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -889,11 +889,11 @@ func TestLinkStateKey(t *testing.T) {

func TestParseLinkStateKey(t *testing.T) {
tests := []struct {
name string
key string
expectedIface string
expectedIsLinkUp bool
expectedIsLinkStateKey bool
name string
key string
expectedIface string
expectedIsLinkUp bool
expectedIsLinkStateKey bool
}{
{
name: "link is UP",
Expand Down Expand Up @@ -936,7 +936,6 @@ func TestParseLinkStateKey(t *testing.T) {
key: "vpp/interface/unnumbered/GigabitEthernet0/8/0",
expectedIsLinkStateKey: false,
},

}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down Expand Up @@ -1032,7 +1031,7 @@ func TestParseRxPlacementKey(t *testing.T) {
expectedIsRxPlacementKey: false,
},
{
name: "missing queue",
name: "missing queue",
key: "vpp/interface/memif0/rx-placement/",
expectedIsRxPlacementKey: false,
},
Expand All @@ -1053,7 +1052,6 @@ func TestParseRxPlacementKey(t *testing.T) {
key: "vpp/interface/memif0/link-state/DOWN",
expectedIsRxPlacementKey: false,
},

}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand All @@ -1072,7 +1070,7 @@ func TestParseRxPlacementKey(t *testing.T) {
}
}

func TestRxModeKey(t *testing.T) {
func TestRxModesKey(t *testing.T) {
tests := []struct {
name string
iface string
Expand All @@ -1081,22 +1079,22 @@ func TestRxModeKey(t *testing.T) {
{
name: "memif",
iface: "memif0",
expectedKey: "vpp/interface/memif0/rx-mode",
expectedKey: "vpp/interface/memif0/rx-modes",
},
{
name: "Gbe",
iface: "GigabitEthernet0/8/0",
expectedKey: "vpp/interface/GigabitEthernet0/8/0/rx-mode",
expectedKey: "vpp/interface/GigabitEthernet0/8/0/rx-modes",
},
{
name: "invalid interface name",
iface: "",
expectedKey: "vpp/interface/<invalid>/rx-mode",
expectedKey: "vpp/interface/<invalid>/rx-modes",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
key := RxModeKey(test.iface)
key := RxModesKey(test.iface)
if key != test.expectedKey {
t.Errorf("failed for: iface=%s\n"+
"expected key:\n\t%q\ngot key:\n\t%q",
Expand All @@ -1106,47 +1104,47 @@ func TestRxModeKey(t *testing.T) {
}
}

func TestParseRxModeKey(t *testing.T) {
func TestParseRxModesKey(t *testing.T) {
tests := []struct {
name string
key string
expectedIface string
expectedIsRxModeKey bool
name string
key string
expectedIface string
expectedIsRxModesKey bool
}{
{
name: "memif",
key: "vpp/interface/memif0/rx-mode",
expectedIface: "memif0",
expectedIsRxModeKey: true,
name: "memif",
key: "vpp/interface/memif0/rx-modes",
expectedIface: "memif0",
expectedIsRxModesKey: true,
},
{
name: "Gbe",
key: "vpp/interface/GigabitEthernet0/8/0/rx-mode",
expectedIface: "GigabitEthernet0/8/0",
expectedIsRxModeKey: true,
name: "Gbe",
key: "vpp/interface/GigabitEthernet0/8/0/rx-modes",
expectedIface: "GigabitEthernet0/8/0",
expectedIsRxModesKey: true,
},
{
name: "invalid interface name",
key: "vpp/interface/<invalid>/rx-mode",
expectedIsRxModeKey: false,
name: "invalid interface name",
key: "vpp/interface/<invalid>/rx-modes",
expectedIsRxModesKey: false,
},
{
name: "missing rx-mode suffix",
key: "vpp/interface/<invalid>",
expectedIsRxModeKey: false,
name: "missing rx-mode suffix",
key: "vpp/interface/<invalid>",
expectedIsRxModesKey: false,
},
{
name: "not rx-mode key",
key: "vpp/interface/memif0/address/192.168.1.12/24",
expectedIsRxModeKey: false,
name: "not rx-mode key",
key: "vpp/interface/memif0/address/192.168.1.12/24",
expectedIsRxModesKey: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
iface, isRxModeKey := ParseRxModeKey(test.key)
if isRxModeKey != test.expectedIsRxModeKey {
t.Errorf("expected isRxModeKey: %v\tgot: %v",
test.expectedIsRxModeKey, isRxModeKey)
iface, isRxModesKey := ParseRxModesKey(test.key)
if isRxModesKey != test.expectedIsRxModesKey {
t.Errorf("expected isRxModesKey: %v\tgot: %v",
test.expectedIsRxModesKey, isRxModesKey)
}
if iface != test.expectedIface {
t.Errorf("expected iface: %s\tgot: %s", test.expectedIface, iface)
Expand Down
2 changes: 1 addition & 1 deletion plugins/vpp/ifplugin/descriptor/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ func (d *InterfaceDescriptor) DerivedValues(key string, intf *interfaces.Interfa
// Rx mode
if len(intf.GetRxModes()) > 0 {
derValues = append(derValues, kvs.KeyValuePair{
Key: interfaces.RxModeKey(intf.GetName()),
Key: interfaces.RxModesKey(intf.GetName()),
Value: &interfaces.Interface{
Name: intf.GetName(),
Type: intf.GetType(),
Expand Down
2 changes: 1 addition & 1 deletion plugins/vpp/ifplugin/descriptor/rx_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func NewRxModeDescriptor(ifHandler vppcalls.InterfaceVppAPI, ifIndex ifaceidx.If

// IsInterfaceRxModeKey returns true if the key is identifying RxMode configuration.
func (d *RxModeDescriptor) IsInterfaceRxModeKey(key string) bool {
_, isValid := interfaces.ParseRxModeKey(key)
_, isValid := interfaces.ParseRxModesKey(key)
return isValid
}

Expand Down

0 comments on commit d06757f

Please sign in to comment.