Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

guests/linux: Update network interface sorting implementation #7866

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions plugins/guests/linux/cap/network_interfaces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,10 @@ def self.network_interfaces(machine, path = "/sbin/ip")
s << data if type == :stdout
end
ifaces = s.split("\n")
eth_prefix = nil
@@logger.debug("Unsorted list: #{ifaces.inspect}")
# Break out integers from strings and sort the arrays to provide
# a natural sort for the interface names
ifaces = ifaces.map do |iface|
if eth_prefix.nil?
eth_prefix = POSSIBLE_ETHERNET_PREFIXES.detect do |prefix|
iface.start_with?(prefix)
end
end
iface.scan(/(.+?)(\d+)/).flatten.map do |iface_part|
if iface_part.to_i.to_s == iface_part
iface_part.to_i
Expand All @@ -40,12 +34,14 @@ def self.network_interfaces(machine, path = "/sbin/ip")
end.sort.map(&:join)
@@logger.debug("Sorted list: #{ifaces.inspect}")
# Extract ethernet devices and place at start of list
if eth_prefix
eth_start = ifaces.index{|iface| iface.start_with?(eth_prefix) }
eth_end = ifaces.rindex{|iface| iface.start_with?(eth_prefix) }
ifaces.unshift(*ifaces.slice!(eth_start, eth_end - 1))
@@logger.debug("Ethernet preferred sorted list: #{ifaces.inspect}")
resorted_ifaces = []
resorted_ifaces += ifaces.find_all do |iface|
POSSIBLE_ETHERNET_PREFIXES.any?{|prefix| iface.start_with?(prefix)} &&
!iface.include?(':')
end
resorted_ifaces += ifaces - resorted_ifaces
ifaces = resorted_ifaces
@@logger.debug("Ethernet preferred sorted list: #{ifaces.inspect}")
ifaces
end
end
Expand Down
12 changes: 12 additions & 0 deletions test/unit/plugins/guests/linux/cap/network_interfaces_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,17 @@
result = cap.network_interfaces(machine)
expect(result).to eq(["enp0s3", "enp0s5", "enp0s8", "bridge0", "docker0"])
end

it "sorts ethernet devices discovered with predictable network interfaces naming first in list with less" do
expect(comm).to receive(:sudo).and_yield(:stdout, "enp0s3\nenp0s8\ndocker0")
result = cap.network_interfaces(machine)
expect(result).to eq(["enp0s3", "enp0s8", "docker0"])
end

it "does not include ethernet devices aliases within prefix device listing" do
expect(comm).to receive(:sudo).and_yield(:stdout, "eth1\neth2\ndocker0\nbridge0\neth0\neth0:0")
result = cap.network_interfaces(machine)
expect(result).to eq(["eth0", "eth1", "eth2", "bridge0", "docker0", "eth0:0"])
end
end
end