Skip to content

Commit

Permalink
Add new Profile.create method and refactor Process method
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Jun 26, 2020
1 parent 61a86c9 commit dfbed59
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 133 deletions.
28 changes: 9 additions & 19 deletions src/modules/AutoinstClone.rb
Expand Up @@ -60,12 +60,11 @@ def createClonableList

# Builds the profile
#
# @return [void]
# @see ProfileClass.Prepare
# @return [void] returns void and sets profile in ProfileClass.current
# @see ProfileClass.create
# @see ProfileClass.current for result
def Process
log.info "Additional resources: #{@additional}"
Profile.Reset
Profile.prepare = true
Mode.SetMode("autoinst_config")

Y2ModuleConfig.ModuleMap.each do |def_resource, resource_map|
Expand All @@ -75,18 +74,14 @@ def Process

next unless @additional.include?(resource)

log.info "Now cloning: #{resource}"
time_start = Time.now
CommonClone(resource_map)
read_module(resource_map)
log.info "Cloning #{resource} took: #{(Time.now - time_start).round} sec"
end

if @additional.include?("general")
Call.Function("general_auto", ["Import", General()])
Call.Function("general_auto", ["SetModified"])
end
Call.Function("general_auto", ["Import", General()]) if @additional.include?("general")

Profile.Prepare
Profile.create(@additional)
nil
end

Expand Down Expand Up @@ -117,12 +112,11 @@ def General
general
end

# Clone a Resource
# Reads module if it is appropriate
#
# @param _resource [String] resource. Not used.
# @param resource_map [Hash] resources map
# @return [Array]
def CommonClone(resource_map)
# @return [void]
def read_module(resource_map)
auto = Ops.get_string(resource_map, "X-SuSE-YaST-AutoInstClient", "")

# Do not read settings from system in first stage, autoyast profile
Expand All @@ -134,10 +128,6 @@ def CommonClone(resource_map)
["software_auto", "storage_auto"].include?(auto)
Call.Function(auto, ["Read"])
end
# Flagging YAST module for export
Call.Function(auto, ["SetModified"])

true
end
end

Expand Down
119 changes: 57 additions & 62 deletions src/modules/Profile.rb
Expand Up @@ -290,6 +290,8 @@ def Import(profile)
end

# Prepare Profile for saving and remove empty data structs
# This is mainly for editing profile when there is some parts we do not write ourself
# For creating new one from given set of modules see {#create}
# @return [void]
def Prepare
return if !@prepare
Expand All @@ -299,74 +301,27 @@ def Prepare
_("This may take a while")
)

e = []

Builtins.foreach(@ModuleMap) do |p, d|
#
# Set resource name, if not using default value
#
resource = Ops.get_string(d, "X-SuSE-YaST-AutoInstResource", "")
resource = p if resource == ""
tomerge = Ops.get_string(d, "X-SuSE-YaST-AutoInstMerge", "")
module_auto = Ops.get_string(d, "X-SuSE-YaST-AutoInstClient", "none")
if Convert.to_boolean(WFM.CallFunction(module_auto, ["GetModified"]))
resource_data = WFM.CallFunction(module_auto, ["Export"])

s = 0
if tomerge == ""
s = if Ops.get_string(d, "X-SuSE-YaST-AutoInstDataType", "map") == "map"
Builtins.size(Convert.to_map(resource_data))
else
Builtins.size(Convert.to_list(resource_data))
end
end

if s != 0 || tomerge != ""
if Ops.greater_than(Builtins.size(tomerge), 0)
i = 0
tomergetypes = Ops.get_string(
d,
"X-SuSE-YaST-AutoInstMergeTypes",
""
)
_MergeTypes = Builtins.splitstring(tomergetypes, ",")

Builtins.foreach(Builtins.splitstring(tomerge, ",")) do |res|
rd = Convert.convert(
resource_data,
from: "any",
to: "map <string, any>"
)

