Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed May 28, 2020
1 parent a01aaf4 commit e17fe0e
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 31 deletions.
18 changes: 18 additions & 0 deletions src/lib/autoinstall/autosetup_helpers.rb
Expand Up @@ -159,6 +159,15 @@ def network_before_proposal?

# Defines convenience methods to check the existence and the content of
# the {Yast::Profile.current} sections
#
# @example networking section methods
#
# profile = { "networking" => { "setup_before_proposal" => true } }
# Yast::Profile.current = profile
#
# client.networking_section? #=> true
# client.networking_section["setup_before_proposal"] #=> true
# client.not_present_section? #=> false
def method_missing(method, *arguments, &block)
case method.to_s
when /(.+)_section$/
Expand All @@ -170,6 +179,15 @@ def method_missing(method, *arguments, &block)
end
end

def respond_to_missing?(method, _include_private = false)
case method.to_s
when /(.+)_section$/, /(.+)_section\?$/
true
else
false
end
end

private

# Backup AutoYaST profile
Expand Down
66 changes: 66 additions & 0 deletions test/lib/autosetup_helpers_test.rb
Expand Up @@ -297,6 +297,50 @@ class DummyClient < Yast::Client
end
end

describe "#method_missing" do
let(:profile) { general_section }
let(:general_section) { { "general" => { "semi-automatic" => ["networking"] } } }
let(:register_section) { { "suse_register" => { "reg_code" => "12345" } } }

before do
Yast::Profile.current = profile
end

context "when the method name matchs /(?<section_name>.*)_section/'" do
it "returns the profile section with the matched :section_name when present" do
expect(client.general_section).to eql(general_section["general"])
end

it "returns an empty hash when the section with the matched :section_name is not present" do
expect(client.networking_section).to eql({})
end
end

context "when the method name matchs /(?<section_name>.*)_section?/'" do
it "returns true if the profile section with the matched name is defined" do
expect(client.general_section?).to eql(true)
end

it "returns false if the matched section name is not defined in the profile" do
expect(client.suse_register_section?).to eql(false)
end
end
end

describe "#respond_to_missing?" do
it "returns true if the method name matchs /(?<section_name>.*)_section/'" do
expect(client.respond_to?("general_section")).to eql(true)
end

it "returns true if the method name matchs /(?<section_name>.*)_section?/'" do
expect(client.respond_to?("suse_register_section")).to eql(true)
end

it "returns false otherwise" do
expect(client.respond_to?("wrong_section_method_name")).to eql(false)
end
end

describe "#general_section" do
let(:profile) { general_section }
let(:general_section) { { "general" => { "semi-automatic" => ["networking"] } } }
Expand All @@ -320,6 +364,28 @@ class DummyClient < Yast::Client
end
end

describe "#general_section?" do
let(:profile) { { "general" => { "semi-automatic" => ["networking"] } } }

before do
Yast::Profile.current = profile
end

context "when the profile contains the section" do
it "returns true" do
expect(client.general_section?).to eql(true)
end
end

context "when the profile does not contain the section" do
let(:profile) { { "suse_register" => { "reg_code" => "12345" } } }

it "returns false" do
expect(client.general_section?).to eql(false)
end
end
end

describe "#semi_auto?" do
let(:profile) { general_section }
let(:general_section) { { "general" => { "semi-automatic" => ["networking"] } } }
Expand Down
103 changes: 72 additions & 31 deletions test/lib/clients/inst_autoinit_test.rb
Expand Up @@ -36,7 +36,6 @@
allow(Yast::AutoinstFunctions).to receive(:available_base_products).and_return([])
allow(Y2Packager::MediumType).to receive(:online?).and_return(true)
Yast::AutoinstConfig.ProfileInRootPart = false

end

describe "#run" do
Expand Down Expand Up @@ -95,40 +94,82 @@
subject.run
end

it "reports error for installation with full medium without specified product" do
allow(Y2Packager::MediumType).to receive(:online?).and_return(false)
allow(Y2Packager::MediumType).to receive(:offline?).and_return(true)
allow(Yast::Mode).to receive(:autoupgrade).and_return(false)

expect(Yast::Popup).to receive(:LongError)

expect(subject.run).to eq :abort
end
context "when using the Full medium" do
it "reports an error when the product is not specified" do
allow(Y2Packager::MediumType).to receive(:online?).and_return(false)
allow(Y2Packager::MediumType).to receive(:offline?).and_return(true)
allow(Yast::Mode).to receive(:autoupgrade).and_return(false)

it "registers system for installation with online medium" do
map = { "suse_register" => { "do_registration" => true } }
allow(Yast::Mode).to receive(:autoupgrade).and_return(false)
allow(Yast::Profile).to receive(:current).and_return(map)
expect(Yast::WFM).to receive(:CallFunction)
.with("scc_auto", ["Import", map["suse_register"]])
expect(Yast::WFM).to receive(:CallFunction).with("scc_auto", ["Write"])
# fake that registration is available to avoid build requires
allow(subject).to receive(:registration_module_available?).and_return(true)
allow(Yast::Profile).to receive(:remove_sections)
expect(Yast::Popup).to receive(:LongError)

subject.run
expect(subject.run).to eq :abort
end
end

it "reports error for installation with online medium without register section" do
map = { "suse_register" => { "do_registration" => false } }
allow(Yast::Mode).to receive(:autoupgrade).and_return(false)
allow(Yast::Profile).to receive(:current).and_return(map)
expect(Yast::WFM).to_not receive(:CallFunction)
.with("scc_auto", ["Import", map["suse_register"]])
expect(Yast::WFM).to_not receive(:CallFunction).with("scc_auto", ["Write"])
expect(Yast::Popup).to receive(:LongError)

expect(subject.run).to eq :abort
context "when using the Online medium for an installation" do
let(:do_registration) { false }
let(:setup_before_proposal) { false }
let(:profile) do
{
"suse_register" => { "do_registration" => do_registration },
"networking" => { "setup_before_proposal" => setup_before_proposal }
}
end

before do
allow(Yast::Mode).to receive(:autoupgrade).and_return(false)
allow(Yast::Profile).to receive(:current).and_return(profile)
end

context "and the network is requested to be configured before the proposal" do
let(:setup_before_proposal) { true }

it "configures the network" do
expect(subject).to receive(:autosetup_network)

subject.run
end
end

context "and the registration is disabled or not present in the profile" do
let(:do_registration) { false }

it "does not try to register the system" do
expect(Yast::WFM).to_not receive(:CallFunction)
.with("scc_auto", ["Import", profile["suse_register"]])
expect(Yast::WFM).to_not receive(:CallFunction).with("scc_auto", ["Write"])

subject.run
end

it "reports an error" do
expect(Yast::WFM).to_not receive(:CallFunction)
.with("scc_auto", ["Import", profile["suse_register"]])
expect(Yast::WFM).to_not receive(:CallFunction).with("scc_auto", ["Write"])
expect(Yast::Popup).to receive(:LongError)

subject.run
end

it "returns :abort" do
expect(subject.run).to eq :abort
end
end

context "and the registration is enabled according to the profile" do
let(:do_registration) { true }

it "registers system" do
expect(Yast::WFM).to receive(:CallFunction)
.with("scc_auto", ["Import", profile["suse_register"]])
expect(Yast::WFM).to receive(:CallFunction).with("scc_auto", ["Write"])
# fake that registration is available to avoid build requires
allow(subject).to receive(:registration_module_available?).and_return(true)
allow(Yast::Profile).to receive(:remove_sections)

subject.run
end
end
end

# TODO: more test for profile processing
Expand Down

0 comments on commit e17fe0e

Please sign in to comment.