Skip to content

Commit

Permalink
Updates from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Jan 27, 2016
1 parent 0102d50 commit 376426c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 30 deletions.
71 changes: 46 additions & 25 deletions src/modules/AutoInstallRules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ module Yast
class AutoInstallRulesClass < Module
include Yast::Logger

# FIXME: why this values?
DEFAULT_IP = "192.168.1.1"
DEFAULT_NETWORK = "192.168.1.0"

def main
Yast.import "UI"
textdomain "autoinst"
Expand Down Expand Up @@ -159,44 +163,27 @@ def getMAC
# Return the network part of the hostaddress
#
# Unless is called during initial stage (Stage.initial),
# it always returns "192.168.1.0".
# it always returns DEFAULT_NETWORK.
#
# @example
# AutoInstallRules.getNetwork #=> "192.168.122.0"
#
# @return [String] Network part of the hostaddress
#
# @see hostaddress
# @see get_network_from_wicked
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
Stage.initial ? get_network_from_wicked : DEFAULT_NETWORK
end

# Return host id (hex ip )
#
# Unless is called during initial stage (Stage.initial),
# it always returns DEFAULT_IP.
#
# @return [String] host ID
def getHostid
if Stage.initial
wicked_ret = SCR.Execute(path(".target.bash_output"), "/usr/sbin/wicked show --verbose all|grep pref-src")
if wicked_ret["exit"] == 0
stdout = wicked_ret["stdout"].split
@hostaddress = stdout[stdout.index("pref-src")+1]
else
log.warn "Cannot evaluate IP address with wicked: #{wicked_ret["stderr"]}"
@hostaddress = nil
end
else
@hostaddress = "192.168.1.1" # FIXME
end
@hostaddress = Stage.initial ? get_ip_from_wicked : DEFAULT_IP
IP.ToHex(@hostaddress)
end

Expand Down Expand Up @@ -1182,6 +1169,40 @@ def AutoInstallRules
publish :function => :AutoInstallRules, :type => "void ()"
end

private

# Return the IP through wicked
#
# @return [String] IP address
def get_ip_from_wicked
wicked_ret = SCR.Execute(path(".target.bash_output"), "/usr/sbin/wicked show --verbose all|grep pref-src")
if wicked_ret["exit"] == 0
stdout = wicked_ret["stdout"].split
stdout[stdout.index("pref-src")+1]
else
log.warn "Cannot evaluate IP address with wicked: #{wicked_ret["stderr"]}"
nil
end
end


# Return the network address through wicked
#
# @return [String] Network IP address
def get_network_from_wicked
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

AutoInstallRules = AutoInstallRulesClass.new
AutoInstallRules.main
end
10 changes: 5 additions & 5 deletions test/AutoInstallRules_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,23 @@

it "returns host IP in hex format (initial Stage)" do
expect(Yast::SCR).to receive(:Execute).
with(Yast::Path.new(".target.bash_output"), "/usr/sbin/wicked show --verbose all|grep pref-src").
with(Yast::Path.new(".target.bash_output"), /wicked show.+|grep pref-src/).
and_return("stdout" => File.read(wicked_output_path), "exit" => 0)
expect(Yast::Stage).to receive(:initial).and_return(true)

expect(subject.getHostid).to eq("C0A864DA")
expect(subject.getHostid).to eq(Yast::IP.ToHex("192.168.100.218"))
end

it "returns fix 192.168.1.1 in hex format (normal Stage)" do
expect(Yast::Stage).to receive(:initial).and_return(false)

expect(subject.getHostid).to eq("C0A80101")
expect(subject.getHostid).to eq(Yast::IP.ToHex("192.168.1.1"))
end

it "returns nil if wicked does not find IP address" do
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").
with(Yast::Path.new(".target.bash_output"), /wicked show.+|grep pref-src/).
and_return("stderr" => "error from wicked", "exit" => 1)

expect(subject.getHostid).to eq(nil)
Expand Down Expand Up @@ -125,7 +125,7 @@
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").
with(Yast::Path.new(".target.bash_output"), /wicked show/).
and_return(wicked_output)
end

Expand Down

0 comments on commit 376426c

Please sign in to comment.