value =
if !rd.key?(res)
nil
elsif Ops.get_string(_MergeTypes, i, "map") == "map"
Ops.get_map(rd, res, {})
else
Ops.get_list(rd, res, [])
end
Ops.set(@current, res, value) if value
i = Ops.add(i, 1)
end
else
Ops.set(@current, resource, resource_data)
end
elsif s == 0
e = Builtins.add(e, resource)
end
end
end

Builtins.foreach(@current) do |k, v|
Ops.set(@current, k, v) if !Builtins.haskey(@current, k) && !Builtins.contains(e, k)
end
edit_profile

Popup.ClearFeedback
@prepare = false
nil
end

# Sets Profile#current to exported values created from given set of modules
# @return [Hash] value set to Profile#current
def create(modules)
Popup.Feedback(
_("Collecting configuration data..."),
_("This may take a while")
) do
@current = {}
edit_profile(modules)
end

@current
end

# Reset profile to initial status
# @return [void]
def Reset
Expand Down Expand Up @@ -824,6 +779,46 @@ def resource_aliases_map
Yast.import "Y2ModuleConfig"
Y2ModuleConfig.resource_aliases_map
end

# Edits profile for given modules. If nil is passed, it used GetModfied method.
def edit_profile(modules = nil)
@ModuleMap.each_pair do |name, module_map|
#
# Set resource name, if not using default value
#
resource = module_map["X-SuSE-YaST-AutoInstResource"] || name
tomerge = module_map["X-SuSE-YaST-AutoInstMerge"] || ""
# TODO: is none valid or better use exception?
module_auto = module_map["X-SuSE-YaST-AutoInstClient"] || "none"
export = if modules
modules.include?(resource) || modules.include?(name)
else
WFM.CallFunction(module_auto, ["GetModified"])
end
next unless export

resource_data = WFM.CallFunction(module_auto, ["Export"])

if tomerge == ""
s = (resource_data || {}).size
if s > 0
@current[resource] = resource_data
else
@current.delete(resource)
end
else
tomerge.split(",").each_with_index do |res, _i|
value = resource_data[res]
if !value
log.warn "key #{res} expected to be exported from #{resource}"
next
end
# FIXME: no deleting for merged keys, is it correct?
@current[res] = value unless value.empty?
end
end
end
end
end

Profile = ProfileClass.new
Expand Down
93 changes: 41 additions & 52 deletions test/autoinst_clone_test.rb
Expand Up @@ -62,6 +62,26 @@
"X-SuSE-YaST-Group" => "System",
"X-SuSE-DocTeamID" => "ycc_org.opensuse.yast.Bootloader",
"X-SuSE-YaST-AutoInstClient" => "bootloader_auto"
},
"software" => {
"Name" => "Software",
"Icon" => "yast-sw_single",
"X-SuSE-YaST-AutoInst" => "configure",
"X-SuSE-YaST-AutoInstResource" => "software",
"X-SuSE-YaST-AutoInstClonable" => "true",
"X-SuSE-YaST-Group" => "System",
"X-SuSE-DocTeamID" => "ycc_org.opensuse.yast.Software",
"X-SuSE-YaST-AutoInstClient" => "software_auto"
},
"storage" => {
"Name" => "Storage",
"Icon" => "yast-storage",
"X-SuSE-YaST-AutoInst" => "configure",
"X-SuSE-YaST-AutoInstResource" => "storage",
"X-SuSE-YaST-AutoInstClonable" => "true",
"X-SuSE-YaST-Group" => "System",
"X-SuSE-DocTeamID" => "ycc_org.opensuse.yast.Storage",
"X-SuSE-YaST-AutoInstClient" => "storage_auto"
}
}
end
Expand All @@ -82,90 +102,58 @@
end

describe "#Process" do
let(:resource_map) { module_map["add-on"] }
let(:initial_stage) { false }

