Bläddra i källkod

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 <ken@bob.sh>
Ken Barber 11 år sedan
förälder
incheckning
e9370fee7b
44 ändrade filer med 320 tillägg och 37 borttagningar
  1. 7 0
      .fixtures.yml
  2. 2 0
      .gitignore
  3. 25 0
      .travis.yml
  4. 17 0
      Gemfile
  5. 10 20
      README.md
  6. 11 1
      Rakefile
  7. 2 4
      lib/facter/postgres_default_version.rb
  8. 15 0
      spec/spec_helper.rb
  9. 1 1
      spec/support/shared_contexts/pg_vm_context.rb
  10. 3 3
      spec/support/vagrant_common.rb
  11. 0 0
      spec/system/README.md
  12. 0 0
      spec/system/base.pp
  13. 2 2
      spec/system/distros/centos6_64/Vagrantfile
  14. 1 1
      spec/system/distros/centos6_64/non_default_pg_spec.rb
  15. 1 1
      spec/system/distros/centos6_64/system_default_pg_spec.rb
  16. 2 2
      spec/system/distros/ubuntu_lucid_64/Vagrantfile
  17. 1 1
      spec/system/distros/ubuntu_lucid_64/non_default_pg_spec.rb
  18. 1 1
      spec/system/distros/ubuntu_lucid_64/system_default_pg_spec.rb
  19. 0 0
      spec/system/test_module/manifests/non_default/test_db.pp
  20. 0 0
      spec/system/test_module/manifests/non_default/test_install.pp
  21. 0 0
      spec/system/test_module/manifests/system_default/test_db.pp
  22. 0 0
      spec/system/test_module/manifests/system_default/test_grant_create.pp
  23. 0 0
      spec/system/test_module/manifests/system_default/test_initdb.pp
  24. 0 0
      spec/system/test_module/manifests/system_default/test_pgconf_include.pp
  25. 0 0
      spec/system/test_module/manifests/system_default/test_pgconf_include_cleanup.pp
  26. 0 0
      spec/system/test_module/manifests/system_default/test_psql.pp
  27. 0 0
      spec/system/test_module/manifests/system_default/test_ruby_psql.pp
  28. 0 0
      spec/system/test_module/manifests/system_default/test_tablespace.pp
  29. 0 0
      spec/system/test_module/manifests/system_default/test_user.pp
  30. 11 0
      spec/unit/classes/client_spec.rb
  31. 11 0
      spec/unit/classes/devel_spec.rb
  32. 11 0
      spec/unit/classes/init_spec.rb
  33. 11 0
      spec/unit/classes/params_spec.rb
  34. 11 0
      spec/unit/classes/server_spec.rb
  35. 21 0
      spec/unit/defines/database_grant_spec.rb
  36. 14 0
      spec/unit/defines/database_spec.rb
  37. 19 0
      spec/unit/defines/database_user_spec.rb
  38. 20 0
      spec/unit/defines/db_spec.rb
  39. 20 0
      spec/unit/defines/psql_spec.rb
  40. 14 0
      spec/unit/defines/role_spec.pp
  41. 14 0
      spec/unit/defines/tablespace_spec.pp
  42. 22 0
      spec/unit/defines/validate_db_connection_spec.rb
  43. 14 0
      spec/unit/facts/postgres_default_version_spec.rb
  44. 6 0
      spec/unit/functions/postgresql_password_spec.rb

+ 7 - 0
.fixtures.yml

@@ -0,0 +1,7 @@
+fixtures:
+  repositories:
+    apt: "git://github.com/puppetlabs/puppetlabs-apt.git"
+    stdlib: "git://github.com/puppetlabs/puppetlabs-stdlib.git"
+    firewall: "git://github.com/puppetlabs/puppetlabs-firewall.git"
+  symlinks:
+    postgresql: "#{source_dir}"

+ 2 - 0
.gitignore

@@ -1,2 +1,4 @@
 .vagrant
 /postgresql/pkg
