Skip to content

Commit

Permalink
Fix for rubocop issues
Browse files Browse the repository at this point in the history
Fixing the rubocop issues introduced in the previous commit and other
commits.

[#183572544] Investigate possible NATS 2.0 race condition

Signed-off-by: Kenneth Lakin <[email protected]>
  • Loading branch information
danielfor authored and klakin-pivotal committed Oct 24, 2022
1 parent 80b9376 commit 23e6dc8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
20 changes: 14 additions & 6 deletions src/bosh-nats-sync/lib/nats_sync/users_sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ def execute_users_sync
vms_uuids = query_all_running_vms
rescue RuntimeError => e
NATSSync.logger.error "Could not query all running vms: #{e.message}"
overwriteable_config_file = is_the_user_file_overwritable?
overwriteable_config_file = user_file_overwritable?
if overwriteable_config_file
NATSSync.logger.info "NATS config file is empty, writing basic users config file."
NATSSync.logger.info 'NATS config file is empty, writing basic users config file.'
else
NATSSync.logger.info "NATS config file is not empty, doing nothing."
NATSSync.logger.info 'NATS config file is not empty, doing nothing.'
end
end

Expand All @@ -33,23 +33,31 @@ def execute_users_sync
write_nats_config_file(vms_uuids, read_subject_file(@bosh_config['director_subject_file']),
read_subject_file(@bosh_config['hm_subject_file']))
new_file_hash = nats_file_hash
UsersSync.reload_nats_server_config(@nats_server_executable, @nats_server_pid_file) unless current_file_hash == new_file_hash
unless current_file_hash == new_file_hash
UsersSync.reload_nats_server_config(@nats_server_executable,
@nats_server_pid_file)
end
end
NATSSync.logger.info 'Finishing NATS Users Synchronization'
end

def self.reload_nats_server_config(nats_server_executable, nats_server_pid_file)
output, status = Open3.capture2e("#{nats_server_executable} --signal reload=#{nats_server_pid_file}")

# rubocop:disable Style/GuardClause
# rubocop:disable Layout/LineLength
unless status.success?
raise("Cannot execute: #{nats_server_executable} --signal reload=#{nats_server_pid_file}, Status Code: #{status} \nError: #{output}")
end
# rubocop:enable Style/GuardClause
# rubocop:enable Layout/LineLength
end

private

def is_the_user_file_overwritable?
def user_file_overwritable?
JSON.parse(File.read(@nats_config_file_path)).empty?
rescue
rescue RuntimeError, JSON::ParserError
true
end

Expand Down
21 changes: 10 additions & 11 deletions src/bosh-nats-sync/spec/nats_sync/users_sync_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module NATSSync
allow(logger).to receive(:debug)
allow(logger).to receive(:error)
allow(logger).to receive(:info)
allow(Open3).to receive(:capture2e).and_return(["Success", capture_status])
allow(Open3).to receive(:capture2e).and_return(['Success', capture_status])
end

subject { UsersSync.new(nats_config_file_path, bosh_config, nats_executable, nats_server_pid_file) }
Expand Down Expand Up @@ -144,9 +144,9 @@ module NATSSync

describe '#execute_nats_sync' do
before do
stub_request(:get, url + '/info')
stub_request(:get, "#{url}/info")
.to_return(status: 200, body: info_json)
stub_request(:get, url + '/deployments')
stub_request(:get, "#{url}/deployments")
.with(headers: { 'Authorization' => 'Bearer xyz' })
.to_return(status: 200, body: deployments_json)
allow(auth_provider).to receive(:new).and_return(auth_provider_double)
Expand All @@ -158,12 +158,12 @@ module NATSSync

describe 'when UAA is not deployed and the BOSH API is not available' do
before do
stub_request(:get, url + '/deployments')
stub_request(:get, "#{url}/deployments")
.with(headers: { 'Authorization' => 'Bearer xyz' })
.to_return(status: 401, body: "Unauthorized")
.to_return(status: 401, body: 'Unauthorized')
end

describe "and the authentication file is empty" do
describe 'and the authentication file is empty' do
it 'should write the basic bosh configuration' do
expect(JSON.parse(File.read(nats_config_file_path)).empty?).to be true
subject.execute_users_sync
Expand All @@ -177,7 +177,7 @@ module NATSSync
end
end

describe "and the authentication file is corrupted" do
describe 'and the authentication file is corrupted' do
before do
File.open(nats_config_file_path, 'w') do |f|
f.write('{invalidchar')
Expand All @@ -195,7 +195,7 @@ module NATSSync
end
end

describe "and the authentication file is not empty" do
describe 'and the authentication file is not empty' do
before do
File.open(nats_config_file_path, 'w') do |f|
f.write('{"authorization": {"users": [{"user": "foo"}]}}')
Expand All @@ -208,7 +208,6 @@ module NATSSync
expect(data_hash).to eq({ 'authorization' => { 'users' => [{ 'user' => 'foo' }] } })
end
end

end

describe 'when there are no deployments with running vms in Bosh' do
Expand All @@ -228,7 +227,7 @@ module NATSSync

describe 'when there are deployments with running vms in Bosh' do
before do
stub_request(:get, url + '/deployments/deployment-1/vms')
stub_request(:get, "#{url}/deployments/deployment-1/vms")
.with(headers: { 'Authorization' => 'Bearer xyz' })
.to_return(status: 200, body: vms_json)
end
Expand Down Expand Up @@ -323,7 +322,7 @@ module NATSSync
describe 'when reloading the NATs server fails' do
let(:capture_status) { instance_double(Process::Status, success?: false) }
before do
allow(Open3).to receive(:capture2e).and_return(["Failed to reload NATs server", capture_status])
allow(Open3).to receive(:capture2e).and_return(['Failed to reload NATs server', capture_status])
end

it 'should raise an error' do
Expand Down

0 comments on commit 23e6dc8

Please sign in to comment.