|
12 | 12 | before(:each) { resource } |
13 | 13 |
|
14 | 14 | describe '#host_to_ip' do |
15 | | - subject { resource } |
| 15 | + subject(:host) { resource } |
16 | 16 |
|
17 | 17 | it { # rubocop:disable RSpec/MultipleExpectations |
18 | 18 | 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' |
21 | 21 | } |
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 } |
28 | 28 | end |
29 | 29 |
|
30 | 30 | describe '#host_to_mask' do |
31 | | - subject { resource } |
| 31 | + subject(:host) { resource } |
32 | 32 |
|
33 | 33 | it { # rubocop:disable RSpec/MultipleExpectations |
34 | 34 | 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' |
39 | 39 | } |
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 } |
52 | 52 | end |
53 | 53 |
|
54 | 54 | describe '#icmp_name_to_number' do |
55 | 55 | describe 'proto unsupported' do |
56 | | - subject { resource } |
| 56 | + subject(:host) { resource } |
57 | 57 |
|
58 | 58 | %w[inet5 inet8 foo].each do |proto| |
59 | 59 | 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) } |
61 | 61 | .to raise_error(ArgumentError, "unsupported protocol family '#{proto}'") |
62 | 62 | end |
63 | 63 | end |
64 | 64 | end |
65 | 65 |
|
66 | 66 | describe 'proto IPv4' do |
67 | 67 | 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' } |
83 | 83 | end |
84 | 84 |
|
85 | 85 | describe 'proto IPv6' do |
86 | 86 | 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' } |
99 | 99 | end |
100 | 100 | end |
101 | 101 |
|
102 | 102 | describe '#string_to_port' do |
103 | | - subject { resource } |
| 103 | + subject(:host) { resource } |
104 | 104 |
|
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' } |
109 | 109 | end |
110 | 110 |
|
111 | 111 | 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 } |
121 | 121 | end |
122 | 122 |
|
123 | 123 | describe '#persist_iptables' do |
124 | 124 | before(:each) { Facter.clear } |
125 | | - subject { resource } |
| 125 | + subject(:host) { resource } |
126 | 126 |
|
127 | 127 | # rubocop:disable RSpec/SubjectStub |
128 | 128 | describe 'when proto is IPv4' do |
|
133 | 133 | allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('RedHat') |
134 | 134 | allow(Facter.fact(:operatingsystemrelease)).to receive(:value).and_return('6') |
135 | 135 |
|
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) |
138 | 138 | end |
139 | 139 |
|
140 | 140 | it 'is expected to exec for systemd if running RHEL 7 or greater' do |
141 | 141 | allow(Facter.fact(:osfamily)).to receive(:value).and_return('RedHat') |
142 | 142 | allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('RedHat') |
143 | 143 | allow(Facter.fact(:operatingsystemrelease)).to receive(:value).and_return('7') |
144 | 144 |
|
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) |
147 | 147 | end |
148 | 148 |
|
149 | 149 | it 'is expected to exec for systemd if running Fedora 15 or greater' do |
150 | 150 | allow(Facter.fact(:osfamily)).to receive(:value).and_return('RedHat') |
151 | 151 | allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('Fedora') |
152 | 152 | allow(Facter.fact(:operatingsystemrelease)).to receive(:value).and_return('15') |
153 | 153 |
|
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) |
156 | 156 | end |
157 | 157 |
|
158 | 158 | it 'is expected to exec for CentOS 6 identified from operatingsystem and operatingsystemrelease' do |
159 | 159 | allow(Facter.fact(:osfamily)).to receive(:value).and_return(nil) |
160 | 160 | allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('CentOS') |
161 | 161 | 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) |
164 | 164 | end |
165 | 165 |
|
166 | 166 | it 'is expected to exec for CentOS 7 identified from operatingsystem and operatingsystemrelease' do |
167 | 167 | allow(Facter.fact(:osfamily)).to receive(:value).and_return(nil) |
168 | 168 | allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('CentOS') |
169 | 169 | 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) |
172 | 172 | end |
173 | 173 |
|
174 | 174 | it 'is expected to exec for Archlinux identified from osfamily' do |
175 | 175 | 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) |
178 | 178 | end |
179 | 179 |
|
180 | 180 | it 'is expected to raise a warning when exec fails' do # rubocop:disable RSpec/ExampleLength |
181 | 181 | allow(Facter.fact(:osfamily)).to receive(:value).and_return('RedHat') |
182 | 182 | allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('RedHat') |
183 | 183 | allow(Facter.fact(:operatingsystemrelease)).to receive(:value).and_return('6') |
184 | 184 |
|
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) |
189 | 188 | end |
190 | 189 | end |
191 | 190 |
|
|
196 | 195 | allow(Facter.fact(:osfamily)).to receive(:value).and_return(nil) |
197 | 196 | allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('Ubuntu') |
198 | 197 | 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) |
201 | 200 | end |
202 | 201 |
|
203 | 202 | it 'is expected to not exec for older Ubuntu which does not support IPv6' do |
204 | 203 | allow(Facter.fact(:osfamily)).to receive(:value).and_return(nil) |
205 | 204 | allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('Ubuntu') |
206 | 205 | 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) |
209 | 208 | end |
210 | 209 |
|
211 | 210 | it 'is expected to not exec for Suse which is not supported' do |
212 | 211 | 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) |
215 | 214 | end |
216 | 215 | end |
217 | 216 | # rubocop:enable RSpec/SubjectStub |
|
0 commit comments