+# This is a library, so don't pin
+Gemfile.lock

+ 25 - 0
.travis.yml

@@ -0,0 +1,25 @@
+language: ruby
+bundler_args: --without development
+script: "bundle exec rake spec SPEC_OPTS='--format documentation'"
+rvm:
+  - 1.8.7
+  - 1.9.3
+  - ruby-head
+env:
+  - PUPPET_GEM_VERSION="~> 2.6"
+  - PUPPET_GEM_VERSION="~> 2.7"
+  - PUPPET_GEM_VERSION="~> 3.0"
+matrix:
+  allow_failures:
+    - rvm: ruby-head
+  exclude:
+    - rvm: 1.9.3
+      env: PUPPET_GEM_VERSION="~> 2.7"
+    - rvm: ruby-head
+      env: PUPPET_GEM_VERSION="~> 2.7"
+    - rvm: 1.9.3
+      env: PUPPET_GEM_VERSION="~> 2.6"
+    - rvm: ruby-head
+      env: PUPPET_GEM_VERSION="~> 2.6"
+notifications:
+  email: false

+ 17 - 0
Gemfile

@@ -0,0 +1,17 @@
+source :rubygems
+
+group :development, :test do
+  gem 'rake'
+  gem 'puppetlabs_spec_helper', :require => false
+  gem 'vagrant', '~> 1.0.5'
+  gem 'sahara', '~> 0.0.13'
+  gem 'puppet-lint', '~> 0.3.2'
+end
+
+if puppetversion = ENV['PUPPET_GEM_VERSION']
+  gem 'puppet', puppetversion, :require => false
+else
+  gem 'puppet', :require => false
+end
+
+# vim:ft=ruby

+ 10 - 20
README.md

@@ -158,36 +158,26 @@ If you need to generate a postgres encrypted password, use `postgresql_password`
 
 ### 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:
-
-* init.pp: just installs the postgres client packages
-
-* 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’.
-
-* postgresql_database.pp: creates a few sample databases with different character sets. Does not create any users for the databases.
-
-* postgresql_database\_user.pp: creates a few sample users.
-
-* postgresql_database\_grant.pp: shows an example of granting a privilege on a database to a certain user/role.
-
-* postgresql_db.pp: creates several test databases, and creates database user accounts with full privileges for each of them.
+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.
 
 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/):
 
     $ curl -L get.rvm.io | bash -s stable
     $ rvm install 1.9.3
     $ rvm use --create 1.9.3@puppet-postgresql
-    $ gem install vagrant sahara rspec
+    $ bundle install 
+
+Run the system tests:
+
+    $ rake spec:system
 
-Run the tests:
+The system test suite will snapshot the VM and rollback between each test.
 
-    $ (cd spec; vagrant up)
-    $ rspec -f -d -c
+We also have some unit tests that utilize rspec-puppet for faster iteration if required:
 
-The test suite will snapshot the VM and rollback between each test. Next, take a look at the manifests used for the automated tests.
+    $ rake spec
 
-	$ cat spec/manifests/test_*.pp
+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.
 
 Implementation
 ---------------

+ 11 - 1
Rakefile

@@ -1,4 +1,14 @@
-#require 'puppetlabs_spec_helper/rake_tasks'
+require 'puppetlabs_spec_helper/rake_tasks'
 require 'puppet-lint/tasks/puppet-lint'
 
 PuppetLint.configuration.send("disable_80chars")
+
+RSpec::Core::RakeTask.new(:system_test) do |c|
+  c.pattern = "spec/system/**/*_spec.rb"
+end
+
+
+namespace :spec do
+  desc 'Run system tests'
+  task :system => :system_test
+end

+ 2 - 4
lib/facter/postgres_default_version.rb

@@ -63,11 +63,9 @@ Facter.add("postgres_default_version") do
     # TODO: not sure if this is really a great idea, but elsewhere in the code
     # it is useful to be able to distinguish between the case where the fact
     # does not exist at all (e.g., if pluginsync is not enabled), and the case
