Skip to content

Commit e9370fe

Browse files
committed
Add unit tests and travis-ci support
This patch includes some very basic and initial unit testing using rspec-puppet and for the case of facts, just normal rspec. I've taken a very light approach here as rspec-puppet can be quite combinatorial when one gets carried away. For now I've just added basic compile failure detection effectively for classes and defined resources. As we continue to work on the code and find regressions this work can be expanded. For facts and functions I've also taken a basic approach for now. One little thing I did change, was the strange string that the fact returns when the default version is undefined. Instead of an error message I've just returned the string 'unknown' which is more in line with other facts I've seen in the wild, and to be quite honest 'unknown' is fairly self-explantory. Since a fact isn't an error reporting message this seemed more appropriate, and looked nicer in the rspec test. As far as travis-ci support, I've added the same configuration that @jmmcune came up with for stdlib which is pretty light and reasonable standard now we propogated that to 4 or so other modules in the puppetlabs/ namespace. It should work out of the box. Signed-off-by: Ken Barber <[email protected]>
1 parent d0419ad commit e9370fe

Some content is hidden

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

44 files changed

+320
-37
lines changed

.fixtures.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fixtures:
2+
repositories:
3+
apt: "git://github.com/puppetlabs/puppetlabs-apt.git"
4+
stdlib: "git://github.com/puppetlabs/puppetlabs-stdlib.git"
5+
firewall: "git://github.com/puppetlabs/puppetlabs-firewall.git"
6+
symlinks:
7+
postgresql: "#{source_dir}"

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.vagrant
22
/postgresql/pkg
3+
# This is a library, so don't pin
4+
Gemfile.lock

.travis.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
language: ruby
2+
bundler_args: --without development
3+
script: "bundle exec rake spec SPEC_OPTS='--format documentation'"
4+
rvm:
5+
- 1.8.7
6+
- 1.9.3
7+
- ruby-head
8+
env:
9+
- PUPPET_GEM_VERSION="~> 2.6"
10+
- PUPPET_GEM_VERSION="~> 2.7"
11+
- PUPPET_GEM_VERSION="~> 3.0"
12+
matrix:
13+
allow_failures:
14+
- rvm: ruby-head
15+
exclude:
16+
- rvm: 1.9.3
17+
env: PUPPET_GEM_VERSION="~> 2.7"
18+
- rvm: ruby-head
19+
env: PUPPET_GEM_VERSION="~> 2.7"
20+
- rvm: 1.9.3
21+
env: PUPPET_GEM_VERSION="~> 2.6"
22+
- rvm: ruby-head
23+
env: PUPPET_GEM_VERSION="~> 2.6"
24+
notifications:
25+
email: false

Gemfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
source :rubygems
2+
3+
group :development, :test do
4+
gem 'rake'
5+
gem 'puppetlabs_spec_helper', :require => false
6+
gem 'vagrant', '~> 1.0.5'
7+
gem 'sahara', '~> 0.0.13'
8+
gem 'puppet-lint', '~> 0.3.2'
9+
end
10+
11+
if puppetversion = ENV['PUPPET_GEM_VERSION']
12+
gem 'puppet', puppetversion, :require => false
13+
else
14+
gem 'puppet', :require => false
15+
end
16+
17+
# vim:ft=ruby

README.md

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -158,36 +158,26 @@ If you need to generate a postgres encrypted password, use `postgresql_password`
158158

159159
### Tests
160160

161-
There are two types of tests distributed with the module. The first set is the
162-
“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:
163-
164-
* init.pp: just installs the postgres client packages
165-
166-
* server.pp: installs the postgres server packages and starts the service; configures the service to accept connections from remote machines, and sets the password for the postgres database user account to ‘postgres’.
167-
168-
* postgresql_database.pp: creates a few sample databases with different character sets. Does not create any users for the databases.
169-
170-
* postgresql_database\_user.pp: creates a few sample users.
171-
172-
* postgresql_database\_grant.pp: shows an example of granting a privilege on a database to a certain user/role.
173-
174-
* postgresql_db.pp: creates several test databases, and creates database user accounts with full privileges for each of them.
161+
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.
175162

176163
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/):
177164

178165
$ curl -L get.rvm.io | bash -s stable
179166
$ rvm install 1.9.3
180167
$ rvm use --create 1.9.3@puppet-postgresql
181-
$ gem install vagrant sahara rspec
168+
$ bundle install
169+
170+
Run the system tests:
171+
172+
$ rake spec:system
182173

183-
Run the tests:
174+
The system test suite will snapshot the VM and rollback between each test.
184175

185-
$ (cd spec; vagrant up)
186-
$ rspec -f -d -c
176+
We also have some unit tests that utilize rspec-puppet for faster iteration if required:
187177

188-
The test suite will snapshot the VM and rollback between each test. Next, take a look at the manifests used for the automated tests.
178+
$ rake spec
189179

190-
$ cat spec/manifests/test_*.pp
180+
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.
191181

