Skip to content

Commit 3ed8b3c

Browse files
committed
Merge pull request #217 from hunner/update_provider_specs
Update providers to use expect syntax
2 parents 3c6c546 + 0dba5e7 commit 3ed8b3c

File tree

6 files changed

+100
-88
lines changed

6 files changed

+100
-88
lines changed

spec/spec_helper.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222
RSpec.configure do |config|
2323
config.tty = true
24-
config.mock_with :mocha
24+
config.mock_with :rspec do |c|
25+
c.syntax = :expect
26+
end
2527
config.module_path = File.join(fixture_path, 'modules')
2628
config.manifest_dir = File.join(fixture_path, 'manifests')
2729
end

spec/unit/puppet/provider/iptables_chain_spec.rb

+27-27
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,32 @@
1515

1616
it "should default to iptables provider if /sbin/(eb|ip|ip6)tables[-save] exists" do
1717
# Stub lookup for /sbin/iptables & /sbin/iptables-save
18-
exists.any_instance.stubs(:which).with("ebtables").
19-
returns "/sbin/ebtables"
20-
exists.any_instance.stubs(:which).with("ebtables-save").
21-
returns "/sbin/ebtables-save"
18+
allow(exists).to receive(:which).with("ebtables").
19+
and_return "/sbin/ebtables"
20+
allow(exists).to receive(:which).with("ebtables-save").
21+
and_return "/sbin/ebtables-save"
2222

23-
exists.any_instance.stubs(:which).with("iptables").
24-
returns "/sbin/iptables"
25-
exists.any_instance.stubs(:which).with("iptables-save").
26-
returns "/sbin/iptables-save"
23+
allow(exists).to receive(:which).with("iptables").
24+
and_return "/sbin/iptables"
25+
allow(exists).to receive(:which).with("iptables-save").
26+
and_return "/sbin/iptables-save"
2727

28-
exists.any_instance.stubs(:which).with("ip6tables").
29-
returns "/sbin/ip6tables"
30-
exists.any_instance.stubs(:which).with("ip6tables-save").
31-
returns "/sbin/ip6tables-save"
28+
allow(exists).to receive(:which).with("ip6tables").
29+
and_return "/sbin/ip6tables"
30+
allow(exists).to receive(:which).with("ip6tables-save").
31+
and_return "/sbin/ip6tables-save"
3232

3333
# Every other command should return false so we don't pick up any
3434
# other providers
35-
exists.any_instance.stubs(:which).with() { |value|
35+
allow(exists).to receive(:which).with() { |value|
3636
value !~ /(eb|ip|ip6)tables(-save)?$/
37-
}.returns false
37+
}.and_return false
3838

3939
# Create a resource instance and make sure the provider is iptables
4040
resource = Puppet::Type.type(:firewallchain).new({
4141
:name => 'test:filter:IPv4',
4242
})
43-
resource.provider.class.to_s.should == "Puppet::Type::Firewallchain::ProviderIptables_chain"
43+
expect(resource.provider.class.to_s).to eq("Puppet::Type::Firewallchain::ProviderIptables_chain")
4444
end
4545
end
4646

@@ -53,21 +53,21 @@
5353
}
5454

5555
before :each do
56-
Puppet::Type::Firewallchain.stubs(:defaultprovider).returns provider
57-
provider.stubs(:command).with(:ebtables_save).returns "/sbin/ebtables-save"
58-
provider.stubs(:command).with(:iptables_save).returns "/sbin/iptables-save"
59-
provider.stubs(:command).with(:ip6tables_save).returns "/sbin/ip6tables-save"
56+
allow(Puppet::Type::Firewallchain).to receive(:defaultprovider).and_return provider
57+
allow(provider).to receive(:command).with(:ebtables_save).and_return "/sbin/ebtables-save"
58+
allow(provider).to receive(:command).with(:iptables_save).and_return "/sbin/iptables-save"
59+
allow(provider).to receive(:command).with(:ip6tables_save).and_return "/sbin/ip6tables-save"
6060
end
6161

6262
it 'should be able to get a list of existing rules' do
6363
# Pretend to return nil from iptables
64-
provider.stubs(:execute).with(['/sbin/ip6tables-save']).returns("")
65-
provider.stubs(:execute).with(['/sbin/ebtables-save']).returns("")
66-
provider.stubs(:execute).with(['/sbin/iptables-save']).returns("")
64+
allow(provider).to receive(:execute).with(['/sbin/ip6tables-save']).and_return("")
65+
allow(provider).to receive(:execute).with(['/sbin/ebtables-save']).and_return("")
66+
allow(provider).to receive(:execute).with(['/sbin/iptables-save']).and_return("")
6767