-    # where the fact is not known for the OS in question.  It might be better
-    # to use a shorter sentinel value here and then check for it elsewhere,
-    # e.g. in the same place we're checking for nil and warning about pluginsync.
+    # where the fact is not known for the OS in question.
     if result == nil
-      result = "Unsupported OS!  Please check `postgres_default_version` fact."
+      result = 'unknown'
     end
     result
   end

+ 15 - 0
spec/spec_helper.rb

@@ -0,0 +1,15 @@
+require 'puppetlabs_spec_helper/module_spec_helper'
+
+RSpec.configure do |config|
+  config.before :each do
+    # Ensure that we don't accidentally cache facts and environment
+    # between test cases.
+    Facter::Util::Loader.any_instance.stubs(:load_all)
+    Facter.clear
+    Facter.clear_messages
+
+    # Store any environment variables away to be restored later
+    @old_env = {}
+    ENV.each_key {|k| @old_env[k] = ENV[k]}
+  end
+end

+ 1 - 1
spec/support/shared_contexts/pg_vm_context.rb

@@ -51,4 +51,4 @@ shared_context :pg_vm_context do
     end
   end
 
-end
+end

+ 3 - 3
spec/support/vagrant_common.rb

@@ -4,11 +4,11 @@ def apply_common_vagrant_config(config)
 
   # 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 "puppetlabs-stdlib-module", "/usr/share/puppet/modules/stdlib", "../../../../../puppetlabs-stdlib"
+  config.vm.share_folder "puppetlabs-apt-module", "/usr/share/puppet/modules/apt", "../../../../../puppetlabs-apt"
 
   # Share the postgressql module
-  config.vm.share_folder "puppet-postgresql-module", "/usr/share/puppet/modules/postgresql", "../../.."
+  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"

+ 0 - 0
spec/README.md → spec/system/README.md


+ 0 - 0
spec/base.pp → spec/system/base.pp


+ 2 - 2
spec/distros/centos6_64/Vagrantfile → spec/system/distros/centos6_64/Vagrantfile

@@ -1,4 +1,4 @@
-require File.expand_path(File.join(__FILE__, '../../../support/vagrant_common'))
+require File.expand_path(File.join(__FILE__, '../../../../support/vagrant_common'))
 
 Vagrant::Config.run do |config|
 
@@ -9,4 +9,4 @@ Vagrant::Config.run do |config|
 
   apply_common_vagrant_config(config)
 
-end
+end

+ 1 - 1
spec/distros/centos6_64/non_default_pg_spec.rb → spec/system/distros/centos6_64/non_default_pg_spec.rb

@@ -4,4 +4,4 @@ describe "CentOS6, 64-bit: non-default postgres" do
   let(:vagrant_dir) { File.dirname(__FILE__) }
   let(:vm) { :centos6 }
   it_behaves_like :non_default_postgres
-end
+end

+ 1 - 1
spec/distros/centos6_64/system_default_pg_spec.rb → spec/system/distros/centos6_64/system_default_pg_spec.rb

@@ -5,4 +5,4 @@ describe "CentOS6, 64-bit: default system postgres" do
   let(:vm) { :centos6 }
   let(:service_name) { 'postgresql' }
   it_behaves_like :system_default_postgres
-end
+end

+ 2 - 2
spec/distros/ubuntu_lucid_64/Vagrantfile → spec/system/distros/ubuntu_lucid_64/Vagrantfile

@@ -1,4 +1,4 @@
-require File.expand_path(File.join(__FILE__, '../../../support/vagrant_common'))
+require File.expand_path(File.join(__FILE__, '../../../../support/vagrant_common'))
 
 Vagrant::Config.run do |config|
 
@@ -10,4 +10,4 @@ Vagrant::Config.run do |config|
 
   apply_common_vagrant_config(config)
 
-end
+end

