Skip to content

Commit

Permalink
Merge pull request #852 from yast/improve_test_coverage
Browse files Browse the repository at this point in the history
add test for parsing ntp servers
  • Loading branch information
jreidinger committed Jul 1, 2019
2 parents 51b941b + e8e8ef4 commit f8d346f
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 24 deletions.
3 changes: 2 additions & 1 deletion src/lib/network/wicked.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
module Yast
module Wicked
BASH_PATH = Path.new(".target.bash")
BASH_OUTPUT_PATH = Path.new(".target.bash_output")

# Reloads configuration for each device named in devs
#
Expand All @@ -28,7 +29,7 @@ def parse_ntp_servers(iface)

lease_files = ["ipv4", "ipv6"].map { |ip| "/var/lib/wicked/lease-#{iface}-dhcp-#{ip}.xml" }
lease_files.find_all { |f| File.file?(f) }.reduce([]) do |stack, file|
result = SCR.Execute(path(".target.bash_output"), "/usr/sbin/wicked xpath --file #{file.shellescape} \"%{//ntp/server}\"")
result = SCR.Execute(BASH_OUTPUT_PATH, "/usr/sbin/wicked xpath --file #{file.shellescape} \"%{//ntp/server}\"")

stack + result.fetch("stdout", "").split("\n")
end
Expand Down
1 change: 1 addition & 0 deletions src/lib/y2network/widgets/vlan_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def label

def help
# TODO: previously not exist, so write it
""
end

def init
Expand Down
1 change: 1 addition & 0 deletions src/lib/y2network/widgets/vlan_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def label

def help
# TODO: previously not exist, so write it
""
end

def items
Expand Down
10 changes: 0 additions & 10 deletions test/routines_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,6 @@ def initialize
end
end

describe "hwlist2items" do
subject(:routines) { RoutinesTestClass.new }
let(:list) { [{ "name" => "x" }, { "name" => "y" }] }

it "creates a list of Items from given array" do
expect(routines.hwlist2items(list, 1))
.to match_array([Item(Id(0), "x", false), Item(Id(1), "y", true)])
end
end

describe "physical_port_id" do
subject(:routines) { RoutinesTestClass.new }
let(:phys_port_id) { "physical_port_id" }
Expand Down
43 changes: 31 additions & 12 deletions test/wicked_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,40 @@ class DummyNetwork
include Yast::Wicked
end

describe "#reload_config" do
let(:subject) { DummyNetwork.new }
describe Yast::Wicked do
subject { DummyNetwork.new }
describe "#reload_config" do

it "raises ArgumentError if dev names parameter is nil" do
expect { subject.reload_config(nil) }.to raise_error("ArgumentError")
end
it "raises ArgumentError if dev names parameter is nil" do
expect { subject.reload_config(nil) }.to raise_error("ArgumentError")
end

it "returns true if given device names are empty" do
expect(subject.reload_config([])).to eql(true)
end
it "returns true if given device names are empty" do
expect(subject.reload_config([])).to eql(true)
end

it "returns true if given devices reload successfully" do
expect(Yast::SCR).to receive(:Execute)
.with(DummyNetwork::BASH_PATH, "/usr/sbin/wicked ifreload eth0 eth1").and_return(0)
it "returns true if given devices reload successfully" do
expect(Yast::SCR).to receive(:Execute)
.with(DummyNetwork::BASH_PATH, "/usr/sbin/wicked ifreload eth0 eth1").and_return(0)

expect(subject.reload_config(["eth0", "eth1"])).to eql(true)
end
end

expect(subject.reload_config(["eth0", "eth1"])).to eql(true)
describe "#parse_ntp_servers" do
before do
allow(Yast::NetworkService).to receive(:is_wicked).and_return(true)
allow(::File).to receive(:file?).and_return(true, false)
allow(Yast::SCR).to receive("Execute").and_return("stdout" => <<WICKED_OUTPUT
10.100.2.10
10.100.2.11
10.100.2.12
WICKED_OUTPUT
)
end

it "returns list of ntp servers defined in dhcp lease" do
expect(subject.parse_ntp_servers("eth0")).to eq(["10.100.2.10", "10.100.2.11", "10.100.2.12"])
end
end
end
131 changes: 130 additions & 1 deletion test/y2network/widgets/boot_protocol_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,142 @@
require "cwm/rspec"

