diff options
author | Zeling Feng <zeling@google.com> | 2020-11-09 13:14:23 -0800 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2020-11-09 13:16:33 -0800 |
commit | c59bdd18d521ebb3c8dedf03a6adfa56dd6245c7 (patch) | |
tree | e060556c4fc7506a279409956fd92e3b4e09f9ed | |
parent | cbca5b2edde0920309029a0c591f28ce65cdad70 (diff) |
parameterize regexp in netdevs.ParseDevices
PiperOrigin-RevId: 341470647
-rw-r--r-- | test/packetimpact/netdevs/netdevs.go | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/test/packetimpact/netdevs/netdevs.go b/test/packetimpact/netdevs/netdevs.go index 006988896..25dcfbf60 100644 --- a/test/packetimpact/netdevs/netdevs.go +++ b/test/packetimpact/netdevs/netdevs.go @@ -43,14 +43,10 @@ var ( inet6Line = regexp.MustCompile(`^\s*inet6 ([0-9a-fA-F:/]+)`) ) -// ParseDevices parses the output from `ip addr show` into a map from device -// name to information about the device. -// -// Note: if multiple IPv6 addresses are assigned to a device, the last address -// displayed by `ip addr show` will be used. This is fine for packetimpact -// because we will always only have at most one IPv6 address assigned to each -// device. -func ParseDevices(cmdOutput string) (map[string]DeviceInfo, error) { +// ParseDevicesWithRegex will parse the output with the given regexps to produce +// a map from device name to device information. It is assumed that deviceLine +// contains both a name and an ID. +func ParseDevicesWithRegex(cmdOutput string, deviceLine, linkLine, inetLine, inet6Line *regexp.Regexp) (map[string]DeviceInfo, error) { var currentDevice string var currentInfo DeviceInfo deviceInfos := make(map[string]DeviceInfo) @@ -93,6 +89,17 @@ func ParseDevices(cmdOutput string) (map[string]DeviceInfo, error) { return deviceInfos, nil } +// ParseDevices parses the output from `ip addr show` into a map from device +// name to information about the device. +// +// Note: if multiple IPv6 addresses are assigned to a device, the last address +// displayed by `ip addr show` will be used. This is fine for packetimpact +// because we will always only have at most one IPv6 address assigned to each +// device. +func ParseDevices(cmdOutput string) (map[string]DeviceInfo, error) { + return ParseDevicesWithRegex(cmdOutput, deviceLine, linkLine, inetLine, inet6Line) +} + // MACToIP converts the MAC address to an IPv6 link local address as described // in RFC 4291 page 20: https://tools.ietf.org/html/rfc4291#page-20 func MACToIP(mac net.HardwareAddr) net.IP { |