before do
allow(subject).to receive(:CommonClone)
allow(Yast::Profile).to receive(:Prepare)
allow(Yast::Profile).to receive(:create)
allow(Yast::Call).to receive(:Function)
allow(Yast::Stage).to receive(:initial).and_return(initial_stage)
subject.additional = ["add-on"]
end

it "sets Mode to 'autoinst_config'" do
expect { subject.Process }.to change { Yast::Mode.mode }.from("normal").to("autoinst_config")
end

it "clones 'additional' modules" do
expect(subject).to receive(:CommonClone).with(module_map["add-on"])
it "reads the module settings" do
expect(Yast::Call).to receive(:Function).with("add-on_auto", ["Read"])
subject.Process
end

it "imports 'general' settings when general is in additional modules" do
subject.additional = ["general"]
expect(Yast::Call).to receive(:Function).with("general_auto", ["Import", Hash])
expect(Yast::Call).to receive(:Function).with("general_auto", ["SetModified"])
subject.Process
end

it "asks the profile to 'prepare'" do
expect(Yast::Profile).to receive(:Reset)
expect(Yast::Profile).to receive(:prepare=).with(true)
expect(Yast::Profile).to receive(:Prepare)
it "creates profile with additional modules" do
subject.additional = ["general"]
expect(Yast::Profile).to receive(:create).with(["general"])
subject.Process
end
end

describe "#CommonClone" do
let(:resource_map) { module_map["add-on"] }
let(:initial_stage) { false }

before do
allow(Yast::Call).to receive(:Function)
allow(Yast::Stage).to receive(:initial).and_return(initial_stage)
end

it "returns true" do
expect(subject.send(:CommonClone, resource_map)).to eq(true)
end

it "reads the module settings" do
expect(Yast::Call).to receive(:Function).with("add-on_auto", ["Read"])
subject.send(:CommonClone, resource_map)
end

it "sets the module as modified" do
expect(Yast::Call).to receive(:Function).with("add-on_auto", ["SetModified"])
subject.send(:CommonClone, resource_map)
end

context "on 1st stage" do
let(:initial_stage) { true }

it "does not read the module settings" do
expect(Yast::Call).to_not receive(:Function).with(anything, ["Read"])
subject.send(:CommonClone, resource_map)
end

it "sets the module as modified" do
expect(Yast::Call).to receive(:Function).with("add-on_auto", ["SetModified"])
subject.send(:CommonClone, resource_map)
subject.Process
end

context "when cloning the software module" do
let(:resource_map) do
{ "X-SuSE-YaST-AutoInstClient" => "software_auto" }
end

it "reads the module settings" do
subject.additional = ["software"]
expect(Yast::Call).to receive(:Function).with("software_auto", ["Read"])
subject.send(:CommonClone, resource_map)
subject.Process
end
end

context "when cloning the storage module" do
let(:resource_map) do
{ "X-SuSE-YaST-AutoInstClient" => "storage_auto" }
end

it "reads the module settings" do
subject.additional = ["storage"]
expect(Yast::Call).to receive(:Function).with("storage_auto", ["Read"])
subject.send(:CommonClone, resource_map)
subject.Process
end
end
end
Expand Down Expand Up @@ -206,15 +194,16 @@
).and_return("Add-On (translated)")

allow(Yast::Builtins)
.to receive(:dpgettext)
.with("desktop_translations", "/usr/share/locale/", /Bootloader/) { |*a| a.last }
.to receive(:dpgettext) { |*a| a.last }
end

it "returns a list of modules to clone with translated names" do
modules = subject.createClonableList
items = modules.map(&:params)
names = items.map(&:last)
expect(names).to eq(["Add-On (translated)", "YaST Boot Loader"])
expect(names).to match_array(
["Software", "Storage", "YaST Add-On Products", "YaST Boot Loader"]
)
end
end
end

0 comments on commit dfbed59

Please sign in to comment.