Skip to content

Commit

Permalink
InterfaceConfigBuilder#name= sets the interface too if it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Sep 2, 2019
1 parent f380234 commit 0bf4a53
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
44 changes: 32 additions & 12 deletions src/lib/y2network/interface_config_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ def self.for(type, config: nil)
end

# @return [String] Device name (eth0, wlan0, etc.)
attr_accessor :name
attr_reader :name
# @return [Y2Network::InterfaceType] type of @see Y2Network::Interface which is intended to be build
attr_accessor :type
# @return [Y2Network::ConnectionConfig] connection config on which builder operates
attr_reader :connection_config
# @return [Symbol,nil] Mechanism to rename the interface (no hardware based, :mac or :bus_id)
# @return [Symbol] Mechanism to rename the interface (:none -no hardware based-, :mac or :bus_id)
attr_writer :renaming_mechanism
# @return [Y2Network::Interface,nil] Underlying interface if it exists
attr_reader :interface

# Constructor
#
Expand All @@ -81,6 +83,17 @@ def initialize(type:, config: nil)
end
end

# Sets the interface name
#
# It initializes the interface using the given name if it exists
#
# @param value [String] Interface name
def name=(value)
@name = value
iface = find_interface
self.interface = iface if iface
end

def newly_added?
@newly_added
end
Expand Down Expand Up @@ -126,7 +139,7 @@ def renamed_interface?
# @param new_name [String] New interface's name
def rename_interface(new_name)
@old_name ||= name
self.name = new_name
@name = new_name
end

# Returns the current renaming mechanism
Expand All @@ -136,15 +149,6 @@ def renaming_mechanism
@renaming_mechanism || interface.renaming_mechanism
end

# Returns the underlying interface
#
# If the interface has been renamed, take the old name into account.
#
# @return [Y2Network::Interface,nil]
def interface
@interface ||= yast_config.interfaces.by_name(@old_name || name)
end

# how many device names is proposed
NEW_DEVICES_COUNT = 10
# Proposes bunch of possible names for interface
Expand Down Expand Up @@ -414,6 +418,22 @@ def save_aliases_to_connection
end
end

# Sets the interface for the builder
#
# @param iface [Interface] Interface to associate the builder with
def interface=(iface)
@interface = iface
@renaming_mechanism ||= @interface.renaming_mechanism
end

# Returns the underlying interface
#
# @return [Y2Network::Interface,nil]
def find_interface
return nil unless yast_config # in some tests, it could return nil
yast_config.interfaces.by_name(name)
end

# Helper method to access to the current configuration
#
# @return [Y2Network::Config]
Expand Down
12 changes: 11 additions & 1 deletion test/y2network/interface_config_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,20 @@
end

describe "#renamed_interface?" do
context "when the interface has been renamed" do
context "when the interface name has not been changed" do
it "returns false" do
expect(config_builder.renamed_interface?).to eq(false)
end

context "but it was initially renamed by udev" do
before do
allow(eth0).to receive(:renaming_mechanism).and_return(:mac)
end

it "returns false" do
expect(config_builder.renamed_interface?).to eq(false)
end
end
end

context "when the interface has been renamed" do
Expand Down

0 comments on commit 0bf4a53

Please sign in to comment.