Skip to content

Commit

Permalink
Merge ba6b718 into 827a32f
Browse files Browse the repository at this point in the history
  • Loading branch information
fschueller committed Nov 12, 2018
2 parents 827a32f + ba6b718 commit 74e0dfa
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 28 deletions.
12 changes: 10 additions & 2 deletions features/activation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,33 @@ Feature: Test product activation
Scenario: System registration
Given I have a system with activated base product

Then the output should contain "Registered SLES 12"
Then the output should contain "Announcing system to SCC/Registration proxy ..."
And the output should contain "Activating SLES 12.3 x86_64 ..."
And the output should contain "-> Adding service to system ..."
And the output should contain "Registered SLES 12"
And the output should contain "To server: https://scc.suse.com"
And the output should contain "=========="
And the output should contain "Successfully registered system."


@skip-sles-12
Scenario: System registration
Given I have a system with activated base product

Then the output should contain "Registered SLES 15"
Then the output should contain "Announcing system to SCC/Registration proxy ..."
And the output should contain "Registered SLES 15"
And the output should contain "To server: https://scc.suse.com"
And the output should contain "=========="

And the output should contain "Registered sle-module-basesystem 15"
And the output should contain "To server: https://scc.suse.com"
And zypper should contain a service for sle-module-basesystem
And the output should contain "=========="

And the output should contain "Registered sle-module-server-applications 15"
And the output should contain "To server: https://scc.suse.com"
And zypper should contain a service for sle-module-server-applications
And the output should contain "=========="

And the output should contain "Successfully registered system."

Expand Down
8 changes: 7 additions & 1 deletion features/extension_activation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ Feature: Test extension/module activation
When System has free extension
And I run `SUSEConnect -d`
Then the exit status should be 0
And the output should contain "To server: https://scc.suse.com"
And the output should contain "Deactivating"
And the output should contain "-> Removing service from system ..."
And the output should contain "-> Removing release package ..."
And the output should contain "Deregistered"
And the output should contain "From server: https://scc.suse.com"
And the output should contain "=========="
And the output should contain "Cleaning up ..."
And the output should contain "Successfully deregistered system."
And It deregisters free extension
34 changes: 26 additions & 8 deletions lib/suse/connect/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,22 @@ def register!
# Only register recommended packages for base products
register_product_tree(show_product(product)) if product.isbase

log.info 'Successfully registered system.'
log.info "Successfully registered system.\n".log_green.bold
end

# Activate the product, add the service and install the release package
def register_product(product, install_release_package = true)
log.info "Activating #{product.identifier} #{product.version} #{product.arch} ..."
service = activate_product(product, @config.email)

log.info '-> Adding service to system ...'
System.add_service(service, !@config.no_zypper_refs)
Zypper.install_release_package(product.identifier) if install_release_package
if install_release_package
log.info '-> Installing release package ...'
Zypper.install_release_package(product.identifier)
end

print_success_message(product)
print_success_message(product, :register)
end

# Deregisters a whole system or a single product
Expand All @@ -60,8 +65,9 @@ def deregister!
deregister_product(product)
end
@api.deregister(system_auth)
log.info 'Cleaning up ...'
System.cleanup!
log.info 'Successfully deregistered system.'
log.info "Successfully deregistered system.\n".log_green.bold
end
end

Expand All @@ -83,6 +89,7 @@ def flatten_tree(tree)
#
# @returns: [Array] login, password tuple. Those credentials are given by SCC/Registration Proxy
def announce_system(distro_target = nil, instance_data_file = nil)
log.info "Announcing system to SCC/Registration proxy ...\n".bold
instance_data = System.read_file(instance_data_file) if instance_data_file
params = [token_auth(@config.token), distro_target, instance_data]
params.push(@config.namespace) if @config.namespace
Expand All @@ -94,6 +101,7 @@ def announce_system(distro_target = nil, instance_data_file = nil)
# Re-send the system's hardware details on SCC
#
def update_system(distro_target = nil, instance_data_file = nil)
log.info "Updating system details on SCC/Registration proxy ...\n".bold
instance_data = System.read_file(instance_data_file) if instance_data_file
params = [system_auth, distro_target, instance_data]
params.push(@config.namespace) if @config.namespace
Expand Down Expand Up @@ -213,10 +221,12 @@ def register_product_tree(product)

def deregister_product(product)
raise BaseProductDeactivationError if product == Zypper.base_product
log.info "Deactivating #{product.identifier} #{product.version} #{product.arch} ..."
service = deactivate_product product
remove_or_refresh_service(service)
log.info '-> Removing release package ...'
Zypper.remove_release_package product.identifier
print_success_message product, action: 'Deregistered'
print_success_message product, :deregister
end

