Skip to content

Commit

Permalink
Merge pull request #440 from SUSE/write-config-even-during-exception
Browse files Browse the repository at this point in the history
Write Config Even During Failure Conditions
  • Loading branch information
tmuntaner committed Sep 2, 2020
2 parents 1c8c9c6 + a716036 commit 0b5188d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/suse/connect/cli.rb
Expand Up @@ -49,8 +49,6 @@ def execute! # rubocop:disable MethodLength, CyclomaticComplexity, PerceivedComp
Client.new(@config).register!
end
end

@config.write! if @config.write_config
rescue Errno::ECONNREFUSED
log.fatal "Error: Connection refused by server #{@config.url}"
exit 64
Expand Down Expand Up @@ -84,6 +82,8 @@ def execute! # rubocop:disable MethodLength, CyclomaticComplexity, PerceivedComp
rescue BaseProductDeactivationError
log.fatal 'Can not deregister base product. Use SUSEConnect -d to deactivate the whole system.'
exit 70
ensure
@config.write! if @config.write_config
end

private
Expand Down
1 change: 1 addition & 0 deletions lib/suse/connect/client.rb
Expand Up @@ -104,6 +104,7 @@ def announce_system(distro_target = nil, instance_data_file = nil)
params.push(@config.namespace) if @config.namespace

response = @api.announce_system(*params)

[response.body['login'], response.body['password']]
end

Expand Down
2 changes: 1 addition & 1 deletion lib/suse/connect/version.rb
@@ -1,5 +1,5 @@
module SUSE
module Connect
VERSION = '0.3.26'
VERSION = '0.3.27'
end
end
11 changes: 10 additions & 1 deletion package/SUSEConnect.changes
@@ -1,5 +1,14 @@
-------------------------------------------------------------------
------------------------------------------------------------------
Mon Aug 31 14:10:15 UTC 2020 - Thomas Muntaner <tmuntaner@suse.com>

- Update to 0.3.27
- SUSEConnect now ensures that it writes its configuration when it
encounters errors. This helps in the situation where SUSEConnect
announces itself, but fails during a later step. Without the saved
configuration, a system could have credentials, but be unsure which
registration proxy they're valid for.

-------------------------------------------------------------------
Wed Aug 26 14:37:17 UTC 2020 - Imobach Gonzalez Sosa <igonzalezsosa@suse.com>

- Update to 0.3.26
Expand Down
13 changes: 13 additions & 0 deletions spec/connect/cli_spec.rb
Expand Up @@ -169,6 +169,7 @@
cli = described_class.new(%w[-r 123 --instance-data /tmp/test --url test])
expect(string_logger).to receive(:error)
.with('Please use either --regcode or --instance-data')
expect_any_instance_of(SUSE::Connect::Config).to receive(:write!)
expect { cli.execute! }.to raise_error(SystemExit)
end

Expand All @@ -179,6 +180,18 @@
expect_any_instance_of(SUSE::Connect::Config).to receive(:write!)
cli.execute!
end

it 'writes config even when exceptions are raised' do
cli = described_class.new(%w[--url http://foo.test.com])
expect(cli.config.write_config).to eq true

response = double(code: 401, body: { 'localized_error' => 'Invalid foo' })
allow(System).to receive(:credentials?).and_return true
allow_any_instance_of(SUSE::Connect::Client).to receive(:register!).and_raise(ApiError.new(response))

expect_any_instance_of(SUSE::Connect::Config).to receive(:write!)
expect { cli.execute! }.to raise_error(SystemExit)
end
end

describe 'de-register command' do
Expand Down

0 comments on commit 0b5188d

Please sign in to comment.