+ 1 - 1
spec/distros/ubuntu_lucid_64/non_default_pg_spec.rb → spec/system/distros/ubuntu_lucid_64/non_default_pg_spec.rb

@@ -4,4 +4,4 @@ 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
+end

+ 1 - 1
spec/distros/ubuntu_lucid_64/system_default_pg_spec.rb → spec/system/distros/ubuntu_lucid_64/system_default_pg_spec.rb

@@ -5,4 +5,4 @@ describe "Ubuntu Lucid, 64-bit: default system postgres" do
   let(:vm) { :lucid }
   let(:service_name) { 'postgresql-8.4' }
   it_behaves_like :system_default_postgres
-end
+end

+ 0 - 0
spec/test_module/manifests/non_default/test_db.pp → spec/system/test_module/manifests/non_default/test_db.pp


+ 0 - 0
spec/test_module/manifests/non_default/test_install.pp → spec/system/test_module/manifests/non_default/test_install.pp


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


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


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


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


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


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


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


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


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


+ 11 - 0
spec/unit/classes/client_spec.rb

@@ -0,0 +1,11 @@
+require 'spec_helper'
+
+describe 'postgresql::client', :type => :class do
+  let :facts do
+    {
+      :postgres_default_version => '8.4',
+      :osfamily => 'Debian',
+    }
+  end
+  it { should include_class("postgresql::client") }
+end

+ 11 - 0
spec/unit/classes/devel_spec.rb

@@ -0,0 +1,11 @@
+require 'spec_helper'
+
+describe 'postgresql::devel', :type => :class do
+  let :facts do
+    {
+      :postgres_default_version => '8.4',
+      :osfamily => 'Debian',
+    }
+  end
+  it { should include_class("postgresql::devel") }
+end

+ 11 - 0
spec/unit/classes/init_spec.rb

@@ -0,0 +1,11 @@
+require 'spec_helper'
+
+describe 'postgresql', :type => :class do
+  let :facts do
+    {
+      :postgres_default_version => '8.4',
+      :osfamily => 'Debian',
+    }
+  end
+  it { should include_class("postgresql") }
+end

+ 11 - 0
spec/unit/classes/params_spec.rb

@@ -0,0 +1,11 @@
+require 'spec_helper'
+
+describe 'postgresql::params', :type => :class do
+  let :facts do
+    {
+      :postgres_default_version => '8.4',
+      :osfamily => 'Debian',
+    }
+  end
+  it { should include_class("postgresql::params") }
+end

+ 11 - 0
spec/unit/classes/server_spec.rb

@@ -0,0 +1,11 @@
+require 'spec_helper'
+
+describe 'postgresql::server', :type => :class do
+  let :facts do
+    {
+      :postgres_default_version => '8.4',
+      :osfamily => 'Debian',
+    }
+  end
+  it { should include_class("postgresql::server") }
+end

+ 21 - 0
spec/unit/defines/database_grant_spec.rb

@@ -0,0 +1,21 @@
+require 'spec_helper'
+
+describe 'postgresql::database_grant', :type => :define do
+  let :facts do
+    {
+      :postgres_default_version => '8.4',
+      :osfamily => 'Debian',
+    }
+  end
+  let :title do
+    'test'
+  end
+  let :params do
+    {
+      :privilege => 'ALL',
+      :db => 'test',
+      :role => 'test',
+    }
+  end
+  it { should include_class("postgresql::params") }
+end

+ 14 - 0
spec/unit/defines/database_spec.rb

@@ -0,0 +1,14 @@
+require 'spec_helper'
+
+describe 'postgresql::database', :type => :define do
+  let :facts do
+    {
+      :postgres_default_version => '8.4',
+      :osfamily => 'Debian',
+    }
+  end
+  let :title do
+    'test'
+  end
+  it { should include_class("postgresql::params") }
+end

+ 19 - 0
spec/unit/defines/database_user_spec.rb