# Announces the system to the server, receiving and storing its credentials.
Expand All @@ -240,17 +250,25 @@ def registered?
# Refreshing the service instead to remove the repos of deregistered product.
def remove_or_refresh_service(service)
if service.name == 'SMT_DUMMY_NOREMOVE_SERVICE'
log.info '-> Refreshing service ...'
Zypper.refresh_all_services
else
log.info '-> Removing service from system ...'
System.remove_service service
end
end

def print_success_message(product, action: 'Registered')
log.info "#{action} #{product.identifier} #{product.version} #{product.arch}"
def print_success_message(product, action)
if action == :register
log.info "\nRegistered #{product.identifier} #{product.version} #{product.arch}".bold
log.info "To server: #{@config.url}" if @config.url
else
log.info "\nDeregistered #{product.identifier} #{product.version} #{product.arch}".bold
log.info "From server: #{@config.url}" if @config.url
end
log.info "Rooted at: #{@config.filesystem_root}" if @config.filesystem_root
log.info "To server: #{@config.url}" if @config.url
log.info "Using E-Mail: #{@config.email}" if @config.email
log.info "==========\n"
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions lib/suse/connect/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ def log
def self.included(base)
base.extend self
end

def log_green
"\e[32m#{self}\e[0m"
end

def log_red
"\e[31m#{self}\e[0m"
end

def bold
"\e[1m#{self}\e[22m"
end
end
end
end
57 changes: 46 additions & 11 deletions spec/connect/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,20 @@
end

before do
SUSE::Connect::GlobalLogger.instance.log = string_logger
api_response = double('api_response')
allow(api_response).to receive_messages(body: { 'login' => 'lg', 'password' => 'pw' })
allow_any_instance_of(Api).to receive_messages(announce_system: api_response)
allow(subject).to receive_messages(token_auth: 'auth')
end

after { SUSE::Connect::GlobalLogger.instance.log = default_logger }

it 'reports about ongoing action' do
expect(string_logger).to receive(:info).with("\e[1mAnnouncing system to SCC/Registration proxy ...\n\e[22m")
subject.announce_system
end

it 'calls underlying api' do
allow(Zypper).to receive :write_base_credentials
expect_any_instance_of(Api).to receive :announce_system
Expand Down Expand Up @@ -121,10 +129,18 @@
subject { SUSE::Connect::Client.new(config) }

before do
SUSE::Connect::GlobalLogger.instance.log = string_logger
allow(subject).to receive_messages(system_auth: 'auth')
allow_any_instance_of(Api).to receive(:update_system)
end

after { SUSE::Connect::GlobalLogger.instance.log = default_logger }

it 'reports about ongoing action' do
expect(string_logger).to receive(:info).with("\e[1mUpdate system details on SCC/Registration proxy ...\n\e[22m")
subject.update_system
end

it 'calls underlying api' do
expect_any_instance_of(Api).to receive(:update_system).with('auth', nil, nil)
subject.update_system
Expand Down Expand Up @@ -323,7 +339,7 @@

it 'prints message on successful activation' do
expect(subject).to receive(:register_product).exactly(4).times
expect(string_logger).to receive(:info).with('Successfully registered system.')
expect(string_logger).to receive(:info).with("\e[1m\e[32mSuccessfully registered system.\n\e[0m\e[22m")
subject.register!
end
end
Expand Down Expand Up @@ -434,9 +450,13 @@
allow(System).to receive(:add_service)
allow(Zypper).to receive(:install_release_package)

expect(string_logger).to receive(:info).with('Registered SLES 15 x86_64')
expect(string_logger).to receive(:info).with('Activating SLES 15 x86_64 ...')
expect(string_logger).to receive(:info).with('-> Adding service to system ...')
expect(string_logger).to receive(:info).with('-> Installing release package ...')
expect(string_logger).to receive(:info).with("\e[1m\nRegistered SLES 15 x86_64\e[22m")
expect(string_logger).to receive(:info).with('To server: https://scc.suse.com')
expect(string_logger).to receive(:info).with('Using E-Mail: email@email.org.what.ever')
expect(string_logger).to receive(:info).with("==========\n")
subject.register_product(product)
end
end
Expand Down Expand Up @@ -575,7 +595,8 @@
before { allow(System).to receive(:cleanup!).and_return(true) }

