mirror of
https://github.com/inspec/inspec
synced 2024-11-10 15:14:23 +00:00
resource/postgres_session: add integration tests, change error handling
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)
This commit is contained in:
parent
72f04ccb8d
commit
56f22a1d2a
5 changed files with 33 additions and 15 deletions
|
@ -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
|
||||
|
@ -39,29 +37,26 @@ class PostgresSession < Inspec.resource(1)
|
|||
@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
|
||||
|
|
|
@ -7,3 +7,4 @@ version '1.0.0'
|
|||
depends 'apt'
|
||||
depends 'yum'
|
||||
depends 'runit'
|
||||
depends 'postgresql'
|
||||
|
|
|
@ -11,3 +11,4 @@ include_recipe('os_prepare::json_yaml_csv_ini')
|
|||
include_recipe('os_prepare::package')
|
||||
include_recipe('os_prepare::registry_key')
|
||||
include_recipe('os_prepare::service')
|
||||
include_recipe('os_prepare::postgres')
|
||||
|
|
12
test/integration/cookbooks/os_prepare/recipes/postgres.rb
Normal file
12
test/integration/cookbooks/os_prepare/recipes/postgres.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue