system_default_postgres.rb 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. require 'support/postgres_test_utils'
  2. require 'support/shared_contexts/pg_vm_context'
  3. shared_examples :system_default_postgres do
  4. include PostgresTestUtils
  5. include_context :pg_vm_context
  6. # this method is required by the pg_vm shared context
  7. def install_postgres
  8. sudo_and_log(vm, 'puppet apply -e "include postgresql::server"')
  9. end
  10. describe 'postgresql::initdb' do
  11. it "should idempotently create a working --pgdata directory so postgres can run" do
  12. @logger.info("starting")
  13. # A bare-minimum class to initdb the specified dir
  14. test_class = 'class {"postgresql_tests::system_default::test_initdb": }'
  15. # Run once to check for crashes
  16. sudo_and_log(vm, "puppet apply -e '#{test_class}'")
  17. # Run again to check for idempotence via --detailed-exitcodes
  18. sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'")
  19. sudo_and_log(vm, "service #{service_name} restart")
  20. # Connect to it and list the databases
  21. sudo_psql_and_log(vm, '--list --tuples-only')
  22. end
  23. end
  24. describe 'postgresql::db' do
  25. it 'should idempotently create a db that we can connect to' do
  26. # A bare-minimum class to add a DB to postgres, which will be running due to ubuntu
  27. test_class = 'class {"postgresql_tests::system_default::test_db": db => "postgresql_test_db" }'
  28. begin
  29. # Run once to check for crashes
  30. sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'; [ $? == 2 ]")
  31. # Run again to check for idempotence
  32. sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'")
  33. # Check that the database name is present
  34. sudo_psql_and_log(vm, 'postgresql_test_db --command="select datname from pg_database limit 1"')
  35. ensure
  36. sudo_psql_and_log(vm, '--command="drop database postgresql_test_db" postgres')
  37. end
  38. end
  39. it 'should take a locale parameter' do
  40. pending('no support for locale parameter with centos 5', :if => vm == :centos5)
  41. manifest = <<-EOS
  42. include postgresql::server
  43. postgresql::db { 'test1':
  44. user => 'test1',
  45. password => 'test1',
  46. charset => 'UTF8',
  47. locale => 'en_NG',
  48. }
  49. EOS
  50. sudo_and_log(vm, "puppet apply -e '#{manifest}'")
  51. # Some basic tests here to check if the db indeed was created with the
  52. # correct locale.
  53. sudo_and_log(vm, 'su postgres -c \'psql -c "show lc_ctype" test1\'')
  54. sudo_and_log(vm, 'su postgres -c \'psql -c "show lc_ctype" test1\' | grep en_NG')
  55. sudo_and_log(vm, 'su postgres -c \'psql -c "show lc_collate" test1\' | grep en_NG')
  56. end
  57. end
  58. describe 'postgresql::psql' do
  59. it 'should emit a deprecation warning' do
  60. test_class = 'class {"postgresql_tests::system_default::test_psql": command => "SELECT * FROM pg_datbase limit 1", unless => "SELECT 1 WHERE 1=1" }'
  61. data = sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'; [ $? == 2 ]")
  62. data.should match /postgresql::psql is deprecated/
  63. end
  64. end
  65. describe 'postgresql_psql' do
  66. it 'should run some SQL when the unless query returns no rows' do
  67. test_class = 'class {"postgresql_tests::system_default::test_ruby_psql": command => "SELECT 1", unless => "SELECT 1 WHERE 1=2" }'
  68. # Run once to get all packages set up
  69. sudo_and_log(vm, "puppet apply -e '#{test_class}'")
  70. # Check for exit code 2
  71. sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}' ; [ $? == 2 ]")
  72. end
  73. it 'should not run SQL when the unless query returns rows' do
  74. test_class = 'class {"postgresql_tests::system_default::test_ruby_psql": command => "SELECT * FROM pg_datbase limit 1", unless => "SELECT 1 WHERE 1=1" }'
  75. # Run once to get all packages set up
  76. sudo_and_log(vm, "puppet apply -e '#{test_class}'")
  77. # Check for exit code 0
  78. sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'")
  79. end
  80. end
  81. describe 'postgresql::user' do
  82. it 'should idempotently create a user who can log in' do
  83. test_class = 'class {"postgresql_tests::system_default::test_user": user => "postgresql_test_user", password => "postgresql_test_password" }'
  84. # Run once to check for crashes
  85. sudo_and_log(vm, "puppet apply -e '#{test_class}'")
  86. # Run again to check for idempotence
  87. sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'")
  88. # Check that the user can log in
  89. sudo_psql_and_log(vm, '--command="select datname from pg_database limit 1" postgres', 'postgresql_test_user')
  90. end
  91. end
  92. describe 'postgresql::grant' do
  93. it 'should grant access so a user can create in a database' do
  94. test_class = 'class {"postgresql_tests::system_default::test_grant_create": db => "postgres", user => "psql_grant_tester", password => "psql_grant_pw" }'
  95. # Run once to check for crashes
  96. sudo_and_log(vm, "puppet apply -e '#{test_class}'")
  97. # Run again to check for idempotence
  98. sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'")
  99. # Check that the user can create a table in the database
  100. sudo_psql_and_log(vm, '--command="create table foo (foo int)" postgres', 'psql_grant_tester')
  101. sudo_psql_and_log(vm, '--command="drop table foo" postgres', 'psql_grant_tester')
  102. end
  103. end
  104. describe 'postgresql::validate_db_connections' do
  105. it 'should run puppet with no changes declared if database connectivity works' do
  106. # Setup
  107. setup_class = 'class {"postgresql_tests::system_default::test_db": db => "foo" }'
  108. sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{setup_class}'; [ $? == 2 ]")
  109. # Run test
  110. test_pp = "postgresql::validate_db_connection {'foo': database_host => 'localhost', database_name => 'foo', database_username => 'foo', database_password => 'foo' }"
  111. sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_pp}'")
  112. end
  113. it 'should fail catalogue if database connectivity fails' do
  114. # Run test
  115. test_pp = "postgresql::validate_db_connection {'foo': database_host => 'localhost', database_name => 'foo', database_username => 'foo', database_password => 'foo' }"
  116. sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_pp}'; [ $? == 4 ]")
  117. end
  118. end
  119. describe 'postgresql::tablespace' do
  120. it 'should idempotently create tablespaces and databases that are using them' do
  121. test_class = 'class {"postgresql_tests::system_default::test_tablespace": }'
  122. # Run once to check for crashes
  123. sudo_and_log(vm, "puppet apply -e '#{test_class}'")
  124. # Run again to check for idempotence
  125. sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'")
  126. # Check that databases use correct tablespaces
  127. 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')
  128. 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')
  129. end
  130. end
  131. describe 'postgresql::pg_hba_rule' do
  132. it 'should create a ruleset in pg_hba.conf' do
  133. manifest = <<-EOS
  134. include postgresql::server
  135. postgresql::pg_hba_rule { "allow application network to access app database":
  136. type => "host",
  137. database => "app",
  138. user => "app",
  139. address => "200.1.2.0/24",
  140. auth_method => md5,
  141. }
  142. EOS
  143. sudo_and_log(vm, "puppet apply -e '#{manifest}'")
  144. sudo_and_log(vm, "grep '200.1.2.0/24' /etc/postgresql/*/*/pg_hba.conf || grep '200.1.2.0/24' /var/lib/pgsql/data/pg_hba.conf")
  145. end
  146. it 'should create a ruleset in pg_hba.conf that denies db access to db test1' do
  147. manifest = <<-EOS
  148. include postgresql::server
  149. postgresql::db { "test1":
  150. user => "test1",
  151. password => "test1",
  152. grant => "all",
  153. }
  154. postgresql::pg_hba_rule { "allow anyone to have access to db test1":
  155. type => "local",
  156. database => "test1",
  157. user => "test1",
  158. auth_method => reject,
  159. order => '001',
  160. }
  161. user { "test1":
  162. shell => "/bin/bash",
  163. managehome => true,
  164. }
  165. EOS
  166. sudo_and_log(vm, "puppet apply -e '#{manifest}'")
  167. sudo_and_log(vm, 'su - test1 -c \'psql -U test1 -c "\q" test1\'; [ $? == 2 ]')
  168. end
  169. end
  170. describe 'postgresql.conf include' do
  171. it "should support an 'include' directive at the end of postgresql.conf" do
  172. pending('no support for include directive with centos 5', :if => vm == :centos5)
  173. test_class = 'class {"postgresql_tests::system_default::test_pgconf_include": }'
  174. # Run once to check for crashes
  175. sudo_and_log(vm, "puppet apply -e '#{test_class}'")
  176. # Run again to check for idempotence
  177. sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'")
  178. sudo_and_log(vm, "service #{service_name} restart")
  179. # Check that the user can create a table in the database
  180. sudo_psql_and_expect_result(vm, '--command="show max_connections" -t', '123', 'postgres')
  181. cleanup_class = 'class {"postgresql_tests::system_default::test_pgconf_include_cleanup": }'
  182. sudo_and_log(vm, "puppet apply -e '#{cleanup_class}'")
  183. end
  184. end
  185. end