Skip to content

Commit

Permalink
Convert system tests to use rspec-system
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
kbarber committed Jun 4, 2013
1 parent 6113f6e commit 80c85e8
Show file tree
Hide file tree
Showing 47 changed files with 784 additions and 904 deletions.
31 changes: 31 additions & 0 deletions .nodeset.yml
Original file line number Diff line number Diff line change
@@ -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'
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
36 changes: 20 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
----------------
Expand Down
18 changes: 8 additions & 10 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
24 changes: 16 additions & 8 deletions manifests/package_source/yum_postgresql_org.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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'|>

}
26 changes: 15 additions & 11 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
}
}
}

Expand Down
85 changes: 54 additions & 31 deletions manifests/server.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 54 additions & 0 deletions spec/spec_helper_system.rb
Original file line number Diff line number Diff line change
@@ -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
14 changes: 0 additions & 14 deletions spec/support/postgres_test_config.rb

This file was deleted.

Loading

0 comments on commit 80c85e8

Please sign in to comment.