Skip to content

Commit

Permalink
Merge 44a051c into 12e4859
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Jan 27, 2021
2 parents 12e4859 + 44a051c commit d7ccb5d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/lib/y2network/interfaces_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 28 additions & 2 deletions test/y2network/interfaces_collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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") }

Expand Down

0 comments on commit d7ccb5d

Please sign in to comment.