Skip to content

Commit

Permalink
Get rid of NetworkInterfaces dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Feb 19, 2020
1 parent c044cb0 commit 8e9b8c0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
45 changes: 16 additions & 29 deletions src/modules/DNS.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ def main
Yast.import "Arch"
Yast.import "Hostname"
Yast.import "IP"
Yast.import "NetworkInterfaces"
Yast.import "ProductFeatures"
Yast.import "Progress"
Yast.import "Service"
Expand Down Expand Up @@ -224,16 +223,14 @@ def Write(netconfig_update: true)
# NOTE: used in yast2-nis-server, yast2-samba-server, yast2-dhcp-server
def IsHostLocal(check_host)
Read()
NetworkInterfaces.Read
dhcp_data = {}

if Ops.greater_than(
Builtins.size(NetworkInterfaces.Locate("BOOTPROTO", "dhcp")),
0
) || dhcp_hostname
dhcp_data = dhcp_data()
Builtins.y2milestone("Got DHCP-configured data: %1", dhcp_data)
current_dhcp_data = {}
connections = Yast::Lan.yast_config.connections

if connections.any?(:dhcp?) || dhcp_hostname
current_dhcp_data = dhcp_data
log.info("Got DHCP-configured data: #{current_dhcp_data.inspect}")
end

# FIXME: May not work properly in following situations:
# - multiple addresses per interface
# - aliases in /etc/hosts
Expand All @@ -242,29 +239,19 @@ def IsHostLocal(check_host)
# loopback interface or localhost hostname
return true if ["127.0.0.1", "::1", "localhost", "localhost.localdomain"].include?(check_host)

ip_addresses = connections.map { |c| c.all_ips.map { |i| i&.address&.address.to_s } }.flatten

# IPv4 address
if IP.Check4(check_host)
if Ops.greater_than(
Builtins.size(NetworkInterfaces.Locate("IPADDR", check_host)),
0
) ||
Ops.get(dhcp_data, "ip", "") == check_host
return true
end
return true if ip_addresses.include?(check_host) || current_dhcp_data["ip"] == check_host
# IPv6 address
elsif IP.Check6(check_host)
Builtins.y2debug(
"TODO make it similar to IPv4 after other code adapted to IPv6"
)
# short hostname
elsif Builtins.findfirstof(check_host, ".").nil?
if Builtins.tolower(check_host) == Builtins.tolower(@hostname) ||
Ops.get(dhcp_data, "hostname_short", "") == check_host
return true
end
elsif Builtins.tolower(check_host) ==
Builtins.tolower(Ops.add(Ops.add(@hostname, "."), @domain)) ||
Ops.get(dhcp_data, "hostname_fq", "") == check_host
log.debug("TODO make it similar to IPv4 after other code adapted to IPv6")
# short hostname or FQDN
elsif check_host.downcase == hostname.to_s.downcase ||
current_dhcp_data["hostname_short"] == check_host ||
current_dhcp_data["hostname_fq"] == check_host

return true
end
false
Expand Down
38 changes: 28 additions & 10 deletions test/dns_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@

describe Yast::DNS do
let(:lan_config) do
Y2Network::Config.new(dns: dns_config, hostname: hostname_config, source: :sysconfig)
Y2Network::Config.new(
dns: dns_config, hostname: hostname_config, source: :sysconfig,
connections: connections, interfaces: interfaces
)
end
let(:dns_config) do
Y2Network::DNS.new
Expand All @@ -42,6 +45,26 @@
Y2Network::Hostname.new(static: "install", dhcp_hostname: true)
end

let(:eth0) { Y2Network::PhysicalInterface.new("eth0") }
let(:interfaces) { Y2Network::InterfacesCollection.new([eth0]) }

let(:eth0_conn) do
Y2Network::ConnectionConfig::Ethernet.new.tap do |conn|
conn.interface = "eth0"
conn.name = "eth0"
conn.ip = ip
end
end

let(:ip) do
Y2Network::ConnectionConfig::IPConfig.new(
Y2Network::IPAddress.from_string("192.168.122.33/24"),
id: "", broadcast: Y2Network::IPAddress.from_string("192.168.122.255")
)
end

let(:connections) { Y2Network::ConnectionConfigsCollection.new([eth0_conn]) }

subject { Yast::DNS }

before do
Expand Down Expand Up @@ -117,14 +140,12 @@
end

describe ".IsHostLocal" do
let(:ip) { "10.111.66.75" }
let(:dhcp_ip) { "10.111.66.75" }
let(:hostname_short) { "test" }
let(:hostname_fq) { "test.test.de" }
let(:output) do
{ "ip" => ip, "hostname_short" => hostname_short, "hostname_fq" => hostname_fq }
end
let(:ipv4) { false }
let(:ipv6) { false }
let(:stdout) { double }

before do
Expand All @@ -133,10 +154,8 @@
.with(/eth[0-9]/)
.and_return(Y2Network::InterfaceType::ETHERNET)
allow(subject).to receive(:Read)
allow(Yast::IP).to receive(:Check4).and_return(ipv4)
allow(Yast::IP).to receive(:Check6).and_return(ipv6)
allow(Yast::Execute).to receive(:stdout).and_return(stdout)
allow(stdout).to receive(:on_target!).with("/usr/bin/hostname -i").and_return(ip)
allow(stdout).to receive(:on_target!).with("/usr/bin/hostname -i").and_return(dhcp_ip)
allow(stdout).to receive(:on_target!).with("/usr/bin/hostname").and_return(hostname_short)
allow(stdout).to receive(:on_target!).with("/usr/bin/hostname -f").and_return(hostname_fq)

Expand All @@ -158,10 +177,9 @@
end

context "for IPv4" do
let(:ipv4) { true }

it "returns true when the ip of local machine is given" do
expect(subject.IsHostLocal(ip)).to eq(true)
expect(subject.IsHostLocal(dhcp_ip)).to eq(true)
expect(subject.IsHostLocal("192.168.122.33")).to eq(true)
end

it "returns false when the ip of local machine is not given" do
Expand Down

0 comments on commit 8e9b8c0

Please sign in to comment.