From 80c85e8f2b032ae2c20a34b1f39757d1a41cca70 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 26 Apr 2013 16:35:09 +0100 Subject: [PATCH] Convert system tests to use rspec-system This patch ports all of the existing system tests to use rspec-system instead. To assist with this conversion some patches were made to fix OS compatibility where necessary. We also added an ensure parameter to the postgresql::server class to assist with removing PostgreSQL configuration to aid with testing cleanups. The documentation has been updated to indicate test usage with rspec-system, we've also renamed the 'tests' directory to 'examples'. Signed-off-by: Ken Barber --- .nodeset.yml | 31 ++ Gemfile | 4 +- README.md | 36 +- Rakefile | 18 +- {tests => examples}/init.pp | 0 .../official-postgresql-repos.pp | 0 {tests => examples}/postgresql_database.pp | 0 {tests => examples}/postgresql_db.pp | 0 {tests => examples}/postgresql_grant.pp | 0 {tests => examples}/postgresql_pg_hba_rule.pp | 0 .../postgresql_pgconf_extras.pp | 0 {tests => examples}/postgresql_tablespace.pp | 0 {tests => examples}/postgresql_user.pp | 0 {tests => examples}/server.pp | 0 .../package_source/yum_postgresql_org.pp | 24 +- manifests/params.pp | 26 +- manifests/server.pp | 85 +-- spec/spec_helper_system.rb | 54 ++ spec/support/postgres_test_config.rb | 14 - spec/support/postgres_test_utils.rb | 29 -- spec/support/shared_contexts/pg_vm_context.rb | 54 -- .../shared_examples/non_default_postgres.rb | 71 --- .../system_default_postgres.rb | 236 --------- spec/support/vagrant_common.rb | 22 - spec/system/base.pp | 41 -- spec/system/distros/centos5_64/Vagrantfile | 12 - .../distros/centos5_64/non_default_pg_spec.rb | 7 - .../centos5_64/system_default_pg_spec.rb | 8 - spec/system/distros/centos6_64/Vagrantfile | 12 - .../distros/centos6_64/non_default_pg_spec.rb | 7 - .../centos6_64/system_default_pg_spec.rb | 8 - .../distros/ubuntu_lucid_64/Vagrantfile | 13 - .../ubuntu_lucid_64/non_default_pg_spec.rb | 7 - .../ubuntu_lucid_64/system_default_pg_spec.rb | 8 - spec/system/install_spec.rb | 489 ++++++++++++++++++ spec/system/non_defaults_spec.rb | 95 ++++ .../manifests/system_default/test_db.pp | 27 - .../system_default/test_grant_create.pp | 45 -- .../manifests/system_default/test_initdb.pp | 23 - .../system_default/test_pgconf_include.pp | 10 - .../test_pgconf_include_cleanup.pp | 11 - .../manifests/system_default/test_psql.pp | 30 -- .../system_default/test_ruby_psql.pp | 30 -- .../system_default/test_tablespace.pp | 68 --- .../manifests/system_default/test_user.pp | 33 -- .../classes/postgresql_java_spec.rb | 0 .../classes/postgresql_python_spec.rb | 0 47 files changed, 784 insertions(+), 904 deletions(-) create mode 100644 .nodeset.yml rename {tests => examples}/init.pp (100%) rename {tests => examples}/official-postgresql-repos.pp (100%) rename {tests => examples}/postgresql_database.pp (100%) rename {tests => examples}/postgresql_db.pp (100%) rename {tests => examples}/postgresql_grant.pp (100%) rename {tests => examples}/postgresql_pg_hba_rule.pp (100%) rename {tests => examples}/postgresql_pgconf_extras.pp (100%) rename {tests => examples}/postgresql_tablespace.pp (100%) rename {tests => examples}/postgresql_user.pp (100%) rename {tests => examples}/server.pp (100%) create mode 100644 spec/spec_helper_system.rb delete mode 100644 spec/support/postgres_test_config.rb delete mode 100644 spec/support/postgres_test_utils.rb delete mode 100644 spec/support/shared_contexts/pg_vm_context.rb delete mode 100644 spec/support/shared_examples/non_default_postgres.rb delete mode 100644 spec/support/shared_examples/system_default_postgres.rb delete mode 100644 spec/support/vagrant_common.rb delete mode 100644 spec/system/base.pp delete mode 100644 spec/system/distros/centos5_64/Vagrantfile delete mode 100644 spec/system/distros/centos5_64/non_default_pg_spec.rb delete mode 100644 spec/system/distros/centos5_64/system_default_pg_spec.rb delete mode 100644 spec/system/distros/centos6_64/Vagrantfile delete mode 100644 spec/system/distros/centos6_64/non_default_pg_spec.rb delete mode 100644 spec/system/distros/centos6_64/system_default_pg_spec.rb delete mode 100644 spec/system/distros/ubuntu_lucid_64/Vagrantfile delete mode 100644 spec/system/distros/ubuntu_lucid_64/non_default_pg_spec.rb delete mode 100644 spec/system/distros/ubuntu_lucid_64/system_default_pg_spec.rb create mode 100644 spec/system/install_spec.rb create mode 100644 spec/system/non_defaults_spec.rb delete mode 100644 spec/system/test_module/manifests/system_default/test_db.pp delete mode 100644 spec/system/test_module/manifests/system_default/test_grant_create.pp delete mode 100644 spec/system/test_module/manifests/system_default/test_initdb.pp delete mode 100644 spec/system/test_module/manifests/system_default/test_pgconf_include.pp delete mode 100644 spec/system/test_module/manifests/system_default/test_pgconf_include_cleanup.pp delete mode 100644 spec/system/test_module/manifests/system_default/test_psql.pp delete mode 100644 spec/system/test_module/manifests/system_default/test_ruby_psql.pp delete mode 100644 spec/system/test_module/manifests/system_default/test_tablespace.pp delete mode 100644 spec/system/test_module/manifests/system_default/test_user.pp rename spec/{ => unit}/classes/postgresql_java_spec.rb (100%) rename spec/{ => unit}/classes/postgresql_python_spec.rb (100%) diff --git a/.nodeset.yml b/.nodeset.yml new file mode 100644 index 0000000000..767f9cd2f6 --- /dev/null +++ b/.nodeset.yml @@ -0,0 +1,31 @@ +--- +default_set: 'centos-64-x64' +sets: + 'centos-59-x64': + nodes: + "main.foo.vm": + prefab: 'centos-59-x64' + 'centos-64-x64': + nodes: + "main.foo.vm": + prefab: 'centos-64-x64' + 'fedora-18-x64': + nodes: + "main.foo.vm": + prefab: 'fedora-18-x64' + 'debian-607-x64': + nodes: + "main.foo.vm": + prefab: 'debian-607-x64' + 'debian-70rc1-x64': + nodes: + "main.foo.vm": + prefab: 'debian-70rc1-x64' + 'ubuntu-server-10044-x64': + nodes: + "main.foo.vm": + prefab: 'ubuntu-server-10044-x64' + 'ubuntu-server-12042-x64': + nodes: + "main.foo.vm": + prefab: 'ubuntu-server-12042-x64' diff --git a/Gemfile b/Gemfile index 9d9b92081c..1e210775e1 100644 --- a/Gemfile +++ b/Gemfile @@ -3,8 +3,8 @@ source 'https://rubygems.org' group :development, :test do gem 'rake' gem 'puppetlabs_spec_helper', :require => false - gem 'vagrant', '~> 1.0.5' - gem 'sahara', '~> 0.0.13' + gem 'rspec-system-puppet', '~>1.0' + gem 'rspec-system', '>=1.2.1' gem 'puppet-lint', '~> 0.3.2' end diff --git a/README.md b/README.md index 853ae39310..6925043436 100644 --- a/README.md +++ b/README.md @@ -205,6 +205,9 @@ This setting can be used to explicitly call the initdb operation after server pa ###Class: postgresql::server Here are the options that you can set in the `config_hash` parameter of `postgresql::server`: +####`ensure` +This value default to `present`. When set to `absent` it will remove all packages, configuration and data so use this with extreme caution. + ####`postgres_password` This value defaults to `undef`, meaning the super user account in the postgres database is a user called `postgres` and this account does not have a password. If you provide this setting, the module will set the password for the `postgres` user to your specified value. @@ -504,34 +507,35 @@ You can read the complete module contribution guide [on the Puppet Labs wiki.](h ### Tests -There are two types of tests distributed with the module. The first set is the 'traditional' Puppet manifest-style smoke tests. You can use these to experiment with the module on a virtual machine or other test environment, via `puppet apply`. You should see the following files in the `tests` directory. +There are two types of tests distributed with the module. Unit tests with rspec-puppet and system tests using rspec-system. + +For unit testing, make sure you have: -In addition to these manifest-based smoke tests, there are some ruby rspec tests in the spec directory. These tests run against a VirtualBox VM, so they are actually testing the live application of the module on a real, running system. To do this, you must install and setup an [RVM](http://beginrescueend.com/) with [vagrant](http://vagrantup.com/), [sahara](https://github.com/jedi4ever/sahara), and [rspec](http://rspec.info/): +* rake +* bundler - $ curl -L get.rvm.io | bash -s stable - $ rvm install 1.9.3 - $ rvm use --create 1.9.3@puppet-postgresql - $ bundle install +Install the necessary gems: -Run the system tests: + bundle install --path=vendor - $ rake spec:system +And then run the unit tests: -Note that these tests will fire up VirtualBox VMs, and set up shared folders for the module source code from your local working copy. This means that you need to have all of the source code for the module dependencies (see the `Modulefile` for a complete list) checked out in the same parent directory where you've checked out the source for the `puppet-postgres` module. + bundle exec rake spec -The system test suite will snapshot the VM and rollback between each test. If you want to only run the tests against an individual distro, you can do run: +The unit tests are ran in Travis-CI as well, if you want to see the results of your own tests regsiter the service hook through Travis-CI via the accounts section for your Github clone of this project. - $ rspec spec/system/distros/ubuntu_lucid_64 +If you want to run the system tests, make sure you also have: -To run only a single specific test against a distro: +* vagrant > 1.2.x +* Virtualbox > 4.2.10 - $ rspec spec/system/distros/ubuntu_lucid_64 -e "should idempotently create a user who can log in" +Then run the tests using: -We also have some unit tests that utilize rspec-puppet for faster iteration if required: + bundle exec rake spec:system - $ rake spec +To run the tests on different operating systems, see the sets available in .nodeset.yml and run the specific set with the following syntax: -The unit tests are ran in Travis-CI as well, if you want to see the results of your own tests regsiter the service hook through Travis-CI via the accounts section for your Github clone of this project. + RSPEC_SET=debian-607-x64 bundle exec rake spec:system Transfer Notice ---------------- diff --git a/Rakefile b/Rakefile index a546dd519e..13af8671f2 100644 --- a/Rakefile +++ b/Rakefile @@ -1,14 +1,12 @@ -require 'puppetlabs_spec_helper/rake_tasks' -require 'puppet-lint/tasks/puppet-lint' - -PuppetLint.configuration.send("disable_80chars") +require 'rubygems' +require 'bundler/setup' -RSpec::Core::RakeTask.new(:system_test) do |c| - c.pattern = "spec/system/**/*_spec.rb" -end +Bundler.require :default +require 'puppetlabs_spec_helper/rake_tasks' +require 'rspec-system/rake_task' +require 'puppet-lint/tasks/puppet-lint' -namespace :spec do - desc 'Run system tests' - task :system => :system_test +task :default do + sh %{rake -T} end diff --git a/tests/init.pp b/examples/init.pp similarity index 100% rename from tests/init.pp rename to examples/init.pp diff --git a/tests/official-postgresql-repos.pp b/examples/official-postgresql-repos.pp similarity index 100% rename from tests/official-postgresql-repos.pp rename to examples/official-postgresql-repos.pp diff --git a/tests/postgresql_database.pp b/examples/postgresql_database.pp similarity index 100% rename from tests/postgresql_database.pp rename to examples/postgresql_database.pp diff --git a/tests/postgresql_db.pp b/examples/postgresql_db.pp similarity index 100% rename from tests/postgresql_db.pp rename to examples/postgresql_db.pp diff --git a/tests/postgresql_grant.pp b/examples/postgresql_grant.pp similarity index 100% rename from tests/postgresql_grant.pp rename to examples/postgresql_grant.pp diff --git a/tests/postgresql_pg_hba_rule.pp b/examples/postgresql_pg_hba_rule.pp similarity index 100% rename from tests/postgresql_pg_hba_rule.pp rename to examples/postgresql_pg_hba_rule.pp diff --git a/tests/postgresql_pgconf_extras.pp b/examples/postgresql_pgconf_extras.pp similarity index 100% rename from tests/postgresql_pgconf_extras.pp rename to examples/postgresql_pgconf_extras.pp diff --git a/tests/postgresql_tablespace.pp b/examples/postgresql_tablespace.pp similarity index 100% rename from tests/postgresql_tablespace.pp rename to examples/postgresql_tablespace.pp diff --git a/tests/postgresql_user.pp b/examples/postgresql_user.pp similarity index 100% rename from tests/postgresql_user.pp rename to examples/postgresql_user.pp diff --git a/tests/server.pp b/examples/server.pp similarity index 100% rename from tests/server.pp rename to examples/server.pp diff --git a/manifests/package_source/yum_postgresql_org.pp b/manifests/package_source/yum_postgresql_org.pp index 94417538ed..5781d43562 100644 --- a/manifests/package_source/yum_postgresql_org.pp +++ b/manifests/package_source/yum_postgresql_org.pp @@ -6,17 +6,25 @@ $package_version = "${version_parts[0]}${version_parts[1]}" file { "/etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG-${package_version}": - source => 'puppet:///modules/postgresql/RPM-GPG-KEY-PGDG' - } -> + source => 'puppet:///modules/postgresql/RPM-GPG-KEY-PGDG', + before => Yumrepo['yum.postgresql.org'] + } + + if($::operatingsystem == 'Fedora') { + $label1 = 'fedora' + $label2 = $label1 + } else { + $label1 = 'redhat' + $label2 = 'rhel' + } yumrepo { 'yum.postgresql.org': - descr => "PostgreSQL ${version} \$releasever - \$basearch", - baseurl => "http://yum.postgresql.org/${version}/redhat/rhel-\$releasever-\$basearch", - enabled => 1, - gpgcheck => 1, - gpgkey => "file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG-${package_version}", + descr => "PostgreSQL ${version} \$releasever - \$basearch", + baseurl => "http://yum.postgresql.org/${version}/${label1}/${label2}-\$releasever-\$basearch", + enabled => 1, + gpgcheck => 1, + gpgkey => "file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG-${package_version}", } Yumrepo['yum.postgresql.org'] -> Package<|tag == 'postgresql'|> - } diff --git a/manifests/params.pp b/manifests/params.pp index b2c862b66b..05200550e8 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -138,23 +138,27 @@ } 'Debian': { - $needs_initdb = pick($run_initdb, false) $firewall_supported = false # TODO: not exactly sure yet what the right thing to do for Debian/Ubuntu is. #$persist_firewall_command = '/sbin/iptables-save > /etc/iptables/rules.v4' - - case $::operatingsystem { - 'Debian': { - $service_name = pick($custom_service_name, 'postgresql') - } - 'Ubuntu': { - # thanks, ubuntu - if($::lsbmajdistrelease == '10' and !$manage_package_repo) { - $service_name = pick($custom_service_name, "postgresql-${version}") - } else { + if $manage_package_repo == true { + $needs_initdb = pick($run_initdb, true) + $service_name = pick($custom_service_name, 'postgresql') + } else { + $needs_initdb = pick($run_initdb, false) + case $::operatingsystem { + 'Debian': { $service_name = pick($custom_service_name, 'postgresql') } + 'Ubuntu': { + # thanks, ubuntu + if($::lsbmajdistrelease == '10') { + $service_name = pick($custom_service_name, "postgresql-${version}") + } else { + $service_name = pick($custom_service_name, 'postgresql') + } + } } } diff --git a/manifests/server.pp b/manifests/server.pp index 6986fe9eed..8c2ae1fbbe 100644 --- a/manifests/server.pp +++ b/manifests/server.pp @@ -21,50 +21,73 @@ # Sample Usage: # class postgresql::server ( + $ensure = 'present', $package_name = $postgresql::params::server_package_name, $package_ensure = 'present', $service_name = $postgresql::params::service_name, $service_provider = $postgresql::params::service_provider, $service_status = $postgresql::params::service_status, - $config_hash = {} + $config_hash = {}, + $datadir = $postgresql::params::datadir ) inherits postgresql::params { - package { 'postgresql-server': - ensure => $package_ensure, - name => $package_name, - tag => 'postgresql', - } + if ($ensure == 'absent') { + service { 'postgresqld': + ensure => stopped, + name => $service_name, + enable => false, + provider => $service_provider, + hasstatus => true, + status => $service_status, + }-> + package { 'postgresql-server': + ensure => purged, + name => $package_name, + tag => 'postgresql', + }-> + file { $datadir: + ensure => absent, + recurse => true, + force => true, + } + } else { + package { 'postgresql-server': + ensure => $package_ensure, + name => $package_name, + tag => 'postgresql', + } - $config_class = { - 'postgresql::config' => $config_hash, - } + $config_class = { + 'postgresql::config' => $config_hash, + } - create_resources( 'class', $config_class ) + create_resources( 'class', $config_class ) + service { 'postgresqld': + ensure => running, + name => $service_name, + enable => true, + require => Package['postgresql-server'], + provider => $service_provider, + hasstatus => true, + status => $service_status, + } - service { 'postgresqld': - ensure => running, - name => $service_name, - enable => true, - require => Package['postgresql-server'], - provider => $service_provider, - hasstatus => true, - status => $service_status, - } + if ($postgresql::params::needs_initdb) { + include postgresql::initdb - if ($postgresql::params::needs_initdb) { - include postgresql::initdb + Package['postgresql-server'] -> Class['postgresql::initdb'] -> Class['postgresql::config'] -> Service['postgresqld'] + } + else { + Package['postgresql-server'] -> Class['postgresql::config'] -> Service['postgresqld'] + } - Package['postgresql-server'] -> Class['postgresql::initdb'] -> Class['postgresql::config'] -> Service['postgresqld'] - } - else { - Package['postgresql-server'] -> Class['postgresql::config'] -> Service['postgresqld'] + exec { 'reload_postgresql': + path => '/usr/bin:/usr/sbin:/bin:/sbin', + command => "service ${service_name} reload", + onlyif => $service_status, + refreshonly => true, + } } - exec { 'reload_postgresql': - path => '/usr/bin:/usr/sbin:/bin:/sbin', - command => "service ${service_name} reload", - onlyif => $service_status, - refreshonly => true, - } } diff --git a/spec/spec_helper_system.rb b/spec/spec_helper_system.rb new file mode 100644 index 0000000000..7fdb9e30c2 --- /dev/null +++ b/spec/spec_helper_system.rb @@ -0,0 +1,54 @@ +require 'rspec-system/spec_helper' +require 'rspec-system-puppet/helpers' +require 'tempfile' + +module LocalHelpers + include RSpecSystem::Util + + def psql(psql_cmd, user = 'postgres', &block) + psql = "psql #{psql_cmd}" + shell("su #{shellescape(user)} -c #{shellescape(psql)}", &block) + end +end + +include RSpecSystemPuppet::Helpers + +RSpec.configure do |c| + # Project root for the firewall code + proj_root = File.expand_path(File.join(File.dirname(__FILE__), '..')) + + # Enable colour in Jenkins + c.tty = true + + # Include in our local helpers + c.include ::LocalHelpers + + # Puppet helpers + c.include RSpecSystemPuppet::Helpers + c.extend RSpecSystemPuppet::Helpers + + # This is where we 'setup' the nodes before running our tests + c.before :suite do + # Install puppet + puppet_install + + # Copy this module into the module path of the test node + puppet_module_install(:source => proj_root, :module_name => 'postgresql') + shell('puppet module install puppetlabs/stdlib') + shell('puppet module install puppetlabs/firewall') + shell('puppet module install puppetlabs/apt') + shell('puppet module install ripienaar/concat') + + file = Tempfile.new('foo') + begin + file.write(<<-EOS) +--- +:logger: noop + EOS + file.close + rcp(:sp => file.path, :dp => '/etc/puppet/hiera.yaml') + ensure + file.unlink + end + end +end diff --git a/spec/support/postgres_test_config.rb b/spec/support/postgres_test_config.rb deleted file mode 100644 index b2c5332493..0000000000 --- a/spec/support/postgres_test_config.rb +++ /dev/null @@ -1,14 +0,0 @@ -module PostgresTestConfig - # Tests are pretty slow w/sahara, and when we destroy the VMs at the beginning - # of the test run. This can be a hindrance for development but is very - # valuable for final testing. This constant allows you to toggle between - # strict testing and less strict testing--the latter being useful for - # development purposes. - HardCoreTesting = true - - # If this value is set to true, then each VM will be suspended after the tests - # against it are completed. This will slow things down a ton during - # iterative development, but will save a lot of system resources by not - # keeping all of the VMs running at the same time. - SuspendVMsAfterSuite = true -end diff --git a/spec/support/postgres_test_utils.rb b/spec/support/postgres_test_utils.rb deleted file mode 100644 index 28efeb0e54..0000000000 --- a/spec/support/postgres_test_utils.rb +++ /dev/null @@ -1,29 +0,0 @@ -module PostgresTestUtils - def sudo_and_log(vm, cmd) - @logger.debug("Running command: '#{cmd}'") - result = "" - @env.vms[vm].channel.sudo("cd /tmp && #{cmd}") do |ch, data| - result << data - @logger.debug(data) - end - result - end - - def sudo_psql_and_log(vm, psql_cmd, user = 'postgres', extras = '') - sudo_and_log(vm, "su #{user} -c 'psql #{psql_cmd}' #{extras}") - end - - def sudo_psql_and_expect_result(vm, psql_cmd, expected, user = 'postgres') - result = sudo_and_log(vm, "su #{user} -c 'psql -t #{psql_cmd}'") - - result.sub!(/stdin: is not a tty/, '') - result.strip! - - ok = result == expected - @logger.debug("Expected: #{expected} => #{ok ? 'OK' : 'BAD'}") - - if !ok - raise "An unexpected result returned - result: '#{result}' / expected: '#{expected}'" - end - end -end diff --git a/spec/support/shared_contexts/pg_vm_context.rb b/spec/support/shared_contexts/pg_vm_context.rb deleted file mode 100644 index d5a95caed7..0000000000 --- a/spec/support/shared_contexts/pg_vm_context.rb +++ /dev/null @@ -1,54 +0,0 @@ -require 'logger' -require 'vagrant' -require 'support/postgres_test_config' - - -if PostgresTestConfig::HardCoreTesting - # this will just make sure that we throw an error if the user tries to - # run w/o having Sahara installed - require 'sahara' -end - -shared_context :pg_vm_context do - before(:all) do - @logger = Logger.new(STDOUT) - @logger.level = Logger::DEBUG # TODO: get from environment or rspec? - - @env = Vagrant::Environment::new(:cwd => vagrant_dir) - - if PostgresTestConfig::HardCoreTesting - @env.cli("destroy", vm.to_s, "--force") # Takes too long - end - - @env.cli("up", vm.to_s) - - if PostgresTestConfig::HardCoreTesting - sudo_and_log(vm, 'if [ "$(facter osfamily)" == "Debian" ] ; then apt-get update ; fi') - end - - install_postgres - - #sudo_and_log(vm, 'puppet apply -e "include postgresql::server"') - - if PostgresTestConfig::HardCoreTesting - # Sahara ignores :cwd so we have to chdir for now, see https://github.com/jedi4ever/sahara/issues/9 - Dir.chdir(vagrant_dir) - @env.cli("sandbox", "on", vm.to_s) - end - end - - after(:all) do - if PostgresTestConfig::SuspendVMsAfterSuite - @logger.debug("Suspending VM") - @env.cli("suspend", vm.to_s) - end - end - - - after(:each) do - if PostgresTestConfig::HardCoreTesting - @env.cli("sandbox", "rollback", vm.to_s) - end - end - -end diff --git a/spec/support/shared_examples/non_default_postgres.rb b/spec/support/shared_examples/non_default_postgres.rb deleted file mode 100644 index f7d070f170..0000000000 --- a/spec/support/shared_examples/non_default_postgres.rb +++ /dev/null @@ -1,71 +0,0 @@ -require 'support/postgres_test_utils' -require 'support/shared_contexts/pg_vm_context' - -shared_examples :non_default_postgres do - include PostgresTestUtils - include_context :pg_vm_context - - # this method is required by the pg_vm shared context - def install_postgres; end - - describe 'postgresql global config' do - it 'with version and manage_package_repo set, we should be able to idempotently create a db that we can connect to' do - manifest = <<-EOS - # Configure version and manage_package_repo globally, install postgres - # and then try to install a new database. - class { "postgresql": - version => "9.2", - manage_package_repo => true, - package_source => "yum.postgresql.org", - }-> - class { "postgresql::server": }-> - postgresql::db { "postgresql_test_db": - user => "foo1", - password => "foo1", - } - EOS - - begin - # Run once to check for crashes - sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{manifest}'; [ $? = 2 ]") - - # Run again to check for idempotence - sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{manifest}'") - - # 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') - end - end - - it 'with locale and charset, the postgres database should reflect that locale' do - pending('no support for initdb with lucid', :if => vm == :lucid) - pending('no support for locale parameter with centos 5', :if => vm == :centos5) - - manifest = <<-EOS - # Set global locale and charset option, and try installing postgres - class { 'postgresql': - locale => 'en_NG', - charset => 'UTF8', - }-> - class { 'postgresql::server': } - EOS - - # Run once to check for crashes - sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{manifest}'; [ $? = 2 ]") - # Run again to check for idempotence - sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{manifest}'") - - # Use the command line to create the database, instead of the puppet way - # to bypass any puppet effects and make sure the default works _always_. - sudo_and_log(vm, "su postgres -c 'createdb test1'") - - # Check locale of database 'postgres' to ensure initdb did the correct - # thing. - sudo_and_log(vm, 'su postgres -c \'psql -c "show lc_ctype" test1\'') - sudo_and_log(vm, 'su postgres -c \'psql -c "show lc_ctype" test1\' | grep en_NG') - sudo_and_log(vm, 'su postgres -c \'psql -c "show lc_collate" test1\' | grep en_NG') - end - end -end diff --git a/spec/support/shared_examples/system_default_postgres.rb b/spec/support/shared_examples/system_default_postgres.rb deleted file mode 100644 index 7320269423..0000000000 --- a/spec/support/shared_examples/system_default_postgres.rb +++ /dev/null @@ -1,236 +0,0 @@ -require 'support/postgres_test_utils' -require 'support/shared_contexts/pg_vm_context' - -shared_examples :system_default_postgres do - include PostgresTestUtils - include_context :pg_vm_context - - # this method is required by the pg_vm shared context - def install_postgres - sudo_and_log(vm, 'puppet apply -e "include postgresql::server"') - end - - describe 'postgresql::initdb' do - it "should idempotently create a working --pgdata directory so postgres can run" do - @logger.info("starting") - - # A bare-minimum class to initdb the specified dir - test_class = 'class {"postgresql_tests::system_default::test_initdb": }' - - # Run once to check for crashes - sudo_and_log(vm, "puppet apply -e '#{test_class}'") - - # Run again to check for idempotence via --detailed-exitcodes - sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'") - - sudo_and_log(vm, "service #{service_name} restart") - - # Connect to it and list the databases - sudo_psql_and_log(vm, '--list --tuples-only') - end - end - - describe 'postgresql::db' do - it 'should idempotently create a db that we can connect to' do - - # 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" }' - - begin - # Run once to check for crashes - sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'; [ $? == 2 ]") - - # Run again to check for idempotence - sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'") - - # 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') - end - end - - it 'should take a locale parameter' do - pending('no support for locale parameter with centos 5', :if => vm == :centos5) - manifest = <<-EOS - include postgresql::server - postgresql::db { 'test1': - user => 'test1', - password => 'test1', - charset => 'UTF8', - locale => 'en_NG', - } - EOS - sudo_and_log(vm, "puppet apply -e '#{manifest}'") - - # Some basic tests here to check if the db indeed was created with the - # correct locale. - sudo_and_log(vm, 'su postgres -c \'psql -c "show lc_ctype" test1\'') - sudo_and_log(vm, 'su postgres -c \'psql -c "show lc_ctype" test1\' | grep en_NG') - sudo_and_log(vm, 'su postgres -c \'psql -c "show lc_collate" test1\' | grep en_NG') - end - end - - 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" }' - - data = sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'; [ $? == 2 ]") - - data.should match /postgresql::psql is deprecated/ - - end - end - - 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" }' - - # Run once to get all packages set up - sudo_and_log(vm, "puppet apply -e '#{test_class}'") - - # Check for exit code 2 - sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}' ; [ $? == 2 ]") - end - - 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" }' - - # Run once to get all packages set up - sudo_and_log(vm, "puppet apply -e '#{test_class}'") - - # Check for exit code 0 - sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'") - end - - end - - 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" }' - - # 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 the user can log in - sudo_psql_and_log(vm, '--command="select datname from pg_database limit 1" postgres', 'postgresql_test_user') - end - end - - 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" }' - - # 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 the user can create a table in the database - sudo_psql_and_log(vm, '--command="create table foo (foo int)" postgres', 'psql_grant_tester') - - sudo_psql_and_log(vm, '--command="drop table foo" postgres', 'psql_grant_tester') - end - end - - 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 - - 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 - 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') - end - end - - describe 'postgresql::pg_hba_rule' do - it 'should create a ruleset in pg_hba.conf' do - manifest = <<-EOS - include postgresql::server - postgresql::pg_hba_rule { "allow application network to access app database": - type => "host", - database => "app", - user => "app", - address => "200.1.2.0/24", - auth_method => md5, - } - EOS - sudo_and_log(vm, "puppet apply -e '#{manifest}'") - 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") - end - - it 'should create a ruleset in pg_hba.conf that denies db access to db test1' do - manifest = <<-EOS - include postgresql::server - postgresql::db { "test1": - user => "test1", - password => "test1", - grant => "all", - } - postgresql::pg_hba_rule { "allow anyone to have access to db test1": - type => "local", - database => "test1", - user => "test1", - auth_method => reject, - order => '001', - } - user { "test1": - shell => "/bin/bash", - managehome => true, - } - EOS - sudo_and_log(vm, "puppet apply -e '#{manifest}'") - sudo_and_log(vm, 'su - test1 -c \'psql -U test1 -c "\q" test1\'; [ $? == 2 ]') - end - end - - describe 'postgresql.conf include' do - it "should support an 'include' directive at the end of postgresql.conf" do - pending('no support for include directive with centos 5', :if => vm == :centos5) - - 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}'") - - sudo_and_log(vm, "service #{service_name} restart") - - # Check that the user can create a table in the database - sudo_psql_and_expect_result(vm, '--command="show max_connections" -t', '123', 'postgres') - - cleanup_class = 'class {"postgresql_tests::system_default::test_pgconf_include_cleanup": }' - sudo_and_log(vm, "puppet apply -e '#{cleanup_class}'") - end - end -end diff --git a/spec/support/vagrant_common.rb b/spec/support/vagrant_common.rb deleted file mode 100644 index fd38e99aca..0000000000 --- a/spec/support/vagrant_common.rb +++ /dev/null @@ -1,22 +0,0 @@ -def apply_common_vagrant_config(config) - # Resolve DNS via NAT - config.vm.customize ["modifyvm", :id, "--natdnshostresolver1", "on"] - - # Share the various required modules - # TODO: it would be better to install this via the puppet module tool - config.vm.share_folder "puppetlabs-stdlib-module", "/usr/share/puppet/modules/stdlib", "../../../../../puppetlabs-stdlib" - config.vm.share_folder "puppetlabs-apt-module", "/usr/share/puppet/modules/apt", "../../../../../puppetlabs-apt" - config.vm.share_folder "ripienaar-concat-module", "/usr/share/puppet/modules/concat", "../../../../../puppet-concat" - - # Share the postgressql module - config.vm.share_folder "puppet-postgresql-module", "/usr/share/puppet/modules/postgresql", "../../../.." - - # Share the module of test classes - config.vm.share_folder "puppet-postgresql-tests", "/usr/share/puppet/modules/postgresql_tests", "../../test_module" - - # Provision with a base puppet config just so we don't have to repeat the puppet user/group - config.vm.provision :puppet do |puppet| - puppet.manifests_path = "../../" - puppet.manifest_file = "base.pp" - end -end diff --git a/spec/system/base.pp b/spec/system/base.pp deleted file mode 100644 index 025cfcd718..0000000000 --- a/spec/system/base.pp +++ /dev/null @@ -1,41 +0,0 @@ -# puppet-postgresql -# For all details and documentation: -# http://github.com/inkling/puppet-postgresql -# -# Copyright 2012- Inkling Systems, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -group {'puppet': - ensure => present, -} - -user {'puppet': - ensure => present, - gid => 'puppet', -} - -file { '/etc/sudoers.d': - ensure => directory, - mode => '0750', - owner => root, - group => root, -} - -file {'/etc/sudoers.d/puppet_postgresql_tests': - ensure => file, - content => 'vagrant ALL=(ALL) ALL', - mode => '0440', - owner => root, - group => root, -} diff --git a/spec/system/distros/centos5_64/Vagrantfile b/spec/system/distros/centos5_64/Vagrantfile deleted file mode 100644 index a5a89b20ae..0000000000 --- a/spec/system/distros/centos5_64/Vagrantfile +++ /dev/null @@ -1,12 +0,0 @@ -require File.expand_path(File.join(__FILE__, '../../../../support/vagrant_common')) - -Vagrant::Config.run do |config| - - config.vm.define :centos5 do |vm_config| - vm_config.vm.box = "centos-58-x64" - vm_config.vm.box_url = "http://puppet-vagrant-boxes.puppetlabs.com/centos-58-x64.box" - end - - apply_common_vagrant_config(config) - -end diff --git a/spec/system/distros/centos5_64/non_default_pg_spec.rb b/spec/system/distros/centos5_64/non_default_pg_spec.rb deleted file mode 100644 index 8845f2f102..0000000000 --- a/spec/system/distros/centos5_64/non_default_pg_spec.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'support/shared_examples/non_default_postgres' - -describe "CentOS 5, 64-bit: non-default postgres" do - let(:vagrant_dir) { File.dirname(__FILE__) } - let(:vm) { :centos5 } - it_behaves_like :non_default_postgres -end diff --git a/spec/system/distros/centos5_64/system_default_pg_spec.rb b/spec/system/distros/centos5_64/system_default_pg_spec.rb deleted file mode 100644 index b74f5929b2..0000000000 --- a/spec/system/distros/centos5_64/system_default_pg_spec.rb +++ /dev/null @@ -1,8 +0,0 @@ -require 'support/shared_examples/system_default_postgres' - -describe "CentOS 5, 64-bit: default system postgres" do - let(:vagrant_dir) { File.dirname(__FILE__) } - let(:vm) { :centos5 } - let(:service_name) { 'postgresql' } - it_behaves_like :system_default_postgres -end diff --git a/spec/system/distros/centos6_64/Vagrantfile b/spec/system/distros/centos6_64/Vagrantfile deleted file mode 100644 index 53f7929b19..0000000000 --- a/spec/system/distros/centos6_64/Vagrantfile +++ /dev/null @@ -1,12 +0,0 @@ -require File.expand_path(File.join(__FILE__, '../../../../support/vagrant_common')) - -Vagrant::Config.run do |config| - - config.vm.define :centos6 do |vm_config| - vm_config.vm.box = "cent63_64" - vm_config.vm.box_url = "https://dl.dropbox.com/u/7225008/Vagrant/CentOS-6.3-x86_64-minimal.box" - end - - apply_common_vagrant_config(config) - -end diff --git a/spec/system/distros/centos6_64/non_default_pg_spec.rb b/spec/system/distros/centos6_64/non_default_pg_spec.rb deleted file mode 100644 index 89e841e4e6..0000000000 --- a/spec/system/distros/centos6_64/non_default_pg_spec.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'support/shared_examples/non_default_postgres' - -describe "CentOS6, 64-bit: non-default postgres" do - let(:vagrant_dir) { File.dirname(__FILE__) } - let(:vm) { :centos6 } - it_behaves_like :non_default_postgres -end diff --git a/spec/system/distros/centos6_64/system_default_pg_spec.rb b/spec/system/distros/centos6_64/system_default_pg_spec.rb deleted file mode 100644 index fef5b804df..0000000000 --- a/spec/system/distros/centos6_64/system_default_pg_spec.rb +++ /dev/null @@ -1,8 +0,0 @@ -require 'support/shared_examples/system_default_postgres' - -describe "CentOS6, 64-bit: default system postgres" do - let(:vagrant_dir) { File.dirname(__FILE__) } - let(:vm) { :centos6 } - let(:service_name) { 'postgresql' } - it_behaves_like :system_default_postgres -end diff --git a/spec/system/distros/ubuntu_lucid_64/Vagrantfile b/spec/system/distros/ubuntu_lucid_64/Vagrantfile deleted file mode 100644 index 172acb5529..0000000000 --- a/spec/system/distros/ubuntu_lucid_64/Vagrantfile +++ /dev/null @@ -1,13 +0,0 @@ -require File.expand_path(File.join(__FILE__, '../../../../support/vagrant_common')) - -Vagrant::Config.run do |config| - - config.vm.define :lucid do |vm_config| - # Test on 64 bit lucid - vm_config.vm.box = "lucid64" - vm_config.vm.box_url = "http://files.vagrantup.com/lucid64.box" - end - - apply_common_vagrant_config(config) - -end diff --git a/spec/system/distros/ubuntu_lucid_64/non_default_pg_spec.rb b/spec/system/distros/ubuntu_lucid_64/non_default_pg_spec.rb deleted file mode 100644 index ca54ba0591..0000000000 --- a/spec/system/distros/ubuntu_lucid_64/non_default_pg_spec.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'support/shared_examples/non_default_postgres' - -describe "Ubuntu Lucid, 64-bit: non-default postgres" do - let(:vagrant_dir) { File.dirname(__FILE__) } - let(:vm) { :lucid } - it_behaves_like :non_default_postgres -end diff --git a/spec/system/distros/ubuntu_lucid_64/system_default_pg_spec.rb b/spec/system/distros/ubuntu_lucid_64/system_default_pg_spec.rb deleted file mode 100644 index 4d2bd12db6..0000000000 --- a/spec/system/distros/ubuntu_lucid_64/system_default_pg_spec.rb +++ /dev/null @@ -1,8 +0,0 @@ -require 'support/shared_examples/system_default_postgres' - -describe "Ubuntu Lucid, 64-bit: default system postgres" do - let(:vagrant_dir) { File.dirname(__FILE__) } - let(:vm) { :lucid } - let(:service_name) { 'postgresql-8.4' } - it_behaves_like :system_default_postgres -end diff --git a/spec/system/install_spec.rb b/spec/system/install_spec.rb new file mode 100644 index 0000000000..1cfddbff50 --- /dev/null +++ b/spec/system/install_spec.rb @@ -0,0 +1,489 @@ +require 'spec_helper_system' + +describe 'install:' do + after :all do + # Cleanup after tests have ran + puppet_apply("class { 'postgresql::server': ensure => absent }") do |r| + r.exit_code.should_not == 1 + end + end + + it 'test postgresql::server' do + pp = <<-EOS + class { 'postgresql::server': } + EOS + + puppet_apply(pp) do |r| + r.exit_code.should_not == 1 + end + + puppet_apply(pp) do |r| + r.exit_code.should be_zero + end + end + + describe 'postgresql::db' do + it 'should idempotently create a db that we can connect to' do + begin + pp = <<-EOS + $db = 'postgresql_test_db' + include postgresql::server + + postgresql::db { $db: + user => $db, + password => $db, + } + EOS + + puppet_apply(pp) do |r| + r.exit_code.should_not == 1 + end + + puppet_apply(pp) do |r| + r.exit_code.should == 0 + end + + psql('--command="select datname from pg_database" postgresql_test_db') do |r| + r.stdout.should =~ /postgresql_test_db/ + r.stderr.should be_empty + r.exit_code.should == 0 + end + ensure + psql('--command="drop database postgresql_test_db" postgres') + end + end + + it 'should take a locale parameter' do + pending('no support for locale parameter with centos 5', :if => (node.facts['osfamily'] == 'RedHat' and node.facts['lsbmajdistrelease'] == '5')) + begin + pp = <<-EOS + class { 'postgresql::server': } + if($::operatingsystem == 'Debian') { + # Need to make sure the correct locale is installed first + file { '/etc/locale.gen': + content => "en_US ISO-8859-1\nen_NG UTF-8\n", + }~> + exec { '/usr/sbin/locale-gen': + logoutput => true, + refreshonly => true, + } + } + postgresql::db { 'test1': + user => 'test1', + password => 'test1', + charset => 'UTF8', + locale => 'en_NG', + } + EOS + + puppet_apply(pp) do |r| + r.exit_code.should_not == 1 + end + + puppet_apply(pp) do |r| + r.exit_code.should == 0 + end + + psql('-c "show lc_ctype" test1') do |r| + r.stdout.should =~ /en_NG/ + end + + psql('-c "show lc_collate" test1') do |r| + r.stdout.should =~ /en_NG/ + end + ensure + psql('--command="drop database test1" postgres') + end + end + end + + describe 'postgresql::psql' do + it 'should work but emit a deprecation warning' do + pp = <<-EOS + include postgresql::server + + postgresql::psql { 'foobar': + db => 'postgres', + user => 'postgres', + command => 'select * from pg_database limit 1', + unless => 'select 1 where 1=1', + require => Class['postgresql::server'], + } + EOS + + puppet_apply(pp) do |r| + r.exit_code.should_not == 1 + r.stdout.should =~ /postgresql::psql is deprecated/ + end + + puppet_apply(pp) do |r| + r.exit_code.should == 2 + r.stdout.should =~ /postgresql::psql is deprecated/ + end + end + end + + describe 'postgresql_psql' do + it 'should run some SQL when the unless query returns no rows' do + pp = <<-EOS + include postgresql::server + + postgresql_psql { 'foobar': + db => 'postgres', + psql_user => 'postgres', + command => 'select 1', + unless => 'select 1 where 1=2', + require => Class['postgresql::server'], + } + EOS + + puppet_apply(pp) do |r| + r.exit_code.should_not == 1 + end + + puppet_apply(pp) do |r| + r.exit_code.should == 2 + end + end + + it 'should not run SQL when the unless query returns rows' do + pp = <<-EOS + include 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 + + puppet_apply(pp) do |r| + r.exit_code.should_not == 1 + end + + puppet_apply(pp) do |r| + r.exit_code.should be_zero + end + end + end + + describe 'postgresql::user' do + it 'should idempotently create a user who can log in' do + pp = <<-EOS + $user = "postgresql_test_user" + $password = "postgresql_test_password" + + include postgresql::server + + # Since we are not testing pg_hba or any of that, make a local user for ident auth + user { $user: + ensure => present, + } + + postgresql::database_user { $user: + password_hash => postgresql_password($user, $password), + require => [ Class['postgresql::server'], + User[$user] ], + } + EOS + + puppet_apply(pp) do |r| + r.exit_code.should_not == 1 + end + + puppet_apply(pp) do |r| + r.exit_code.should be_zero + end + + # Check that the user can log in + psql('--command="select datname from pg_database" postgres', 'postgresql_test_user') do |r| + r.stdout.should =~ /template1/ + r.stderr.should be_empty + r.exit_code.should == 0 + end + end + end + + describe 'postgresql::grant' do + it 'should grant access so a user can create in a database' do + begin + pp = <<-EOS + $db = 'postgres' + $user = 'psql_grant_tester' + $password = 'psql_grant_pw' + + include postgresql::server + + # Since we are not testing pg_hba or any of that, make a local user for ident auth + user { $user: + ensure => present, + } + + postgresql::database_user { $user: + password_hash => postgresql_password($user, $password), + require => [ Class['postgresql::server'], + User[$user] ], + } + + postgresql::database { $db: + require => Class['postgresql::server'], + } + + postgresql::database_grant { 'grant create test': + privilege => 'CREATE', + db => $db, + role => $user, + require => [ Postgresql::Database[$db], + Postgresql::Database_user[$user] ], + } + EOS + + puppet_apply(pp) do |r| + r.exit_code.should_not == 1 + end + + puppet_apply(pp) do |r| + r.exit_code.should be_zero + end + + # Check that the user can create a table in the database + psql('--command="create table foo (foo int)" postgres', 'psql_grant_tester') do |r| + r.stdout.should =~ /CREATE TABLE/ + r.stderr.should be_empty + r.exit_code.should == 0 + end + ensure + psql('--command="drop table foo" postgres', 'psql_grant_tester') + end + end + end + + describe 'postgresql::validate_db_connections' do + it 'should run puppet with no changes declared if database connectivity works' do + pp = <<-EOS + $db = 'foo' + include postgresql::server + + postgresql::db { $db: + user => $db, + password => $db, + } + EOS + + puppet_apply(pp) do |r| + r.exit_code.should_not == 1 + end + + puppet_apply(pp) do |r| + r.exit_code.should be_zero + end + + pp = <<-EOS + 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 be_zero + end + end + + it 'should fail catalogue if database connectivity fails' do + pp = <<-EOS + 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 + + describe 'postgresql::tablespace' do + it 'should idempotently create tablespaces and databases that are using them' do + pp = <<-EOS + include postgresql::server + + file { '/tmp/pg_tablespaces': + ensure => 'directory', + owner => 'postgres', + group => 'postgres', + mode => '0700', + }~> + # This works around rubies that lack Selinux support, I'm looking at you RHEL5 + exec { "chcon -u system_u -r object_r -t postgresql_db_t /tmp/pg_tablespaces": + refreshonly => true, + path => "/bin:/usr/bin", + onlyif => "which chcon", + before => File["/tmp/pg_tablespaces/space1", "/tmp/pg_tablespaces/space2"] + } + + postgresql::tablespace{ 'tablespace1': + location => '/tmp/pg_tablespaces/space1', + require => [Class['postgresql::server'], File['/tmp/pg_tablespaces']], + } + postgresql::database{ 'tablespacedb1': + charset => 'utf8', + tablespace => 'tablespace1', + require => Postgresql::Tablespace['tablespace1'], + } + postgresql::db{ 'tablespacedb2': + user => 'dbuser2', + password => 'dbuser2', + tablespace => 'tablespace1', + require => Postgresql::Tablespace['tablespace1'], + } + + postgresql::database_user{ 'spcuser': + password_hash => postgresql_password('spcuser', 'spcuser'), + require => Class['postgresql::server'], + } + postgresql::tablespace{ 'tablespace2': + location => '/tmp/pg_tablespaces/space2', + owner => 'spcuser', + require => [Postgresql::Database_user['spcuser'], File['/tmp/pg_tablespaces']], + } + postgresql::database{ 'tablespacedb3': + charset => 'utf8', + tablespace => 'tablespace2', + require => Postgresql::Tablespace['tablespace2'], + } + EOS + + puppet_apply(pp) do |r| + r.exit_code.should_not == 1 + end + + puppet_apply(pp) do |r| + r.exit_code.should == 0 + end + + # Check that databases use correct tablespaces + psql('--command="select ts.spcname from pg_database db, pg_tablespace ts where db.dattablespace = ts.oid and db.datname = \'"\'tablespacedb1\'"\'"') do |r| + r.stdout.should =~ /tablespace1/ + r.stderr.should be_empty + r.exit_code.should == 0 + end + + psql('--command="select ts.spcname from pg_database db, pg_tablespace ts where db.dattablespace = ts.oid and db.datname = \'"\'tablespacedb3\'"\'"') do |r| + r.stdout.should =~ /tablespace2/ + r.stderr.should be_empty + r.exit_code.should == 0 + end + end + end + + describe 'postgresql::pg_hba_rule' do + it 'should create a ruleset in pg_hba.conf' do + pp = <<-EOS + include postgresql::server + postgresql::pg_hba_rule { "allow application network to access app database": + type => "host", + database => "app", + user => "app", + address => "200.1.2.0/24", + auth_method => md5, + } + EOS + + puppet_apply(pp) do |r| + r.exit_code.should_not == 1 + end + + puppet_apply(pp) do |r| + r.exit_code.should be_zero + end + + shell("grep '200.1.2.0/24' /etc/postgresql/*/*/pg_hba.conf || grep '200.1.2.0/24' /var/lib/pgsql/data/pg_hba.conf") do |r| + r.exit_code.should be_zero + end + end + + it 'should create a ruleset in pg_hba.conf that denies db access to db test1' do + pp = <<-EOS + include postgresql::server + postgresql::db { "test1": + user => "test1", + password => "test1", + grant => "all", + } + postgresql::pg_hba_rule { "allow anyone to have access to db test1": + type => "local", + database => "test1", + user => "test1", + auth_method => reject, + order => '001', + } + user { "test1": + shell => "/bin/bash", + managehome => true, + } + EOS + puppet_apply(pp) do |r| + r.exit_code.should_not == 1 + end + + shell('su - test1 -c \'psql -U test1 -c "\q" test1\'') do |r| + r.exit_code.should == 2 + end + end + end + + describe 'postgresql.conf include' do + it "should support an 'include' directive at the end of postgresql.conf" do + pending('no support for include directive with centos 5/postgresql 8.1', :if => (node.facts['osfamily'] == 'RedHat' and node.facts['lsbmajdistrelease'] == '5')) + pp = <<-EOS + class pg_test { + class { 'postgresql::server': } + + $pg_conf_include_file = "${postgresql::params::confdir}/postgresql_puppet_extras.conf" + + file { $pg_conf_include_file : + content => 'max_connections = 123', + notify => Service['postgresqld'], + } + } + class { 'pg_test': } + EOS + + puppet_apply(pp) do |r| + r.exit_code.should_not == 1 + end + + puppet_apply(pp) do |r| + r.exit_code.should be_zero + end + + psql('--command="show max_connections" -t') do |r| + r.stdout.should =~ /123/ + r.stderr.should be_empty + r.exit_code.should be_zero + end + + pp = <<-EOS + class cleanup { + require postgresql::params + + $pg_conf_include_file = "${postgresql::params::confdir}/postgresql_puppet_extras.conf" + + file { $pg_conf_include_file : + ensure => absent + } + } + class { 'cleanup': } + EOS + puppet_apply(pp) + end + end +end diff --git a/spec/system/non_defaults_spec.rb b/spec/system/non_defaults_spec.rb new file mode 100644 index 0000000000..b8321ace39 --- /dev/null +++ b/spec/system/non_defaults_spec.rb @@ -0,0 +1,95 @@ +require 'spec_helper_system' + +describe 'non defaults:' do + context 'test installing non-default version of postgresql' do + after :each do + # Cleanup + psql('--command="drop database postgresql_test_db" postgres') + pp = <<-EOS + class { "postgresql": + version => "9.2", + manage_package_repo => true, + }-> + class { 'postgresql::server': + ensure => absent, + service_status => 'service postgresql-9.2 status', + } + EOS + puppet_apply(pp) + end + + it 'perform installation and create a db' do + pp = <<-EOS + # Configure version and manage_package_repo globally, install postgres + # and then try to install a new database. + class { "postgresql": + version => "9.2", + manage_package_repo => true, + }-> + class { "postgresql::server": }-> + postgresql::db { "postgresql_test_db": + user => "foo1", + password => "foo1", + } + EOS + + puppet_apply(pp) do |r| + # Currently puppetlabs/apt shows deprecated messages + #r.stderr.should be_empty + [2,6].should include(r.exit_code) + end + + puppet_apply(pp) do |r| + # Currently puppetlabs/apt shows deprecated messages + #r.stderr.should be_empty + # It also returns a 4 + [0,4].should include(r.exit_code) + end + + psql('postgresql_test_db --command="select datname from pg_database limit 1"') + end + end + + context 'override locale and charset' do + it 'perform installation with different locale and charset' do + puts node.facts.inspect + pending('no support for locale parameter with centos 5', :if => (node.facts['osfamily'] == 'RedHat' and node.facts['lsbmajdistrelease'] == '5')) + pending('no support for initdb with debian/ubuntu', :if => (node.facts['osfamily'] == 'Debian')) + + # TODO: skip for ubuntu and centos 5 + pp = <<-EOS + # Set global locale and charset option, and try installing postgres + class { 'postgresql': + locale => 'en_NG', + charset => 'UTF8', + }-> + class { 'postgresql::server': } + EOS + + puppet_apply(pp) do |r| + # Currently puppetlabs/apt shows deprecated messages + #r.stderr.should be_empty + # It also returns a 6 + [2,6].should include(r.exit_code) + end + + puppet_apply(pp) do |r| + # Currently puppetlabs/apt shows deprecated messages + #r.stderr.should be_empty + # It also returns a 2 + [0,4].should include(r.exit_code) + end + + # Remove db first, if it exists for some reason + shell('su postgres -c "dropdb test1"') + shell('su postgres -c "createdb test1"') + shell('su postgres -c \'psql -c "show lc_ctype" test1\'') do |r| + r.stdout.should =~ /en_NG/ + end + + shell('su postgres -c \'psql -c "show lc_collate" test1\'') do |r| + r.stdout.should =~ /en_NG/ + end + end + end +end diff --git a/spec/system/test_module/manifests/system_default/test_db.pp b/spec/system/test_module/manifests/system_default/test_db.pp deleted file mode 100644 index 7a44faa822..0000000000 --- a/spec/system/test_module/manifests/system_default/test_db.pp +++ /dev/null @@ -1,27 +0,0 @@ -# puppet-postgresql -# For all details and documentation: -# http://github.com/inkling/puppet-postgresql -# -# Copyright 2012- Inkling Systems, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -class postgresql_tests::system_default::test_db($db) { - - include postgresql::server - - postgresql::db { $db: - user => $db, - password => $db, - } -} diff --git a/spec/system/test_module/manifests/system_default/test_grant_create.pp b/spec/system/test_module/manifests/system_default/test_grant_create.pp deleted file mode 100644 index b3e2169dfd..0000000000 --- a/spec/system/test_module/manifests/system_default/test_grant_create.pp +++ /dev/null @@ -1,45 +0,0 @@ -# puppet-postgresql -# For all details and documentation: -# http://github.com/inkling/puppet-postgresql -# -# Copyright 2012- Inkling Systems, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -class postgresql_tests::system_default::test_grant_create($user, $password, $db) { - - include postgresql::server - - # Since we are not testing pg_hba or any of that, make a local user for ident auth - user { $user: - ensure => present, - } - - postgresql::database_user { $user: - password_hash => postgresql_password($user, $password), - require => [ Class['postgresql::server'], - User[$user] ], - } - - postgresql::database { $db: - require => Class['postgresql::server'], - } - - postgresql::database_grant { 'grant create test': - privilege => 'CREATE', - db => $db, - role => $user, - require => [ Postgresql::Database[$db], - Postgresql::Database_user[$user] ], - } -} diff --git a/spec/system/test_module/manifests/system_default/test_initdb.pp b/spec/system/test_module/manifests/system_default/test_initdb.pp deleted file mode 100644 index 9b7cf725a1..0000000000 --- a/spec/system/test_module/manifests/system_default/test_initdb.pp +++ /dev/null @@ -1,23 +0,0 @@ -# puppet-postgresql -# For all details and documentation: -# http://github.com/inkling/puppet-postgresql -# -# Copyright 2012- Inkling Systems, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -class postgresql_tests::system_default::test_initdb { - - include postgresql::initdb - -} diff --git a/spec/system/test_module/manifests/system_default/test_pgconf_include.pp b/spec/system/test_module/manifests/system_default/test_pgconf_include.pp deleted file mode 100644 index 5bb665f1db..0000000000 --- a/spec/system/test_module/manifests/system_default/test_pgconf_include.pp +++ /dev/null @@ -1,10 +0,0 @@ -class postgresql_tests::system_default::test_pgconf_include { - - require postgresql::params - - $pg_conf_include_file = "${postgresql::params::confdir}/postgresql_puppet_extras.conf" - - file { $pg_conf_include_file : - content => 'max_connections = 123' - } -} diff --git a/spec/system/test_module/manifests/system_default/test_pgconf_include_cleanup.pp b/spec/system/test_module/manifests/system_default/test_pgconf_include_cleanup.pp deleted file mode 100644 index ed0380e71e..0000000000 --- a/spec/system/test_module/manifests/system_default/test_pgconf_include_cleanup.pp +++ /dev/null @@ -1,11 +0,0 @@ -class postgresql_tests::system_default::test_pgconf_include_cleanup { - - require postgresql::params - - $pg_conf_include_file = "${postgresql::params::confdir}/postgresql_puppet_extras.conf" - - file { $pg_conf_include_file : - ensure => absent - } - -} diff --git a/spec/system/test_module/manifests/system_default/test_psql.pp b/spec/system/test_module/manifests/system_default/test_psql.pp deleted file mode 100644 index 49a74e3b08..0000000000 --- a/spec/system/test_module/manifests/system_default/test_psql.pp +++ /dev/null @@ -1,30 +0,0 @@ -# puppet-postgresql -# For all details and documentation: -# http://github.com/inkling/puppet-postgresql -# -# Copyright 2012- Inkling Systems, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -class postgresql_tests::system_default::test_psql($command = $title, $unless) { - - include postgresql::server - - postgresql::psql { $title: - db => 'postgres', - user => 'postgres', - command => $command, - unless => $unless, - require => Class['postgresql::server'], - } -} diff --git a/spec/system/test_module/manifests/system_default/test_ruby_psql.pp b/spec/system/test_module/manifests/system_default/test_ruby_psql.pp deleted file mode 100644 index c11cc6adf0..0000000000 --- a/spec/system/test_module/manifests/system_default/test_ruby_psql.pp +++ /dev/null @@ -1,30 +0,0 @@ -# puppet-postgresql -# For all details and documentation: -# http://github.com/inkling/puppet-postgresql -# -# Copyright 2012- Inkling Systems, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -class postgresql_tests::system_default::test_ruby_psql($command = $title, $unless) { - - include postgresql::server - - postgresql_psql { $title: - db => 'postgres', - psql_user => 'postgres', - command => $command, - unless => $unless, - require => Class['postgresql::server'], - } -} diff --git a/spec/system/test_module/manifests/system_default/test_tablespace.pp b/spec/system/test_module/manifests/system_default/test_tablespace.pp deleted file mode 100644 index 86e47ed135..0000000000 --- a/spec/system/test_module/manifests/system_default/test_tablespace.pp +++ /dev/null @@ -1,68 +0,0 @@ -# puppet-postgresql -# For all details and documentation: -# http://github.com/inkling/puppet-postgresql -# -# Copyright 2012- Inkling Systems, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -class postgresql_tests::system_default::test_tablespace { - - include postgresql::server - - file { '/tmp/pg_tablespaces': - ensure => 'directory', - owner => 'postgres', - group => 'postgres', - mode => '0700', - }~> - # This works around rubies that lack Selinux support, I'm looking at you RHEL5 - exec { "chcon system_u:object_r:postgresql_db_t /tmp/pg_tablespaces": - refreshonly => true, - path => "/bin:/usr/bin", - onlyif => "which chcon", - before => File["/tmp/pg_tablespaces/space1", "/tmp/pg_tablespaces/space2"] - } - - postgresql::tablespace{ 'tablespace1': - location => '/tmp/pg_tablespaces/space1', - require => [Class['postgresql::server'], File['/tmp/pg_tablespaces']], - } - postgresql::database{ 'tablespacedb1': - charset => 'utf8', - tablespace => 'tablespace1', - require => Postgresql::Tablespace['tablespace1'], - } - postgresql::db{ 'tablespacedb2': - user => 'dbuser2', - password => 'dbuser2', - tablespace => 'tablespace1', - require => Postgresql::Tablespace['tablespace1'], - } - - postgresql::database_user{ 'spcuser': - password_hash => postgresql_password('spcuser', 'spcuser'), - require => Class['postgresql::server'], - } - postgresql::tablespace{ 'tablespace2': - location => '/tmp/pg_tablespaces/space2', - owner => 'spcuser', - require => [Postgresql::Database_user['spcuser'], File['/tmp/pg_tablespaces']], - } - postgresql::database{ 'tablespacedb3': - charset => 'utf8', - tablespace => 'tablespace2', - require => Postgresql::Tablespace['tablespace2'], - } - -} diff --git a/spec/system/test_module/manifests/system_default/test_user.pp b/spec/system/test_module/manifests/system_default/test_user.pp deleted file mode 100644 index 476ee8967d..0000000000 --- a/spec/system/test_module/manifests/system_default/test_user.pp +++ /dev/null @@ -1,33 +0,0 @@ -# puppet-postgresql -# For all details and documentation: -# http://github.com/inkling/puppet-postgresql -# -# Copyright 2012- Inkling Systems, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -class postgresql_tests::system_default::test_user($user, $password) { - - include postgresql::server - - # Since we are not testing pg_hba or any of that, make a local user for ident auth - user { $user: - ensure => present, - } - - postgresql::database_user { $user: - password_hash => postgresql_password($user, $password), - require => [ Class['postgresql::server'], - User[$user] ], - } -} diff --git a/spec/classes/postgresql_java_spec.rb b/spec/unit/classes/postgresql_java_spec.rb similarity index 100% rename from spec/classes/postgresql_java_spec.rb rename to spec/unit/classes/postgresql_java_spec.rb diff --git a/spec/classes/postgresql_python_spec.rb b/spec/unit/classes/postgresql_python_spec.rb similarity index 100% rename from spec/classes/postgresql_python_spec.rb rename to spec/unit/classes/postgresql_python_spec.rb