@@ -0,0 +1,19 @@
+require 'spec_helper'
+
+describe 'postgresql::database_user', :type => :define do
+  let :facts do
+    {
+      :postgres_default_version => '8.4',
+      :osfamily => 'Debian',
+    }
+  end
+  let :title do
+    'test'
+  end
+  let :params do
+    {
+      :password_hash => 'test',
+    }
+  end
+  it { should include_class("postgresql::params") }
+end

+ 20 - 0
spec/unit/defines/db_spec.rb

@@ -0,0 +1,20 @@
+require 'spec_helper'
+
+describe 'postgresql::db', :type => :define do
+  let :facts do
+    {
+      :postgres_default_version => '8.4',
+      :osfamily => 'Debian',
+    }
+  end
+  let :title do
+    'test'
+  end
+  let :params do
+    {
+      :user => 'test',
+      :password => 'test',
+    }
+  end
+  it { should include_class("postgresql::params") }
+end

+ 20 - 0
spec/unit/defines/psql_spec.rb

@@ -0,0 +1,20 @@
+require 'spec_helper'
+
+describe 'postgresql::psql', :type => :define do
+  let :facts do
+    {
+      :postgres_default_version => '8.4',
+      :osfamily => 'Debian',
+    }
+  end
+  let :title do
+    'test'
+  end
+  let :params do
+    {
+      :db => 'test',
+      :unless => 'test',
+    }
+  end
+  it { should include_class("postgresql::params") }
+end

+ 14 - 0
spec/unit/defines/role_spec.pp

@@ -0,0 +1,14 @@
+require 'spec_helper'
+
+describe 'postgresql::role', :type => :define do
+  let :facts do
+    {
+      :postgres_default_version => '8.4',
+      :osfamily => 'Debian',
+    }
+  end
+  let :title do
+    'test'
+  end
+  it { should include_class("postgresql::params") }
+end

+ 14 - 0
spec/unit/defines/tablespace_spec.pp

@@ -0,0 +1,14 @@
+require 'spec_helper'
+
+describe 'postgresql::tablespace', :type => :define do
+  let :facts do
+    {
+      :postgres_default_version => '8.4',
+      :osfamily => 'Debian',
+    }
+  end
+  let :title do
+    'test'
+  end
+  it { should include_class("postgresql::params") }
+end

+ 22 - 0
spec/unit/defines/validate_db_connection_spec.rb

@@ -0,0 +1,22 @@
+require 'spec_helper'
+
+describe 'postgresql::validate_db_connection', :type => :define do
+  let :facts do
+    {
+      :postgres_default_version => '8.4',
+      :osfamily => 'Debian',
+    }
+  end
+  let :title do
+    'test'
+  end
+  let :params do
+    {
+      :database_host => 'test',
+      :database_name => 'test',
+      :database_password => 'test',
+      :database_username => 'test',
+    }
+  end
+  it { should include_class("postgresql::params") }
+end

+ 14 - 0
spec/unit/facts/postgres_default_version_spec.rb

@@ -0,0 +1,14 @@
+require 'spec_helper'
+
+describe 'postgres_default_version', :type => :fact do
+  it 'should handle redhat 6.0' do
+    Facter.fact(:osfamily).stubs(:value).returns 'RedHat'
+    Facter.fact(:operatingsystemrelease).stubs(:value).returns '6.0'
+    Facter.fact(:postgres_default_version).value.should == '8.4'
+  end
+
+  it 'should return unknown if osfamily is unknown' do
+    Facter.fact(:osfamily).expects(:value).returns 'test'
+    Facter.fact(:postgres_default_version).value.should eq 'unknown'
+  end
+end

+ 6 - 0
spec/unit/functions/postgresql_password_spec.rb

@@ -0,0 +1,6 @@
+require 'spec_helper'
+
+describe 'postgresql_password', :type => :puppet_function do
+  it { should run.with_params('foo', 'bar').
+    and_return('md596948aad3fcae80c08a35c9b5958cd89') }
+end