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

resource/postgres_session: add integration tests, change error handling #381

Merged
merged 1 commit into from Jan 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 10 additions & 15 deletions lib/resources/postgres_session.rb
Expand Up @@ -5,17 +5,15 @@
# license: All rights reserved

class Lines
attr_reader :output

def initialize(raw, desc)
@raw = raw
@output = raw
@desc = desc
end

def output
@raw
end

def lines
@raw.split("\n")
output.split("\n")
end

def to_s
Expand All @@ -39,29 +37,26 @@ def initialize(user, pass)
@pass = pass
end

def query(query, db = [], &block)
def query(query, db = [])
dbs = db.map { |x| "-d #{x}" }.join(' ')
# TODO: simple escape, must be handled by a library
# that does this securely
escaped_query = query.gsub(/\\/, '\\\\').gsub(/"/, '\\"').gsub(/\$/, '\\$')
# run the query
cmd = inspec.command("PGPASSWORD='#{@pass}' psql -U #{@user} #{dbs} -c \"#{escaped_query}\"")
cmd = inspec.command("PGPASSWORD='#{@pass}' psql -U #{@user} #{dbs} -h localhost -c \"#{escaped_query}\"")
out = cmd.stdout + "\n" + cmd.stderr
if out =~ /could not connect to .*/ or
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
RSpec.describe(cmd) do
it 'is skipped', skip: out do
end
end
skip_resource "Can't read run query #{query.inspect} on postgres_session: #{out}"
else
# remove the whole header (i.e. up to the first ^-----+------+------$)
# remove the tail
lines = cmd.stdout
.sub(/(.*\n)+([-]+[+])*[-]+\n/, '')
.sub(/\n[^\n]*\n\n$/, '')
l = Lines.new(lines.strip, "PostgreSQL query: #{query}")
RSpec.__send__('describe', l, &block)
Lines.new(lines.strip, "PostgreSQL query: #{query}")
end
end
end
1 change: 1 addition & 0 deletions test/integration/cookbooks/os_prepare/metadata.rb
Expand Up @@ -7,3 +7,4 @@
depends 'apt'
depends 'yum'
depends 'runit'
depends 'postgresql'
1 change: 1 addition & 0 deletions test/integration/cookbooks/os_prepare/recipes/default.rb
Expand Up @@ -11,3 +11,4 @@
include_recipe('os_prepare::package')
include_recipe('os_prepare::registry_key')
include_recipe('os_prepare::service')
include_recipe('os_prepare::postgres')
12 changes: 12 additions & 0 deletions test/integration/cookbooks/os_prepare/recipes/postgres.rb
@@ -0,0 +1,12 @@
# encoding: utf-8
# author: Stephan Renatus
#
# installs everyting for the postgres tests

# hw-cookbooks/postgresql is tested on these platforms
case node['platform']
when 'ubuntu', 'centos'
node.default['postgresql']['config']['listen_addresses'] = 'localhost'
node.default['postgresql']['password']['postgres'] = 'md506be11be01439cb4abd537e454df34ea' # "inspec"
include_recipe 'postgresql::server'
end
@@ -0,0 +1,9 @@
# encoding: utf-8

# postgres-server is installed on these platforms
if ['ubuntu', 'centos'].include? os['family']
postgres = postgres_session('postgres', 'inspec')
describe postgres.query('show ssl;') do
its('output') { should eq 'on' }
end
end