Skip to content

Commit

Permalink
Add Y2Firewall::Firewalld::Interface unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Sep 19, 2018
1 parent 02bef2a commit 6fa7203
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
5 changes: 1 addition & 4 deletions library/network/src/lib/y2firewall/firewalld/interface.rb
Expand Up @@ -21,6 +21,7 @@

require "yast"
require "y2firewall/firewalld"
Yast.import "NetworkInterfaces"

module Y2Firewall
class Firewalld
Expand All @@ -33,18 +34,14 @@ class Interface
#
# @param name [String] interface name
def initialize(name)
Yast.import "NetworkInterfaces"

@id = name.to_sym
end

# Return an array with all the known or configured interfaces
#
# @return [Array<Y2Firewall::Firewalld::Interface>] known interfaces
def self.known
Yast.import "NetworkInterfaces"
interfaces = Yast::NetworkInterfaces.List("").reject { |i| i == "lo" }

interfaces.map { |i| new(i) }
end

Expand Down
86 changes: 86 additions & 0 deletions library/network/test/y2firewall/firewalld/interface_test.rb
@@ -0,0 +1,86 @@
#!/usr/bin/env rspec
# encoding: utf-8

# Copyright (c) [2018] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require_relative "../../test_helper"
require "y2firewall/firewalld/interface"

describe Y2Firewall::Firewalld::Interface do
subject(:iface) { described_class.new("eth0") }

describe ".known" do
before do
allow(Yast::NetworkInterfaces).to receive(:List).with("")
.and_return(["eth0", "lo", "wlan0"])
end

it "returns an object for each known interface except 'lo'" do
expect(described_class.known).to contain_exactly(
an_object_having_attributes(name: "eth0"),
an_object_having_attributes(name: "wlan0")
)
end
end

describe "#name" do
it "returns the interface name" do
expect(iface.name).to eq("eth0")
end
end

describe "#id" do
it "returns the name as a symbol" do
expect(iface.id).to eq(:eth0)
end
end

describe "#device_name" do
DEVICE_NAME = "Some Device Name".freeze

before do
allow(Yast::NetworkInterfaces).to receive(:GetValue).with("eth0", "NAME")
.and_return(DEVICE_NAME)
end

it "returns the device name" do
expect(iface.device_name).to eq(DEVICE_NAME)
end
end

describe "#zone" do
let(:public_zone) do
instance_double(Y2Firewall::Firewalld::Zone, interfaces: ["eth1"])
end

let(:dmz_zone) do
instance_double(Y2Firewall::Firewalld::Zone, interfaces: ["eth0"])
end

before do
allow(Y2Firewall::Firewalld.instance).to receive(:zones)
.and_return([public_zone, dmz_zone])
end

it "returns the zone where the interface belongs to" do
expect(iface.zone).to eq(dmz_zone)
end
end
end

0 comments on commit 6fa7203

Please sign in to comment.