Skip to content

Commit

Permalink
Unit tests for dhcp hostname.
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Sep 26, 2017
1 parent 8867b67 commit 85791cd
Showing 1 changed file with 91 additions and 5 deletions.
96 changes: 91 additions & 5 deletions test/inst_setup_dhcp_test.rb
Expand Up @@ -5,9 +5,7 @@
require "network/clients/inst_setup_dhcp"

describe Yast::SetupDhcp do
before do
allow(Yast::NetworkInterfaces).to receive(:adapt_old_config!)
end
let(:subject) { Yast::SetupDhcp.instance }

describe "#main" do

Expand All @@ -16,15 +14,103 @@
.to receive(:any_iface_active?)
.and_return(true)

expect(Yast::SetupDhcp.instance.main).to eql :next
expect(subject.main).to eql :next
end

it "returns :next when autoconfiguration is not performed" do
allow(Yast::NetworkAutoconfiguration)
.to receive(:any_iface_active?)
.and_return(false)

expect(Yast::SetupDhcp.instance.main).to eql :next
expect(subject.main).to eql :next
end

it "runs network dhcp autoconfiguration if no active interfaces" do
allow(Yast::NetworkAutoconfiguration)
.to receive(:any_iface_active?)
.and_return(false)
expect(Yast::NetworkAutoconfiguration.instance)
.to receive(:configure_dhcp)

subject.main
end

context "in the initial Stage" do
it "writes DHCLIENT_SET_HOSTNAME in /etc/sysconfig/network/dhcp" do
expect(Yast::Stage).to receive(:initial).and_return(true)
expect(subject).to receive(:set_dhcp_hostname!)

subject.main
end
end
end

describe "#set_dhcp_hostname!" do
let(:dhclient_set_hostname_path) do
Yast::Path.new(".sysconfig.network.dhcp.DHCLIENT_SET_HOSTNAME")
end

before do
allow(Yast::SCR).to receive(:Write)
end

context "when the linurc sethostname option has been used" do
before do
allow(subject).to receive(:set_hostname_used?).and_return(true)
end

it "sets DNS.dhcp_hostname according to the linuxrc sethosname value" do
expect(subject).to receive(:set_dhcp_hostname?).and_return(false)
expect(Yast::DNS).to receive(:dhcp_hostname=).with(false)

subject.set_dhcp_hostname!
end
end

context "when the linurc sethostname option has not been used" do
before do
allow(subject).to receive(:set_hostname_used?).and_return(false)
end

it "sets DNS.dhcp_hostname according to DNS.default_dhcp_hostname" do
expect(subject).to_not receive(:set_dhcp_hostname?)
expect(Yast::DNS).to receive(:default_dhcp_hostname).and_return(true)
expect(Yast::DNS).to receive(:dhcp_hostname=).with(true)

subject.set_dhcp_hostname!
end
end

context "once initialized DNS.dhcp_hostname" do
it "writes global DHCLIENT_SET_HOSTNAME in /etc/sysconfig/network/dhcp with it" do
allow(Yast::DNS).to receive(:dhcp_hostname=)
allow(Yast::DNS).to receive(:dhcp_hostname).and_return(false)
expect(Yast::SCR).to receive(:Write).with(dhclient_set_hostname_path, "no")

subject.set_dhcp_hostname!
end
end
end

describe "set_dhcp_hostname?" do
let(:set_hostname) { "1" }

before do
allow(Yast::Linuxrc).to receive(:InstallInf)
.with("SetHostname").and_return(set_hostname)
end
context "when dhcp hostname has not been disabled by linuxrc" do
it "returns true" do
expect(subject.set_dhcp_hostname?).to eql(true)
end
end

context "when dhcp hostname has been disabled by linuxrc" do
let(:set_hostname) { "0" }

it "returns false" do
expect(subject.set_dhcp_hostname?).to eql(false)
end
end
end
end

0 comments on commit 85791cd

Please sign in to comment.