Skip to content

Commit

Permalink
add error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
lslezak committed Jun 15, 2015
1 parent d080349 commit 3d1a95d
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 44 deletions.
107 changes: 77 additions & 30 deletions src/lib/registration/ui/migration_repos_workflow.rb
Expand Up @@ -17,37 +17,36 @@ class MigrationReposWorkflow
Yast.import "Report"
Yast.import "Sequencer"

# run workflow for selecting the migration services
# run workflow for adding the migration services
def self.run
workflow = MigrationReposWorkflow.new
workflow.run
end

# the constructor
# @param repo_id [Fixnum] the repository ID with the media add-on
def initialize
textdomain "registration"

url = UrlHelpers.registration_url
self.registration = Registration.new(url)
self.registration_ui = RegistrationUI.new(registration)
self.registered_services = []
self.error = false
end

# The media add-on workflow is:
# The repositories migration workflow is:
#
# - find the add-on product resolvable from repo_id
# - register the base system if it is not registered yet
# - get available addons from SCC
# - find the matching add-on product from media
# - ask for a reg code if needed
# - register the add-on
# - find all installed products
# - ask the registration server for the available product migrations
# - user selects the migration target
# - the registered products are upgraded and new services/repositories
# are added to the system
# - return the migration summary to the caller
# @return [Hash] the result is a Hash with migration repositores details
# @example Example return value
# or error details
#
# @example Migration to the new services succeeded
# {
# # if aborted the other keys are missing
# # TODO: add error reporting as well
# aborted: false,
# # service details
# registered_services: [
# { name: "SUSE_Linux_Enterprise_Server_12_SP1_12.1_x86_64",
Expand Down Expand Up @@ -78,8 +77,22 @@ def initialize
# }]
# }
# ],
# # user requests manual repository adjustment
# manual_repo_selection: false
# }
#
# @example User aborted the migration
# {
# aborted: true
# }
#
# @example An error occured during registeration of migration products
# {
# error: true,
# # (translated) error description
# error_msg: _("Error in ...")
# }
#
def run
aliases = {
"find_products" => [->() { find_products }, true],
Expand All @@ -92,26 +105,36 @@ def run
"ws_start" => "find_products",
"find_products" => {
abort: :abort,
cancel: :cancel,
next: "load_migration_products"
},
"load_migration_products" => {
abort: :abort,
next: "select_migration_products"
abort: :abort,
cancel: :cancel,
next: "select_migration_products"
},
"select_migration_products" => {
abort: :abort,
next: "register_migration_products"
abort: :abort,
cancel: :cancel,
next: "register_migration_products"
},
"register_migration_products" => {
abort: :abort,
next: :next
cancel: :cancel,
abort: :abort,
next: :next
}
}

log.info "Starting migration repositories sequence"
ui = Yast::Sequencer.Run(aliases, sequence)
log.info "User input: #{ui}"
self.aborted = ui == :abort || ui == :cancel

begin
ui = Yast::Sequencer.Run(aliases, sequence)
log.info "User input: #{ui}"
self.aborted = !error && (ui == :abort || ui == :cancel)
rescue => e
log.error "Caught error: #{e.class}: #{e.message.inspect}, #{e.backtrace}"
set_error(e.message)
end

ret = client_result
log.info "Client result: #{ret}"
Expand All @@ -122,13 +145,13 @@ def run

attr_accessor :result, :products, :migrations, :registration,
:registration_ui, :selected_migrations, :registered_services,
:manual_repo_selection, :aborted
:manual_repo_selection, :aborted, :error, :error_msg

# check if the add-on repository provides a product resolvable
# @return [Symbol] workflow symbol (:next, :finish or :abort)
# find all installed products
# @return [Symbol] workflow symbol (:next or :abort)
def find_products
if !SwMgmt.init
Yast::Report.Error(Yast::Pkg.LastError)
set_error(Yast::Pkg.LastError)
return :abort
end

