Browse Source

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 <ken@bob.sh>
Ken Barber 11 years ago
parent
commit
80c85e8f2b
47 changed files with 784 additions and 904 deletions
  1. 31 0
      .nodeset.yml
  2. 2 2
      Gemfile
  3. 20 16
      README.md
  4. 8 10
      Rakefile
  5. 0 0
      examples/init.pp
  6. 0 0
      examples/official-postgresql-repos.pp
  7. 0 0
      examples/postgresql_database.pp
  8. 0 0
      examples/postgresql_db.pp
  9. 0 0
      examples/postgresql_grant.pp
  10. 0 0
      examples/postgresql_pg_hba_rule.pp
  11. 0 0
      examples/postgresql_pgconf_extras.pp
  12. 0 0
      examples/postgresql_tablespace.pp
  13. 0 0
      examples/postgresql_user.pp
  14. 0 0
      examples/server.pp
  15. 16 8
      manifests/package_source/yum_postgresql_org.pp
  16. 15 11
      manifests/params.pp
  17. 54 31
      manifests/server.pp
  18. 54 0
      spec/spec_helper_system.rb
  19. 0 14
      spec/support/postgres_test_config.rb
  20. 0 29
      spec/support/postgres_test_utils.rb
  21. 0 54
      spec/support/shared_contexts/pg_vm_context.rb
  22. 0 71
      spec/support/shared_examples/non_default_postgres.rb
  23. 0 236
      spec/support/shared_examples/system_default_postgres.rb
  24. 0 22
      spec/support/vagrant_common.rb
  25. 0 41
      spec/system/base.pp
  26. 0 12
      spec/system/distros/centos5_64/Vagrantfile
  27. 0 7
      spec/system/distros/centos5_64/non_default_pg_spec.rb
  28. 0 8
      spec/system/distros/centos5_64/system_default_pg_spec.rb
  29. 0 12
      spec/system/distros/centos6_64/Vagrantfile
  30. 0 7
      spec/system/distros/centos6_64/non_default_pg_spec.rb
  31. 0 8
      spec/system/distros/centos6_64/system_default_pg_spec.rb
  32. 0 13
      spec/system/distros/ubuntu_lucid_64/Vagrantfile
  33. 0 7
      spec/system/distros/ubuntu_lucid_64/non_default_pg_spec.rb
  34. 0 8
      spec/system/distros/ubuntu_lucid_64/system_default_pg_spec.rb
  35. 489 0
      spec/system/install_spec.rb
  36. 95 0
      spec/system/non_defaults_spec.rb
  37. 0 27
      spec/system/test_module/manifests/system_default/test_db.pp
  38. 0 45
      spec/system/test_module/manifests/system_default/test_grant_create.pp
  39. 0 23
      spec/system/test_module/manifests/system_default/test_initdb.pp
  40. 0 10
      spec/system/test_module/manifests/system_default/test_pgconf_include.pp
  41. 0 11
      spec/system/test_module/manifests/system_default/test_pgconf_include_cleanup.pp
  42. 0 30
      spec/system/test_module/manifests/system_default/test_psql.pp
  43. 0 30
      spec/system/test_module/manifests/system_default/test_ruby_psql.pp
  44. 0 68
      spec/system/test_module/manifests/system_default/test_tablespace.pp
  45. 0 33
      spec/system/test_module/manifests/system_default/test_user.pp
  46. 0 0
      spec/unit/classes/postgresql_java_spec.rb
  47. 0 0
      spec/unit/classes/postgresql_python_spec.rb

+ 31 - 0
.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'

+ 2 - 2
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
 

+ 20 - 16
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
 ----------------

+ 8 - 10
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

+ 0 - 0
tests/init.pp → examples/init.pp


+ 0 - 0
tests/official-postgresql-repos.pp → examples/official-postgresql-repos.pp


+ 0 - 0
tests/postgresql_database.pp → examples/postgresql_database.pp


+ 0 - 0
tests/postgresql_db.pp → examples/postgresql_db.pp


