Skip to content

Commit

Permalink
fix adding alias in update and test it
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Oct 12, 2016
1 parent 671bc1c commit 183af41
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 24 deletions.
13 changes: 5 additions & 8 deletions src/modules/Host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ def EnsureHostnameResolvable
def Read
return true if @initialized


# read /etc/hosts
if Ops.greater_than(SCR.Read(path(".target.size"), CFA::Hosts::PATH), 0)
@hosts = CFA::Hosts.new
Expand Down Expand Up @@ -176,7 +175,6 @@ def Import(settings)
@hosts = CFA::Hosts.new
imported_hosts = Builtins.eval(Ops.get_map(settings, "hosts", {}))


# convert from old format to the new one
# use ::1 entry as a reference
if (imported_hosts["::1"] || []).size > 1
Expand Down Expand Up @@ -236,12 +234,8 @@ def Update(oldhn, newhn, ip)
)
@modified = true

nick = Ops.get(Hostname.SplitFQ(newhn), 0, "")

# Remove old hostname from hosts
if !oldhn.empty?
@hosts.remove_hostname(oldhn)
end
@hosts.remove_hostname(oldhn) if !oldhn.empty?

# Add localhost if missing
if @hosts.host("127.0.0.1").empty?
Expand All @@ -253,12 +247,15 @@ def Update(oldhn, newhn, ip)
# Omit invalid newhn
return true if [nil, ""].include?(newhn)

nick = Hostname.SplitFQ(newhn)[0] || ""
nick = nick.empty? ? [] : [nick]
hosts = @hosts.host(ip)
if hosts.empty?
@hosts.add_host(ip, newhn)
@hosts.add_host(ip, newhn, nick)
else
canonical, *aliases = hosts.last
aliases << newhn
aliases.concat(nick)
@hosts.set_host(ip, canonical, aliases)
end

Expand Down
24 changes: 8 additions & 16 deletions test/host_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,6 @@
end
end

describe ".set_names" do
it "adds new adress with names if address is not yet in hosts table" do
Yast::Host.Read
Yast::Host.set_names("1.1.1.1", ["test test2.suse.cz"])

expect(Yast::Host.names("1.1.1.1")).to eq(["test test2.suse.cz"])
end

it "replaces entry with given address if already used" do
Yast::Host.Read
Yast::Host.set_names("10.100.128.72", ["test test2.suse.cz"])

expect(Yast::Host.names("10.100.128.72")).to eq(["test test2.suse.cz"])
end
end

describe ".add_name" do
it "adds host to hosts entry even if it is already there" do
Yast::Host.Read
Expand Down Expand Up @@ -176,5 +160,13 @@
tested_ip = "10.0.0.1"
expect(Yast::Host.name_map[tested_ip]).to eql etc_hosts_new[tested_ip]
end

it "adds alias for added hostname" do
Yast::Host.Import("hosts" => etc_hosts)
Yast::Host.Update("", "newname.suse.cz", "10.0.0.42")

tested_ip = "10.0.0.42"
expect(Yast::Host.name_map[tested_ip]).to eql ["newname.suse.cz newname"]
end
end
end

0 comments on commit 183af41

Please sign in to comment.