Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/linux_admin/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ def initialize(result)
end

class NetworkInterfaceError < AwesomeSpawn::CommandResultError; end

class MissingConfigurationFileError < StandardError; end
end
2 changes: 2 additions & 0 deletions lib/linux_admin/network_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def self.dist_class(clear_cache = false)
# Creates an instance of the correct NetworkInterface subclass for the local distro
def self.new(*args)
self == LinuxAdmin::NetworkInterface ? dist_class.new(*args) : super
rescue MissingConfigurationFileError
NetworkInterfaceGeneric.new(*args)
end

# @return [String] the interface for networking operations
Expand Down
3 changes: 2 additions & 1 deletion lib/linux_admin/network_interface/network_interface_rh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ class NetworkInterfaceRH < NetworkInterface

# @param interface [String] Name of the network interface to manage
def initialize(interface)
@interface_file = Pathname.new(IFACE_DIR).join("ifcfg-#{interface}")
raise MissingConfigurationFileError unless File.exist?(@interface_file)
super
@interface_file = Pathname.new(IFACE_DIR).join("ifcfg-#{@interface}")
parse_conf
end

Expand Down
2 changes: 2 additions & 0 deletions spec/network_interface/network_interface_rh_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ def result(output, exit_status)
end

subject(:dhcp_interface) do
allow(File).to receive(:exist?).and_return(true)
stub_foreach_to_string(IFCFG_FILE_DHCP)
allow(AwesomeSpawn).to receive(:run!).twice.and_return(result("", 0))
described_class.new(DEVICE_NAME)
end

subject(:static_interface) do
allow(File).to receive(:exist?).and_return(true)
stub_foreach_to_string(IFCFG_FILE_STATIC)
allow(AwesomeSpawn).to receive(:run!).twice.and_return(result("", 0))
described_class.new(DEVICE_NAME)
Expand Down
17 changes: 15 additions & 2 deletions spec/network_interface_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
describe LinuxAdmin::NetworkInterface do
context "on redhat systems" do
subject do
subject(:subj_success) do
allow_any_instance_of(described_class).to receive(:ip_show).and_return(nil)
allow(LinuxAdmin::Distros).to receive(:local).and_return(LinuxAdmin::Distros.rhel)
described_class.dist_class(true)
allow(File).to receive(:exist?).and_return(true)
allow(File).to receive(:foreach).and_return("")
described_class.new("eth0")
end

subject(:subj_failure) do
allow_any_instance_of(described_class).to receive(:ip_show).and_return(nil)
allow(LinuxAdmin::Distros).to receive(:local).and_return(LinuxAdmin::Distros.rhel)
described_class.dist_class(true)
allow(File).to receive(:exist?).and_return(false)
described_class.new("eth0")
end

describe ".dist_class" do
it "returns NetworkInterfaceRH" do
allow(LinuxAdmin::Distros).to receive(:local).and_return(LinuxAdmin::Distros.rhel)
Expand All @@ -17,7 +26,11 @@

describe ".new" do
it "creates a NetworkInterfaceRH instance" do
expect(subject).to be_an_instance_of(LinuxAdmin::NetworkInterfaceRH)
expect(subj_success).to be_an_instance_of(LinuxAdmin::NetworkInterfaceRH)
end

it "creates a NetworkInterfaceGeneric instance if the config file does not exist" do
expect(subj_failure).to be_an_instance_of(LinuxAdmin::NetworkInterfaceGeneric)
end
end
end
Expand Down