module-postgresql/spec/acceptance/postgresql_psql_spec.rb
Dominic Cleal b7cbe60d4b (MODULES-775) Fix refresh/unless parameter interactions
Interactions between resource refreshes and the 'unless' parameter have been
fixed to follow the behaviour of the 'exec' type.

The 'unless' parameter is now always taken into account, whether in ordinary
operation, during a refresh, or when refreshonly is set to true.  The resource
will not run the SQL command when the 'unless' clause matches a row.

Previously a refresh on a resource would ignore the 'unless' parameter if set
which could cause a failure re-running a command, such as attempting to create
a role that already exists.

The following examples have been fixed:

  * should not run SQL when refreshed and the unless query returns rows
  * with refreshonly should not run SQL when the unless query returns no rows
  * with refreshonly should not run SQL when refreshed and the unless query
    returns rows

This is done by moving the logic for refreshonly and whether to run the SQL
command from the provider into the type, and consolidating it in the
should_run_sql method which is called during 'command' property retrieval
(instead of sync) and during refresh.
2014-06-03 10:25:13 +01:00

134 lines
3.9 KiB
Ruby

require 'spec_helper_acceptance'
describe 'postgresql_psql:', :unless => UNSUPPORTED_PLATFORMS.include?(fact('osfamily')) do
after :all do
# Cleanup after tests have ran
apply_manifest("class { 'postgresql::server': ensure => absent }", :catch_failures => true)
end
it 'should always run SQL' do
pp = <<-EOS.unindent
class { 'postgresql::server': }
postgresql_psql { 'foobar':
db => 'postgres',
psql_user => 'postgres',
command => 'select 1',
require => Class['postgresql::server'],
}
EOS
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_failures => true)
end
it 'should run some SQL when the unless query returns no rows' do
pp = <<-EOS.unindent
class { 'postgresql::server': }
postgresql_psql { 'foobar':
db => 'postgres',
psql_user => 'postgres',
command => 'select 1',
unless => 'select 1 where 1=2',
require => Class['postgresql::server'],
}
EOS
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_failures => true)
end
it 'should not run SQL when the unless query returns rows' do
pp = <<-EOS.unindent
class { 'postgresql::server': }
postgresql_psql { 'foobar':
db => 'postgres',
psql_user => 'postgres',
command => 'select * from pg_database limit 1',
unless => 'select 1 where 1=1',
require => Class['postgresql::server'],
}
EOS
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_changes => true)
end
it 'should not run SQL when refreshed and the unless query returns rows' do
pp = <<-EOS.unindent
class { 'postgresql::server': }
notify { 'trigger': } ~>
postgresql_psql { 'foobar':
db => 'postgres',
psql_user => 'postgres',
command => 'invalid sql statement',
unless => 'select 1 where 1=1',
require => Class['postgresql::server'],
}
EOS
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_failures => true)
end
context 'with refreshonly' do
it 'should not run SQL when the unless query returns no rows' do
pp = <<-EOS.unindent
class { 'postgresql::server': }
postgresql_psql { 'foobar':
db => 'postgres',
psql_user => 'postgres',
command => 'select 1',
unless => 'select 1 where 1=2',
refreshonly => true,
require => Class['postgresql::server'],
}
EOS
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_changes => true)
end
it 'should run SQL when refreshed and the unless query returns no rows' do
pp = <<-EOS.unindent
class { 'postgresql::server': }
notify { 'trigger': } ~>
postgresql_psql { 'foobar':
db => 'postgres',
psql_user => 'postgres',
command => 'select 1',
unless => 'select 1 where 1=2',
refreshonly => true,
require => Class['postgresql::server'],
}
EOS
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_failures => true)
end
it 'should not run SQL when refreshed and the unless query returns rows' do
pp = <<-EOS.unindent
class { 'postgresql::server': }
notify { 'trigger': } ~>
postgresql_psql { 'foobar':
db => 'postgres',
psql_user => 'postgres',
command => 'invalid sql query',
unless => 'select 1 where 1=1',
refreshonly => true,
require => Class['postgresql::server'],
}
EOS
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_failures => true)
end
end
end