192182
Implementation
193183
---------------

Rakefile

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
#require 'puppetlabs_spec_helper/rake_tasks'
1+
require 'puppetlabs_spec_helper/rake_tasks'
22
require 'puppet-lint/tasks/puppet-lint'
33

44
PuppetLint.configuration.send("disable_80chars")
5+
6+
RSpec::Core::RakeTask.new(:system_test) do |c|
7+
c.pattern = "spec/system/**/*_spec.rb"
8+
end
9+
10+
11+
namespace :spec do
12+
desc 'Run system tests'
13+
task :system => :system_test
14+
end

lib/facter/postgres_default_version.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,9 @@ def get_redhatfamily_postgres_version
6363
# TODO: not sure if this is really a great idea, but elsewhere in the code
6464
# it is useful to be able to distinguish between the case where the fact
6565
# does not exist at all (e.g., if pluginsync is not enabled), and the case
66-
# where the fact is not known for the OS in question. It might be better
67-
# to use a shorter sentinel value here and then check for it elsewhere,
68-
# e.g. in the same place we're checking for nil and warning about pluginsync.
66+
# where the fact is not known for the OS in question.
6967
if result == nil
70-
result = "Unsupported OS! Please check `postgres_default_version` fact."
68+
result = 'unknown'
7169
end
7270
result
7371
end

spec/spec_helper.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'puppetlabs_spec_helper/module_spec_helper'
2+
3+
RSpec.configure do |config|
4+
config.before :each do
5+
# Ensure that we don't accidentally cache facts and environment
6+
# between test cases.
7+
Facter::Util::Loader.any_instance.stubs(:load_all)
8+
Facter.clear
9+
Facter.clear_messages
10+
11+
# Store any environment variables away to be restored later
12+
@old_env = {}
13+
ENV.each_key {|k| @old_env[k] = ENV[k]}
14+
end
15+
end

spec/support/shared_contexts/pg_vm_context.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@
5151
end
5252
end
5353

54-
end
54+
end

spec/support/vagrant_common.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ def apply_common_vagrant_config(config)
44

55
# Share the various required modules
66
# TODO: it would be better to install this via the puppet module tool
7-
config.vm.share_folder "puppetlabs-stdlib-module", "/usr/share/puppet/modules/stdlib", "../../../../puppetlabs-stdlib"
8-
config.vm.share_folder "puppetlabs-apt-module", "/usr/share/puppet/modules/apt", "../../../../puppetlabs-apt"
7+
config.vm.share_folder "puppetlabs-stdlib-module", "/usr/share/puppet/modules/stdlib", "../../../../../puppetlabs-stdlib"
8+
config.vm.share_folder "puppetlabs-apt-module", "/usr/share/puppet/modules/apt", "../../../../../puppetlabs-apt"
99

1010
# Share the postgressql module
11-
config.vm.share_folder "puppet-postgresql-module", "/usr/share/puppet/modules/postgresql", "../../.."
11+
config.vm.share_folder "puppet-postgresql-module", "/usr/share/puppet/modules/postgresql", "../../../.."
1212

1313
# Share the module of test classes
1414
config.vm.share_folder "puppet-postgresql-tests", "/usr/share/puppet/modules/postgresql_tests", "../../test_module"
File renamed without changes.
File renamed without changes.

spec/distros/centos6_64/Vagrantfile renamed to spec/system/distros/centos6_64/Vagrantfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require File.expand_path(File.join(__FILE__, '../../../support/vagrant_common'))
1+
require File.expand_path(File.join(__FILE__, '../../../../support/vagrant_common'))
22

33
Vagrant::Config.run do |config|
44

@@ -9,4 +9,4 @@ Vagrant::Config.run do |config|
99

1010
apply_common_vagrant_config(config)
1111

12-
end
12+
end

spec/distros/centos6_64/non_default_pg_spec.rb renamed to spec/system/distros/centos6_64/non_default_pg_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
let(:vagrant_dir) { File.dirname(__FILE__) }
55
let(:vm) { :centos6 }
66
it_behaves_like :non_default_postgres
7-
end
7+
end

spec/distros/centos6_64/system_default_pg_spec.rb renamed to spec/system/distros/centos6_64/system_default_pg_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
let(:vm) { :centos6 }
66
let(:service_name) { 'postgresql' }
77
it_behaves_like :system_default_postgres
8-
end
8+
end

spec/distros/ubuntu_lucid_64/Vagrantfile renamed to spec/system/distros/ubuntu_lucid_64/Vagrantfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require File.expand_path(File.join(__FILE__, '../../../support/vagrant_common'))
1+
require File.expand_path(File.join(__FILE__, '../../../../support/vagrant_common'))
22

33
Vagrant::Config.run do |config|
44

@@ -10,4 +10,4 @@ Vagrant::Config.run do |config|
1010

1111
apply_common_vagrant_config(config)
1212

13-
end
13+
end

