b7cbe60d4b
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.
79 lines
2.5 KiB
Ruby
79 lines
2.5 KiB
Ruby
require 'spec_helper'
|
|
|
|
describe Puppet::Type.type(:postgresql_psql).provider(:ruby) do
|
|
let(:name) { 'rspec psql test' }
|
|
let(:resource) do
|
|
Puppet::Type.type(:postgresql_psql).new({ :name => name, :provider => :ruby }.merge attributes)
|
|
end
|
|
|
|
let(:provider) { resource.provider }
|
|
|
|
context("#run_sql_command") do
|
|
describe "with default attributes" do
|
|
let(:attributes) do { :db => 'spec_db' } end
|
|
|
|
it "executes with the given psql_path on the given DB" do
|
|
expect(provider).to receive(:run_command).with(['psql', '-d',
|
|
attributes[:db], '-t', '-c', 'SELECT something'], 'postgres',
|
|
'postgres')
|
|
|
|
provider.run_sql_command("SELECT something")
|
|
end
|
|
end
|
|
describe "with psql_path and db" do
|
|
let(:attributes) do {
|
|
:psql_path => '/opt/postgres/psql',
|
|
:psql_user => 'spec_user',
|
|
:psql_group => 'spec_group',
|
|
:cwd => '/spec',
|
|
:db => 'spec_db'
|
|
} end
|
|
|
|
it "executes with the given psql_path on the given DB" do
|
|
expect(Dir).to receive(:chdir).with(attributes[:cwd]).and_yield
|
|
expect(provider).to receive(:run_command).with([attributes[:psql_path],
|
|
'-d', attributes[:db], '-t', '-c', 'SELECT something'],
|
|
attributes[:psql_user], attributes[:psql_group])
|
|
|
|
provider.run_sql_command("SELECT something")
|
|
end
|
|
end
|
|
describe "with search_path string" do
|
|
let(:attributes) do {
|
|
:search_path => "schema1"
|
|
} end
|
|
|
|
it "executes with the given search_path" do
|
|
expect(provider).to receive(:run_command).with(['psql', '-t', '-c',
|
|
'set search_path to schema1; SELECT something'],
|
|
'postgres', 'postgres')
|
|
|
|
provider.run_sql_command("SELECT something")
|
|
end
|
|
end
|
|
describe "with search_path array" do
|
|
let(:attributes) do {
|
|
:search_path => ['schema1','schema2'],
|
|
} end
|
|
|
|
it "executes with the given search_path" do
|
|
expect(provider).to receive(:run_command).with(['psql', '-t', '-c',
|
|
'set search_path to schema1,schema2; SELECT something'],
|
|
'postgres',
|
|
'postgres'
|
|
)
|
|
|
|
provider.run_sql_command("SELECT something")
|
|
end
|
|
end
|
|
end
|
|
|
|
context("#run_unless_sql_command") do
|
|
let(:attributes) do { } end
|
|
|
|
it "calls #run_sql_command with SQL" do
|
|
expect(provider).to receive(:run_sql_command).with('SELECT COUNT(*) FROM (SELECT 1) count')
|
|
provider.run_unless_sql_command('SELECT 1')
|
|
end
|
|
end
|
|
end
|