Skip to content

Commit

Permalink
Added rspec-system test support
Browse files Browse the repository at this point in the history
  • Loading branch information
dgolja committed Nov 4, 2013
1 parent 6bc06b7 commit 7a1d376
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 4 deletions.
27 changes: 27 additions & 0 deletions .nodeset.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
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'
'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'
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ group :development, :test do
gem 'rspec-puppet', :require => false
gem 'puppetlabs_spec_helper', :require => false
gem 'puppet-lint', :require => false
gem 'rspec-system-puppet', '~>2.0'
gem 'rspec-system-serverspec', '~>1.0'
end

if puppetversion = ENV['PUPPET_GEM_VERSION']
gem 'puppet', puppetversion, :require => false
else
gem 'puppet', :require => false
end

40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Tested with Tavis CI

####Install GnuPG package

include gnupg
include '::gnupg'

####Add public key 20BC0A86 from PGP server from hkp://pgp.mit.edu/ to user root

Expand Down Expand Up @@ -67,7 +67,9 @@ gnupg_key {'root_remove':

#####`package_ensure`

Valid value present/absent. Default: present
Valid value present/absent. In most cases you should never uninstall this package,
because most of the modern Linux distros rely on gnupg for package verification, etc
Default: present

#####`package_name`

Expand Down Expand Up @@ -109,6 +111,40 @@ puppet:///modules/name_of_module/filename
PGP key server from where to retrieve the public key. Valid URI schemes are
*http*, *https*, *ldap* and *hkp*.

### Tests

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:

* rake
* bundler

Install the necessary gems:

bundle install --path=vendor

And then run the unit tests:

bundle exec rake spec


If you want to run the system tests, make sure you also have:

* vagrant > 1.3.x
* Virtualbox > 4.2.10

Then run the tests using:

bundle exec rake spec:system

To run the tests on different operating systems, see the sets available in .nodeset.yml and run the specific set with the following syntax:

RSPEC_SET=debian-607-x64 bundle exec rake spec:system

##Credits
This module design, tests, etc are based on current puppetlabs- and sensu- modules and the help received via #puppet and #puppet-dev, especially from _rc and ken_barber

## Supported Platforms

* Debian Wheezy
Expand Down
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
require 'rubygems'
require 'puppetlabs_spec_helper/rake_tasks'
require 'rspec-system/rake_task'
3 changes: 2 additions & 1 deletion manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#
# [*package_ensure*]
# Remove or install the s3tools package. Possible values
# present or absent
# present or absent, however most of modern Linux distros relays on
# gnupg so you shouldn't remove the package
#
# [*package_name*]
# name of the package usually gnupg/gnupg2 depends of the distro
Expand Down
66 changes: 66 additions & 0 deletions spec/spec_helper_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require 'rspec-system/spec_helper'
require 'rspec-system-puppet/helpers'
require 'rspec-system-serverspec/helpers'
require 'tempfile'

include Serverspec::Helper::RSpecSystem
include Serverspec::Helper::DetectOS
include RSpecSystemPuppet::Helpers

class String
# Provide ability to remove indentation from strings, for the purpose of
# left justifying heredoc blocks.
def unindent
gsub(/^#{scan(/^\s*/).min_by{|l|l.length}}/, "")
end
end

module LocalHelpers
include RSpecSystem::Util

def gpg(gpg_cmd, user = 'root', &block)
gpg = "gpg #{gpg_cmd}"
shell("su #{shellescape(user)} -c #{shellescape(gpg)}", &block)
end
end

RSpec.configure do |c|
# Project root
proj_root = File.expand_path(File.join(File.dirname(__FILE__), '..'))

# Enable colour
c.tty = true

# Include in our local helpers, because some puppet images run
# as diffrent users
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

# Install my module from the current working copy
puppet_module_install(:source => proj_root, :module_name => 'gnupg')
shell 'whoami'
shell 'puppet module list'

# disable hiera warnings
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

128 changes: 128 additions & 0 deletions spec/system/gnupg_key_install_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
require 'spec_helper_system'

describe 'gnupg_key install' do

before :all do
puppet_apply("class {'gnupg': } ") do |r|
r.exit_code.should == 0
end
end

it 'should install a key from a URL address' do
pp = <<-EOS.unindent
gnupg_key { 'jenkins_key':
ensure => present,
user => 'root',
key_source => 'http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key',
key_id => 'D50582E6',
}
EOS

puppet_apply(pp) do |r|
r.exit_code.should == 2
r.refresh
r.exit_code.should == 0
end

# check that gnupg installed the key
gpg("--list-keys D50582E6") do |r|
r.stdout.should =~ /D50582E6/
r.stderr.should == ''
r.exit_code == 0
end
end

it 'should install a key from a key server' do
pp = <<-EOS.unindent
gnupg_key { 'root_key_foo':
ensure => present,
user => 'root',
key_server => 'hkp://pgp.mit.edu/',
key_id => '20BC0A86',
}
EOS

puppet_apply(pp) do |r|
r.exit_code.should == 2
r.refresh
r.exit_code.should == 0
end

# check that gnupg installed the key
gpg("--list-keys 20BC0A86") do |r|
r.stdout.should =~ /20BC0A86/
r.stderr.should == ''
r.exit_code == 0
end
end

it 'should remove key 20BC0A86' do
pp = <<-EOS.unindent
gnupg_key { 'bye_bye_key':
ensure => absent,
key_id => 20BC0A86,
user => root,
}
EOS

puppet_apply(pp) do |r|
r.exit_code.should == 2
r.refresh
r.exit_code.should == 0
end
end

it 'should install key from the puppet fileserver/module repository' do
pp = <<-EOS.unindent
gnupg_key {'add_key_by_remote_source':
ensure => present,
key_id => 20BC0A86,
user => root,
key_source => "puppet:///modules/gnupg/random.key",
}
EOS

puppet_apply(pp) do |r|
r.exit_code.should == 2
r.refresh
r.exit_code.should == 0
end

# check that gnupg installed the key
gpg("--list-keys 20BC0A86") do |r|
r.stdout.should =~ /20BC0A86/
r.stderr.should == ''
r.exit_code == 0
end
end

it 'should not install a key, because local resource does not exists' do
pp = <<-EOS.unindent
gnupg_key { 'jenkins_key':
ensure => present,
user => 'root',
key_source => '/santa/claus/does/not/exists/org/sorry/kids.key',
key_id => '40404040',
}
EOS

puppet_apply(pp) do |r|
r.exit_code.should == 4
end
end

it 'should fail because there is no content on the URL address' do
pp = <<-EOS.unindent
gnupg_key { 'jenkins_key':
ensure => present,
user => 'root',
key_source => 'http://foo.com/key-not-there.key',
key_id => '40404040',
}
EOS

puppet_apply(pp) do |r|
r.exit_code.should == 4
end
end
end
16 changes: 16 additions & 0 deletions spec/system/install_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'spec_helper_system'

describe 'install gnupg:' do

it 'test loading class with no arguments' do
pp = <<-EOS.unindent
class {'gnupg':}
EOS

puppet_apply(pp) do |r|
r.exit_code.should == 0
r.refresh
r.exit_code.should == 0
end
end
end

0 comments on commit 7a1d376

Please sign in to comment.