Skip to content

Commit

Permalink
add tests for addon selection dialogs and bunch of small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Jun 6, 2014
1 parent 11282af commit 0d5d7fe
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/clients/inst_scc.rb
Expand Up @@ -678,7 +678,7 @@ def init_registration

# helper method for accessing the registered addons
def registered_addons
::Registration::Storage::Cache.instance.registered_addons
Registration::Addon.registereds
end

end unless defined?(InstSccClient)
Expand Down
44 changes: 44 additions & 0 deletions test/addon_selection_dialog_test.rb
@@ -0,0 +1,44 @@
require_relative "spec_helper"
require "registration/ui/addon_selection_dialog"

describe Registration::UI::AddonSelectionDialog do
subject { Registration::UI::AddonSelectionDialog }

before(:each) do
# generic UI stubs for the wizard dialog
stub_const("Yast::UI", double())
allow(Yast::UI).to receive(:WizardCommand)
allow(Yast::UI).to receive(:WidgetExists).and_return(true)
allow(Yast::UI).to receive(:ChangeWidget)
allow(Yast::UI).to receive(:SetFocus)
allow(Yast::UI).to receive(:ReplaceWidget)
allow(Yast::UI).to receive(:TextMode).and_return(false)

addon_reset_cache
end

describe ".run" do
it "returns response from addon selection according to pressed button" do
expect(Yast::UI).to receive(:UserInput).and_return(:abort)
registration = double(:get_addon_list => [])
expect(subject.run(registration)).to eq :abort
end

it "returns `:skip` if no addon is selected and user click next" do
expect(Yast::UI).to receive(:UserInput).and_return(:next)
registration = double(:get_addon_list => [])
expect(subject.run(registration)).to eq :skip
end

it "returns `:next` if some addons are selected and user click next" do
test_addon = addon_generator
expect(Yast::UI).to receive(:UserInput).and_return(test_addon.product_ident, :next)
# mock that widget is selected
expect(Yast::UI).to receive(:QueryWidget).
with(Yast::Term.new(:id, test_addon.product_ident), :Value).
and_return(true)
registration = double(:get_addon_list => [test_addon])
expect(subject.run(registration)).to eq :next
end
end
end
62 changes: 6 additions & 56 deletions test/addon_spec.rb
Expand Up @@ -7,19 +7,8 @@

describe Registration::Addon do

# add cache reset, which is not needed in common case
class Registration::Addon
class << self
def reset_cache
@cached_addons = nil
@registereds = nil
@selecteds = nil
end
end
end

before(:each) do
Registration::Addon.reset_cache
addon_reset_cache
end

def product_generator(attrs = {})
Expand All @@ -38,29 +27,27 @@ def product_generator(attrs = {})
end

subject(:addon) do
Registration::Addon.new(SUSE::Connect::Product.new(product_generator))
Registration::Addon.new(addon_generator)
end

describe ".find_all" do
it "find all addons for current base product" do
prod1 = SUSE::Connect::Product.new(product_generator)
prod2 = SUSE::Connect::Product.new(product_generator)
prod1 = addon_generator
prod2 = addon_generator
registration = double(:get_addon_list => [prod1, prod2])

expect(Registration::Addon.find_all(registration).size).to be 2
end

it "find even dependend products" do
prod_child = product_generator
prod1 = SUSE::Connect::Product.new(product_generator('extensions' => [prod_child]))
prod1 = addon_with_child_generator
registration = double(:get_addon_list => [prod1])

expect(Registration::Addon.find_all(registration).size).to be 2
end

it "sets properly dependencies between addons" do
prod_child = product_generator
prod1 = SUSE::Connect::Product.new(product_generator('extensions' => [prod_child]))
prod1 = addon_with_child_generator
registration = double(:get_addon_list => [prod1])

addons = Registration::Addon.find_all(registration)
Expand Down Expand Up @@ -113,42 +100,5 @@ def product_generator(attrs = {})
it "do nothing if addon is not selected" do
expect{addon.unselected}.to_not raise_error
end

end
end

=begin
describe Registration::AddonSorter do
describe ".registration_order" do
it "returns registration order according to the dependencies" do
addon1 = Registration::Addon.new("SUSE_ADDON1", "12", "x86_64")
addon2 = Registration::Addon.new("SUSE_ADDON2", "12", "x86_64")
addon3 = Registration::Addon.new("SUSE_ADDON3", "12", "x86_64", depends_on: [addon1, addon2])
addon4 = Registration::Addon.new("SUSE_ADDON4", "12", "x86_64")
addon5 = Registration::Addon.new("SUSE_ADDON5", "12", "x86_64")
addon6 = Registration::Addon.new("SUSE_ADDON6", "12", "x86_64", depends_on: [addon4, addon5])
addon7 = Registration::Addon.new("SUSE_ADDON7", "12", "x86_64", depends_on: [addon3, addon6])
# deliberately use an order which does not follow dependencies to make sure it is changed
addons = [addon7, addon2, addon3, addon5, addon4, addon6, addon1]
solved = Registration::AddonSorter.registration_order(addons)
# check the order, iterate over the list and check for missing dependencies
registered = []
solved.each do |a|
# check that all dependendent add-ons are already registered
expect(a.depends_on - registered).to be_empty
registered << a
end
end
it "raises KeyError exception when there is an unresolved dependency" do
addon1 = Registration::Addon.new("SUSE_ADDON1", "12", "x86_64")
addon2 = Registration::Addon.new("SUSE_ADDON3", "12", "x86_64", depends_on: [addon1])
expect{Registration::AddonSorter.registration_order([addon2])}.to raise_error(KeyError)
end
end
end
=end
41 changes: 41 additions & 0 deletions test/factories.rb
@@ -0,0 +1,41 @@
require "registration/addon"
require "suse/connect"

def suse_connect_product_generator(attrs={})
params = {}
params['name'] = attrs['name'] || "Product#{rand(100000)}"
params['long_name'] = attrs['long_name'] || "The best cool #{params['name']}"
params['description'] = attrs['description'] || "Bla bla bla bla!"
params['zypper_name'] = attrs['zypper_name'] || "prod#{rand(100000)}"
params['zypper_version'] = attrs['version'] || "#{rand(13)}"
params['arch'] = attrs['arch'] || "x86_64"
params['free'] = attrs.fetch('free', true)
params['eula_url'] = attrs['eula_url']
params["extensions"] = attrs['extensions'] || []

return params
end

def addon_generator(params={})
SUSE::Connect::Product.new(suse_connect_product_generator(params))
end

def addon_with_child_generator(parent_params={})
prod_child = suse_connect_product_generator
SUSE::Connect::Product.new(suse_connect_product_generator(parent_params.merge('extensions' => [prod_child])))
end

# add cache reset, which is not needed in runtime, but for test it is critical
class Registration::Addon
class << self
def reset_cache
@cached_addons = nil
@registereds = nil
@selecteds = nil
end
end
end

def addon_reset_cache
Registration::Addon.reset_cache
end
4 changes: 4 additions & 0 deletions test/spec_helper.rb
Expand Up @@ -14,10 +14,14 @@
SimpleCov.start
end


$:.unshift(File.expand_path("../../src/lib", __FILE__))

ENV["Y2DIR"] = File.expand_path("../../src", __FILE__)

def fixtures_file(file)
File.expand_path(File.join("../fixtures", file), __FILE__)
end

# load data generators
require_relative "factories"

0 comments on commit 0d5d7fe

Please sign in to comment.