Skip to content

Commit

Permalink
Added tests for QETH layer2 support widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Sep 18, 2019
1 parent 652cfa9 commit f8a15c2
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/lib/y2network/widgets/s390_common.rb
Expand Up @@ -247,7 +247,8 @@ def handle(event)
def validate
return true if !layer2? || valid_mac?(mac_address_widget.value)

report_mac_error && false
report_mac_error
false
end

private
Expand All @@ -258,24 +259,36 @@ def report_mac_error
Yast::Popup.Error(msg)
end

# Convenience method to check whether layer2 support is enabled or not
#
# @return [Boolean] true if enabled; false otherwise
def layer2?
!!support_widget.value
end

# Convenience method to check whether the MAC address provided is valid
# or not
#
# @return [Boolean] true when valid; false otherwise
# @param mac_address [String]
def valid_mac?(mac_address)
return false if mac_address.to_s.empty?
return false if mac_address == "00:00:00:00:00:00"
!!(mac_address =~ /^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$/i)
end

# Convenience method to enable or disable the mac address widget when the
# layer2 support is modified
def refresh
support_widget.checked? ? mac_address_widget.enable : mac_address_widget.disable
end

# @return [S390Layer2Support]
def support_widget
@support_widget ||= S390Layer2Support.new(@settings)
end

# @return [S390Layer2Address]
def mac_address_widget
@mac_address_widget ||= S390Layer2Address.new(@settings)
end
Expand Down
84 changes: 84 additions & 0 deletions test/y2network/widgets/s390_common_test.rb
Expand Up @@ -73,3 +73,87 @@

include_examples "CWM::InputField"
end

describe Y2Network::Widgets::S390Layer2 do
let(:builder) { Y2Network::InterfaceConfigBuilder.for("qeth") }
let(:layer2_support) { true }
let(:layer2_address) { "00:00:00:00:00:00" }
let(:layer2_support_widget) do
instance_double("Y2Network::WidgetsS390Layer2Support",
value: layer2_support, widget_id: "layer2_support")
end

let(:layer2_address_widget) do
instance_double("Y2Network::WidgetsS390Layer2Address",
value: layer2_address, widget_id: "layer2_address")
end

subject { described_class.new(builder) }

before do
allow(subject).to receive(:support_widget).and_return(layer2_support_widget)
allow(subject).to receive(:mac_address_widget).and_return(layer2_address_widget)
end

include_examples "CWM::CustomWidget"

describe "#handle" do
context "when the event handled is for the layer2_support widget" do
it "refresh the mac address widget" do
expect(subject).to receive(:refresh)
subject.handle("ID" => "layer2_support")
end
end

it "returns nil" do
allow(subject).to receive(:refresh)
expect(subject.handle("ID" => "layer2_address")).to be_nil
expect(subject.handle("ID" => "layer2_support")).to be_nil
end
end

describe "#validate" do
context "when the layer2 support is not enabled" do
let(:layer2_support) { false }
it "returns true" do
expect(subject.validate).to eql(true)
end
end

context "when the layer2 support is enabled" do
context "and the MAC provided is valid" do
let(:layer2_address) { "02:00:00:00:01:FD" }

it "returns true" do
expect(subject.validate).to eql(true)
end
end

context "and the MAC address provided is invalid" do
it "returns false" do
allow(Yast::Popup).to receive(:Error)
expect(subject.validate).to eql(false)
end

it "reports an error" do
expect(Yast::Popup).to receive(:Error).with(/MAC address provided is not valid/)
expect(subject.validate).to eql(false)
end
end
end
end
end

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

include_examples "CWM::CheckBox"
end

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

include_examples "CWM::InputField"
end

0 comments on commit f8a15c2

Please sign in to comment.