From: Ken Barber Date: Fri, 29 Mar 2013 20:35:04 +0000 (+0000) Subject: Initial start on rspec-system tests X-Git-Tag: 0.3.0~21^2 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=5da92c830ea79e3a6ba7cac21072c6044ebda335;p=puppet-modules%2Fpuppetlabs-firewall.git Initial start on rspec-system tests This patch includes system tests using rspec-system. You can try these out with: rake spec:system Consult the docs in the README.md for details on how to run tests on different OS variants. Signed-off-by: Ken Barber --- diff --git a/.nodeset.yml b/.nodeset.yml new file mode 100644 index 0000000..857a3c2 --- /dev/null +++ b/.nodeset.yml @@ -0,0 +1,23 @@ +--- +default_set: 'centos-63-x64' +sets: + 'centos-58-x64': + nodes: + "main": + prefab: 'centos-58-x64' + 'centos-63-x64': + nodes: + "main": + prefab: 'centos-63-x64' + 'debian-606-x64': + nodes: + "main": + prefab: 'debian-606-x64' + 'ubuntu-server-1004-x64': + nodes: + "main": + prefab: 'ubuntu-server-1004-x64' + 'ubuntu-server-1204-x64': + nodes: + "main": + prefab: 'ubuntu-server-1204-x64' diff --git a/Gemfile b/Gemfile index 7dde5a9..ba56930 100644 --- a/Gemfile +++ b/Gemfile @@ -2,6 +2,7 @@ source 'https://rubygems.org' group :development, :test do gem 'puppetlabs_spec_helper', :require => false + gem 'rspec-system', '0.1.3' end if puppetversion = ENV['PUPPET_GEM_VERSION'] diff --git a/README.markdown b/README.markdown index c31dad4..0c9b181 100644 --- a/README.markdown +++ b/README.markdown @@ -381,3 +381,10 @@ Install the necessary gems: And run the tests from the root of the source code: rake test + +If you have a copy of Vagrant 1.1.0 you can also run the system tests: + + RSPEC_SET=debian-606-x64 rake spec:system + RSPEC_SET=centos-58-x64 rake spec:system + +*Note:* system testing is fairly alpha at this point, your mileage may vary. diff --git a/Rakefile b/Rakefile index 79d29df..4847ddd 100644 --- a/Rakefile +++ b/Rakefile @@ -5,6 +5,7 @@ Bundler.require :default require 'rspec/core/rake_task' require 'puppetlabs_spec_helper/rake_tasks' +require 'rspec-system/rake_task' task :default do sh %{rake -T} diff --git a/spec/spec_helper_system.rb b/spec/spec_helper_system.rb new file mode 100644 index 0000000..f4b91e3 --- /dev/null +++ b/spec/spec_helper_system.rb @@ -0,0 +1,51 @@ +# This helper file is specific to the system tests for puppetlabs-firewall +# and should be included by all tests under spec/system +require 'rspec-system/spec_helper' + +RSpec.configure do |c| + # Project root for the firewall code + proj_root = File.expand_path(File.join(File.dirname(__FILE__), '..')) + + # This is where we 'setup' the nodes before running our tests + c.system_setup_block = proc do + # TODO: find a better way of importing these into this namespace + include RSpecSystem::Helpers + include RSpecSystem::Log + + # TODO: this setup stuff is fairly generic, should move this into a plugin + # for rspec-system. + + # Grab facts from node + facts = system_node.facts + + # Remove annoying mesg n from profile, otherwise on Debian we get: + # stdin: is not a tty which messes with our tests later on. + if facts['osfamily'] == 'Debian' + log.info("Remove 'mesg n' from profile to stop noise") + system_run("sed -i 's/^mesg n/# mesg n/' /root/.profile") + end + + # Grab PL repository and install PL copy of puppet + log.info "Starting installation of puppet from PL repos" + if facts['osfamily'] == 'RedHat' + system_run('rpm -ivh http://yum.puppetlabs.com/el/5/products/i386/puppetlabs-release-5-6.noarch.rpm') + system_run('yum install -y puppet') + elsif facts['osfamily'] == 'Debian' + system_run("wget http://apt.puppetlabs.com/puppetlabs-release-#{facts['lsbdistcodename']}.deb") + system_run("dpkg -i puppetlabs-release-#{facts['lsbdistcodename']}.deb") + system_run('apt-get update') + system_run('apt-get install -y puppet') + end + + # Prep modules dir + log.info("Preparing modules dir") + system_run('mkdir -p /etc/puppet/modules') + + # Copy the current code into appropriate module dir + # TODO: we could always use the build process, copy tarball across etc. + # just a shame the puppet module tool doesn't handle standalone tarballs + # yet. + log.info("Now transferring module onto node") + system_rcp(:sp => proj_root, :dp => '/etc/puppet/modules/firewall') + end +end diff --git a/spec/system/basic_spec.rb b/spec/system/basic_spec.rb new file mode 100644 index 0000000..a412417 --- /dev/null +++ b/spec/system/basic_spec.rb @@ -0,0 +1,49 @@ +require 'spec_helper_system' + +# TODO: we probably wanna break this into pieces +describe "basic tests:" do + # This helper flushes all tables on the default machine. + # + # It checks that the flush command returns with no errors. + def iptables_flush_all_tables + ['filter', 'nat', 'mangle', 'raw'].each do |t| + system_run("iptables -t #{t} -F") do |s, o, e| + s.exitstatus.should == 0 + e.should == '' + end + end + end + + context 'prelim:' do + it 'make sure we have copied the module across' do + # No point diagnosing any more if the module wasn't copied properly + system_run("ls /etc/puppet/modules/firewall") do |s, o, e| + s.exitstatus.should == 0 + o.should =~ /Modulefile/ + e.should == '' + end + end + end + + context 'puppet resource firewall command:' do + it 'make sure it returns no errors when executed on a clean machine' do + # Except for the absence of iptables, it should run perfectly usually + # most hosts have iptables at least. + system_run('puppet resource firewall') do |s, o, e| + s.exitstatus.should == 0 + # don't check stdout, some boxes come with rules, that is normal + e.should == '' + end + end + + it 'flush iptables and make sure it returns nothing afterwards' do + iptables_flush_all_tables + # No rules, means no output thanks. And no errors as well. + system_run('puppet resource firewall') do |s, o, e| + s.exitstatus.should == 0 + e.should == '' + o.should == "\n" + end + end + end +end diff --git a/spec/classes/firewall_linux_debian_spec.rb b/spec/unit/classes/firewall_linux_debian_spec.rb similarity index 83% rename from spec/classes/firewall_linux_debian_spec.rb rename to spec/unit/classes/firewall_linux_debian_spec.rb index 3215ce7..ed468f5 100644 --- a/spec/classes/firewall_linux_debian_spec.rb +++ b/spec/unit/classes/firewall_linux_debian_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe 'firewall::linux::debian' do +describe 'firewall::linux::debian', :type => :class do it { should contain_package('iptables-persistent').with( :ensure => 'present' )} diff --git a/spec/classes/firewall_linux_redhat_spec.rb b/spec/unit/classes/firewall_linux_redhat_spec.rb similarity index 69% rename from spec/classes/firewall_linux_redhat_spec.rb rename to spec/unit/classes/firewall_linux_redhat_spec.rb index a4a307b..f61e781 100644 --- a/spec/classes/firewall_linux_redhat_spec.rb +++ b/spec/unit/classes/firewall_linux_redhat_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe 'firewall::linux::redhat' do +describe 'firewall::linux::redhat', :type => :class do it { should contain_service('iptables').with( :ensure => 'running', :enable => 'true' diff --git a/spec/classes/firewall_linux_spec.rb b/spec/unit/classes/firewall_linux_spec.rb similarity index 94% rename from spec/classes/firewall_linux_spec.rb rename to spec/unit/classes/firewall_linux_spec.rb index 61a1b64..8245915 100644 --- a/spec/classes/firewall_linux_spec.rb +++ b/spec/unit/classes/firewall_linux_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe 'firewall::linux' do +describe 'firewall::linux', :type => :class do let(:facts_default) {{ :kernel => 'Linux' }} it { should contain_package('iptables').with_ensure('present') } diff --git a/spec/classes/firewall_spec.rb b/spec/unit/classes/firewall_spec.rb similarity index 79% rename from spec/classes/firewall_spec.rb rename to spec/unit/classes/firewall_spec.rb index d97443f..8d1b7c2 100644 --- a/spec/classes/firewall_spec.rb +++ b/spec/unit/classes/firewall_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe 'firewall' do +describe 'firewall', :type => :class do context 'kernel => Linux' do let(:facts) {{ :kernel => 'Linux' }} it { should include_class('firewall::linux') }