Skip to content

Commit

Permalink
EditNicName test for updating routes.
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Feb 15, 2019
1 parent e5379fc commit 76e4ad7
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 118 deletions.
6 changes: 3 additions & 3 deletions src/include/network/lan/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1373,7 +1373,7 @@ def AddressDialog

# When virtual interfaces are added the list of routing devices needs
# to be updated to offer them
update_route_devices! if update_route_devices?
update_routing_devices! if update_routing_devices?
end

if LanItems.type == "vlan"
Expand Down Expand Up @@ -1445,12 +1445,12 @@ def initialize_address_settings
# to include the current interface name
#
# @return [Boolean] false if the current interface name is already present
def update_route_devices?
def update_routing_devices?
!Routing.devices.include?(LanItems.current_name)
end

# Convenience method to update the {Yast::Routing} devices list
def update_route_devices!
def update_routing_devices!
Routing.SetDevices(LanItems.current_device_names)
end

Expand Down
4 changes: 2 additions & 2 deletions src/include/network/lan/complex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ def handleOverview(_key, event)
end

LanItems.DeleteItem
update_route_devices!
update_routing_devices!
initOverview("")
end
end
Expand Down Expand Up @@ -574,7 +574,7 @@ def overview_buttons
end

# Convenience method to update the {Yast::Routing} devices list
def update_route_devices!
def update_routing_devices!
Routing.SetDevices(LanItems.current_device_names)
end
end
Expand Down
9 changes: 6 additions & 3 deletions src/lib/network/edit_nic_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Yast
Yast.import "UI"
Yast.import "LanItems"
Yast.import "Popup"
Yast.import "Routing"

# The class represents a simple dialog which allows user to input new NIC
# name. It also allows to select a device attribute (MAC, Bus id, ...) which will
Expand Down Expand Up @@ -74,8 +75,10 @@ def run
# and / or its value only, name is changed elsewhere
LanItems.update_item_udev_rule!(udev_type)

update_route_devices!
update_routes!(old_name) if update_routes?(old_name)
if new_name != old_name
update_routing_devices!
update_routes!(old_name) if update_routes?(old_name)
end
end

close
Expand Down Expand Up @@ -187,7 +190,7 @@ def update_routes!(previous_name)
end
end

def update_route_devices!
def update_routing_devices!
Routing.SetDevices(LanItems.current_device_names)
end
end
Expand Down
1 change: 0 additions & 1 deletion test/address_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,4 @@ def initialize
end
end
end

end
109 changes: 0 additions & 109 deletions test/edit_nic_name_test.rb

This file was deleted.

137 changes: 137 additions & 0 deletions test/lib/network/edit_nic_name_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#!/usr/bin/env rspec

require_relative "../../test_helper"

require "yast"
require "network/edit_nic_name"

current_name = "spec0".freeze
new_name = "new1".freeze
existing_new_name = "existing_new_name".freeze

Yast.import "LanItems"

describe Yast::EditNicName do
let(:subject) { described_class.new }
let(:current_name) { "spec0" }
let(:new_name) { "new1" }
let(:existing_new_name) { existing_new_name }
let(:interface_hwinfo) { { "dev_name" => current_name, "mac" => "00:01:02:03:04:05" } }

describe "#run" do
# general mocking stuff is placed here
before(:each) do
# NetworkInterfaces are too low level. Everything needed should be mocked
stub_const("NetworkInterfaces", double(adapt_old_config!: nil))

# mock devices configuration
allow(Yast::LanItems).to receive(:ReadHardware).and_return([interface_hwinfo])
allow(Yast::LanItems).to receive(:getNetworkInterfaces).and_return([current_name])
allow(Yast::LanItems).to receive(:GetItemUdev) { "" }
allow(Yast::LanItems).to receive(:current_udev_name).and_return(current_name)
allow(Yast::LanItems).to receive(:GetItemUdev).with("ATTR{address}") { "00:01:02:03:04:05" }
allow(Yast::LanItems).to receive(:GetNetcardNames).and_return([current_name])

# LanItems initialization

Yast::LanItems.Read
Yast::LanItems.FindAndSelect(current_name)
end

context "when closed without any change" do
before(:each) do
# emulate Yast::UI work
allow(Yast::UI).to receive(:QueryWidget).with(:dev_name, :Value) { current_name }
allow(Yast::UI).to receive(:QueryWidget).with(:udev_type, :CurrentButton) { :mac }
allow(Yast::UI).to receive(:UserInput) { :ok }
allow(Yast::LanItems).to receive(:update_item_udev_rule!)
end

it "returns current name when used Ok button" do
expect(subject.run).to be_equal current_name
end

it "returns current name when used Cancel button" do
allow(Yast::UI).to receive(:UserInput) { :cancel }

expect(subject.run).to be_equal current_name
end

it "does not execute any other callback" do
expect(Yast::Routing).to_not receive(:update_routing_devices!)

subject.run
end
end

context "when name changed" do
before(:each) do
# emulate Yast::UI work
allow(Yast::UI).to receive(:QueryWidget).with(:dev_name, :Value) { new_name }
allow(Yast::UI).to receive(:QueryWidget).with(:udev_type, :CurrentButton) { :mac }
allow(Yast::UI).to receive(:UserInput) { :ok }
allow(subject).to receive(:update_routing_devices!)
allow(subject).to receive(:update_routes?).and_return(false)
allow(subject).to receive(:update_routes!)
end

context "and closed confirming the changes" do
it "returns the new name" do
expect(subject.run).to be_equal new_name
end

it "asks for new user input when name already exists" do
allow(Yast::UI).to receive(:QueryWidget)
.with(:dev_name, :Value).and_return(existing_new_name, new_name)
expect(subject).to receive(:CheckUdevNicName).with(existing_new_name).and_return(false)
expect(subject).to receive(:CheckUdevNicName).with(new_name).and_return(true)
expect(Yast::UI).to receive(:SetFocus)
expect(Yast::LanItems).to receive(:rename).with(new_name)
subject.run
end

it "updates the Routing devices list with the new name" do
expect(subject).to receive(:update_routing_devices!).and_call_original
subject.run
expect(Yast::Routing.devices).to include(new_name)
end

context "and there are some routes referencing the previous name" do
before do
allow(Yast::Routing).to receive(:device_routes?).with(current_name).and_return(true)
expect(subject).to receive(:update_routes?).with(current_name).and_call_original
allow(subject).to receive(:update_routes!).with(current_name)
end

it "asks the user about updating the routes device name" do
expect(Yast::Popup).to receive(:YesNoHeadline)

subject.run
end

it "updates the routes if the user accepts to do it" do
expect(Yast::Popup).to receive(:YesNoHeadline).and_return(true)
expect(subject).to receive(:update_routes!).with(current_name)

subject.run
end

it "does not touch the routes if the user does not want to touch them" do
expect(Yast::Popup).to receive(:YesNoHeadline).and_return(false)
expect(subject).to_not receive(:update_routes!)
subject.run
end
end
end

context "and closed canceling the changes" do
it "returns current name when used Cancel button" do
allow(Yast::UI).to receive(:UserInput) { :cancel }

expect(subject.run).to be_equal current_name
end
end

end
end
end

0 comments on commit 76e4ad7

Please sign in to comment.