From: Hunter Haugen Date: Mon, 8 Jul 2013 23:40:59 +0000 (-0700) Subject: Update providers to use expect syntax X-Git-Tag: list~2^2 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=0dba5e7996584eac45e52b1eebd7a9e423e30806;p=puppet-modules%2Fpuppetlabs-firewall.git Update providers to use expect syntax The expect syntax is much better than the should syntax and ships with RSpec 2.14. It caused a few conflicts with the mocha stubbing, so I updated the docs to use rspec test doubles instead. --- diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d276845..dc8bc39 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -21,7 +21,9 @@ fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures')) RSpec.configure do |config| config.tty = true - config.mock_with :mocha + config.mock_with :rspec do |c| + c.syntax = :expect + end config.module_path = File.join(fixture_path, 'modules') config.manifest_dir = File.join(fixture_path, 'manifests') end diff --git a/spec/unit/puppet/provider/iptables_chain_spec.rb b/spec/unit/puppet/provider/iptables_chain_spec.rb index 6079ebb..11aa57b 100755 --- a/spec/unit/puppet/provider/iptables_chain_spec.rb +++ b/spec/unit/puppet/provider/iptables_chain_spec.rb @@ -15,32 +15,32 @@ describe 'iptables chain provider detection' do it "should default to iptables provider if /sbin/(eb|ip|ip6)tables[-save] exists" do # Stub lookup for /sbin/iptables & /sbin/iptables-save - exists.any_instance.stubs(:which).with("ebtables"). - returns "/sbin/ebtables" - exists.any_instance.stubs(:which).with("ebtables-save"). - returns "/sbin/ebtables-save" + allow(exists).to receive(:which).with("ebtables"). + and_return "/sbin/ebtables" + allow(exists).to receive(:which).with("ebtables-save"). + and_return "/sbin/ebtables-save" - exists.any_instance.stubs(:which).with("iptables"). - returns "/sbin/iptables" - exists.any_instance.stubs(:which).with("iptables-save"). - returns "/sbin/iptables-save" + allow(exists).to receive(:which).with("iptables"). + and_return "/sbin/iptables" + allow(exists).to receive(:which).with("iptables-save"). + and_return "/sbin/iptables-save" - exists.any_instance.stubs(:which).with("ip6tables"). - returns "/sbin/ip6tables" - exists.any_instance.stubs(:which).with("ip6tables-save"). - returns "/sbin/ip6tables-save" + allow(exists).to receive(:which).with("ip6tables"). + and_return "/sbin/ip6tables" + allow(exists).to receive(:which).with("ip6tables-save"). + and_return "/sbin/ip6tables-save" # Every other command should return false so we don't pick up any # other providers - exists.any_instance.stubs(:which).with() { |value| + allow(exists).to receive(:which).with() { |value| value !~ /(eb|ip|ip6)tables(-save)?$/ - }.returns false + }.and_return false # Create a resource instance and make sure the provider is iptables resource = Puppet::Type.type(:firewallchain).new({ :name => 'test:filter:IPv4', }) - resource.provider.class.to_s.should == "Puppet::Type::Firewallchain::ProviderIptables_chain" + expect(resource.provider.class.to_s).to eq("Puppet::Type::Firewallchain::ProviderIptables_chain") end end @@ -53,21 +53,21 @@ describe 'iptables chain provider' do } before :each do - Puppet::Type::Firewallchain.stubs(:defaultprovider).returns provider - provider.stubs(:command).with(:ebtables_save).returns "/sbin/ebtables-save" - provider.stubs(:command).with(:iptables_save).returns "/sbin/iptables-save" - provider.stubs(:command).with(:ip6tables_save).returns "/sbin/ip6tables-save" + allow(Puppet::Type::Firewallchain).to receive(:defaultprovider).and_return provider + allow(provider).to receive(:command).with(:ebtables_save).and_return "/sbin/ebtables-save" + allow(provider).to receive(:command).with(:iptables_save).and_return "/sbin/iptables-save" + allow(provider).to receive(:command).with(:ip6tables_save).and_return "/sbin/ip6tables-save" end it 'should be able to get a list of existing rules' do # Pretend to return nil from iptables - provider.stubs(:execute).with(['/sbin/ip6tables-save']).returns("") - provider.stubs(:execute).with(['/sbin/ebtables-save']).returns("") - provider.stubs(:execute).with(['/sbin/iptables-save']).returns("") + allow(provider).to receive(:execute).with(['/sbin/ip6tables-save']).and_return("") + allow(provider).to receive(:execute).with(['/sbin/ebtables-save']).and_return("") + allow(provider).to receive(:execute).with(['/sbin/iptables-save']).and_return("") provider.instances.each do |chain| - chain.should be_instance_of(provider) - chain.properties[:provider].to_s.should == provider.name.to_s + expect(chain).to be_instance_of(provider) + expect(chain.properties[:provider].to_s).to eq(provider.name.to_s) end end @@ -89,7 +89,7 @@ describe 'iptables chain resource parsing' do 'NAT:OUTPUT:ethernet', 'NAT:POSTROUTING:ethernet', ] - provider.stubs(:execute).with(['/sbin/ebtables-save']).returns(' + allow(provider).to receive(:execute).with(['/sbin/ebtables-save']).and_return(' *broute :BROUTING ACCEPT :broute ACCEPT @@ -126,7 +126,7 @@ describe 'iptables chain resource parsing' do 'NAT:mangle:IPv4', ':$5()*&%\'"^$): :IPv4', ] - provider.stubs(:execute).with(['/sbin/iptables-save']).returns(' + allow(provider).to receive(:execute).with(['/sbin/iptables-save']).and_return(' # Generated by iptables-save v1.4.9 on Mon Jan 2 01:20:06 2012 *raw :PREROUTING ACCEPT [12:1780] @@ -175,7 +175,7 @@ COMMIT ':OUTPUT:IPv6', ':test:IPv6', ] - provider.stubs(:execute).with(['/sbin/ip6tables-save']).returns(' + allow(provider).to receive(:execute).with(['/sbin/ip6tables-save']).and_return(' # Generated by ip6tables-save v1.4.9 on Mon Jan 2 01:31:39 2012 *raw :PREROUTING ACCEPT [2173:489241] diff --git a/spec/unit/puppet/provider/iptables_spec.rb b/spec/unit/puppet/provider/iptables_spec.rb index 9bcde6e..c13b9c9 100644 --- a/spec/unit/puppet/provider/iptables_spec.rb +++ b/spec/unit/puppet/provider/iptables_spec.rb @@ -15,22 +15,22 @@ describe 'iptables provider detection' do it "should default to iptables provider if /sbin/iptables[-save] exists" do # Stub lookup for /sbin/iptables & /sbin/iptables-save - exists.any_instance.stubs(:which).with("iptables"). - returns "/sbin/iptables" - exists.any_instance.stubs(:which).with("iptables-save"). - returns "/sbin/iptables-save" + allow(exists).to receive(:which).with("iptables"). + and_return "/sbin/iptables" + allow(exists).to receive(:which).with("iptables-save"). + and_return "/sbin/iptables-save" # Every other command should return false so we don't pick up any # other providers - exists.any_instance.stubs(:which).with() { |value| + allow(exists).to receive(:which).with() { |value| ! ["iptables","iptables-save"].include?(value) - }.returns false + }.and_return false # Create a resource instance and make sure the provider is iptables resource = Puppet::Type.type(:firewall).new({ :name => '000 test foo', }) - resource.provider.class.to_s.should == "Puppet::Type::Firewall::ProviderIptables" + expect(resource.provider.class.to_s).to eq("Puppet::Type::Firewall::ProviderIptables") end end @@ -45,28 +45,28 @@ describe 'iptables provider' do before :each do Puppet::Type::Firewall.stubs(:defaultprovider).returns provider - provider.stubs(:command).with(:iptables_save).returns "/sbin/iptables-save" + allow(provider).to receive(:command).with(:iptables_save).and_return "/sbin/iptables-save" # Stub iptables version - Facter.fact(:iptables_version).stubs(:value).returns("1.4.2") + allow(Facter.fact(:iptables_version)).to receive(:value).and_return("1.4.2") - Puppet::Util::Execution.stubs(:execute).returns "" - Puppet::Util.stubs(:which).with("iptables-save"). - returns "/sbin/iptables-save" + allow(Puppet::Util::Execution).to receive(:execute).and_return "" + allow(Puppet::Util).to receive(:which).with("iptables-save"). + and_return "/sbin/iptables-save" end it 'should be able to get a list of existing rules' do provider.instances.each do |rule| - rule.should be_instance_of(provider) - rule.properties[:provider].to_s.should == provider.name.to_s + expect(rule).to be_instance_of(provider) + expect(rule.properties[:provider].to_s).to eq(provider.name.to_s) end end it 'should ignore lines with fatal errors' do - Puppet::Util::Execution.stubs(:execute).with(['/sbin/iptables-save']). - returns("FATAL: Could not load /lib/modules/2.6.18-028stab095.1/modules.dep: No such file or directory") + allow(Puppet::Util::Execution).to receive(:execute).with(['/sbin/iptables-save']). + and_return("FATAL: Could not load /lib/modules/2.6.18-028stab095.1/modules.dep: No such file or directory") - provider.instances.length.should == 0 + expect(provider.instances.length).to be_zero end # Load in ruby hash for test fixtures. @@ -80,7 +80,7 @@ describe 'iptables provider' do # If this option is enabled, make sure the parameters exactly match if data[:compare_all] then it "the parameter hash keys should be the same as returned by rules_to_hash" do - resource.keys.should =~ data[:params].keys + expect(resource.keys).to match_array(data[:params].keys) end end @@ -89,9 +89,9 @@ describe 'iptables provider' do it "the parameter '#{param_name.to_s}' should match #{param_value.inspect}" do # booleans get cludged to string "true" if param_value == true then - resource[param_name].should == "true" + expect(resource[param_name]).to be_true else - resource[param_name].should == data[:params][param_name] + expect(resource[param_name]).to eq(data[:params][param_name]) end end end @@ -107,7 +107,7 @@ describe 'iptables provider' do let(:instance) { provider.new(resource) } it 'general_args should be valid' do - instance.general_args.flatten.should == data[:args] + expect(instance.general_args.flatten).to eq(data[:args]) end end end @@ -121,7 +121,7 @@ describe 'iptables provider' do let(:instance) { provider.new(resource) } it 'rule name contains a MD5 sum of the line' do - resource[:name].should == "9000 #{Digest::MD5.hexdigest(resource[:line])}" + expect(resource[:name]).to eq("9000 #{Digest::MD5.hexdigest(resource[:line])}") end end @@ -129,7 +129,7 @@ describe 'iptables provider' do let(:instance) { provider.new(resource) } it 'insert_args should be an array' do - instance.insert_args.class.should == Array + expect(instance.insert_args.class).to eq(Array) end end @@ -137,7 +137,7 @@ describe 'iptables provider' do let(:instance) { provider.new(resource) } it 'update_args should be an array' do - instance.update_args.class.should == Array + expect(instance.update_args.class).to eq(Array) end end @@ -153,12 +153,12 @@ describe 'iptables provider' do end it 'delete_args is an array' do - instance.delete_args.class.should == Array + expect(instance.delete_args.class).to eq(Array) end it 'delete_args is the same as the rule string when joined' do - instance.delete_args.join(' ').should == sample_rule.gsub(/\-A/, - '-t filter -D') + expect(instance.delete_args.join(' ')).to eq(sample_rule.gsub(/\-A/, + '-t filter -D')) end end end diff --git a/spec/unit/puppet/type/firewall_spec.rb b/spec/unit/puppet/type/firewall_spec.rb index 6e5ceca..5b8abdb 100755 --- a/spec/unit/puppet/type/firewall_spec.rb +++ b/spec/unit/puppet/type/firewall_spec.rb @@ -7,7 +7,7 @@ firewall = Puppet::Type.type(:firewall) describe firewall do before :each do @class = firewall - @provider = stub 'provider' + @provider = double 'provider' @provider.stubs(:name).returns(:iptables) Puppet::Type::Firewall.stubs(:defaultprovider).returns @provider @@ -15,6 +15,11 @@ describe firewall do # Stub iptables version Facter.fact(:iptables_version).stubs(:value).returns("1.4.2") + Facter.fact(:ip6tables_version).stubs(:value).returns("1.4.2") + + # Stub confine facts + Facter.fact(:kernel).stubs(:value).returns("Linux") + Facter.fact(:operatingsystem).stubs(:value).returns("Debian") end it 'should have :name be its namevar' do diff --git a/spec/unit/puppet/type/firewallchain_spec.rb b/spec/unit/puppet/type/firewallchain_spec.rb index de0035e..dcae102 100755 --- a/spec/unit/puppet/type/firewallchain_spec.rb +++ b/spec/unit/puppet/type/firewallchain_spec.rb @@ -5,9 +5,14 @@ require 'spec_helper' firewallchain = Puppet::Type.type(:firewallchain) describe firewallchain do + before do + # Stub confine facts + Facter.fact(:kernel).stubs(:value).returns("Linux") + Facter.fact(:operatingsystem).stubs(:value).returns("Debian") + end let(:klass) { firewallchain } let(:provider) { - prov = stub 'provider' + prov = double 'provider' prov.stubs(:name).returns(:iptables_chain) prov } diff --git a/spec/unit/puppet/util/firewall_spec.rb b/spec/unit/puppet/util/firewall_spec.rb index 1fcba73..be1576f 100644 --- a/spec/unit/puppet/util/firewall_spec.rb +++ b/spec/unit/puppet/util/firewall_spec.rb @@ -3,9 +3,9 @@ require 'spec_helper' describe 'Puppet::Util::Firewall' do let(:resource) { type = Puppet::Type.type(:firewall) - provider = stub 'provider' - provider.stubs(:name).returns(:iptables) - Puppet::Type::Firewall.stubs(:defaultprovider).returns(provider) + provider = double 'provider' + allow(provider).to receive(:name).and_return(:iptables) + allow(Puppet::Type::Firewall).to receive(:defaultprovider).and_return(provider) type.new({:name => '000 test foo'}) } @@ -14,7 +14,7 @@ describe 'Puppet::Util::Firewall' do describe '#host_to_ip' do subject { resource } specify { - Resolv.expects(:getaddress).with('puppetlabs.com').returns('96.126.112.51') + expect(Resolv).to receive(:getaddress).with('puppetlabs.com').and_return('96.126.112.51') subject.host_to_ip('puppetlabs.com').should == '96.126.112.51/32' } specify { subject.host_to_ip('96.126.112.51').should == '96.126.112.51/32' } @@ -96,42 +96,42 @@ describe 'Puppet::Util::Firewall' do let(:proto) { 'IPv4' } it 'should exec for RedHat identified from osfamily' do - Facter.fact(:osfamily).stubs(:value).returns('RedHat') - Facter.fact(:operatingsystem).stubs(:value).returns('RedHat') + allow(Facter.fact(:osfamily)).to receive(:value).and_return('RedHat') + allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('RedHat') - subject.expects(:execute).with(%w{/sbin/service iptables save}) + expect(subject).to receive(:execute).with(%w{/sbin/service iptables save}) subject.persist_iptables(proto) end it 'should exec for systemd if running Fedora 15 or greater' do - Facter.fact(:osfamily).stubs(:value).returns('RedHat') - Facter.fact(:operatingsystem).stubs(:value).returns('Fedora') - Facter.fact(:operatingsystemrelease).stubs(:value).returns('15') + allow(Facter.fact(:osfamily)).to receive(:value).and_return('RedHat') + allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('Fedora') + allow(Facter.fact(:operatingsystemrelease)).to receive(:value).and_return('15') - subject.expects(:execute).with(%w{/usr/libexec/iptables.init save}) + expect(subject).to receive(:execute).with(%w{/usr/libexec/iptables.init save}) subject.persist_iptables(proto) end it 'should exec for CentOS identified from operatingsystem' do - Facter.fact(:osfamily).stubs(:value).returns(nil) - Facter.fact(:operatingsystem).stubs(:value).returns('CentOS') - subject.expects(:execute).with(%w{/sbin/service iptables save}) + allow(Facter.fact(:osfamily)).to receive(:value).and_return(nil) + allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('CentOS') + expect(subject).to receive(:execute).with(%w{/sbin/service iptables save}) subject.persist_iptables(proto) end it 'should exec for Archlinux identified from osfamily' do - Facter.fact(:osfamily).stubs(:value).returns('Archlinux') - subject.expects(:execute).with(['/bin/sh', '-c', '/usr/sbin/iptables-save > /etc/iptables/iptables.rules']) + allow(Facter.fact(:osfamily)).to receive(:value).and_return('Archlinux') + expect(subject).to receive(:execute).with(['/bin/sh', '-c', '/usr/sbin/iptables-save > /etc/iptables/iptables.rules']) subject.persist_iptables(proto) end it 'should raise a warning when exec fails' do - Facter.fact(:osfamily).stubs(:value).returns('RedHat') - Facter.fact(:operatingsystem).stubs(:value).returns('RedHat') + allow(Facter.fact(:osfamily)).to receive(:value).and_return('RedHat') + allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('RedHat') - subject.expects(:execute).with(%w{/sbin/service iptables save}). - raises(Puppet::ExecutionFailure, 'some error') - subject.expects(:warning).with('Unable to persist firewall rules: some error') + expect(subject).to receive(:execute).with(%w{/sbin/service iptables save}). + and_raise(Puppet::ExecutionFailure, 'some error') + expect(subject).to receive(:warning).with('Unable to persist firewall rules: some error') subject.persist_iptables(proto) end end @@ -140,24 +140,24 @@ describe 'Puppet::Util::Firewall' do let(:proto) { 'IPv6' } it 'should exec for newer Ubuntu' do - Facter.fact(:osfamily).stubs(:value).returns(nil) - Facter.fact(:operatingsystem).stubs(:value).returns('Ubuntu') - Facter.fact(:iptables_persistent_version).stubs(:value).returns('0.5.3ubuntu2') - subject.expects(:execute).with(%w{/usr/sbin/service iptables-persistent save}) + allow(Facter.fact(:osfamily)).to receive(:value).and_return(nil) + allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('Ubuntu') + allow(Facter.fact(:iptables_persistent_version)).to receive(:value).and_return('0.5.3ubuntu2') + expect(subject).to receive(:execute).with(%w{/usr/sbin/service iptables-persistent save}) subject.persist_iptables(proto) end it 'should not exec for older Ubuntu which does not support IPv6' do - Facter.fact(:osfamily).stubs(:value).returns(nil) - Facter.fact(:operatingsystem).stubs(:value).returns('Ubuntu') - Facter.fact(:iptables_persistent_version).stubs(:value).returns('0.0.20090701') - subject.expects(:execute).never + allow(Facter.fact(:osfamily)).to receive(:value).and_return(nil) + allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('Ubuntu') + allow(Facter.fact(:iptables_persistent_version)).to receive(:value).and_return('0.0.20090701') + expect(subject).to receive(:execute).never subject.persist_iptables(proto) end it 'should not exec for Suse which is not supported' do - Facter.fact(:osfamily).stubs(:value).returns('Suse') - subject.expects(:execute).never + allow(Facter.fact(:osfamily)).to receive(:value).and_return('Suse') + expect(subject).to receive(:execute).never subject.persist_iptables(proto) end end