-
Notifications
You must be signed in to change notification settings - Fork 681
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
updated the psql command so that it does not print the headers and ex… #1714
Conversation
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.
Hi @aaronlippold and thanks for your contribution.
I'm concerned that this change is not technically backward compatible with existing versions and users of InSpec 1.x. If users are using this resource and expecting the output to not be stripped and not expecting that the spaces before and after the field separator to be missing, we can potentially break their existing tests. I would definitely flag this change as a change acceptable for InSpec v2.
A potential workaround is to introduce a new rows_from_query
method that takes a query and formats it as an array of hashes, each row being an a hash in that array. That way the query
method return format remains unaltered.
Also, you'll need to sign-off each of your commits in accordance with the DCO.
@arlimus and @chris-rock, your feedback requested on the backward compatibility aspect of this change.
lib/resources/postgres_session.rb
Outdated
out = cmd.stdout + "\n" + cmd.stderr | ||
if cmd.exit_status != 0 or | ||
out =~ /could not connect to .*/ or | ||
out.downcase =~ /^error/ | ||
# skip this test if the server can't run the query | ||
skip_resource "Can't read run query #{query.inspect} on postgres_session: #{out}" | ||
else | ||
Lines.new(cmd.stdout.strip, "PostgreSQL query: #{query}") | ||
# @note the below should not be needed now that you are passing -A -t to psql |
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.
Let's delete these comments from lines 60-65... they're not needed anymore.
lib/resources/postgres_session.rb
Outdated
end | ||
alias_method psql query |
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.
Let's not create aliases unless they are absolutely necessary. Commands like SHOW port
are still queries and can be handled with the query
method.
lib/resources/postgres_session.rb
Outdated
@@ -31,6 +31,10 @@ class PostgresSession < Inspec.resource(1) | |||
describe sql.query('SELECT * FROM pg_shadow WHERE passwd IS NULL;') do | |||
its('output') { should eq('') } | |||
end | |||
|
|||
describe sql.psql('SHOW port') do |
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.
I do not agree with creating a psql
method. No matter how the query is performed, even SHOW port
is still a query so the query
method will suffice.
c2e39b3
to
0c9d31e
Compare
@adamleff the change specifically for the extra command flags to the psql command do not change how the function operates at all it just removes the need to have the extra substring munging to remove text that was and has been in the codebase.
|
Tracked down psql a bit for the added commandline options; Afaics 7.0 had |
lib/resources/postgres_session.rb
Outdated
sql = postgres_session('username', 'password', 'host') | ||
|
||
query(string): required | ||
db(array[string]): optional, defaults to `username == db` |
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.
Small suggestion:
sql = postgres_session('username', 'password', 'host')
# with default values:
# username: 'postgres'
# host: 'localhost'
Thank you for the updates @aaronlippold Once the example is updated this looks good from my perspective |
03f6e0d
to
82d5702
Compare
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.
Great updates thank you!
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.
One small thing and then I'm OK to approve.
lib/resources/postgres_session.rb
Outdated
|
||
describe sql.query('SELECT * FROM pg_shadow WHERE passwd IS NULL;') do | ||
its('output') { should eq('') } | ||
end | ||
" | ||
|
||
def initialize(user, pass) | ||
def initialize(user, pass, host) |
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.
This isn't backward compatible with the existing resource. We should probably change this to:
def initialize(user, pass, host=nil)
Otherwise, anyone already using a postgres_session
resource will get:
ArgumentError: wrong number of arguments (given 2, expected 3)
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.
Great catch @adamleff ! I missed that
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.
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.
Ok. Made the change, I guess it doesn't matter what is in the method signature given that the constructer sets the value right away anyway. Changes pushed.
lib/resources/postgres_session.rb
Outdated
@user = user || 'postgres' | ||
@pass = pass | ||
@host = host || 'localhost' |
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.
i.e. here...
82d5702
to
9d1d025
Compare
Signed-off-by: Aaron Lippold <lippold@gmail.com>
9d1d025
to
684d81d
Compare
…tra output