diff --git a/src/modules/AutoInstallRules.rb b/src/modules/AutoInstallRules.rb index e964c2c24..b3ad1a5e6 100644 --- a/src/modules/AutoInstallRules.rb +++ b/src/modules/AutoInstallRules.rb @@ -33,6 +33,7 @@ def main Yast.import "URL" Yast.import "IP" Yast.import "Product" + Yast.import "Hostname" Yast.include self, "autoinstall/io.rb" @@ -154,6 +155,31 @@ def getMAC cleanmac end + # Return the network part of the hostaddress + # + # Unless is called during initial stage (Stage.initial), + # it always returns "192.168.1.0". + # + # @example + # AutoInstallRules.getNetwork #=> "192.168.122.0" + # + # @return [String] Network part of the hostaddress + # + # @see hostaddress + def getNetwork + return "192.168.1.0" unless Stage.initial # FIXME + wicked_ret = SCR.Execute(path(".target.bash_output"), + "/usr/sbin/wicked show --verbose all") + + # Regexp to fetch match the network address. + regexp = / ([\h:\.]+)\/\d+ dev.+pref-src #{hostaddress}/ + if match = regexp.match(wicked_ret["stdout"]) + match[1] + else + log.warn "Cannot find network address through wicked: #{wicked_ret}" + nil + end + end # Return host id (hex ip ) # @return [String] host ID @@ -293,9 +319,9 @@ def ProbeRules Ops.set(@ATTR, "hostid", @hostid) Ops.set(@ATTR, "hostname", getHostname) - @domain = Convert.to_string(SCR.Read(path(".etc.install_inf.Domain"))) + @domain = Hostname.CurrentDomain Ops.set(@ATTR, "domain", @domain) - @network = Convert.to_string(SCR.Read(path(".etc.install_inf.Network"))) + @network = getNetwork Ops.set(@ATTR, "network", @network) @haspcmcia = Convert.to_string( SCR.Read(path(".etc.install_inf.HasPCMCIA")) diff --git a/test/AutoInstallRules_test.rb b/test/AutoInstallRules_test.rb index 06c7714c1..fd140ab8b 100755 --- a/test/AutoInstallRules_test.rb +++ b/test/AutoInstallRules_test.rb @@ -71,17 +71,16 @@ end describe "#ProbeRules" do - it "reads installed product properties from content file" do + it "detect system properties" do expect(Yast::SCR).to receive(:Read).with(Yast::Path.new(".probe.bios")).and_return([]) expect(Yast::SCR).to receive(:Read).with(Yast::Path.new(".probe.memory")).and_return([]) expect(Yast::Arch).to receive(:architecture).and_return("x86_64") expect(Yast::Kernel).to receive(:GetPackages).and_return([]) - expect(Yast::SCR).to receive(:Execute).with(Yast::Path.new(".target.bash_output"), "/bin/hostname") - expect(Yast::SCR).to receive(:Read).with(Yast::Path.new(".etc.install_inf.Domain")) - expect(Yast::SCR).to receive(:Read).with(Yast::Path.new(".etc.install_inf.Hostname")) - expect(Yast::SCR).to receive(:Read).with(Yast::Path.new(".etc.install_inf.Network")) + expect(subject).to receive(:getNetwork).and_return("192.168.1.0") + expect(subject).to receive(:getHostname).and_return("myhost") expect(Yast::SCR).to receive(:Read).with(Yast::Path.new(".etc.install_inf.HasPCMCIA")) expect(Yast::SCR).to receive(:Read).with(Yast::Path.new(".etc.install_inf.XServer")) + expect(Yast::Hostname).to receive(:CurrentDomain).and_return("mydomain.lan") expect(Yast::StorageControllers).to receive(:Initialize) expect(Yast::Storage).to receive(:GetTargetMap).and_return({}) @@ -132,11 +131,100 @@ expect(Yast::Stage).to receive(:initial).and_return(true) expect(Yast::SCR).to receive(:Execute).with(Yast::Path.new(".target.bash_output"), "/usr/sbin/wicked show --verbose all|grep pref-src").and_return({"stderr"=>"error from wicked", "exit"=>1}) - expect(subject.getHostid).to eq(nil) end + end + + describe "#getHostname" do + before do + allow(Yast::SCR).to receive(:Execute). + with(Yast::Path.new(".target.bash_output"), "/bin/hostname"). + and_return(hostname_output) + end + + context "/bin/hostname returns the hostname properly" do + let(:hostname_output) { { "stdout" => "myhost", "exit" => 0 } } + + it "returns that hostname" do + expect(subject.getHostname).to eq("myhost") + end + end + + context "/bin/hostname fails" do + let(:hostname_output) { { "stderr" => "error from hostname", "stdout" => "", "exit" => 1 } } + before do + allow(Yast::SCR).to receive(:Read).with(Yast::Path.new(".etc.install_inf.Hostname")) + .and_return(inf_hostname) + end + + context "and install.inf contains a Hostname" do + let(:inf_hostname) { "myhost" } + + it "returns the name stored in install.inf" do + expect(subject.getHostname).to eq("myhost") + end + end + + context "and install.inf does not contain a Hostname" do + let(:inf_hostname) { nil } + + it "returns nil" do + expect(subject.getHostname).to eq(nil) + end + end + end end + describe "#getNetwork" do + before do + allow(Yast::Stage).to receive(:initial).and_return(initial) + end + context "in initial stage" do + let(:hostaddress) { "10.163.2.8" } + let(:initial) { true } + let(:wicked_output) { { "stdout" => wicked_content, "exit" => 0 } } + let(:wicked_content) do + File.read(File.join(root_path, "test", "fixtures", "network", "wicked.out")) + end + + before do + allow(subject).to receive(:hostaddress).and_return(hostaddress) + allow(Yast::SCR).to receive(:Execute). + with(Yast::Path.new(".target.bash_output"), "/usr/sbin/wicked show --verbose all"). + and_return(wicked_output) + end + + context "the host address is known to wicked" do + it "returns the network for the system's hostaddress" do + expect(subject.getNetwork).to eq("10.163.2.0") + end + end + + context "the host address is unknown to wicked" do + let(:hostaddress) { "10.163.2.9" } + + it "returns nil" do + expect(subject.getNetwork).to be_nil + end + end + + context "wicked fails" do + let(:wicked_output) { { "stderr" => "some error from wicked", "stdout" => "", "exit" => 1 } } + + it "returns nil" do + expect(subject.getNetwork).to be_nil + end + end + end + + context "not in initial stage" do + let(:initial) { false } + + it "returns fixed 192.168.1.0" do + expect(subject.getNetwork).to eq("192.168.1.0") + end + end + end end diff --git a/test/fixtures/network/wicked.out b/test/fixtures/network/wicked.out new file mode 100644 index 000000000..aeb79eceb --- /dev/null +++ b/test/fixtures/network/wicked.out @@ -0,0 +1,25 @@ +lo up + link: #1, state up + type: loopback + control: persistent + config: compat:suse:/etc/sysconfig/network/ifcfg-lo, + uuid: 1040df49-3c8f-515e-abe4-0aa3134b1a21 + leases: ipv4 static granted + leases: ipv6 static granted + addr: ipv4 127.0.0.1/8 [static] + addr: ipv6 ::1/128 [static] + route: ipv6 ::1/128 dev #0 type local table main scope universe protocol kernel priority 256 + +em1 up + link: #2, state up, mtu 1500 + type: ethernet, hwaddr 99:99:99:99:99:99 + control: none + config: compat:suse:/etc/sysconfig/network/ifcfg-em1, + uuid: 2c1933h4-e982-50f4-96ae-929556183dd3 + leases: ipv4 dhcp granted [group] + leases: ipv6 dhcp requesting [group] + addr: ipv6 fe80::9a90:96ff:fed1:f2b2/64 + addr: ipv4 10.163.2.8/28 [dhcp] + route: ipv4 0.0.0.0/0 via 10.163.2.1 dev #0 type unicast table main scope universe protocol dhcp + route: ipv4 10.163.2.0/28 dev #0 type unicast table main scope link protocol kernel pref-src 10.163.2.8 + route: ipv6 fe80::/64 dev #0 type unicast table main scope universe protocol kernel priority 256