diff --git a/src/lib/y2network/interface_config_builder.rb b/src/lib/y2network/interface_config_builder.rb index b7e7bb067..4f7f699a4 100644 --- a/src/lib/y2network/interface_config_builder.rb +++ b/src/lib/y2network/interface_config_builder.rb @@ -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 # @@ -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 @@ -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 @@ -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 @@ -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] diff --git a/test/y2network/interface_config_builder_test.rb b/test/y2network/interface_config_builder_test.rb index 78d59ff52..31b41baa3 100644 --- a/test/y2network/interface_config_builder_test.rb +++ b/test/y2network/interface_config_builder_test.rb @@ -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