-
Notifications
You must be signed in to change notification settings - Fork 73
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
pass PGPASSWORD via env directly, not via shell #939
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
require 'test_helper' | ||
|
||
class FakeDatabase | ||
include ForemanMaintain::Concerns::BaseDatabase | ||
include ForemanMaintain::Concerns::SystemHelpers | ||
end | ||
|
||
module ForemanMaintain | ||
describe Concerns::BaseDatabase do | ||
let(:db) { FakeDatabase.new } | ||
let(:local_config) do | ||
{ | ||
'host' => 'localhost', | ||
'database' => 'fakedb', | ||
'username' => 'fakeuser', | ||
'password' => 'fakepassword', | ||
} | ||
end | ||
let(:remote_config) do | ||
{ | ||
'host' => 'db.example.com', | ||
'database' => 'fakedb', | ||
'username' => 'fakeuser', | ||
'password' => 'fakepassword', | ||
} | ||
end | ||
|
||
it 'accepts localhost as local' do | ||
assert db.local?(local_config) | ||
end | ||
|
||
it 'accepts db.example.com as remote' do | ||
refute db.local?(remote_config) | ||
end | ||
|
||
it 'fetches server version' do | ||
db.expects(:ping).with(local_config).returns(true) | ||
db.expects(:execute!).with( | ||
'psql -h localhost -p 5432 -U fakeuser -d fakedb -c "SHOW server_version" -t -A', | ||
env: { "PGPASSWORD" => "fakepassword" } | ||
).returns('13.16') | ||
|
||
assert db.db_version(local_config) | ||
end | ||
|
||
it 'drops local db' do | ||
db.expects(:execute!).with("runuser - postgres -c 'dropdb fakedb'").returns('') | ||
|
||
assert db.dropdb(local_config) | ||
end | ||
|
||
it 'drops remote db' do | ||
select_statement = <<-SQL | ||
select string_agg('drop table if exists \"' || tablename || '\" cascade;', '') | ||
from pg_tables | ||
where schemaname = 'public'; | ||
SQL | ||
delete_statement = 'drop table if exists \"faketable\"' | ||
db.expects(:psql).with(select_statement).returns(delete_statement) | ||
db.expects(:psql).with(delete_statement).returns('') | ||
assert db.dropdb(remote_config) | ||
end | ||
|
||
it 'restores local db' do | ||
file = '/backup/fake.dump' | ||
|
||
db.expects(:execute!).with("runuser - postgres -c 'pg_restore -C -d postgres #{file}'"). | ||
returns('') | ||
|
||
assert db.restore_dump(file, true, local_config) | ||
end | ||
|
||
it 'restores remote db' do | ||
file = '/backup/fake.dump' | ||
restore_cmd = <<~CMD.strip | ||
pg_restore -h db.example.com -p 5432 -U fakeuser --no-privileges --clean --disable-triggers -n public -d fakedb #{file} | ||
CMD | ||
|
||
db.expects(:execute!).with( | ||
restore_cmd, | ||
valid_exit_statuses: [0, 1], | ||
env: { "PGPASSWORD" => "fakepassword" } | ||
).returns('') | ||
|
||
assert db.restore_dump(file, false, remote_config) | ||
end | ||
|
||
it 'dumps local db' do | ||
file = '/backup/fake.dump' | ||
|
||
db.expects(:execute!).with( | ||
"pg_dump -h localhost -p 5432 -U fakeuser -Fc fakedb > /backup/fake.dump", | ||
env: { "PGPASSWORD" => "fakepassword" } | ||
).returns('') | ||
|
||
assert db.dump_db(file, local_config) | ||
end | ||
|
||
it 'dumps remote db' do | ||
file = '/backup/fake.dump' | ||
|
||
db.expects(:execute!).with( | ||
"pg_dump -h db.example.com -p 5432 -U fakeuser -Fc fakedb > /backup/fake.dump", | ||
env: { "PGPASSWORD" => "fakepassword" } | ||
).returns('') | ||
|
||
assert db.dump_db(file, remote_config) | ||
end | ||
|
||
it 'pings db' do | ||
db.expects(:execute?).with("psql -h localhost -p 5432 -U fakeuser -d fakedb", | ||
stdin: "SELECT 1 as ping", env: { "PGPASSWORD" => "fakepassword" }).returns(true) | ||
|
||
assert db.ping(local_config) | ||
end | ||
|
||
it 'runs db queries' do | ||
psql_return = <<~PSQL | ||
test | ||
------ | ||
42 | ||
(1 row) | ||
PSQL | ||
|
||
db.expects(:ping).with(local_config).returns(true) | ||
db.expects(:execute).with("psql -h localhost -p 5432 -U fakeuser -d fakedb", | ||
stdin: "SELECT 42 as test", env: { "PGPASSWORD" => "fakepassword" }).returns(psql_return) | ||
|
||
assert db.psql('SELECT 42 as test', local_config) | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels like this should use
psql('SHOW server_version')
, but you probably also saw that and considered it out of scope.And as always, I think it should have used
--no-align
instead of-A
to make the comment above it redundant.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe, but I also didn't feel like actually touching the code here