diff --git a/src/lib/y2network/interfaces_collection.rb b/src/lib/y2network/interfaces_collection.rb index cb797a8e1..218a7b10d 100644 --- a/src/lib/y2network/interfaces_collection.rb +++ b/src/lib/y2network/interfaces_collection.rb @@ -83,10 +83,11 @@ def by_busid(busid) # Returns list of interfaces of given type # - # @param type [InterfaceType] device type + # @param type [InterfaceType,String,Symbol] device type or its short name # @return [InterfacesCollection] list of found interfaces def by_type(type) - InterfacesCollection.new(interfaces.select { |i| i.type == type }) + short_name = type.is_a?(Y2Network::InterfaceType) ? type.short_name : type.to_s + InterfacesCollection.new(interfaces.select { |i| i.type.short_name == short_name }) end # Returns the list of physical interfaces diff --git a/test/y2network/interfaces_collection_test.rb b/test/y2network/interfaces_collection_test.rb index 07b1d18c3..5c75ff937 100644 --- a/test/y2network/interfaces_collection_test.rb +++ b/test/y2network/interfaces_collection_test.rb @@ -26,8 +26,8 @@ subject(:collection) { described_class.new(interfaces) } let(:eth0) { Y2Network::PhysicalInterface.new("eth0") } - let(:br0) { Y2Network::VirtualInterface.new("br0") } - let(:wlan0) { Y2Network::PhysicalInterface.new("wlan0") } + let(:br0) { Y2Network::VirtualInterface.new("br0", type: Y2Network::InterfaceType::BRIDGE) } + let(:wlan0) { Y2Network::PhysicalInterface.new("wlan0", type: Y2Network::InterfaceType::WIRELESS) } let(:interfaces) { [eth0, br0, wlan0] } describe "#by_name" do @@ -52,6 +52,32 @@ end end + describe "#by_type" do + context "when an InterfaceType instance is given" do + it "returns a collection containing the interfaces with the given type" do + by_type = collection.by_type(Y2Network::InterfaceType::WIRELESS) + expect(by_type.to_a).to eq([wlan0]) + expect(by_type).to be_a(described_class) + end + end + + context "when type's shortname is given as string" do + it "returns a collection containing the interfaces with the given type" do + by_type = collection.by_type("br") + expect(by_type.to_a).to eq([br0]) + expect(by_type).to be_a(described_class) + end + end + + context "when type's shortname is given as symbol" do + it "returns a collection containing the interfaces with the given type" do + by_type = collection.by_type(:eth) + expect(by_type.to_a).to eq([eth0]) + expect(by_type).to be_a(described_class) + end + end + end + describe "#push" do let(:wlan1) { Y2Network::PhysicalInterface.new("wlan1") }