spec/distros/ubuntu_lucid_64/non_default_pg_spec.rb renamed to spec/system/distros/ubuntu_lucid_64/non_default_pg_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
let(:vagrant_dir) { File.dirname(__FILE__) }
55
let(:vm) { :lucid }
66
it_behaves_like :non_default_postgres
7-
end
7+
end

spec/distros/ubuntu_lucid_64/system_default_pg_spec.rb renamed to spec/system/distros/ubuntu_lucid_64/system_default_pg_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
let(:vm) { :lucid }
66
let(:service_name) { 'postgresql-8.4' }
77
it_behaves_like :system_default_postgres
8-
end
8+
end

spec/unit/classes/client_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'spec_helper'
2+
3+
describe 'postgresql::client', :type => :class do
4+
let :facts do
5+
{
6+
:postgres_default_version => '8.4',
7+
:osfamily => 'Debian',
8+
}
9+
end
10+
it { should include_class("postgresql::client") }
11+
end

spec/unit/classes/devel_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'spec_helper'
2+
3+
describe 'postgresql::devel', :type => :class do
4+
let :facts do
5+
{
6+
:postgres_default_version => '8.4',
7+
:osfamily => 'Debian',
8+
}
9+
end
10+
it { should include_class("postgresql::devel") }
11+
end

spec/unit/classes/init_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'spec_helper'
2+
3+
describe 'postgresql', :type => :class do
4+
let :facts do
5+
{
6+
:postgres_default_version => '8.4',
7+
:osfamily => 'Debian',
8+
}
9+
end
10+
it { should include_class("postgresql") }
11+
end

spec/unit/classes/params_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'spec_helper'
2+
3+
describe 'postgresql::params', :type => :class do
4+
let :facts do
5+
{
6+
:postgres_default_version => '8.4',
7+
:osfamily => 'Debian',
8+
}
9+
end
10+
it { should include_class("postgresql::params") }
11+
end

spec/unit/classes/server_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'spec_helper'
2+
3+
describe 'postgresql::server', :type => :class do
4+
let :facts do
5+
{
6+
:postgres_default_version => '8.4',
7+
:osfamily => 'Debian',
8+
}
9+
end
10+
it { should include_class("postgresql::server") }
11+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'spec_helper'
2+
3+
describe 'postgresql::database_grant', :type => :define do
4+
let :facts do
5+
{
6+
:postgres_default_version => '8.4',
7+
:osfamily => 'Debian',
8+
}
9+
end
10+
let :title do
11+
'test'
12+
end
13+
let :params do
14+
{
15+
:privilege => 'ALL',
16+
:db => 'test',
17+
:role => 'test',
18+
}
19+
end
20+
it { should include_class("postgresql::params") }
21+
end

spec/unit/defines/database_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'spec_helper'
2+
3+
describe 'postgresql::database', :type => :define do
4+
let :facts do
5+
{
6+
:postgres_default_version => '8.4',
7+
:osfamily => 'Debian',
8+
}
9+
end
10+
let :title do
11+
'test'
12+
end
13+
it { should include_class("postgresql::params") }
14+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'spec_helper'
2+
3+
describe 'postgresql::database_user', :type => :define do
4+
let :facts do
5+
{
6+
:postgres_default_version => '8.4',
7+
:osfamily => 'Debian',
8+
}
9+
end
10+
let :title do
11+
'test'
12+
end
13+
let :params do
14+
{
15+
:password_hash => 'test',
16+
}
17+
end
18+
it { should include_class("postgresql::params") }
19+
end

spec/unit/defines/db_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require 'spec_helper'
2+
3+
describe 'postgresql::db', :type => :define do
4+
let :facts do
5+
{
6+
:postgres_default_version => '8.4',
7+
:osfamily => 'Debian',
8+
}
9+
end
10+
let :title do
11+
'test'
12+
end
13+
let :params do
14+
{
15+
:user => 'test',
16+
:password => 'test',
17+
}
18+
end
19+
it { should include_class("postgresql::params") }
20+
end

spec/unit/defines/psql_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require 'spec_helper'
2+
3+
describe 'postgresql::psql', :type => :define do
4+
let :facts do
5+
{
6+
:postgres_default_version => '8.4',
7+
:osfamily => 'Debian',
8+
}
9+
end
10+
let :title do
11+
'test'
12+
end
13+
let :params do
14+
{
15+
:db => 'test',
16+
:unless => 'test',
17+
}
18+
end
19+
it { should include_class("postgresql::params") }
20+
end

spec/unit/defines/role_spec.pp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'spec_helper'
2+
3+
describe 'postgresql::role', :type => :define do
4+
let :facts do
5+
{
6+
:postgres_default_version => '8.4',
7+
:osfamily => 'Debian',
8+
}
9+
end
10+
let :title do
11+
'test'
12+
end
13+
it { should include_class("postgresql::params") }
14+
end

0 commit comments

Comments
 (0)