+ 0 - 0
tests/postgresql_grant.pp → examples/postgresql_grant.pp


+ 0 - 0
tests/postgresql_pg_hba_rule.pp → examples/postgresql_pg_hba_rule.pp


+ 0 - 0
tests/postgresql_pgconf_extras.pp → examples/postgresql_pgconf_extras.pp


+ 0 - 0
tests/postgresql_tablespace.pp → examples/postgresql_tablespace.pp


+ 0 - 0
tests/postgresql_user.pp → examples/postgresql_user.pp


+ 0 - 0
tests/server.pp → examples/server.pp


+ 16 - 8
manifests/package_source/yum_postgresql_org.pp

@@ -6,17 +6,25 @@ class postgresql::package_source::yum_postgresql_org(
   $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'|>
-
 }

+ 15 - 11
manifests/params.pp

@@ -138,23 +138,27 @@ class postgresql::params(
     }
 
     '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')
+            }
+          }
         }
       }
 

+ 54 - 31
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,
-  }
 }

+ 54 - 0
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

+ 0 - 14
spec/support/postgres_test_config.rb

@@ -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

+ 0 - 29
spec/support/postgres_test_utils.rb

@@ -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

+ 0 - 54
spec/support/shared_contexts/pg_vm_context.rb

@@ -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

+ 0 - 71
spec/support/shared_examples/non_default_postgres.rb

@@ -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

+ 0 - 236
spec/support/shared_examples/system_default_postgres.rb

@@ -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

+ 0 - 22
spec/support/vagrant_common.rb

@@ -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

+ 0 - 41
spec/system/base.pp

@@ -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,
-}

+ 0 - 12
spec/system/distros/centos5_64/Vagrantfile

@@ -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

+ 0 - 7
spec/system/distros/centos5_64/non_default_pg_spec.rb

@@ -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

+ 0 - 8
spec/system/distros/centos5_64/system_default_pg_spec.rb

@@ -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

+ 0 - 12
spec/system/distros/centos6_64/Vagrantfile

@@ -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

+ 0 - 7
spec/system/distros/centos6_64/non_default_pg_spec.rb

@@ -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

+ 0 - 8
spec/system/distros/centos6_64/system_default_pg_spec.rb

@@ -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

+ 0 - 13
spec/system/distros/ubuntu_lucid_64/Vagrantfile

@@ -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

+ 0 - 7
spec/system/distros/ubuntu_lucid_64/non_default_pg_spec.rb

@@ -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

+ 0 - 8
spec/system/distros/ubuntu_lucid_64/system_default_pg_spec.rb

@@ -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

+ 489 - 0
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

+ 95 - 0
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

+ 0 - 27
spec/system/test_module/manifests/system_default/test_db.pp

@@ -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,
-  }
-}

+ 0 - 45
spec/system/test_module/manifests/system_default/test_grant_create.pp

@@ -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] ],
-  }
-}

+ 0 - 23
spec/system/test_module/manifests/system_default/test_initdb.pp

@@ -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
-
-}

+ 0 - 10
spec/system/test_module/manifests/system_default/test_pgconf_include.pp

@@ -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'
-  }
-}

+ 0 - 11
spec/system/test_module/manifests/system_default/test_pgconf_include_cleanup.pp

@@ -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
-  }
-
-}

+ 0 - 30
spec/system/test_module/manifests/system_default/test_psql.pp

@@ -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'],
-  }
-}

+ 0 - 30
spec/system/test_module/manifests/system_default/test_ruby_psql.pp

@@ -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'],
-  }
-}

+ 0 - 68
spec/system/test_module/manifests/system_default/test_tablespace.pp

@@ -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'],
-  }
-
-}

+ 0 - 33
spec/system/test_module/manifests/system_default/test_user.pp

@@ -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] ],
-  }
-}

+ 0 - 0
spec/classes/postgresql_java_spec.rb → spec/unit/classes/postgresql_java_spec.rb


+ 0 - 0
spec/classes/postgresql_python_spec.rb → spec/unit/classes/postgresql_python_spec.rb