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
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
}
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
'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
'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]
':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]
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
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.
# 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
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
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
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
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
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
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
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
# 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
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
}
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'})
}
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' }
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
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