it 'prints confirmation message' do
expect(string_logger).to receive(:info).with('Successfully deregistered system.')
expect(string_logger).to receive(:info).with('Cleaning up ...')
expect(string_logger).to receive(:info).with("\e[1m\e[32mSuccessfully deregistered system.\n\e[0m\e[22m")
subject
end
end
Expand Down Expand Up @@ -619,15 +640,25 @@
subject
end

# rubocop:disable RSpec/MultipleExpectations
it 'prints confirmation message' do
expect(string_logger).to receive(:info).with('Deregistered 4-2-Extension 83 x86_64')
expect(string_logger).to receive(:info).with('Deregistered 4-Extension 1337 x86_64')
expect(string_logger).to receive(:info).with('Deregistered 2-2-Recommended 83 x86_64')
expect(string_logger).to receive(:info).with('Deregistered 2-Recommended 15 x86_64')
expect(string_logger).to receive(:info).with('To server: https://scc.suse.com').exactly(4).times
expect(string_logger).to receive(:info).with('Successfully deregistered system.')
expect(string_logger).to receive(:info).with('Deactivating 4-2-Extension 83 x86_64 ...')
expect(string_logger).to receive(:info).with("\e[1m\nDeregistered 4-2-Extension 83 x86_64\e[22m")
expect(string_logger).to receive(:info).with('Deactivating 4-Extension 1337 x86_64 ...')
expect(string_logger).to receive(:info).with("\e[1m\nDeregistered 4-Extension 1337 x86_64\e[22m")
expect(string_logger).to receive(:info).with('Deactivating 2-2-Recommended 83 x86_64 ...')
expect(string_logger).to receive(:info).with("\e[1m\nDeregistered 2-2-Recommended 83 x86_64\e[22m")
expect(string_logger).to receive(:info).with('Deactivating 2-Recommended 15 x86_64 ...')
expect(string_logger).to receive(:info).with("\e[1m\nDeregistered 2-Recommended 15 x86_64\e[22m")
expect(string_logger).to receive(:info).with('-> Removing service from system ...').exactly(4).times
expect(string_logger).to receive(:info).with('-> Removing release package ...').exactly(4).times
expect(string_logger).to receive(:info).with('From server: https://scc.suse.com').exactly(4).times
expect(string_logger).to receive(:info).with("==========\n").exactly(4).times
expect(string_logger).to receive(:info).with('Cleaning up ...')
expect(string_logger).to receive(:info).with("\e[1m\e[32mSuccessfully deregistered system.\n\e[0m\e[22m")
subject
end
# rubocop:enable RSpec/MultipleExpectations
end

context 'for single product' do
Expand Down Expand Up @@ -659,8 +690,12 @@
it 'logs success' do
allow(System).to receive :remove_service
allow(Zypper).to receive :remove_release_package
expect(string_logger).to receive(:info).with('Deregistered SLES HA 12 x86_64')
expect(string_logger).to receive(:info).with('To server: https://scc.suse.com')
expect(string_logger).to receive(:info).with('Deactivating SLES HA 12 x86_64 ...')
expect(string_logger).to receive(:info).with('-> Removing service from system ...')
expect(string_logger).to receive(:info).with('-> Removing release package ...')
expect(string_logger).to receive(:info).with("\e[1m\nDeregistered SLES HA 12 x86_64\e[22m")
expect(string_logger).to receive(:info).with('From server: https://scc.suse.com')
expect(string_logger).to receive(:info).with("==========\n")
subject
end
end
Expand Down
24 changes: 18 additions & 6 deletions spec/connect/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,31 @@ def test_logging
end

describe SUSE::Connect::DefaultLogger do
it 'by default does not log debug messages' do
stringio = StringIO.new
default_logger = SUSE::Connect::DefaultLogger.new(stringio)
let(:stringio) { StringIO.new }
let(:default_logger) { SUSE::Connect::DefaultLogger.new(stringio) }

it 'by default does not log debug messages' do
default_logger.debug 'TEST'
expect(stringio.string).to eq ''
end

it 'logs only the message without extra data or formatting' do
stringio = StringIO.new
default_logger = SUSE::Connect::DefaultLogger.new(stringio)

default_logger.warn 'TEST'
expect(stringio.string).to eq "TEST\n"
end

it 'logs in green font' do
default_logger.info 'TEST'.log_green
expect(stringio.string).to eq "\e[32mTEST\e[0m\n"
end

it 'logs in red font' do
default_logger.info 'TEST'.log_red
expect(stringio.string).to eq "\e[31mTEST\e[0m\n"
end

it 'logs in bold font' do
default_logger.info 'TEST'.bold
expect(stringio.string).to eq "\e[1mTEST\e[22m\n"
end
end

0 comments on commit 74e0dfa

Please sign in to comment.