diff --git a/.nodeset.yml b/.nodeset.yml new file mode 100644 index 0000000..c9a6e5a --- /dev/null +++ b/.nodeset.yml @@ -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' diff --git a/Gemfile b/Gemfile index 0370e2c..e190ac3 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,8 @@ 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'] @@ -12,4 +14,3 @@ if puppetversion = ENV['PUPPET_GEM_VERSION'] else gem 'puppet', :require => false end - diff --git a/README.md b/README.md index 89ad529..9a29e2a 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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` @@ -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 diff --git a/Rakefile b/Rakefile index 14f1c24..fe480b9 100644 --- a/Rakefile +++ b/Rakefile @@ -1,2 +1,3 @@ require 'rubygems' require 'puppetlabs_spec_helper/rake_tasks' +require 'rspec-system/rake_task' diff --git a/manifests/init.pp b/manifests/init.pp index f3a3b09..964df4b 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -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 diff --git a/spec/spec_helper_system.rb b/spec/spec_helper_system.rb new file mode 100644 index 0000000..12e7ada --- /dev/null +++ b/spec/spec_helper_system.rb @@ -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 + diff --git a/spec/system/gnupg_key_install_spec.rb b/spec/system/gnupg_key_install_spec.rb new file mode 100644 index 0000000..f3ab9be --- /dev/null +++ b/spec/system/gnupg_key_install_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/system/install_spec.rb b/spec/system/install_spec.rb new file mode 100644 index 0000000..1a4e906 --- /dev/null +++ b/spec/system/install_spec.rb @@ -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