Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up load_configuration methods in *_database #946

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions definitions/features/candlepin_database.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require 'uri'
require 'cgi'

class Features::CandlepinDatabase < ForemanMaintain::Feature
CANDLEPIN_DB_CONFIG = '/etc/candlepin/candlepin.conf'.freeze

Expand All @@ -19,7 +22,7 @@ def services
end

def configuration
@configuration || load_configuration
@configuration ||= load_configuration
end

def check_option_using_cpdb_help(option_name, parent_cmd = '')
Expand All @@ -30,20 +33,23 @@ def check_option_using_cpdb_help(option_name, parent_cmd = '')

private

def raw_config
File.read(CANDLEPIN_DB_CONFIG)
end

def load_configuration
raw_config = File.read(CANDLEPIN_DB_CONFIG)
full_config = Hash[raw_config.scan(/(^[^#\n][^=]*)=(.*)/)]
uri_regexp = %r{://(([^/:]*):?([^/]*))/([^?]*)\??(ssl=([^&]*))?}
url = full_config['jpa.config.hibernate.connection.url']
uri = uri_regexp.match(url)
@configuration = {
uri = URI.parse(url.delete_prefix('jdbc:'))
query = uri.query ? CGI.parse(uri.query) : {}
{
'username' => full_config['jpa.config.hibernate.connection.username'],
'password' => full_config['jpa.config.hibernate.connection.password'],
'database' => uri[4],
'host' => uri[2],
'port' => uri[3] || '5432',
'ssl' => (fetch_extra_param(url, 'ssl') == 'true'),
'sslfactory' => fetch_extra_param(url, 'sslfactory'),
'database' => uri.path,
'host' => uri.host,
'port' => uri.port || '5432',
'ssl' => query['ssl']&.first == 'true',
'sslfactory' => query['sslfactory']&.first,
'driver_class' => full_config['jpa.config.hibernate.connection.driver_class'],
Comment on lines +51 to 53
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We never use these, so why do we have these?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pretty sure we don't need them and can drop 'em

'url' => url,
}
Expand All @@ -57,11 +63,4 @@ def extend_with_db_options
end
db_options
end

def fetch_extra_param(url, key_name)
query_string = url.split('?')[1]
return nil unless query_string
output = /#{key_name}=([^&]*)?/.match(query_string)
output[1] if output
end
end
7 changes: 3 additions & 4 deletions definitions/features/foreman_database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Features::ForemanDatabase < ForemanMaintain::Feature
end

def configuration
@configuration || load_configuration
@configuration ||= load_configuration
end

def config_files
Expand Down Expand Up @@ -41,8 +41,7 @@ def load_configuration
{ 'production' => {} }
end

@configuration = config['production']
@configuration['host'] ||= 'localhost'
@configuration
config['production']['host'] ||= 'localhost'
config['production']
end
end
18 changes: 9 additions & 9 deletions definitions/features/pulpcore_database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Features::PulpcoreDatabase < ForemanMaintain::Feature
end

def configuration
@configuration || load_configuration
@configuration ||= load_configuration
end

def services
Expand All @@ -34,13 +34,13 @@ def load_configuration
manager_result = execute!(manager_command)
db_config = JSON.parse(manager_result)

@configuration = {}
@configuration['adapter'] = 'postgresql'
@configuration['host'] = db_config['HOST']
@configuration['port'] = db_config['PORT']
@configuration['database'] = db_config['NAME']
@configuration['username'] = db_config['USER']
@configuration['password'] = db_config['PASSWORD']
@configuration
{
'adapter' => 'postgresql',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We never use this, so why do we need this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be dropped, I guess

'host' => db_config['HOST'],
'port' => db_config['PORT'],
'database' => db_config['NAME'],
'username' => db_config['USER'],
'password' => db_config['PASSWORD'],
}
end
end
40 changes: 20 additions & 20 deletions test/definitions/features/candlepin_database_test.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
require 'test_helper'
require 'minitest/stub_const'

describe Features::CandlepinDatabase do
include DefinitionsTestHelper
subject { Features::CandlepinDatabase.new }
let(:subject_ins) { Features::CandlepinDatabase.any_instance }

let(:cp_config_dir) do
File.expand_path('../../support', __dir__)
end

def stub_with_ssl_config(&block)
Features::CandlepinDatabase.stub_const(:CANDLEPIN_DB_CONFIG,
cp_config_dir + '/candlepin_with_ssl.conf', &block)
end

def stub_without_ssl_config(&block)
Features::CandlepinDatabase.stub_const(:CANDLEPIN_DB_CONFIG,
cp_config_dir + '/candlepin_without_ssl.conf', &block)
def stub_config(&block)
subject.stub(:raw_config, File.read(File.join(cp_config_dir, config)), &block)
end

describe '.configuration' do
it 'The url includes ssl attributes when ssl is enabled' do
stub_with_ssl_config do
url = subject.configuration['url']
assert_includes url, 'ssl=true'
assert_includes url, 'sslrootcert=/usr/share/foreman/root.crt'
let(:configuration) { subject.configuration }

describe 'with ssl' do
let(:config) { 'candlepin_with_ssl.conf' }

it 'sets ssl to true' do
stub_config do
assert_includes configuration['url'], 'ssl=true'
assert configuration['ssl']
end
end
end

it 'The url does not include ssl attributes when ssl is disabled' do
stub_without_ssl_config do
url = subject.configuration['url']
refute_includes url, 'ssl=true'
refute_includes url, 'sslrootcert=/usr/share/foreman/root.crt'
describe 'without ssl' do
let(:config) { 'candlepin_without_ssl.conf' }

it 'sets ssl to false' do
stub_config do
refute_includes configuration['url'], 'ssl=true'
refute configuration['ssl']
end
end
end
end
Expand Down
Loading