diff --git a/lib/resources/postgres_session.rb b/lib/resources/postgres_session.rb index 989918f93..1749bc3e5 100644 --- a/lib/resources/postgres_session.rb +++ b/lib/resources/postgres_session.rb @@ -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 diff --git a/test/integration/cookbooks/os_prepare/metadata.rb b/test/integration/cookbooks/os_prepare/metadata.rb index c19012faa..462bbe770 100644 --- a/test/integration/cookbooks/os_prepare/metadata.rb +++ b/test/integration/cookbooks/os_prepare/metadata.rb @@ -7,3 +7,4 @@ version '1.0.0' depends 'apt' depends 'yum' depends 'runit' +depends 'postgresql' diff --git a/test/integration/cookbooks/os_prepare/recipes/default.rb b/test/integration/cookbooks/os_prepare/recipes/default.rb index a1afa035c..329be0e4a 100644 --- a/test/integration/cookbooks/os_prepare/recipes/default.rb +++ b/test/integration/cookbooks/os_prepare/recipes/default.rb @@ -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') diff --git a/test/integration/cookbooks/os_prepare/recipes/postgres.rb b/test/integration/cookbooks/os_prepare/recipes/postgres.rb new file mode 100644 index 000000000..858fd1ace --- /dev/null +++ b/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 diff --git a/test/integration/test/integration/default/postgres_session_spec.rb b/test/integration/test/integration/default/postgres_session_spec.rb new file mode 100644 index 000000000..7f2b881e1 --- /dev/null +++ b/test/integration/test/integration/default/postgres_session_spec.rb @@ -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