From e0cfd505afd1874b5941585b1cf910859befdfd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Imobach=20Gonz=C3=A1lez=20Sosa?= Date: Mon, 13 Feb 2017 16:07:11 +0000 Subject: [PATCH] Simplify InstUpdateInstaller tests --- test/inst_update_installer_test.rb | 350 ++------------------ test/lib/update_repositories_finder_test.rb | 7 +- test/support/fake_registration.rb | 11 + 3 files changed, 38 insertions(+), 330 deletions(-) create mode 100644 test/support/fake_registration.rb diff --git a/test/inst_update_installer_test.rb b/test/inst_update_installer_test.rb index d89510dd4..1dc6d5994 100755 --- a/test/inst_update_installer_test.rb +++ b/test/inst_update_installer_test.rb @@ -1,20 +1,11 @@ #!/usr/bin/env rspec require_relative "test_helper" +require_relative "./support/fake_registration" require "installation/clients/inst_update_installer" require "singleton" describe Yast::InstUpdateInstaller do - # Registration::Storage::InstallationOptions fake - class FakeInstallationOptions - include Singleton - attr_accessor :custom_url - end - - class FakeRegConfig - include Singleton - def import(_args); end - end Yast.import "Linuxrc" Yast.import "ProductFeatures" @@ -37,27 +28,27 @@ def import(_args); end let(:ay_profile) { double("Yast::Profile", current: profile) } let(:ay_profile_location) { double("Yast::ProfileLocation") } let(:finder) { ::Installation::UpdateRepositoriesFinder.new } + let(:update) { double("update", uri: URI(real_url)) } + let(:updates) { [update] } before do allow(::Installation::UpdateRepositoriesFinder).to receive(:new).and_return(finder) allow(Yast::GetInstArgs).to receive(:going_back).and_return(false) - allow(Yast::Pkg).to receive(:GetArchitecture).and_return(arch) - allow(Yast::Mode).to receive(:auto).and_return(false) allow(Yast::NetworkService).to receive(:isNetworkRunning).and_return(network_running) allow(::Installation::UpdatesManager).to receive(:new).and_return(manager) allow(Yast::Installation).to receive(:restarting?).and_return(restarting) - allow(Yast::Installation).to receive(:finish_restarting!) allow(Yast::Installation).to receive(:restart!) { :restart_yast } + allow(finder).to receive(:updates).and_return(updates) allow(subject).to receive(:require).with("registration/url_helpers").and_raise(LoadError) - allow(::FileUtils).to receive(:touch) stub_const("Registration::Storage::InstallationOptions", FakeInstallationOptions) stub_const("Registration::Storage::Config", FakeRegConfig) + # skip the libzypp initialization globally, enable in the specific tests allow(subject).to receive(:initialize_packager).and_return(true) allow(subject).to receive(:finish_packager) - allow(finder).to receive(:add_installation_repo) allow(subject).to receive(:fetch_profile).and_return(ay_profile) allow(subject).to receive(:process_profile) + allow(finder).to receive(:add_installation_repo) # stub the Profile module to avoid dependency on autoyast2-installation stub_const("Yast::Profile", ay_profile) @@ -114,7 +105,7 @@ def import(_args); end subject.main end - context "when update URL is configured in control.xml" do + context "when some update is available" do before do allow(Yast::ProductFeatures).to receive(:GetStringFeature).and_return(url) end @@ -188,7 +179,7 @@ def import(_args); end end context "and self-update URL is not remote" do - let(:url) { "cd:/?device=sr0" } + let(:real_url) { "cd:/?device=sr0" } it "shows a dialog suggesting to check the network configuration" do expect(Yast::Popup).to_not receive(:YesNo) @@ -199,308 +190,6 @@ def import(_args); end end end - context "when an URL is specified through Linuxrc" do - let(:custom_url) { "http://example.net/sles12/" } - - before do - allow(Yast::Linuxrc).to receive(:InstallInf).with("SelfUpdate").and_return(custom_url) - end - - it "tries to update the installer using the given URL" do - expect(manager).to receive(:add_repository).with(URI(custom_url)).and_return(true) - expect(manager).to receive(:apply_all) - allow(::FileUtils).to receive(:touch) - expect(subject.main).to eq(:restart_yast) - end - - it "shows an error if update is not found" do - expect(Yast::Popup).to receive(:Error) - expect(manager).to receive(:add_repository).with(URI(custom_url)) - .and_raise(::Installation::UpdatesManager::NotValidRepo) - expect(subject.main).to eq(:next) - end - end - - context "when no URL is specified through Linuxrc" do - before do - allow(Yast::ProductFeatures).to receive(:GetStringFeature).and_return(url) - end - - context "in standard installation" do - it "gets URL from control file" do - allow(::FileUtils).to receive(:touch) - expect(manager).to receive(:add_repository).with(URI(real_url)).and_return(true) - expect(subject.main).to eq(:restart_yast) - end - - it "does not show an error if update is not found" do - expect(Yast::Popup).to_not receive(:Error) - expect(manager).to receive(:add_repository).with(URI(real_url)) - .and_raise(::Installation::UpdatesManager::NotValidRepo) - expect(subject.main).to eq(:next) - end - - context "and control file doesn't have an URL" do - let(:url) { "" } - - it "does not update the installer" do - expect(subject).to_not receive(:update_installer) - end - end - end - - context "when a SCC/SMT server defines the URL" do - let(:smt0) { double("service", slp_url: "http://update.suse.com") } - let(:smt1) { double("service", slp_url: "http://update.example.net") } - - let(:update0) do - OpenStruct.new( - name: "SLES-12-Installer-Updates-0", - url: "http://update.suse.com/updates/sle12/12.2" - ) - end - - let(:update1) do - OpenStruct.new( - name: "SLES-12-Installer-Updates-1", - url: "http://update.suse.com/updates/sles12/12.2" - ) - end - - let(:regservice_selection) { Class.new } - - let(:url_helpers) { double("url_helpers", registration_url: smt0.slp_url, slp_discovery: []) } - let(:regurl) { nil } - - let(:registration) { double("registration", url: smt0.slp_url) } - let(:registration_class) { double("registration_class", new: registration) } - - let(:updates) { [update0, update1] } - - before do - # Load registration libraries - allow(subject).to receive(:require).with("registration/url_helpers") - .and_return(true) - allow(subject).to receive(:require).with("registration/registration") - .and_return(true) - allow(subject).to receive(:require).with("registration/ui/regservice_selection_dialog") - .and_return(true) - stub_const("Registration::Registration", registration_class) - stub_const("Registration::UrlHelpers", url_helpers) - stub_const("Registration::UI::RegserviceSelectionDialog", regservice_selection) - - allow(url_helpers).to receive(:service_url) { |u| u } - allow(url_helpers).to receive(:boot_reg_url).and_return(regurl) - allow(registration).to receive(:get_updates_list).and_return(updates) - allow(manager).to receive(:add_repository).and_return(true) - allow(File).to receive(:write) - end - - it "initializes the package management" do - # override the global stubs - expect(subject).to receive(:initialize_packager).and_call_original - expect(finder).to receive(:add_installation_repo).and_call_original - - url = "cd:///" - expect(Yast::Pkg).to receive(:SetTextLocale) - expect(Yast::Pkg).to receive(:TargetInitialize).with("/") - expect(Yast::Packages).to receive(:ImportGPGKeys) - expect(Yast::InstURL).to receive(:installInf2Url).and_return(url) - expect(Yast::Pkg).to receive(:SourceCreateBase).with(url, "").and_return(0) - - # just a shortcut to avoid mocking the whole update - allow(subject).to receive(:update_installer).and_return(false) - subject.main - end - - it "tries to update the installer using the given URL" do - expect(manager).to receive(:add_repository).with(URI(update0.url)) - .and_return(true) - expect(manager).to receive(:add_repository).with(URI(update1.url)) - .and_return(true) - expect(subject.main).to eq(:restart_yast) - end - - context "when the registration server returns empty URL list or fails" do - before do - allow(registration).to receive(:get_updates_list).and_return([]) - end - - it "displays a warning about using the default URL from control.xml" do - expect(Yast::Report).to receive(:LongWarning) - .with(/#{Regexp.escape("http://update.opensuse.org/x86_64/update.dud")}/) - subject.main - end - end - - context "when more than one SMT server exist" do - before do - allow(url_helpers).to receive(:slp_discovery).and_return([smt0, smt1]) - end - - context "if the user selects a SMT server" do - before do - allow(regservice_selection).to receive(:run).and_return(smt0) - end - - it "asks that SMT server for the updates URLs" do - expect(registration_class).to receive(:new).with(smt0.slp_url) - .and_return(registration) - allow(manager).to receive(:add_repository) - subject.main - end - - it "saves the registration URL to be used later" do - allow(manager).to receive(:add_repository) - expect(FakeInstallationOptions.instance).to receive(:custom_url=).with(smt0.slp_url) - .and_call_original - expect(File).to receive(:write).with(/\/inst_update_installer.yaml\z/, - { "custom_url" => smt0.slp_url }.to_yaml) - subject.main - end - end - - context "if user cancels the dialog" do - before do - allow(regservice_selection).to receive(:run).and_return(:cancel) - allow(manager).to receive(:add_repository) # it will use the default URL - end - - it "does not search for updates" do - expect(registration).to_not receive(:get_updates_list) - subject.main - end - end - - context "if users selects the SCC server" do - before do - allow(regservice_selection).to receive(:run).and_return(:scc) - end - - it "asks the SCC server for the updates URLs" do - expect(registration_class).to receive(:new).with(nil) - .and_return(registration) - allow(manager).to receive(:add_repository) - subject.main - end - - it "does not save the registration URL to be used later" do - allow(manager).to receive(:add_repository) - allow(registration).to receive(:url).and_return(nil) - expect(FakeInstallationOptions.instance).to receive(:custom_url=).with(nil) - expect(File).to_not receive(:write).with(/inst_update_installer.yaml/, anything) - subject.main - end - end - - context "when a regurl was specified via Linuxrc" do - let(:regurl) { "http://regserver.example.net" } - - it "uses the given server" do - expect(registration_class).to receive(:new).with(regurl) - .and_return(registration) - subject.main - end - end - end - - context "when a registration configuration is specified via AutoYaST profile" do - let(:reg_server_url) { "http://ay.test.example.com/update" } - let(:profile) { { "suse_register" => { "reg_server" => reg_server_url } } } - - before do - allow(Yast::Mode).to receive(:auto).at_least(1).and_return(true) - end - - it "uses the given server" do - expect(registration_class).to receive(:new).with(reg_server_url) - .and_return(registration) - subject.main - end - - it "imports profile settings into registration configuration" do - allow(manager).to receive(:add_repository) - expect(FakeRegConfig.instance).to receive(:import).with(profile["suse_register"]) - subject.main - end - end - end - - context "in AutoYaST installation or upgrade" do - let(:profile_url) { "http://ay.test.example.com/update" } - let(:profile) { { "general" => { "self_update_url" => profile_url } } } - - before do - expect(Yast::Mode).to receive(:auto).at_least(1).and_return(true) - allow(::FileUtils).to receive(:touch) - end - - it "tries to process the profile from the given URL" do - expect(subject).to receive(:process_profile) - expect(manager).to receive(:add_repository).with(URI(profile_url)) - .and_return(true) - - subject.main - end - - context "the profile defines the update URL" do - it "gets the URL from AutoYaST profile" do - expect(manager).to receive(:add_repository).with(URI(profile_url)) - .and_return(true) - subject.main - end - - it "returns :restart_yast" do - allow(manager).to receive(:add_repository).with(URI(profile_url)) - .and_return(true) - expect(subject.main).to eq(:restart_yast) - end - - it "shows an error and returns :next if update fails" do - expect(Yast::Report).to receive(:Error) - expect(manager).to receive(:add_repository) - .and_raise(::Installation::UpdatesManager::CouldNotFetchUpdateFromRepo) - expect(subject.main).to eq(:next) - end - end - - context "the profile does not define the update URL" do - let(:profile_url) { nil } - - it "gets URL from control file" do - expect(manager).to receive(:add_repository).with(URI(real_url)) - .and_return(true) - expect(subject.main).to eq(:restart_yast) - end - - it "does not show an error if update is not found" do - expect(Yast::Report).to_not receive(:Error) - expect(manager).to receive(:add_repository).with(URI(real_url)) - .and_raise(::Installation::UpdatesManager::NotValidRepo) - expect(subject.main).to eq(:next) - end - - context "and control file doesn't have an URL" do - let(:url) { "" } - - it "does not update the installer" do - expect(subject).to_not receive(:update_installer) - expect(subject.main).to eq(:next) - end - end - end - - context "when update is disabled through the profile" do - let(:profile) { { "general" => { "self_update" => false } } } - - it "does not update the installer" do - expect(subject).to_not receive(:update_installer) - expect(subject.main).to eq(:next) - end - end - end - end - context "when network is not available" do let(:network_running) { false } @@ -531,7 +220,7 @@ def import(_args); end context "when restarting YaST2" do let(:restarting) { true } - let(:data_file_exists) { true } + let(:data_file_exists) { false } let(:smt_url) { "https://smt.example.net" } let(:registration_libs) { true } @@ -542,9 +231,12 @@ def import(_args); end allow(subject).to receive(:require_registration_libraries) .and_return(registration_libs) allow(File).to receive(:exist?).with(/installer_updated/).and_return(true) + allow(Yast::Installation).to receive(:restart!) end context "and data file is available" do + let(:data_file_exists) { true } + it "sets custom_url" do allow(File).to receive(:read).and_return("---\ncustom_url: #{smt_url}\n") expect(FakeInstallationOptions.instance).to receive(:custom_url=) @@ -554,8 +246,6 @@ def import(_args); end end context "and data file is not available" do - let(:data_file_exists) { false } - it "does not set custom_url" do expect(FakeInstallationOptions.instance).to_not receive(:custom_url=) subject.main @@ -564,12 +254,18 @@ def import(_args); end context "and yast2-registration is not available" do let(:registration_libs) { false } + let(:data_file_exists) { true } it "does not load custom_url" do expect(FakeInstallationOptions.instance).to_not receive(:custom_url=) subject.main end end + + it "finishes the restarting process" do + expect(Yast::Installation).to receive(:finish_restarting!) + subject.main + end end end @@ -579,8 +275,7 @@ def import(_args); end before do allow(Yast::Linuxrc).to receive(:InstallInf).with("Insecure").and_return(insecure) - allow(Yast::Linuxrc).to receive(:InstallInf).with("SelfUpdate") - .and_return(url) + allow(Yast::Linuxrc).to receive(:InstallInf).with("SelfUpdate").and_return(url) end context "when update works" do @@ -590,5 +285,12 @@ def import(_args); end expect(subject.update_installer).to eq(true) end end + + context "when update fails" do + it "returns false" do + allow(manager).to receive(:add_repository).and_return(false) + expect(subject.update_installer).to eq(false) + end + end end end diff --git a/test/lib/update_repositories_finder_test.rb b/test/lib/update_repositories_finder_test.rb index 8485e5211..36c4d33e7 100644 --- a/test/lib/update_repositories_finder_test.rb +++ b/test/lib/update_repositories_finder_test.rb @@ -1,17 +1,12 @@ #!/usr/bin/env rspec require_relative "../test_helper" +require_relative "../support/fake_registration" require "installation/update_repositories_finder" Yast.import "Linuxrc" describe Installation::UpdateRepositoriesFinder do - # Registration::Storage::InstallationOptions fake - class FakeInstallationOptions - include Singleton - attr_accessor :custom_url - end - describe "#updates" do let(:url_from_linuxrc) { nil } let(:url_from_control) { "http://update.opensuse.org/\$arch/42.2" } diff --git a/test/support/fake_registration.rb b/test/support/fake_registration.rb new file mode 100644 index 000000000..9f85f3624 --- /dev/null +++ b/test/support/fake_registration.rb @@ -0,0 +1,11 @@ +# Registration::Storage::InstallationOptions fake +class FakeInstallationOptions + include Singleton + attr_accessor :custom_url +end + +# Registration::Storage::Config fake +class FakeRegConfig + include Singleton + def import(_args); end +end