5df36cf1f7
This patch is a fix for the race condition that keeps occuring during postgresql setup. Its very rare on its own, but when you are using this module in a CI environment it happens quite frequently. Basically what happens is that sometimes the service will announce the database has started, but really it is still working in the background. Sometimes the unix socket may not be listening, and sometimes the system is still loading and you get a weird client error. The fix itself is a modification to postgresql::validate_db_connection so that it is able to connect on the local unix socket, plus retry until the database is available. This new and improved validate_db_connection can then be put into the build pipeline (in the service class in particular) to ensure the database is started before continuing on with the remaining steps. This in effect blocks the puppet module from continuing until the postgresql database is fully started and able to receive connections which is perfect. Tests and documentation provided. Signed-off-by: Ken Barber <ken@bob.sh>
91 lines
2.4 KiB
Ruby
91 lines
2.4 KiB
Ruby
require 'spec_helper_system'
|
|
|
|
describe 'postgresql::validate_db_connection:' do
|
|
before :all do
|
|
# Setup postgresql server and a sample database for tests to use.
|
|
pp = <<-EOS.unindent
|
|
$db = 'foo'
|
|
class { 'postgresql::server': }
|
|
|
|
postgresql::server::db { $db:
|
|
user => $db,
|
|
password => postgresql_password($db, $db),
|
|
}
|
|
EOS
|
|
|
|
puppet_apply(pp) do |r|
|
|
r.exit_code.should_not == 1
|
|
end
|
|
end
|
|
|
|
after :all do
|
|
# Remove postgresql server after all tests have ran.
|
|
puppet_apply("class { 'postgresql::server': ensure => absent }") do |r|
|
|
r.exit_code.should_not == 1
|
|
end
|
|
end
|
|
|
|
it 'should run puppet with no changes declared if socket connectivity works' do
|
|
pp = <<-EOS.unindent
|
|
postgresql::validate_db_connection { 'foo':
|
|
database_name => 'foo',
|
|
run_as => 'postgres',
|
|
}
|
|
EOS
|
|
|
|
puppet_apply(pp) do |r|
|
|
r.exit_code.should == 0
|
|
end
|
|
end
|
|
|
|
it 'should keep retrying if database is down' do
|
|
# So first we shut the db down, then background a startup routine with a
|
|
# sleep 10 in front of it. That way rspec-system should continue while
|
|
# the pause and db startup happens in the background.
|
|
shell("/etc/init.d/postgresql* stop")
|
|
shell('nohup bash -c "sleep 10; /etc/init.d/postgresql* start" > /dev/null 2>&1 &')
|
|
|
|
pp = <<-EOS.unindent
|
|
postgresql::validate_db_connection { 'foo':
|
|
database_name => 'foo',
|
|
tries => 30,
|
|
sleep => 1,
|
|
run_as => 'postgres',
|
|
}
|
|
EOS
|
|
|
|
puppet_apply(pp) do |r|
|
|
r.exit_code.should == 0
|
|
end
|
|
end
|
|
|
|
it 'should run puppet with no changes declared if db ip connectivity works' do
|
|
pp = <<-EOS.unindent
|
|
postgresql::validate_db_connection { 'foo':
|
|
database_host => 'localhost',
|
|
database_name => 'foo',
|
|
database_username => 'foo',
|
|
database_password => 'foo',
|
|
}
|
|
EOS
|
|
|
|
puppet_apply(pp) do |r|
|
|
r.exit_code.should == 0
|
|
end
|
|
end
|
|
|
|
it 'should fail catalogue if database connectivity fails' do
|
|
pp = <<-EOS.unindent
|
|
postgresql::validate_db_connection { 'foobarbaz':
|
|
database_host => 'localhost',
|
|
database_name => 'foobarbaz',
|
|
database_username => 'foobarbaz',
|
|
database_password => 'foobarbaz',
|
|
}
|
|
EOS
|
|
|
|
puppet_apply(pp) do |r|
|
|
r.exit_code.should == 4
|
|
end
|
|
end
|
|
end
|