Skip to content

Commit

Permalink
resource/postgres_session: add integration tests, change error handling
Browse files Browse the repository at this point in the history
this makes it work (tested with default-ubuntu-1404), but doesn't
improve the error handling (i.e., the skip_resource doesn't really
prevent the failure)
  • Loading branch information
srenatus committed Jan 21, 2016
1 parent a5ed04d commit 849136f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
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 @@ -6,3 +6,4 @@
version '1.0.0'
depends 'apt'
depends 'yum'
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

0 comments on commit 849136f

Please sign in to comment.