require "y2network/widgets/boot_protocol"
require "y2network/interface_config_builder"

describe Y2Network::Widgets::BootProtocol do
subject { described_class.new({}) }
let(:builder) { Y2Network::InterfaceConfigBuilder.for("eth") }
subject { described_class.new(builder) }

before do
allow(subject).to receive(:value).and_return(:bootproto_none)
allow(Yast::UI).to receive(:ChangeWidget)
end

include_examples "CWM::CustomWidget"

def expect_set_widget(id, value, value_type: :Value)
expect(Yast::UI).to receive(:ChangeWidget).with(Id(id), value_type, value)
end

describe "#init" do
context "for other types then eth" do
let(:builder) { Y2Network::InterfaceConfigBuilder.for("br") }

it "hides iBFT checkbox" do
expect(Yast::UI).to receive(:ReplaceWidget).with(:bootproto_rp, Empty())

subject.init
end
end

context "static configuration" do
before do
builder["BOOTPROTO"] = "static"
builder["IPADDR"] = "10.5.0.6"
builder["PREFIXLEN"] = "24"
builder["HOSTNAME"] = "pepa"
allow(subject).to receive(:value).and_return(:bootproto_static)
allow(Yast::UI).to receive(:QueryWidget).and_return("pepa")
end

it "sets static configuration" do
expect_set_widget(:bootproto, :bootproto_static, value_type: :CurrentButton)

subject.init
end

it "sets ip address" do
expect_set_widget(:bootproto_ipaddr, "10.5.0.6")

subject.init
end

it "sets netmask" do
expect_set_widget(:bootproto_netmask, "/24")

subject.init
end

it "sets hostname" do
expect_set_widget(:bootproto_hostname, "pepa")

subject.init
end
end

context "dhcp configuration" do
before do
builder["BOOTPROTO"] = "dhcp"
allow(subject).to receive(:value).and_return(:bootproto_dynamic)
end

it "does not crash" do
subject.init
end
end

context "dhcp4 configuration" do
before do
builder["BOOTPROTO"] = "dhcp4"
allow(subject).to receive(:value).and_return(:bootproto_dynamic)
end

it "does not crash" do
subject.init
end
end

context "dhcp6 configuration" do
before do
builder["BOOTPROTO"] = "dhcp6"
allow(subject).to receive(:value).and_return(:bootproto_dynamic)
end

it "does not crash" do
subject.init
end
end

context "dhcp+autoip configuration" do
before do
builder["BOOTPROTO"] = "dhcp+autoip"
allow(subject).to receive(:value).and_return(:bootproto_dynamic)
end

it "does not crash" do
subject.init
end
end

context "autoip configuration" do
before do
builder["BOOTPROTO"] = "autoip"
allow(subject).to receive(:value).and_return(:bootproto_dynamic)
end

it "does not crash" do
subject.init
end
end

context "none configuration" do
before do
builder["BOOTPROTO"] = "none"
end

it "does not crash" do
subject.init
end
end

context "ibft configuration" do
before do
builder["BOOTPROTO"] = "ibft"
end

it "does not crash" do
subject.init
end
end
end
end
31 changes: 31 additions & 0 deletions test/y2network/widgets/vlan_id_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright (c) [2019] 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 "cwm/rspec"

require "y2network/widgets/vlan_id"
require "y2network/interface_config_builder"

describe Y2Network::Widgets::VlanID do
let(:builder) { Y2Network::InterfaceConfigBuilder.for("vlan") }
subject { described_class.new(builder) }

include_examples "CWM::IntField"
end
31 changes: 31 additions & 0 deletions test/y2network/widgets/vlan_interface_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright (c) [2019] 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 "cwm/rspec"

require "y2network/widgets/vlan_interface"
require "y2network/interface_config_builder"

describe Y2Network::Widgets::VlanInterface do
let(:builder) { Y2Network::InterfaceConfigBuilder.for("vlan") }
subject { described_class.new(builder) }

include_examples "CWM::ComboBox"
end

0 comments on commit f8d346f

Please sign in to comment.