From: Ken Barber Date: Wed, 5 Jun 2013 01:52:06 +0000 (+0100) Subject: Update rspec-system tests for rspec-system-puppet 1.5 X-Git-Tag: 0.3.1~2^2 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=7f5fb164d7ad6ada52457a25660e986246e28dd1;p=puppet-modules%2Fpuppetlabs-firewall.git Update rspec-system tests for rspec-system-puppet 1.5 Signed-off-by: Ken Barber --- diff --git a/Gemfile b/Gemfile index cccc5a8..3d7afb1 100644 --- a/Gemfile +++ b/Gemfile @@ -2,7 +2,7 @@ source 'https://rubygems.org' group :development, :test do gem 'puppetlabs_spec_helper', :require => false - gem 'rspec-system-puppet', '~>1.1', '>=1.1.0' + gem 'rspec-system-puppet', '~>1.2', '>=1.2.0' end if puppetversion = ENV['PUPPET_GEM_VERSION'] diff --git a/spec/spec_helper_system.rb b/spec/spec_helper_system.rb index 53a4f83..cb1763b 100644 --- a/spec/spec_helper_system.rb +++ b/spec/spec_helper_system.rb @@ -17,14 +17,16 @@ module LocalHelpers # end def iptables_flush_all_tables ['filter', 'nat', 'mangle', 'raw'].each do |t| - system_run("/sbin/iptables -t #{t} -F") do |r| - r[:exit_code].should == 0 - r[:stderr].should == '' + shell "/sbin/iptables -t #{t} -F" do |r| + r.stderr.should be_empty + r.exit_code.should be_zero end end end end +include RSpecSystemPuppet::Helpers + RSpec.configure do |c| # Project root for the firewall code proj_root = File.expand_path(File.join(File.dirname(__FILE__), '..')) @@ -34,12 +36,10 @@ RSpec.configure do |c| # Import in our local helpers c.include ::LocalHelpers + c.include RSpecSystemPuppet::Helpers # This is where we 'setup' the nodes before running our tests - c.system_setup_block = proc do - # TODO: find a better way of importing this into this namespace - include RSpecSystemPuppet::Helpers - + c.before :suite do # Install puppet puppet_install diff --git a/spec/system/basic_spec.rb b/spec/system/basic_spec.rb index 5dcfef5..2dc917b 100644 --- a/spec/system/basic_spec.rb +++ b/spec/system/basic_spec.rb @@ -4,10 +4,10 @@ require 'spec_helper_system' describe "basic tests:" 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 |r| - r[:exit_code].should == 0 - r[:stdout].should =~ /Modulefile/ - r[:stderr].should == '' + shell 'ls /etc/puppet/modules/firewall' do |r| + r.stdout.should =~ /Modulefile/ + r.stderr.should be_empty + r.exit_code.should be_zero end end end diff --git a/spec/system/class_spec.rb b/spec/system/class_spec.rb index 6199c2f..20682c8 100644 --- a/spec/system/class_spec.rb +++ b/spec/system/class_spec.rb @@ -1,73 +1,53 @@ require 'spec_helper_system' describe "firewall class:" do - context 'no params:' do - let(:pp) do - pp = <<-EOS.gsub(/^\s{8}/,'') - class { 'firewall': } - EOS - end + it 'should run successfully' do + pp = "class { 'firewall': }" - it "should run without event" do - puppet_apply(pp) do |r| - r[:stderr].should == '' - r[:exit_code].should_not eq(1) - end + puppet_apply(pp) do |r| + r.stderr.should be_empty + r.exit_code.should_not == 1 end - it "should be idempotent" do - puppet_apply(pp) do |r| - r[:stderr].should == '' - r[:exit_code].should == 0 - end + puppet_apply(pp) do |r| + r.stderr.should be_empty + r.exit_code.should be_zero end end - context 'ensure => stopped:' do - let(:pp) do - pp = <<-EOS.gsub(/^\s{8}/,'') - class { 'firewall': - ensure => stopped, - } - EOS - end + it 'ensure => stopped:' do + pp = <<-EOS + class { 'firewall': + ensure => stopped, + } + EOS - it "should run without event" do - puppet_apply(pp) do |r| - r[:stderr].should == '' - r[:exit_code].should_not eq(1) - end + puppet_apply(pp) do |r| + r.stderr.should be_empty + r.exit_code.should_not == 1 end - it "should be idempotent" do - puppet_apply(pp) do |r| - r[:stderr].should == '' - r[:exit_code].should == 0 - end + puppet_apply(pp) do |r| + r.stderr.should be_empty + r.exit_code.should be_zero end end - context 'ensure => running:' do - let(:pp) do - pp = <<-EOS.gsub(/^\s{8}/,'') - class { 'firewall': - ensure => running, - } - EOS - end + it 'ensure => running:' do + pp = <<-EOS + class { 'firewall': + ensure => running, + } + EOS - it "should run without event" do - puppet_apply(pp) do |r| - r[:stderr].should == '' - r[:exit_code].should_not eq(1) - end + puppet_apply(pp) do |r| + r.stderr.should be_empty + r.exit_code.should_not == 1 end - it "should be idempotent" do - puppet_apply(pp) do |r| - r[:stderr].should == '' - r[:exit_code].should == 0 - end + puppet_apply(pp) do |r| + r.stderr.should be_empty + r.exit_code.should be_zero end end end diff --git a/spec/system/params_spec.rb b/spec/system/params_spec.rb index 3e653d4..1ffdfae 100644 --- a/spec/system/params_spec.rb +++ b/spec/system/params_spec.rb @@ -1,6 +1,7 @@ require 'spec_helper_system' describe "param based tests:" do + # Takes a hash and converts it into a firewall resource def pp(params) name = params.delete('name') || '100 test' pm = <<-EOS @@ -20,7 +21,7 @@ firewall { '#{name}': end it 'test various params' do - facts = system_node.facts + facts = node.facts unless (facts['operatingsystem'] == 'CentOS') && \ facts['operatingsystemrelease'] =~ /^5\./ then @@ -35,14 +36,14 @@ firewall { '#{name}': 'log_level' => 'debug', }) puppet_apply(ppm) do |r| - r[:stderr].should == '' - r[:exit_code].should == 2 + r.stderr.should be_empty + r.exit_code.should == 2 end # check idempotency puppet_apply(ppm) do |r| - r[:stderr].should == '' - r[:exit_code].should == 0 + r.stderr.should be_empty + r.exit_code.should be_zero end end end @@ -57,14 +58,14 @@ firewall { '#{name}': 'log_level' => 'debug', }) puppet_apply(ppm) do |r| - r.stderr.should == '' + r.stderr.should be_empty r.exit_code.should == 2 end # check idempotency puppet_apply(ppm) do |r| - r.stderr.should == '' - r.exit_code.should == 0 + r.stderr.should be_empty + r.exit_code.should be_zero end end @@ -127,7 +128,7 @@ firewall { '#{name}': puppet_apply(ppm1) do |r| r.stderr.should be_empty - r.exit_code.should == 0 + r.exit_code.should be_zero end end end diff --git a/spec/system/purge_spec.rb b/spec/system/purge_spec.rb index 0c14ee1..9403aed 100644 --- a/spec/system/purge_spec.rb +++ b/spec/system/purge_spec.rb @@ -4,8 +4,8 @@ describe "purge tests:" do it 'make sure duplicate existing rules get purged' do iptables_flush_all_tables - system_run('/sbin/iptables -A INPUT -s 1.2.1.2') - system_run('/sbin/iptables -A INPUT -s 1.2.1.2') + shell('/sbin/iptables -A INPUT -s 1.2.1.2') + shell('/sbin/iptables -A INPUT -s 1.2.1.2') pp = <<-EOS class { 'firewall': } resources { 'firewall': @@ -13,13 +13,13 @@ resources { 'firewall': } EOS puppet_apply(pp) do |r| - r[:stderr].should == '' - r[:exit_code].should == 2 + r.stderr.should be_empty + r.exit_code.should == 2 end system_run('/sbin/iptables-save') do |r| - r[:stdout].should_not =~ /1\.2\.1\.2/ - r[:stderr].should == '' + r.stdout.should_not =~ /1\.2\.1\.2/ + r.stderr.should be_empty end end end diff --git a/spec/system/resource_cmd_spec.rb b/spec/system/resource_cmd_spec.rb index 091faae..34ac6b2 100644 --- a/spec/system/resource_cmd_spec.rb +++ b/spec/system/resource_cmd_spec.rb @@ -6,9 +6,9 @@ require 'spec_helper_system' describe 'puppet resource firewall command:' do it 'make sure it returns no errors when executed on a clean machine' do puppet_resource('firewall') do |r| - r[:exit_code].should == 0 + r.exit_code.should be_zero # don't check stdout, some boxes come with rules, that is normal - r[:stderr].should == '' + r.stderr.should be_empty end end @@ -17,31 +17,31 @@ describe 'puppet resource firewall command:' do # No rules, means no output thanks. And no errors as well. puppet_resource('firewall') do |r| - r[:exit_code].should == 0 - r[:stderr].should == '' - r[:stdout].should == "\n" + r.exit_code.should be_zero + r.stderr.should be_empty + r.stdout.should == "\n" end end - + it 'accepts rules without comments' do iptables_flush_all_tables - system_run('/sbin/iptables -A INPUT -j ACCEPT -p tcp --dport 80') + shell('/sbin/iptables -A INPUT -j ACCEPT -p tcp --dport 80') puppet_resource('firewall') do |r| - r[:exit_code].should == 0 + r.exit_code.should be_zero # don't check stdout, testing preexisting rules, output is normal - r[:stderr].should == '' + r.stderr.should be_empty end end it 'accepts rules with invalid comments' do iptables_flush_all_tables - system_run('/sbin/iptables -A INPUT -j ACCEPT -p tcp --dport 80 -m comment --comment "http"') + shell('/sbin/iptables -A INPUT -j ACCEPT -p tcp --dport 80 -m comment --comment "http"') puppet_resource('firewall') do |r| - r[:exit_code].should == 0 + r.exit_code.should be_zero # don't check stdout, testing preexisting rules, output is normal - r[:stderr].should == '' + r.stderr.should be_empty end end end diff --git a/spec/system/stanard_usage_spec.rb b/spec/system/stanard_usage_spec.rb deleted file mode 100644 index fc935f7..0000000 --- a/spec/system/stanard_usage_spec.rb +++ /dev/null @@ -1,65 +0,0 @@ -require 'spec_helper_system' - -# Some tests for the standard recommended usage -describe "standard usage:" do - let(:pp) do - pp = <<-EOS -class my_fw::pre { - Firewall { - require => undef, - } - - # Default firewall rules - firewall { '000 accept all icmp': - proto => 'icmp', - action => 'accept', - }-> - firewall { '001 accept all to lo interface': - proto => 'all', - iniface => 'lo', - action => 'accept', - }-> - firewall { '002 accept related established rules': - proto => 'all', - state => ['RELATED', 'ESTABLISHED'], - action => 'accept', - } -} -class my_fw::post { - firewall { '999 drop all': - proto => 'all', - action => 'drop', - before => undef, - } -} -resources { "firewall": - purge => true -} -Firewall { - before => Class['my_fw::post'], - require => Class['my_fw::pre'], -} -class { ['my_fw::pre', 'my_fw::post']: } -class { 'firewall': } -firewall { '500 open up port 22': - action => 'accept', - proto => 'tcp', - dport => 22, -} - EOS - end - - it 'make sure it runs without error' do - puppet_apply(pp) do |r| - r[:stderr].should == '' - r[:exit_code].should_not eq(1) - end - end - - it 'should be idempotent' do - puppet_apply(pp) do |r| - r[:stderr].should == '' - r[:exit_code].should == 0 - end - end -end diff --git a/spec/system/standard_usage_spec.rb b/spec/system/standard_usage_spec.rb new file mode 100644 index 0000000..ef4dbec --- /dev/null +++ b/spec/system/standard_usage_spec.rb @@ -0,0 +1,61 @@ +require 'spec_helper_system' + +# Some tests for the standard recommended usage +describe 'standard usage tests:' do + it 'standard 1' do + pp = <<-EOS + class my_fw::pre { + Firewall { + require => undef, + } + + # Default firewall rules + firewall { '000 accept all icmp': + proto => 'icmp', + action => 'accept', + }-> + firewall { '001 accept all to lo interface': + proto => 'all', + iniface => 'lo', + action => 'accept', + }-> + firewall { '002 accept related established rules': + proto => 'all', + state => ['RELATED', 'ESTABLISHED'], + action => 'accept', + } + } + class my_fw::post { + firewall { '999 drop all': + proto => 'all', + action => 'drop', + before => undef, + } + } + resources { "firewall": + purge => true + } + Firewall { + before => Class['my_fw::post'], + require => Class['my_fw::pre'], + } + class { ['my_fw::pre', 'my_fw::post']: } + class { 'firewall': } + firewall { '500 open up port 22': + action => 'accept', + proto => 'tcp', + dport => 22, + } + EOS + + puppet_apply(pp) do |r| + r.stderr.should be_empty + r.exit_code.should_not == 1 + end + + puppet_apply(pp) do |r| + r.stderr.should be_empty + r.exit_code.should be_zero + end + end +end