Skip to content

Commit ee11737

Browse files
david22swantphoney
authored andcommitted
RubocopFix (#746)
1 parent d1fbcde commit ee11737

2 files changed

Lines changed: 127 additions & 128 deletions

File tree

spec/unit/puppet/util/firewall_spec.rb

Lines changed: 91 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -12,117 +12,117 @@
1212
before(:each) { resource }
1313

1414
describe '#host_to_ip' do
15-
subject { resource }
15+
subject(:host) { resource }
1616

1717
it { # rubocop:disable RSpec/MultipleExpectations
1818
allow(Resolv).to receive(:each_address).at_least(:once).with('puppetlabs.com').and_yield('96.126.112.51').and_yield('2001:DB8:4650::13:8A')
19-
expect(subject.host_to_ip('puppetlabs.com', :IPv4)).to eql '96.126.112.51/32'
20-
expect(subject.host_to_ip('puppetlabs.com', :IPv6)).to eql '2001:db8:4650::13:8a/128'
19+
expect(host.host_to_ip('puppetlabs.com', :IPv4)).to eql '96.126.112.51/32'
20+
expect(host.host_to_ip('puppetlabs.com', :IPv6)).to eql '2001:db8:4650::13:8a/128'
2121
}
22-
it { expect(subject.host_to_ip('96.126.112.51')).to eql '96.126.112.51/32' }
23-
it { expect(subject.host_to_ip('96.126.112.51/32')).to eql '96.126.112.51/32' }
24-
it { expect(subject.host_to_ip('2001:db8:85a3:0:0:8a2e:370:7334')).to eql '2001:db8:85a3::8a2e:370:7334/128' }
25-
it { expect(subject.host_to_ip('2001:db8:1234::/48')).to eql '2001:db8:1234::/48' }
26-
it { expect(subject.host_to_ip('0.0.0.0/0')).to be nil }
27-
it { expect(subject.host_to_ip('::/0')).to be nil }
22+
it { expect(host.host_to_ip('96.126.112.51')).to eql '96.126.112.51/32' }
23+
it { expect(host.host_to_ip('96.126.112.51/32')).to eql '96.126.112.51/32' }
24+
it { expect(host.host_to_ip('2001:db8:85a3:0:0:8a2e:370:7334')).to eql '2001:db8:85a3::8a2e:370:7334/128' }
25+
it { expect(host.host_to_ip('2001:db8:1234::/48')).to eql '2001:db8:1234::/48' }
26+
it { expect(host.host_to_ip('0.0.0.0/0')).to be nil }
27+
it { expect(host.host_to_ip('::/0')).to be nil }
2828
end
2929

3030
describe '#host_to_mask' do
31-
subject { resource }
31+
subject(:host) { resource }
3232

3333
it { # rubocop:disable RSpec/MultipleExpectations
3434
allow(Resolv).to receive(:each_address).at_least(:once).with('puppetlabs.com').and_yield('96.126.112.51').and_yield('2001:DB8:4650::13:8A')
35-
expect(subject.host_to_mask('puppetlabs.com', :IPv4)).to eql '96.126.112.51/32'
36-
expect(subject.host_to_mask('!puppetlabs.com', :IPv4)).to eql '! 96.126.112.51/32'
37-
expect(subject.host_to_mask('puppetlabs.com', :IPv6)).to eql '2001:db8:4650::13:8a/128'
38-
expect(subject.host_to_mask('!puppetlabs.com', :IPv6)).to eql '! 2001:db8:4650::13:8a/128'
35+
expect(host.host_to_mask('puppetlabs.com', :IPv4)).to eql '96.126.112.51/32'
36+
expect(host.host_to_mask('!puppetlabs.com', :IPv4)).to eql '! 96.126.112.51/32'
37+
expect(host.host_to_mask('puppetlabs.com', :IPv6)).to eql '2001:db8:4650::13:8a/128'
38+
expect(host.host_to_mask('!puppetlabs.com', :IPv6)).to eql '! 2001:db8:4650::13:8a/128'
3939
}
40-
it { expect(subject.host_to_mask('96.126.112.51', :IPv4)).to eql '96.126.112.51/32' }
41-
it { expect(subject.host_to_mask('!96.126.112.51', :IPv4)).to eql '! 96.126.112.51/32' }
42-
it { expect(subject.host_to_mask('96.126.112.51/32', :IPv4)).to eql '96.126.112.51/32' }
43-
it { expect(subject.host_to_mask('! 96.126.112.51/32', :IPv4)).to eql '! 96.126.112.51/32' }
44-
it { expect(subject.host_to_mask('2001:db8:85a3:0:0:8a2e:370:7334', :IPv6)).to eql '2001:db8:85a3::8a2e:370:7334/128' }
45-
it { expect(subject.host_to_mask('!2001:db8:85a3:0:0:8a2e:370:7334', :IPv6)).to eql '! 2001:db8:85a3::8a2e:370:7334/128' }
46-
it { expect(subject.host_to_mask('2001:db8:1234::/48', :IPv6)).to eql '2001:db8:1234::/48' }
47-
it { expect(subject.host_to_mask('! 2001:db8:1234::/48', :IPv6)).to eql '! 2001:db8:1234::/48' }
48-
it { expect(subject.host_to_mask('0.0.0.0/0', :IPv4)).to be nil }
49-
it { expect(subject.host_to_mask('!0.0.0.0/0', :IPv4)).to be nil }
50-
it { expect(subject.host_to_mask('::/0', :IPv6)).to be nil }
51-
it { expect(subject.host_to_mask('! ::/0', :IPv6)).to be nil }
40+
it { expect(host.host_to_mask('96.126.112.51', :IPv4)).to eql '96.126.112.51/32' }
41+
it { expect(host.host_to_mask('!96.126.112.51', :IPv4)).to eql '! 96.126.112.51/32' }
42+
it { expect(host.host_to_mask('96.126.112.51/32', :IPv4)).to eql '96.126.112.51/32' }
43+
it { expect(host.host_to_mask('! 96.126.112.51/32', :IPv4)).to eql '! 96.126.112.51/32' }
44+
it { expect(host.host_to_mask('2001:db8:85a3:0:0:8a2e:370:7334', :IPv6)).to eql '2001:db8:85a3::8a2e:370:7334/128' }
45+
it { expect(host.host_to_mask('!2001:db8:85a3:0:0:8a2e:370:7334', :IPv6)).to eql '! 2001:db8:85a3::8a2e:370:7334/128' }
46+
it { expect(host.host_to_mask('2001:db8:1234::/48', :IPv6)).to eql '2001:db8:1234::/48' }
47+
it { expect(host.host_to_mask('! 2001:db8:1234::/48', :IPv6)).to eql '! 2001:db8:1234::/48' }
48+
it { expect(host.host_to_mask('0.0.0.0/0', :IPv4)).to be nil }
49+
it { expect(host.host_to_mask('!0.0.0.0/0', :IPv4)).to be nil }
50+
it { expect(host.host_to_mask('::/0', :IPv6)).to be nil }
51+
it { expect(host.host_to_mask('! ::/0', :IPv6)).to be nil }
5252
end
5353

5454
describe '#icmp_name_to_number' do
5555
describe 'proto unsupported' do
56-
subject { resource }
56+
subject(:host) { resource }
5757

5858
%w[inet5 inet8 foo].each do |proto|
5959
it "should reject invalid proto #{proto}" do
60-
expect { subject.icmp_name_to_number('echo-reply', proto) }
60+
expect { host.icmp_name_to_number('echo-reply', proto) }
6161
.to raise_error(ArgumentError, "unsupported protocol family '#{proto}'")
6262
end
6363
end
6464
end
6565

6666
describe 'proto IPv4' do
6767
proto = 'inet'
68-
subject { resource }
69-
70-
it { expect(subject.icmp_name_to_number('echo-reply', proto)).to eql '0' }
71-
it { expect(subject.icmp_name_to_number('destination-unreachable', proto)).to eql '3' }
72-
it { expect(subject.icmp_name_to_number('source-quench', proto)).to eql '4' }
73-
it { expect(subject.icmp_name_to_number('redirect', proto)).to eql '6' }
74-
it { expect(subject.icmp_name_to_number('echo-request', proto)).to eql '8' }
75-
it { expect(subject.icmp_name_to_number('router-advertisement', proto)).to eql '9' }
76-
it { expect(subject.icmp_name_to_number('router-solicitation', proto)).to eql '10' }
77-
it { expect(subject.icmp_name_to_number('time-exceeded', proto)).to eql '11' }
78-
it { expect(subject.icmp_name_to_number('parameter-problem', proto)).to eql '12' }
79-
it { expect(subject.icmp_name_to_number('timestamp-request', proto)).to eql '13' }
80-
it { expect(subject.icmp_name_to_number('timestamp-reply', proto)).to eql '14' }
81-
it { expect(subject.icmp_name_to_number('address-mask-request', proto)).to eql '17' }
82-
it { expect(subject.icmp_name_to_number('address-mask-reply', proto)).to eql '18' }
68+
subject(:host) { resource }
69+
70+
it { expect(host.icmp_name_to_number('echo-reply', proto)).to eql '0' }
71+
it { expect(host.icmp_name_to_number('destination-unreachable', proto)).to eql '3' }
72+
it { expect(host.icmp_name_to_number('source-quench', proto)).to eql '4' }
73+
it { expect(host.icmp_name_to_number('redirect', proto)).to eql '6' }
74+
it { expect(host.icmp_name_to_number('echo-request', proto)).to eql '8' }
75+
it { expect(host.icmp_name_to_number('router-advertisement', proto)).to eql '9' }
76+
it { expect(host.icmp_name_to_number('router-solicitation', proto)).to eql '10' }
77+
it { expect(host.icmp_name_to_number('time-exceeded', proto)).to eql '11' }
78+
it { expect(host.icmp_name_to_number('parameter-problem', proto)).to eql '12' }
79+
it { expect(host.icmp_name_to_number('timestamp-request', proto)).to eql '13' }
80+
it { expect(host.icmp_name_to_number('timestamp-reply', proto)).to eql '14' }
81+
it { expect(host.icmp_name_to_number('address-mask-request', proto)).to eql '17' }
82+
it { expect(host.icmp_name_to_number('address-mask-reply', proto)).to eql '18' }
8383
end
8484

8585
describe 'proto IPv6' do
8686
proto = 'inet6'
87-
subject { resource }
88-
89-
it { expect(subject.icmp_name_to_number('destination-unreachable', proto)).to eql '1' }
90-
it { expect(subject.icmp_name_to_number('time-exceeded', proto)).to eql '3' }
91-
it { expect(subject.icmp_name_to_number('parameter-problem', proto)).to eql '4' }
92-
it { expect(subject.icmp_name_to_number('echo-request', proto)).to eql '128' }
93-
it { expect(subject.icmp_name_to_number('echo-reply', proto)).to eql '129' }
94-
it { expect(subject.icmp_name_to_number('router-solicitation', proto)).to eql '133' }
95-
it { expect(subject.icmp_name_to_number('router-advertisement', proto)).to eql '134' }
96-
it { expect(subject.icmp_name_to_number('neighbour-solicitation', proto)).to eql '135' }
97-
it { expect(subject.icmp_name_to_number('neighbour-advertisement', proto)).to eql '136' }
98-
it { expect(subject.icmp_name_to_number('redirect', proto)).to eql '137' }
87+
subject(:host) { resource }
88+
89+
it { expect(host.icmp_name_to_number('destination-unreachable', proto)).to eql '1' }
90+
it { expect(host.icmp_name_to_number('time-exceeded', proto)).to eql '3' }
91+
it { expect(host.icmp_name_to_number('parameter-problem', proto)).to eql '4' }
92+
it { expect(host.icmp_name_to_number('echo-request', proto)).to eql '128' }
93+
it { expect(host.icmp_name_to_number('echo-reply', proto)).to eql '129' }
94+
it { expect(host.icmp_name_to_number('router-solicitation', proto)).to eql '133' }
95+
it { expect(host.icmp_name_to_number('router-advertisement', proto)).to eql '134' }
96+
it { expect(host.icmp_name_to_number('neighbour-solicitation', proto)).to eql '135' }
97+
it { expect(host.icmp_name_to_number('neighbour-advertisement', proto)).to eql '136' }
98+
it { expect(host.icmp_name_to_number('redirect', proto)).to eql '137' }
9999
end
100100
end
101101

102102
describe '#string_to_port' do
103-
subject { resource }
103+
subject(:host) { resource }
104104

105-
it { expect(subject.string_to_port('80', 'tcp')).to eql '80' }
106-
it { expect(subject.string_to_port(80, 'tcp')).to eql '80' }
107-
it { expect(subject.string_to_port('http', 'tcp')).to eql '80' }
108-
it { expect(subject.string_to_port('domain', 'udp')).to eql '53' }
105+
it { expect(host.string_to_port('80', 'tcp')).to eql '80' }
106+
it { expect(host.string_to_port(80, 'tcp')).to eql '80' }
107+
it { expect(host.string_to_port('http', 'tcp')).to eql '80' }
108+
it { expect(host.string_to_port('domain', 'udp')).to eql '53' }
109109
end
110110

111111
describe '#to_hex32' do
112-
subject { resource }
113-
114-
it { expect(subject.to_hex32('0')).to eql '0x0' }
115-
it { expect(subject.to_hex32('0x32')).to eql '0x32' }
116-
it { expect(subject.to_hex32('42')).to eql '0x2a' }
117-
it { expect(subject.to_hex32('4294967295')).to eql '0xffffffff' }
118-
it { expect(subject.to_hex32('4294967296')).to be nil }
119-
it { expect(subject.to_hex32('-1')).to be nil }
120-
it { expect(subject.to_hex32('bananas')).to be nil }
112+
subject(:host) { resource }
113+
114+
it { expect(host.to_hex32('0')).to eql '0x0' }
115+
it { expect(host.to_hex32('0x32')).to eql '0x32' }
116+
it { expect(host.to_hex32('42')).to eql '0x2a' }
117+
it { expect(host.to_hex32('4294967295')).to eql '0xffffffff' }
118+
it { expect(host.to_hex32('4294967296')).to be nil }
119+
it { expect(host.to_hex32('-1')).to be nil }
120+
it { expect(host.to_hex32('bananas')).to be nil }
121121
end
122122

123123
describe '#persist_iptables' do
124124
before(:each) { Facter.clear }
125-
subject { resource }
125+
subject(:host) { resource }
126126

127127
# rubocop:disable RSpec/SubjectStub
128128
describe 'when proto is IPv4' do
@@ -133,59 +133,58 @@
133133
allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('RedHat')
134134
allow(Facter.fact(:operatingsystemrelease)).to receive(:value).and_return('6')
135135

136-
allow(subject).to receive(:execute).with(%w[/sbin/service iptables save])
137-
subject.persist_iptables(proto)
136+
allow(host).to receive(:execute).with(%w[/sbin/service iptables save])
137+
host.persist_iptables(proto)
138138
end
139139

140140
it 'is expected to exec for systemd if running RHEL 7 or greater' do
141141
allow(Facter.fact(:osfamily)).to receive(:value).and_return('RedHat')
142142
allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('RedHat')
143143
allow(Facter.fact(:operatingsystemrelease)).to receive(:value).and_return('7')
144144

145-
allow(subject).to receive(:execute).with(%w[/usr/libexec/iptables/iptables.init save])
146-
subject.persist_iptables(proto)
145+
allow(host).to receive(:execute).with(%w[/usr/libexec/iptables/iptables.init save])
146+
host.persist_iptables(proto)
147147
end
148148

149149
it 'is expected to exec for systemd if running Fedora 15 or greater' do
150150
allow(Facter.fact(:osfamily)).to receive(:value).and_return('RedHat')
151151
allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('Fedora')
152152
allow(Facter.fact(:operatingsystemrelease)).to receive(:value).and_return('15')
153153

154-
allow(subject).to receive(:execute).with(%w[/usr/libexec/iptables/iptables.init save])
155-
subject.persist_iptables(proto)
154+
allow(host).to receive(:execute).with(%w[/usr/libexec/iptables/iptables.init save])
155+
host.persist_iptables(proto)
156156
end
157157

158158
it 'is expected to exec for CentOS 6 identified from operatingsystem and operatingsystemrelease' do
159159
allow(Facter.fact(:osfamily)).to receive(:value).and_return(nil)
160160
allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('CentOS')
161161
allow(Facter.fact(:operatingsystemrelease)).to receive(:value).and_return('6.5')
162-
allow(subject).to receive(:execute).with(%w[/sbin/service iptables save])
163-
subject.persist_iptables(proto)
162+
allow(host).to receive(:execute).with(%w[/sbin/service iptables save])
163+
host.persist_iptables(proto)
164164
end
165165

166166
it 'is expected to exec for CentOS 7 identified from operatingsystem and operatingsystemrelease' do
167167
allow(Facter.fact(:osfamily)).to receive(:value).and_return(nil)
168168
allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('CentOS')
169169
allow(Facter.fact(:operatingsystemrelease)).to receive(:value).and_return('7.0.1406')
170-
allow(subject).to receive(:execute).with(%w[/usr/libexec/iptables/iptables.init save])
171-
subject.persist_iptables(proto)
170+
allow(host).to receive(:execute).with(%w[/usr/libexec/iptables/iptables.init save])
171+
host.persist_iptables(proto)
172172
end
173173

174174
it 'is expected to exec for Archlinux identified from osfamily' do
175175
allow(Facter.fact(:osfamily)).to receive(:value).and_return('Archlinux')
176-
allow(subject).to receive(:execute).with(['/bin/sh', '-c', '/usr/sbin/iptables-save > /etc/iptables/iptables.rules'])
177-
subject.persist_iptables(proto)
176+
allow(host).to receive(:execute).with(['/bin/sh', '-c', '/usr/sbin/iptables-save > /etc/iptables/iptables.rules'])
177+
host.persist_iptables(proto)
178178
end
179179

180180
it 'is expected to raise a warning when exec fails' do # rubocop:disable RSpec/ExampleLength
181181
allow(Facter.fact(:osfamily)).to receive(:value).and_return('RedHat')
182182
allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('RedHat')
183183
allow(Facter.fact(:operatingsystemrelease)).to receive(:value).and_return('6')
184184

185-
allow(subject).to receive(:execute).with(%w[/sbin/service iptables save])
186-
.and_raise(Puppet::ExecutionFailure, 'some error')
187-
allow(subject).to receive(:warning).with('Unable to persist firewall rules: some error')
188-
subject.persist_iptables(proto)
185+
allow(host).to receive(:execute).with(%w[/sbin/service iptables save]).and_raise(Puppet::ExecutionFailure, 'some error')
186+
allow(host).to receive(:warning).with('Unable to persist firewall rules: some error')
187+
host.persist_iptables(proto)
189188
end
190189
end
191190

@@ -196,22 +195,22 @@
196195
allow(Facter.fact(:osfamily)).to receive(:value).and_return(nil)
197196
allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('Ubuntu')
198197
allow(Facter.fact(:iptables_persistent_version)).to receive(:value).and_return('0.5.3ubuntu2')
199-
allow(subject).to receive(:execute).with(%w[/usr/sbin/service iptables-persistent save])
200-
subject.persist_iptables(proto)
198+
allow(host).to receive(:execute).with(%w[/usr/sbin/service iptables-persistent save])
199+
host.persist_iptables(proto)
201200
end
202201

203202
it 'is expected to not exec for older Ubuntu which does not support IPv6' do
204203
allow(Facter.fact(:osfamily)).to receive(:value).and_return(nil)
205204
allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('Ubuntu')
206205
allow(Facter.fact(:iptables_persistent_version)).to receive(:value).and_return('0.0.20090701')
207-
allow(subject).to receive(:execute).never
208-
subject.persist_iptables(proto)
206+
allow(host).to receive(:execute).never
207+
host.persist_iptables(proto)
209208
end
210209

211210
it 'is expected to not exec for Suse which is not supported' do
212211
allow(Facter.fact(:osfamily)).to receive(:value).and_return('Suse')
213-
allow(subject).to receive(:execute).never
214-
subject.persist_iptables(proto)
212+
allow(host).to receive(:execute).never
213+
host.persist_iptables(proto)
215214
end
216215
end
217216
# rubocop:enable RSpec/SubjectStub

0 commit comments

Comments
 (0)