Skip to content

Commit

Permalink
fix crash when dummy ip is not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Oct 17, 2016
1 parent 49d4756 commit 3c0cfeb
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/lib/cfa/hosts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ def delete_hostname(hostname)
end
end

# returns true if hosts include entry with given IP
def include_ip?(ip)
entries = data.select(ip_matcher(ip))
!entries.empty?
end

private

# returns matcher for cfa to find entries with given ip
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def EnsureHostnameResolvable

fqhostname = Hostname.MergeFQ(DNS.hostname, DNS.domain)
set_names(local_ip, ["#{fqhostname} #{DNS.hostname}"])
elsif Builtins.haskey(@hosts, local_ip)
elsif @hosts.include_ip?(local_ip)
# Do not add it if product default says no
# and remove 127.0.02 entry if it exists

Expand Down
1 change: 1 addition & 0 deletions test/data/hosts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#

127.0.0.1 localhost
127.0.0.2 localhost

# special IPv6 addresses
::1 localhost ipv6-localhost ipv6-loopback
Expand Down
37 changes: 37 additions & 0 deletions test/host_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,41 @@
expect(content.lines).to include("10.100.128.72 pepa pepa2 newname.suse.cz newname\n")
end
end

describe ".EnsureHostnameResolvable" do
context "need dummy ip" do
before do
allow(Yast::DNS).to receive(:write_hostname).and_return(true)
end

it "sets entry for 127.0.0.2 to hostname and hostname with domain" do
allow(Yast::DNS).to receive(:hostname).and_return("localmachine")
allow(Yast::DNS).to receive(:domain).and_return("domain.local")
Yast::Host.Read
Yast::Host.EnsureHostnameResolvable
Yast::Host.Write

content = file.content

expect(content.lines).to include("127.0.0.2 localmachine.domain.local localmachine\n")
end
end

context "do not need dummy ip" do
before do
allow(Yast::DNS).to receive(:write_hostname).and_return(false)
end

it "deletes entry for 127.0.0.2" do
Yast::Host.Read
Yast::Host.EnsureHostnameResolvable
Yast::Host.Write

content = file.content

expect(content.lines.grep(/^127.0.0.2/)).to be_empty
end

end
end
end

0 comments on commit 3c0cfeb

Please sign in to comment.