Expand All @@ -140,24 +163,30 @@ def find_products
end

if products.empty?
# Error
# TRANSLATORS: Error message
set_error(_("No installed product found."))
return :abort
end

:next
end

# load migration products for the installed products from the registration server
# @return [Symbol] workflow symbol (:next or :abort)
def load_migration_products
self.migrations = registration_ui.migration_products(products)

if migrations.empty?
# FIXME
# TRANSLATORS: Error message
set_error(_("No migration product found."))
return :abort
end

:next
end

# run the migration target selection dialog
# @return [Symbol] workflow symbol (:next or :abort)
def select_migration_products
dialog = MigrationSelectionDialog.new(migrations)
ret = dialog.run
Expand All @@ -171,6 +200,8 @@ def select_migration_products
ret
end

# upgrade the services to the new version
# @return [Symbol] workflow symbol (:next)
def register_migration_products
Yast::Popup.Feedback(RegistrationUI::CONTACTING_MESSAGE,
# TRANSLATORS: Progress label
Expand All @@ -187,12 +218,23 @@ def register_migration_products
:next
end

# format the client response
# @return [Hash] client response
# @see #run for examples
def client_result
# just a mininal result when aborted
# abort result
return { aborted: true } if aborted

# error result
if error
return {
error: true,
error_msg: error_msg
}
end

# success
{
aborted: false,
registered_services: registered_services.map do |service|
{
name: service.name,
Expand All @@ -210,6 +252,11 @@ def client_result
manual_repo_selection: manual_repo_selection
}
end

def set_error(msg)
self.error = true
self.error_msg = msg
end
end
end
end
33 changes: 19 additions & 14 deletions test/migration_repos_workflow_spec.rb
Expand Up @@ -9,28 +9,31 @@
before do
# Url of the registration server
allow(Registration::UrlHelpers).to receive(:registration_url)
# SwMgmt initialization
allow(Registration::SwMgmt).to receive(:init).and_return swmgmt_init
# Load source information
allow(Yast::Pkg).to receive(:SourceLoad)
end

context "if package management initialization fails" do
let(:swmgmt_init) { false }
it "aborts if package management initialization fails" do
msg = "Initialization failed"
expect(Registration::SwMgmt).to receive(:init).and_return(false)
expect(Yast::Pkg).to receive(:LastError).and_return(msg)

it "aborts" do
expect(Yast::Pkg).to receive(:LastError)
expect(Yast::Report).to receive(:Error)
expect(subject.run).to eq(aborted: true)
end
expect(subject.run).to eq(error: true, error_msg: msg)
end

it "handles any exception raised and reports an error" do
msg = "Something failed..."
expect(Yast::Sequencer).to receive(:Run).and_raise(msg)

expect(subject.run).to eq(error: true, error_msg: msg)
end

context "if package management initialization succeeds" do
let(:swmgmt_init) { true }
let(:migrations) { load_yaml_fixture("migation_to_sles12_sp1.yml") }
let(:migration_service) { load_yaml_fixture("migration_service.yml") }

before do
# Load source information
allow(Yast::Pkg).to receive(:SourceLoad)
expect(Registration::SwMgmt).to receive(:init).and_return(true)

# installed SLES12
expect(Registration::SwMgmt).to receive(:installed_products)
Expand All @@ -51,9 +54,11 @@
allow(Registration::SwMgmt).to receive(:service_repos).and_return([])
end

it "registeres the selected migration products" do
it "registers the selected migration products" do
ret = subject.run
expect(ret[:aborted]).to eq(false)
# no error reported
expect(ret).to_not have_key(:aborted)
expect(ret).to_not have_key(:error)
expect(ret[:registered_services].size).to eq(1)
service = ret[:registered_services].first
expect(service[:name]).to eq("SUSE_Linux_Enterprise_Server_12_SP1_12.1_x86_64")
Expand Down

0 comments on commit 3d1a95d

Please sign in to comment.