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

updated the psql command so that it does not print the headers and ex… #1714

Merged
merged 1 commit into from
May 11, 2017

Conversation

aaronlippold
Copy link
Collaborator

…tra output

Copy link
Contributor

@adamleff adamleff left a 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.

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
Copy link
Contributor

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.

end
alias_method psql query
Copy link
Contributor

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.

@@ -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
Copy link
Contributor

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.

@aaronlippold aaronlippold force-pushed the postgres-session-update branch 5 times, most recently from c2e39b3 to 0c9d31e Compare May 3, 2017 19:17
@aaronlippold
Copy link
Collaborator Author

@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.

 -                   .sub(/(.*\n)+([-]+[+])*[-]+\n/, '')		
 -                   .sub(/\n[^\n]*\n\n$/, '')```

I just had psql do that for us rather than doing it ourselves.

* Alias function removed
*  Formatting fixed
* Reviewed by secondary source

@arlimus
Copy link
Contributor

arlimus commented May 9, 2017

Tracked down psql a bit for the added commandline options; Afaics 7.0 had -A -t included too (since 2000). Thank you for raising this, it's good that we know about these things and we need to include them in these resources when this part of the platforms RFC lands (#1661)

sql = postgres_session('username', 'password', 'host')

query(string): required
db(array[string]): optional, defaults to `username == db`
Copy link
Contributor

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'

@arlimus
Copy link
Contributor

arlimus commented May 9, 2017

Thank you for the updates @aaronlippold
Kudos for the review @adamleff
👍

Once the example is updated this looks good from my perspective

@aaronlippold aaronlippold force-pushed the postgres-session-update branch 2 times, most recently from 03f6e0d to 82d5702 Compare May 9, 2017 16:01
Copy link
Contributor

@arlimus arlimus left a 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!

Copy link
Contributor

@adamleff adamleff left a 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.


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)
Copy link
Contributor

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)

Copy link
Contributor

@arlimus arlimus May 10, 2017

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@adamleff @arlimus would we want to make it nil or localhost, if we make it localhost as the function header default then we could remove L46?

Copy link
Collaborator Author

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.

@user = user || 'postgres'
@pass = pass
@host = host || 'localhost'
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

i.e. here...

@aaronlippold aaronlippold force-pushed the postgres-session-update branch from 82d5702 to 9d1d025 Compare May 11, 2017 12:53
Signed-off-by: Aaron Lippold <lippold@gmail.com>
@aaronlippold aaronlippold force-pushed the postgres-session-update branch from 9d1d025 to 684d81d Compare May 11, 2017 12:59
@adamleff adamleff merged commit a5e5cc0 into inspec:master May 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants