Skip to content

Commit 80c85e8

Browse files
committed
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 <[email protected]>
1 parent 6113f6e commit 80c85e8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+784
-904
lines changed

.nodeset.yml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
default_set: 'centos-64-x64'
3+
sets:
4+
'centos-59-x64':
5+
nodes:
6+
"main.foo.vm":
7+
prefab: 'centos-59-x64'
8+
'centos-64-x64':
9+
nodes:
10+
"main.foo.vm":
11+
prefab: 'centos-64-x64'
12+
'fedora-18-x64':
13+
nodes:
14+
"main.foo.vm":
15+
prefab: 'fedora-18-x64'
16+
'debian-607-x64':
17+
nodes:
18+
"main.foo.vm":
19+
prefab: 'debian-607-x64'
20+
'debian-70rc1-x64':
21+
nodes:
22+
"main.foo.vm":
23+
prefab: 'debian-70rc1-x64'
24+
'ubuntu-server-10044-x64':
25+
nodes:
26+
"main.foo.vm":
27+
prefab: 'ubuntu-server-10044-x64'
28+
'ubuntu-server-12042-x64':
29+
nodes:
30+
"main.foo.vm":
31+
prefab: 'ubuntu-server-12042-x64'

Gemfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ source 'https://rubygems.org'
33
group :development, :test do
44
gem 'rake'
55
gem 'puppetlabs_spec_helper', :require => false
6-
gem 'vagrant', '~> 1.0.5'
7-
gem 'sahara', '~> 0.0.13'
6+
gem 'rspec-system-puppet', '~>1.0'
7+
gem 'rspec-system', '>=1.2.1'
88
gem 'puppet-lint', '~> 0.3.2'
99
end
1010

README.md

+20-16
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ This setting can be used to explicitly call the initdb operation after server pa
205205
###Class: postgresql::server
206206
Here are the options that you can set in the `config_hash` parameter of `postgresql::server`:
207207

208+
####`ensure`
209+
This value default to `present`. When set to `absent` it will remove all packages, configuration and data so use this with extreme caution.
210+
208211
####`postgres_password`
209212
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.
210213

@@ -504,34 +507,35 @@ You can read the complete module contribution guide [on the Puppet Labs wiki.](h
504507

505508
### Tests
506509

507-
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.
510+
There are two types of tests distributed with the module. Unit tests with rspec-puppet and system tests using rspec-system.
511+
512+
For unit testing, make sure you have:
508513

509-
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/):
514+
* rake
515+
* bundler
510516

511-
$ curl -L get.rvm.io | bash -s stable
512-
$ rvm install 1.9.3
513-
$ rvm use --create 1.9.3@puppet-postgresql
514-
$ bundle install
517+
Install the necessary gems:
515518

516-
Run the system tests:
519+
bundle install --path=vendor
517520

518-
$ rake spec:system
521+
And then run the unit tests:
519522

520-
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.
523+
bundle exec rake spec
521524

522-
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:
525+
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.
523526

524-
$ rspec spec/system/distros/ubuntu_lucid_64
527+
If you want to run the system tests, make sure you also have:
525528

526-
To run only a single specific test against a distro:
529+
* vagrant > 1.2.x
530+
* Virtualbox > 4.2.10
527531

528-
$ rspec spec/system/distros/ubuntu_lucid_64 -e "should idempotently create a user who can log in"
532+
Then run the tests using:
529533

530-
We also have some unit tests that utilize rspec-puppet for faster iteration if required:
534+
bundle exec rake spec:system
531535

532-
$ rake spec
536+
To run the tests on different operating systems, see the sets available in .nodeset.yml and run the specific set with the following syntax:
533537

534-
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.
538+
RSPEC_SET=debian-607-x64 bundle exec rake spec:system
535539

536540
Transfer Notice
537541
----------------

Rakefile

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
require 'puppetlabs_spec_helper/rake_tasks'
2-
require 'puppet-lint/tasks/puppet-lint'
3-
4-
PuppetLint.configuration.send("disable_80chars")
1+
require 'rubygems'
2+
require 'bundler/setup'
53

6-
RSpec::Core::RakeTask.new(:system_test) do |c|
7-
c.pattern = "spec/system/**/*_spec.rb"
8-
end
4+
Bundler.require :default
95

6+
require 'puppetlabs_spec_helper/rake_tasks'
7+
require 'rspec-system/rake_task'
8+
require 'puppet-lint/tasks/puppet-lint'
109