6868
provider.instances.each do |chain|
69-
chain.should be_instance_of(provider)
70-
chain.properties[:provider].to_s.should == provider.name.to_s
69+
expect(chain).to be_instance_of(provider)
70+
expect(chain.properties[:provider].to_s).to eq(provider.name.to_s)
7171
end
7272
end
7373

@@ -89,7 +89,7 @@
8989
'NAT:OUTPUT:ethernet',
9090
'NAT:POSTROUTING:ethernet',
9191
]
92-
provider.stubs(:execute).with(['/sbin/ebtables-save']).returns('
92+
allow(provider).to receive(:execute).with(['/sbin/ebtables-save']).and_return('
9393
*broute
9494
:BROUTING ACCEPT
9595
:broute ACCEPT
@@ -126,7 +126,7 @@
126126
'NAT:mangle:IPv4',
127127
':$5()*&%\'"^$): :IPv4',
128128
]
129-
provider.stubs(:execute).with(['/sbin/iptables-save']).returns('
129+
allow(provider).to receive(:execute).with(['/sbin/iptables-save']).and_return('
130130
# Generated by iptables-save v1.4.9 on Mon Jan 2 01:20:06 2012
131131
*raw
132132
:PREROUTING ACCEPT [12:1780]
@@ -175,7 +175,7 @@
175175
':OUTPUT:IPv6',
176176
':test:IPv6',
177177
]
178-
provider.stubs(:execute).with(['/sbin/ip6tables-save']).returns('
178+
allow(provider).to receive(:execute).with(['/sbin/ip6tables-save']).and_return('
179179
# Generated by ip6tables-save v1.4.9 on Mon Jan 2 01:31:39 2012
180180
*raw
181181
:PREROUTING ACCEPT [2173:489241]

spec/unit/puppet/provider/iptables_spec.rb

+27-27
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@
1515

1616
it "should default to iptables provider if /sbin/iptables[-save] exists" do
1717
# Stub lookup for /sbin/iptables & /sbin/iptables-save
18-
exists.any_instance.stubs(:which).with("iptables").
19-
returns "/sbin/iptables"
20-
exists.any_instance.stubs(:which).with("iptables-save").
21-
returns "/sbin/iptables-save"
18+
allow(exists).to receive(:which).with("iptables").
19+
and_return "/sbin/iptables"
20+
allow(exists).to receive(:which).with("iptables-save").
21+
and_return "/sbin/iptables-save"
2222

2323
# Every other command should return false so we don't pick up any
2424
# other providers
25-
exists.any_instance.stubs(:which).with() { |value|
25+
allow(exists).to receive(:which).with() { |value|
2626
! ["iptables","iptables-save"].include?(value)
27-
}.returns false
27+
}.and_return false
2828

2929
# Create a resource instance and make sure the provider is iptables
3030
resource = Puppet::Type.type(:firewall).new({
3131
:name => '000 test foo',
3232
})
33-
resource.provider.class.to_s.should == "Puppet::Type::Firewall::ProviderIptables"
33+
expect(resource.provider.class.to_s).to eq("Puppet::Type::Firewall::ProviderIptables")
3434
end
3535
end
3636

@@ -45,28 +45,28 @@
4545

4646
before :each do
4747
Puppet::Type::Firewall.stubs(:defaultprovider).returns provider
48-
provider.stubs(:command).with(:iptables_save).returns "/sbin/iptables-save"
48+
allow(provider).to receive(:command).with(:iptables_save).and_return "/sbin/iptables-save"
4949

5050
# Stub iptables version
51-
Facter.fact(:iptables_version).stubs(:value).returns("1.4.2")
51+
allow(Facter.fact(:iptables_version)).to receive(:value).and_return("1.4.2")
5252

53-
Puppet::Util::Execution.stubs(:execute).returns ""
54-
Puppet::Util.stubs(:which).with("iptables-save").
55-
returns "/sbin/iptables-save"
53+
allow(Puppet::Util::Execution).to receive(:execute).and_return ""
54+
allow(Puppet::Util).to receive(:which).with("iptables-save").
55+
and_return "/sbin/iptables-save"
5656
end
5757

5858
it 'should be able to get a list of existing rules' do
5959
provider.instances.each do |rule|
60-
rule.should be_instance_of(provider)
61-
rule.properties[:provider].to_s.should == provider.name.to_s
60+
expect(rule).to be_instance_of(provider)
61+
expect(rule.properties[:provider].to_s).to eq(provider.name.to_s)
6262
end
6363
end
6464

6565
it 'should ignore lines with fatal errors' do
66-
Puppet::Util::Execution.stubs(:execute).with(['/sbin/iptables-save']).
67-
returns("FATAL: Could not load /lib/modules/2.6.18-028stab095.1/modules.dep: No such file or directory")
66+
allow(Puppet::Util::Execution).to receive(:execute).with(['/sbin/iptables-save']).
67+
and_return("FATAL: Could not load /lib/modules/2.6.18-028stab095.1/modules.dep: No such file or directory")
6868

69-
provider.instances.length.should == 0
69+
expect(provider.instances.length).to be_zero
7070
end
7171

7272
# Load in ruby hash for test fixtures.
@@ -80,7 +80,7 @@
8080
# If this option is enabled, make sure the parameters exactly match
8181
if data[:compare_all] then
8282
it "the parameter hash keys should be the same as returned by rules_to_hash" do
83-
resource.keys.should =~ data[:params].keys
83+
expect(resource.keys).to match_array(data[:params].keys)
8484
end
8585
end
8686

@@ -89,9 +89,9 @@
8989
it "the parameter '#{param_name.to_s}' should match #{param_value.inspect}" do
9090
# booleans get cludged to string "true"
9191
if param_value == true then
92-
resource[param_name].should == "true"
92+
expect(resource[param_name]).to be_true
9393
else
94-
resource[param_name].should == data[:params][param_name]
94+
expect(resource[param_name]).to eq(data[:params][param_name])
9595
end
9696
end
9797
end
@@ -107,7 +107,7 @@
107107
let(:instance) { provider.new(resource) }
108108

109109
it 'general_args should be valid' do
110-
instance.general_args.flatten.should == data[:args]
110+
expect(instance.general_args.flatten).to eq(data[:args])
111111
end
112112
end
113113
end
@@ -121,23 +121,23 @@
121121
let(:instance) { provider.new(resource) }
122122

123123
it 'rule name contains a MD5 sum of the line' do
124-
resource[:name].should == "9000 #{Digest::MD5.hexdigest(resource[:line])}"
124+
expect(resource[:name]).to eq("9000 #{Digest::MD5.hexdigest(resource[:line])}")
125125
end
126126
end
127127

128128
describe 'when creating resources' do
129129
let(:instance) { provider.new(resource) }
130130

131131
it 'insert_args should be an array' do
132-
instance.insert_args.class.should == Array
132+
expect(instance.insert_args.class).to eq(Array)
133133
end
134134
end
135135

136136
describe 'when modifying resources' do
137137
let(:instance) { provider.new(resource) }
138138

139139
it 'update_args should be an array' do
140-
instance.update_args.class.should == Array
140+
expect(instance.update_args.class).to eq(Array)
141141
end
142142
end
143143

@@ -153,12 +153,12 @@
153153
end
154154

155155
it 'delete_args is an array' do
156-
instance.delete_args.class.should == Array
156+
expect(instance.delete_args.class).to eq(Array)
157157
end
158158

159159
it 'delete_args is the same as the rule string when joined' do
160-
instance.delete_args.join(' ').should == sample_rule.gsub(/\-A/,
161-
'-t filter -D')
160+
expect(instance.delete_args.join(' ')).to eq(sample_rule.gsub(/\-A/,
161+
'-t filter -D'))
162162
end
163163
end
164164
end

spec/unit/puppet/type/firewall_spec.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@
77
describe firewall do
88
before :each do
99
@class = firewall
10-
@provider = stub 'provider'
10+
@provider = double 'provider'
1111
@provider.stubs(:name).returns(:iptables)
1212
Puppet::Type::Firewall.stubs(:defaultprovider).returns @provider
1313

1414
@resource = @class.new({:name => '000 test foo'})
1515

1616
# Stub iptables version
1717
Facter.fact(:iptables_version).stubs(:value).returns("1.4.2")
18+
Facter.fact(:ip6tables_version).stubs(:value).returns("1.4.2")
19+
20+
# Stub confine facts
21+
Facter.fact(:kernel).stubs(:value).returns("Linux")
22+
Facter.fact(:operatingsystem).stubs(:value).returns("Debian")
1823
end
1924

2025
it 'should have :name be its namevar' do

spec/unit/puppet/type/firewallchain_spec.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
firewallchain = Puppet::Type.type(:firewallchain)
66

77
describe firewallchain do
8+
before do
9+
# Stub confine facts
10+
Facter.fact(:kernel).stubs(:value).returns("Linux")
11+
Facter.fact(:operatingsystem).stubs(:value).returns("Debian")
12+
end
813
let(:klass) { firewallchain }
914
let(:provider) {
10-
prov = stub 'provider'
15+
prov = double 'provider'
1116
prov.stubs(:name).returns(:iptables_chain)
1217
prov
1318
}

0 commit comments

Comments
 (0)