PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/puphpet/puppet/modules/firewall/spec/unit/puppet/util/firewall_spec.rb

https://github.com/innesm4/Environment-setup
Ruby | 197 lines | 171 code | 26 blank | 0 comment | 53 complexity | 7fe65c0ab8ea7438ec99a6fab0e2c619 MD5 | raw file
Possible License(s): Apache-2.0
  1. require 'spec_helper'
  2. describe 'Puppet::Util::Firewall' do
  3. let(:resource) {
  4. type = Puppet::Type.type(:firewall)
  5. provider = double 'provider'
  6. allow(provider).to receive(:name).and_return(:iptables)
  7. allow(Puppet::Type::Firewall).to receive(:defaultprovider).and_return(provider)
  8. type.new({:name => '000 test foo'})
  9. }
  10. before(:each) { resource }
  11. describe '#host_to_ip' do
  12. subject { resource }
  13. specify {
  14. expect(Resolv).to receive(:getaddress).with('puppetlabs.com').and_return('96.126.112.51')
  15. subject.host_to_ip('puppetlabs.com').should == '96.126.112.51/32'
  16. }
  17. specify { subject.host_to_ip('96.126.112.51').should == '96.126.112.51/32' }
  18. specify { subject.host_to_ip('96.126.112.51/32').should == '96.126.112.51/32' }
  19. specify { subject.host_to_ip('2001:db8:85a3:0:0:8a2e:370:7334').should == '2001:db8:85a3::8a2e:370:7334/128' }
  20. specify { subject.host_to_ip('2001:db8:1234::/48').should == '2001:db8:1234::/48' }
  21. specify { subject.host_to_ip('0.0.0.0/0').should == nil }
  22. specify { subject.host_to_ip('::/0').should == nil }
  23. end
  24. describe '#host_to_mask' do
  25. subject { resource }
  26. specify {
  27. expect(Resolv).to receive(:getaddress).at_least(:once).with('puppetlabs.com').and_return('96.126.112.51')
  28. subject.host_to_mask('puppetlabs.com').should == '96.126.112.51/32'
  29. subject.host_to_mask('!puppetlabs.com').should == '! 96.126.112.51/32'
  30. }
  31. specify { subject.host_to_mask('96.126.112.51').should == '96.126.112.51/32' }
  32. specify { subject.host_to_mask('!96.126.112.51').should == '! 96.126.112.51/32' }
  33. specify { subject.host_to_mask('96.126.112.51/32').should == '96.126.112.51/32' }
  34. specify { subject.host_to_mask('! 96.126.112.51/32').should == '! 96.126.112.51/32' }
  35. specify { subject.host_to_mask('2001:db8:85a3:0:0:8a2e:370:7334').should == '2001:db8:85a3::8a2e:370:7334/128' }
  36. specify { subject.host_to_mask('!2001:db8:85a3:0:0:8a2e:370:7334').should == '! 2001:db8:85a3::8a2e:370:7334/128' }
  37. specify { subject.host_to_mask('2001:db8:1234::/48').should == '2001:db8:1234::/48' }
  38. specify { subject.host_to_mask('! 2001:db8:1234::/48').should == '! 2001:db8:1234::/48' }
  39. specify { subject.host_to_mask('0.0.0.0/0').should == nil }
  40. specify { subject.host_to_mask('!0.0.0.0/0').should == nil }
  41. specify { subject.host_to_mask('::/0').should == nil }
  42. specify { subject.host_to_mask('! ::/0').should == nil }
  43. end
  44. describe '#icmp_name_to_number' do
  45. describe 'proto unsupported' do
  46. subject { resource }
  47. %w{inet5 inet8 foo}.each do |proto|
  48. it "should reject invalid proto #{proto}" do
  49. expect { subject.icmp_name_to_number('echo-reply', proto) }.
  50. to raise_error(ArgumentError, "unsupported protocol family '#{proto}'")
  51. end
  52. end
  53. end
  54. describe 'proto IPv4' do
  55. proto = 'inet'
  56. subject { resource }
  57. specify { subject.icmp_name_to_number('echo-reply', proto).should == '0' }
  58. specify { subject.icmp_name_to_number('destination-unreachable', proto).should == '3' }
  59. specify { subject.icmp_name_to_number('source-quench', proto).should == '4' }
  60. specify { subject.icmp_name_to_number('redirect', proto).should == '6' }
  61. specify { subject.icmp_name_to_number('echo-request', proto).should == '8' }
  62. specify { subject.icmp_name_to_number('router-advertisement', proto).should == '9' }
  63. specify { subject.icmp_name_to_number('router-solicitation', proto).should == '10' }
  64. specify { subject.icmp_name_to_number('time-exceeded', proto).should == '11' }
  65. specify { subject.icmp_name_to_number('parameter-problem', proto).should == '12' }
  66. specify { subject.icmp_name_to_number('timestamp-request', proto).should == '13' }
  67. specify { subject.icmp_name_to_number('timestamp-reply', proto).should == '14' }
  68. specify { subject.icmp_name_to_number('address-mask-request', proto).should == '17' }
  69. specify { subject.icmp_name_to_number('address-mask-reply', proto).should == '18' }
  70. end
  71. describe 'proto IPv6' do
  72. proto = 'inet6'
  73. subject { resource }
  74. specify { subject.icmp_name_to_number('destination-unreachable', proto).should == '1' }
  75. specify { subject.icmp_name_to_number('time-exceeded', proto).should == '3' }
  76. specify { subject.icmp_name_to_number('parameter-problem', proto).should == '4' }
  77. specify { subject.icmp_name_to_number('echo-request', proto).should == '128' }
  78. specify { subject.icmp_name_to_number('echo-reply', proto).should == '129' }
  79. specify { subject.icmp_name_to_number('router-solicitation', proto).should == '133' }
  80. specify { subject.icmp_name_to_number('router-advertisement', proto).should == '134' }
  81. specify { subject.icmp_name_to_number('redirect', proto).should == '137' }
  82. end
  83. end
  84. describe '#string_to_port' do
  85. subject { resource }
  86. specify { subject.string_to_port('80','tcp').should == '80' }
  87. specify { subject.string_to_port(80,'tcp').should == '80' }
  88. specify { subject.string_to_port('http','tcp').should == '80' }
  89. specify { subject.string_to_port('domain','udp').should == '53' }
  90. end
  91. describe '#to_hex32' do
  92. subject { resource }
  93. specify { subject.to_hex32('0').should == '0x0' }
  94. specify { subject.to_hex32('0x32').should == '0x32' }
  95. specify { subject.to_hex32('42').should == '0x2a' }
  96. specify { subject.to_hex32('4294967295').should == '0xffffffff' }
  97. specify { subject.to_hex32('4294967296').should == nil }
  98. specify { subject.to_hex32('-1').should == nil }
  99. specify { subject.to_hex32('bananas').should == nil }
  100. end
  101. describe '#persist_iptables' do
  102. before { Facter.clear }
  103. subject { resource }
  104. describe 'when proto is IPv4' do
  105. let(:proto) { 'IPv4' }
  106. it 'should exec /sbin/service if running RHEL 6 or earlier' do
  107. allow(Facter.fact(:osfamily)).to receive(:value).and_return('RedHat')
  108. allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('RedHat')
  109. allow(Facter.fact(:operatingsystemrelease)).to receive(:value).and_return('6')
  110. expect(subject).to receive(:execute).with(%w{/sbin/service iptables save})
  111. subject.persist_iptables(proto)
  112. end
  113. it 'should exec for systemd if running RHEL 7 or greater' do
  114. allow(Facter.fact(:osfamily)).to receive(:value).and_return('RedHat')
  115. allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('RedHat')
  116. allow(Facter.fact(:operatingsystemrelease)).to receive(:value).and_return('7')
  117. expect(subject).to receive(:execute).with(%w{/usr/libexec/iptables/iptables.init save})
  118. subject.persist_iptables(proto)
  119. end
  120. it 'should exec for systemd if running Fedora 15 or greater' do
  121. allow(Facter.fact(:osfamily)).to receive(:value).and_return('RedHat')
  122. allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('Fedora')
  123. allow(Facter.fact(:operatingsystemrelease)).to receive(:value).and_return('15')
  124. expect(subject).to receive(:execute).with(%w{/usr/libexec/iptables/iptables.init save})
  125. subject.persist_iptables(proto)
  126. end
  127. it 'should exec for CentOS identified from operatingsystem' do
  128. allow(Facter.fact(:osfamily)).to receive(:value).and_return(nil)
  129. allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('CentOS')
  130. expect(subject).to receive(:execute).with(%w{/sbin/service iptables save})
  131. subject.persist_iptables(proto)
  132. end
  133. it 'should exec for Archlinux identified from osfamily' do
  134. allow(Facter.fact(:osfamily)).to receive(:value).and_return('Archlinux')
  135. expect(subject).to receive(:execute).with(['/bin/sh', '-c', '/usr/sbin/iptables-save > /etc/iptables/iptables.rules'])
  136. subject.persist_iptables(proto)
  137. end
  138. it 'should raise a warning when exec fails' do
  139. allow(Facter.fact(:osfamily)).to receive(:value).and_return('RedHat')
  140. allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('RedHat')
  141. allow(Facter.fact(:operatingsystemrelease)).to receive(:value).and_return('6')
  142. expect(subject).to receive(:execute).with(%w{/sbin/service iptables save}).
  143. and_raise(Puppet::ExecutionFailure, 'some error')
  144. expect(subject).to receive(:warning).with('Unable to persist firewall rules: some error')
  145. subject.persist_iptables(proto)
  146. end
  147. end
  148. describe 'when proto is IPv6' do
  149. let(:proto) { 'IPv6' }
  150. it 'should exec for newer Ubuntu' do
  151. allow(Facter.fact(:osfamily)).to receive(:value).and_return(nil)
  152. allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('Ubuntu')
  153. allow(Facter.fact(:iptables_persistent_version)).to receive(:value).and_return('0.5.3ubuntu2')
  154. expect(subject).to receive(:execute).with(%w{/usr/sbin/service iptables-persistent save})
  155. subject.persist_iptables(proto)
  156. end
  157. it 'should not exec for older Ubuntu which does not support IPv6' do
  158. allow(Facter.fact(:osfamily)).to receive(:value).and_return(nil)
  159. allow(Facter.fact(:operatingsystem)).to receive(:value).and_return('Ubuntu')
  160. allow(Facter.fact(:iptables_persistent_version)).to receive(:value).and_return('0.0.20090701')
  161. expect(subject).to receive(:execute).never
  162. subject.persist_iptables(proto)
  163. end
  164. it 'should not exec for Suse which is not supported' do
  165. allow(Facter.fact(:osfamily)).to receive(:value).and_return('Suse')
  166. expect(subject).to receive(:execute).never
  167. subject.persist_iptables(proto)
  168. end
  169. end
  170. end
  171. end