11-
namespace :spec do
12-
desc 'Run system tests'
13-
task :system => :system_test
10+
task :default do
11+
sh %{rake -T}
1412
end

tests/init.pp examples/init.pp

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.

tests/server.pp examples/server.pp

File renamed without changes.

manifests/package_source/yum_postgresql_org.pp

+16-8
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,25 @@
66
$package_version = "${version_parts[0]}${version_parts[1]}"
77

88
file { "/etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG-${package_version}":
9-
source => 'puppet:///modules/postgresql/RPM-GPG-KEY-PGDG'
10-
} ->
9+
source => 'puppet:///modules/postgresql/RPM-GPG-KEY-PGDG',
10+
before => Yumrepo['yum.postgresql.org']
11+
}
12+
13+
if($::operatingsystem == 'Fedora') {
14+
$label1 = 'fedora'
15+
$label2 = $label1
16+
} else {
17+
$label1 = 'redhat'
18+
$label2 = 'rhel'
19+
}
1120

1221
yumrepo { 'yum.postgresql.org':
13-
descr => "PostgreSQL ${version} \$releasever - \$basearch",
14-
baseurl => "http://yum.postgresql.org/${version}/redhat/rhel-\$releasever-\$basearch",
15-
enabled => 1,
16-
gpgcheck => 1,
17-
gpgkey => "file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG-${package_version}",
22+
descr => "PostgreSQL ${version} \$releasever - \$basearch",
23+
baseurl => "http://yum.postgresql.org/${version}/${label1}/${label2}-\$releasever-\$basearch",
24+
enabled => 1,
25+
gpgcheck => 1,
26+
gpgkey => "file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG-${package_version}",
1827
}
1928

2029
Yumrepo['yum.postgresql.org'] -> Package<|tag == 'postgresql'|>
21-
2230
}

manifests/params.pp

+15-11
Original file line numberDiff line numberDiff line change
@@ -138,23 +138,27 @@
138138
}
139139

