Skip to content

Commit

Permalink
load pulpcore db configuration by asking django, not by parsing python
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeni committed Oct 17, 2024
1 parent 3319775 commit 5ce4b6d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
19 changes: 11 additions & 8 deletions definitions/features/pulpcore_database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,21 @@ def services

private

# rubocop:disable Metrics/AbcSize
def load_configuration
full_config = File.read(PULPCORE_DB_CONFIG).split(/[\s,'":]/).reject(&:empty?)
python_command = <<~PYTHON.strip
from django.conf import settings; import json; print(json.dumps(settings.DATABASES["default"]))
PYTHON
manager_command = pulpcore_manager("shell --command '#{python_command}'")
manager_result = execute!(manager_command)
db_config = JSON.parse(manager_result)

@configuration = {}
@configuration['adapter'] = 'postgresql'
@configuration['host'] = full_config[full_config.index('HOST') + 1]
@configuration['port'] = full_config[full_config.index('PORT') + 1]
@configuration['database'] = full_config[full_config.index('NAME') + 1]
@configuration['username'] = full_config[full_config.index('USER') + 1]
@configuration['password'] = full_config[full_config.index('PASSWORD') + 1]
@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
end
# rubocop:enable Metrics/AbcSize
end
22 changes: 22 additions & 0 deletions test/definitions/features/pulpcore_database_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'test_helper'

describe Features::PulpcoreDatabase do
include DefinitionsTestHelper

subject { Features::PulpcoreDatabase.new }

describe '.configuration' do
it 'returns hash with DB config' do
expected_command = <<~CMD.strip
PULP_SETTINGS=/etc/pulp/settings.py runuser -u pulp -- pulpcore-manager shell --command 'from django.conf import settings; import json; print(json.dumps(settings.DATABASES["default"]))'
CMD
manager_return = <<~JSON
{"ENGINE": "django.db.backends.postgresql", "NAME": "pulpcore", "USER": "pulp", "PASSWORD": "password", "HOST": "remotedb", "PORT": "5432"}
JSON
subject.expects(:execute!).with(expected_command).returns(manager_return)
expected = { "adapter" => "postgresql", "host" => "remotedb", "port" => "5432",
"database" => "pulpcore", "username" => "pulp", "password" => "password" }
assert_equal expected, subject.configuration
end
end
end

0 comments on commit 5ce4b6d

Please sign in to comment.