2012-12-03 01:41:35 +01:00
require 'support/postgres_test_utils'
require 'support/shared_contexts/pg_vm_context'
2012-12-03 00:28:25 +01:00
shared_examples :system_default_postgres do
2012-12-03 01:41:35 +01:00
include PostgresTestUtils
include_context :pg_vm_context
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
# this method is required by the pg_vm shared context
def install_postgres
sudo_and_log ( vm , 'puppet apply -e "include postgresql::server"' )
2012-12-03 00:28:25 +01:00
end
2012-12-03 01:41:35 +01:00
describe 'postgresql::initdb' do
it " should idempotently create a working --pgdata directory so postgres can run " do
@logger . info ( " starting " )
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
# A bare-minimum class to initdb the specified dir
test_class = 'class {"postgresql_tests::system_default::test_initdb": }'
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
# Run once to check for crashes
sudo_and_log ( vm , " puppet apply -e ' #{ test_class } ' " )
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
# Run again to check for idempotence via --detailed-exitcodes
sudo_and_log ( vm , " puppet apply --detailed-exitcodes -e ' #{ test_class } ' " )
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
sudo_and_log ( vm , " service #{ service_name } restart " )
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
# Connect to it and list the databases
sudo_psql_and_log ( vm , '--list --tuples-only' )
2012-12-03 00:28:25 +01:00
end
2012-12-03 01:41:35 +01:00
end
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
describe 'postgresql::db' do
it 'should idempotently create a db that we can connect to' do
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
# A bare-minimum class to add a DB to postgres, which will be running due to ubuntu
test_class = 'class {"postgresql_tests::system_default::test_db": db => "postgresql_test_db" }'
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
begin
2012-12-03 00:28:25 +01:00
# Run once to check for crashes
2012-12-03 01:41:35 +01:00
sudo_and_log ( vm , " puppet apply --detailed-exitcodes -e ' #{ test_class } '; [ $? == 2 ] " )
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
# Run again to check for idempotence
2012-12-03 00:28:25 +01:00
sudo_and_log ( vm , " puppet apply --detailed-exitcodes -e ' #{ test_class } ' " )
2012-12-03 01:41:35 +01:00
# Check that the database name is present
sudo_psql_and_log ( vm , 'postgresql_test_db --command="select datname from pg_database limit 1"' )
ensure
sudo_psql_and_log ( vm , '--command="drop database postgresql_test_db" postgres' )
2012-12-03 00:28:25 +01:00
end
end
2012-12-03 01:41:35 +01:00
end
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
describe 'postgresql::psql' do
it 'should emit a deprecation warning' do
test_class = 'class {"postgresql_tests::system_default::test_psql": command => "SELECT * FROM pg_datbase limit 1", unless => "SELECT 1 WHERE 1=1" }'
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
data = sudo_and_log ( vm , " puppet apply --detailed-exitcodes -e ' #{ test_class } '; [ $? == 2 ] " )
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
data . should match / postgresql::psql is deprecated /
2012-12-03 00:28:25 +01:00
end
2012-12-03 01:41:35 +01:00
end
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
describe 'postgresql_psql' do
it 'should run some SQL when the unless query returns no rows' do
test_class = 'class {"postgresql_tests::system_default::test_ruby_psql": command => "SELECT 1", unless => "SELECT 1 WHERE 1=2" }'
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
# Run once to get all packages set up
sudo_and_log ( vm , " puppet apply -e ' #{ test_class } ' " )
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
# Check for exit code 2
sudo_and_log ( vm , " puppet apply --detailed-exitcodes -e ' #{ test_class } ' ; [ $? == 2 ] " )
2012-12-03 00:28:25 +01:00
end
2012-12-03 01:41:35 +01:00
it 'should not run SQL when the unless query returns rows' do
test_class = 'class {"postgresql_tests::system_default::test_ruby_psql": command => "SELECT * FROM pg_datbase limit 1", unless => "SELECT 1 WHERE 1=1" }'
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
# Run once to get all packages set up
sudo_and_log ( vm , " puppet apply -e ' #{ test_class } ' " )
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
# Check for exit code 0
sudo_and_log ( vm , " puppet apply --detailed-exitcodes -e ' #{ test_class } ' " )
2012-12-03 00:28:25 +01:00
end
2012-12-03 01:41:35 +01:00
end
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
describe 'postgresql::user' do
it 'should idempotently create a user who can log in' do
test_class = 'class {"postgresql_tests::system_default::test_user": user => "postgresql_test_user", password => "postgresql_test_password" }'
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
# Run once to check for crashes
sudo_and_log ( vm , " puppet apply -e ' #{ test_class } ' " )
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
# Run again to check for idempotence
sudo_and_log ( vm , " puppet apply --detailed-exitcodes -e ' #{ test_class } ' " )
# Check that the user can log in
sudo_psql_and_log ( vm , '--command="select datname from pg_database limit 1" postgres' , 'postgresql_test_user' )
2012-12-03 00:28:25 +01:00
end
2012-12-03 01:41:35 +01:00
end
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
describe 'postgresql::grant' do
it 'should grant access so a user can create in a database' do
test_class = 'class {"postgresql_tests::system_default::test_grant_create": db => "postgres", user => "psql_grant_tester", password => "psql_grant_pw" }'
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
# Run once to check for crashes
sudo_and_log ( vm , " puppet apply -e ' #{ test_class } ' " )
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
# Run again to check for idempotence
sudo_and_log ( vm , " puppet apply --detailed-exitcodes -e ' #{ test_class } ' " )
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
# Check that the user can create a table in the database
sudo_psql_and_log ( vm , '--command="create table foo (foo int)" postgres' , 'psql_grant_tester' )
2012-12-03 00:28:25 +01:00
2012-12-03 01:41:35 +01:00
sudo_psql_and_log ( vm , '--command="drop table foo" postgres' , 'psql_grant_tester' )
2012-12-03 00:28:25 +01:00
end
end
2013-01-11 13:45:07 +01:00
describe 'postgresql::validate_db_connections' do
it 'should run puppet with no changes declared if database connectivity works' do
# Setup
setup_class = 'class {"postgresql_tests::system_default::test_db": db => "foo" }'
sudo_and_log ( vm , " puppet apply --detailed-exitcodes -e ' #{ setup_class } '; [ $? == 2 ] " )
# Run test
test_pp = " postgresql::validate_db_connection {'foo': database_host => 'localhost', database_name => 'foo', database_username => 'foo', database_password => 'foo' } "
sudo_and_log ( vm , " puppet apply --detailed-exitcodes -e ' #{ test_pp } ' " )
end
it 'should fail catalogue if database connectivity fails' do
# Run test
test_pp = " postgresql::validate_db_connection {'foo': database_host => 'localhost', database_name => 'foo', database_username => 'foo', database_password => 'foo' } "
sudo_and_log ( vm , " puppet apply --detailed-exitcodes -e ' #{ test_pp } '; [ $? == 4 ] " )
end
end
2013-01-28 18:01:11 +01:00
describe 'postgresql::tablespace' do
it 'should idempotently create tablespaces and databases that are using them' do
test_class = 'class {"postgresql_tests::system_default::test_tablespace": }'
# Run once to check for crashes
sudo_and_log ( vm , " puppet apply -e ' #{ test_class } ' " )
# Run again to check for idempotence
sudo_and_log ( vm , " puppet apply --detailed-exitcodes -e ' #{ test_class } ' " )
# Check that databases use correct tablespaces
2013-01-30 14:35:14 +01:00
sudo_psql_and_expect_result ( vm , '--command="select ts.spcname from pg_database db, pg_tablespace ts where db.dattablespace = ts.oid and db.datname = \'"\'tablespacedb1\'"\'"' , 'tablespace1' )
sudo_psql_and_expect_result ( vm , '--command="select ts.spcname from pg_database db, pg_tablespace ts where db.dattablespace = ts.oid and db.datname = \'"\'tablespacedb3\'"\'"' , 'tablespace2' )
2013-01-28 18:01:11 +01:00
end
end
2013-01-14 23:08:56 +01:00
describe 'postgresql.conf include' do
it " should support an 'include' directive at the end of postgresql.conf " do
test_class = 'class {"postgresql_tests::system_default::test_pgconf_include": }'
# Run once to check for crashes
sudo_and_log ( vm , " puppet apply -e ' #{ test_class } ' " )
# Run again to check for idempotence
sudo_and_log ( vm , " puppet apply --detailed-exitcodes -e ' #{ test_class } ' " )
2013-01-31 14:30:21 +01:00
sudo_and_log ( vm , " service #{ service_name } restart " )
2013-01-14 23:08:56 +01:00
# Check that the user can create a table in the database
2013-01-31 14:30:21 +01:00
sudo_psql_and_log ( vm , '--command="show max_connections" -t' , 'postgres' , ' |grep "123"' )
2013-01-17 01:18:37 +01:00
cleanup_class = 'class {"postgresql_tests::system_default::test_pgconf_include_cleanup": }'
sudo_and_log ( vm , " puppet apply -e ' #{ cleanup_class } ' " )
2013-01-14 23:08:56 +01:00
end
end
2013-01-11 13:45:07 +01:00
end