140140
'Debian': {
141-
$needs_initdb = pick($run_initdb, false)
142141
$firewall_supported = false
143142
# TODO: not exactly sure yet what the right thing to do for Debian/Ubuntu is.
144143
#$persist_firewall_command = '/sbin/iptables-save > /etc/iptables/rules.v4'
145144

146-
147-
case $::operatingsystem {
148-
'Debian': {
149-
$service_name = pick($custom_service_name, 'postgresql')
150-
}
151-
'Ubuntu': {
152-
# thanks, ubuntu
153-
if($::lsbmajdistrelease == '10' and !$manage_package_repo) {
154-
$service_name = pick($custom_service_name, "postgresql-${version}")
155-
} else {
145+
if $manage_package_repo == true {
146+
$needs_initdb = pick($run_initdb, true)
147+
$service_name = pick($custom_service_name, 'postgresql')
148+
} else {
149+
$needs_initdb = pick($run_initdb, false)
150+
case $::operatingsystem {
151+
'Debian': {
156152
$service_name = pick($custom_service_name, 'postgresql')
157153
}
154+
'Ubuntu': {
155+
# thanks, ubuntu
156+
if($::lsbmajdistrelease == '10') {
157+
$service_name = pick($custom_service_name, "postgresql-${version}")
158+
} else {
159+
$service_name = pick($custom_service_name, 'postgresql')
160+
}
161+
}
158162
}
159163
}
160164

manifests/server.pp

+54-31
Original file line numberDiff line numberDiff line change
@@ -21,50 +21,73 @@
2121
# Sample Usage:
2222
#
2323
class postgresql::server (
24+
$ensure = 'present',
2425
$package_name = $postgresql::params::server_package_name,
2526
$package_ensure = 'present',
2627
$service_name = $postgresql::params::service_name,
2728
$service_provider = $postgresql::params::service_provider,
2829
$service_status = $postgresql::params::service_status,
29-
$config_hash = {}
30+
$config_hash = {},
31+
$datadir = $postgresql::params::datadir
3032
) inherits postgresql::params {
3133

32-
package { 'postgresql-server':
33-
ensure => $package_ensure,
34-
name => $package_name,
35-
tag => 'postgresql',
36-
}
34+
if ($ensure == 'absent') {
35+
service { 'postgresqld':
36+
ensure => stopped,
37+
name => $service_name,
38+
enable => false,
39+
provider => $service_provider,
40+
hasstatus => true,
41+
status => $service_status,
42+
}->
43+
package { 'postgresql-server':
44+
ensure => purged,
45+
name => $package_name,
46+
tag => 'postgresql',
47+
}->
48+
file { $datadir:
49+
ensure => absent,
50+
recurse => true,
51+
force => true,
52+
}
53+
} else {
54+
package { 'postgresql-server':
55+
ensure => $package_ensure,
56+
name => $package_name,
57+
tag => 'postgresql',
58+
}
3759

38-
$config_class = {
39-
'postgresql::config' => $config_hash,
40-
}
60+
$config_class = {
61+
'postgresql::config' => $config_hash,
62+
}
4163

42-
create_resources( 'class', $config_class )
64+
create_resources( 'class', $config_class )
4365

66+
service { 'postgresqld':
67+
ensure => running,
68+
name => $service_name,
69+
enable => true,
70+
require => Package['postgresql-server'],
71+
provider => $service_provider,
72+
hasstatus => true,
73+
status => $service_status,
74+
}
4475

45-
service { 'postgresqld':
46-
ensure => running,
47-
name => $service_name,
48-
enable => true,
49-
require => Package['postgresql-server'],
50-
provider => $service_provider,
51-
hasstatus => true,
52-
status => $service_status,
53-
}
76+
if ($postgresql::params::needs_initdb) {
77+
include postgresql::initdb
5478

55-
if ($postgresql::params::needs_initdb) {
56-
include postgresql::initdb
79+
Package['postgresql-server'] -> Class['postgresql::initdb'] -> Class['postgresql::config'] -> Service['postgresqld']
80+
}
81+
else {
82+
Package['postgresql-server'] -> Class['postgresql::config'] -> Service['postgresqld']
83+
}
5784

58-
Package['postgresql-server'] -> Class['postgresql::initdb'] -> Class['postgresql::config'] -> Service['postgresqld']
59-
}
60-
else {
61-
Package['postgresql-server'] -> Class['postgresql::config'] -> Service['postgresqld']
85+
exec { 'reload_postgresql':
86+
path => '/usr/bin:/usr/sbin:/bin:/sbin',
87+
command => "service ${service_name} reload",
88+
onlyif => $service_status,
89+
refreshonly => true,
90+
}
6291
}
6392

64-
exec { 'reload_postgresql':
65-
path => '/usr/bin:/usr/sbin:/bin:/sbin',
66-
command => "service ${service_name} reload",
67-
onlyif => $service_status,
68-
refreshonly => true,
69-
}
7093
}

spec/spec_helper_system.rb

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
require 'rspec-system/spec_helper'
2+
require 'rspec-system-puppet/helpers'
3+
require 'tempfile'
4+
5+
module LocalHelpers
6+
include RSpecSystem::Util
7+
8+
def psql(psql_cmd, user = 'postgres', &block)
9+
psql = "psql #{psql_cmd}"
10+
shell("su #{shellescape(user)} -c #{shellescape(psql)}", &block)
11+
end
12+
end
13+
14+
include RSpecSystemPuppet::Helpers
15+
16+
RSpec.configure do |c|
17+
# Project root for the firewall code
18+
proj_root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
19+
20+
# Enable colour in Jenkins
21+
c.tty = true
22+
23+
# Include in our local helpers
24+
c.include ::LocalHelpers
25+
26+
# Puppet helpers
27+
c.include RSpecSystemPuppet::Helpers
28+
c.extend RSpecSystemPuppet::Helpers
29+
30+
# This is where we 'setup' the nodes before running our tests
31+
c.before :suite do
32+
# Install puppet
33+
puppet_install
34+
35+
# Copy this module into the module path of the test node
36+
puppet_module_install(:source => proj_root, :module_name => 'postgresql')
37+
shell('puppet module install puppetlabs/stdlib')
38+
shell('puppet module install puppetlabs/firewall')
39+
shell('puppet module install puppetlabs/apt')
40+
shell('puppet module install ripienaar/concat')
41+
42+
file = Tempfile.new('foo')
43+
begin
44+
file.write(<<-EOS)
45+
---
46+
:logger: noop
47+
EOS
48+
file.close
49+
rcp(:sp => file.path, :dp => '/etc/puppet/hiera.yaml')
50+
ensure
51+
file.unlink
52+
end
53+
end
54+
end

spec/support/postgres_test_config.rb

-14
This file was deleted.

0 commit comments

Comments
 (0)