|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper_acceptance' |
| 4 | + |
| 5 | +describe 'puppet resource firewallchain command' do |
| 6 | + before :all do |
| 7 | + iptables_flush_all_tables |
| 8 | + ip6tables_flush_all_tables |
| 9 | + end |
| 10 | + |
| 11 | + describe 'IPv4' do |
| 12 | + context 'when present' do |
| 13 | + pp1 = <<-PUPPETCODE |
| 14 | + firewallchain { 'MY_CHAIN:filter:IPv4': |
| 15 | + ensure => present, |
| 16 | + } |
| 17 | + PUPPETCODE |
| 18 | + it 'applies cleanly' do |
| 19 | + # Run it twice and test for idempotency |
| 20 | + idempotent_apply(pp1) |
| 21 | + end |
| 22 | + |
| 23 | + it 'finds the chain' do |
| 24 | + run_shell('iptables-save') do |r| |
| 25 | + expect(r.stdout).to match(%r{MY_CHAIN}) |
| 26 | + end |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + context 'when absent' do |
| 31 | + pp2 = <<-PUPPETCODE |
| 32 | + firewallchain { 'MY_CHAIN:filter:IPv4': |
| 33 | + ensure => absent, |
| 34 | + } |
| 35 | + PUPPETCODE |
| 36 | + it 'applies cleanly' do |
| 37 | + # Run it twice and test for idempotency |
| 38 | + idempotent_apply(pp2) |
| 39 | + end |
| 40 | + |
| 41 | + it 'fails to find the chain' do |
| 42 | + run_shell('iptables-save') do |r| |
| 43 | + expect(r.stdout).not_to match(%r{MY_CHAIN}) |
| 44 | + end |
| 45 | + end |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + describe 'IPv6' do |
| 50 | + context 'when present' do |
| 51 | + pp3 = <<-PUPPETCODE |
| 52 | + firewallchain { 'MY_CHAIN:filter:IPv6': |
| 53 | + ensure => present, |
| 54 | + } |
| 55 | + PUPPETCODE |
| 56 | + it 'applies cleanly' do |
| 57 | + # Run it twice and test for idempotency |
| 58 | + idempotent_apply(pp3) |
| 59 | + end |
| 60 | + |
| 61 | + it 'finds the chain' do |
| 62 | + run_shell('ip6tables-save') do |r| |
| 63 | + expect(r.stdout).to match(%r{MY_CHAIN}) |
| 64 | + end |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + context 'when absent' do |
| 69 | + pp4 = <<-PUPPETCODE |
| 70 | + firewallchain { 'MY_CHAIN:filter:IPv6': |
| 71 | + ensure => absent, |
| 72 | + } |
| 73 | + PUPPETCODE |
| 74 | + it 'applies cleanly' do |
| 75 | + # Run it twice and test for idempotency |
| 76 | + idempotent_apply(pp4) |
| 77 | + end |
| 78 | + |
| 79 | + it 'fails to find the chain' do |
| 80 | + run_shell('ip6tables-save') do |r| |
| 81 | + expect(r.stdout).not_to match(%r{MY_CHAIN}) |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + # XXX purge => false is not yet implemented |
| 88 | + # context 'when adding a firewall rule to a chain:' do |
| 89 | + # pp5 = <<-PUPPETCODE |
| 90 | + # firewallchain { 'MY_CHAIN:filter:IPv4': |
| 91 | + # ensure => present, |
| 92 | + # } |
| 93 | + # firewall { '100 my rule': |
| 94 | + # chain => 'MY_CHAIN', |
| 95 | + # action => 'accept', |
| 96 | + # proto => 'tcp', |
| 97 | + # dport => 5000, |
| 98 | + # } |
| 99 | + # PUPPETCODE |
| 100 | + # it 'applies cleanly' do |
| 101 | + # # Run it twice and test for idempotency |
| 102 | + # apply_manifest(pp5, :catch_failures => true) |
| 103 | + # apply_manifest(pp5, :catch_changes => do_catch_changes) |
| 104 | + # end |
| 105 | + # end |
| 106 | + |
| 107 | + # context 'when not purge firewallchain chains:' do |
| 108 | + # pp6 = <<-PUPPETCODE |
| 109 | + # firewallchain { 'MY_CHAIN:filter:IPv4': |
| 110 | + # ensure => present, |
| 111 | + # purge => false, |
| 112 | + # before => Resources['firewall'], |
| 113 | + # } |
| 114 | + # resources { 'firewall': |
| 115 | + # purge => true, |
| 116 | + # } |
| 117 | + # PUPPETCODE |
| 118 | + # it 'does not purge the rule' do |
| 119 | + # # Run it twice and test for idempotency |
| 120 | + # apply_manifest(pp6, :catch_failures => true) do |r| |
| 121 | + # expect(r.stdout).to_not match(/removed/) |
| 122 | + # expect(r.stderr).to eq('') |
| 123 | + # end |
| 124 | + # apply_manifest(pp6, :catch_changes => do_catch_changes) |
| 125 | + # end |
| 126 | + |
| 127 | + # pp7 = <<-PUPPETCODE |
| 128 | + # firewall { '100 my rule': |
| 129 | + # chain => 'MY_CHAIN', |
| 130 | + # action => 'accept', |
| 131 | + # proto => 'tcp', |
| 132 | + # dport => 5000, |
| 133 | + # } |
| 134 | + # PUPPETCODE |
| 135 | + # it 'still has the rule' do |
| 136 | + # # Run it twice and test for idempotency |
| 137 | + # apply_manifest(pp7, :catch_changes => do_catch_changes) |
| 138 | + # end |
| 139 | + # end |
| 140 | + |
| 141 | + describe 'policy' do |
| 142 | + after :all do |
| 143 | + run_shell('iptables -t filter -P FORWARD ACCEPT') |
| 144 | + end |
| 145 | + |
| 146 | + context 'when DROP' do |
| 147 | + pp8 = <<-PUPPETCODE |
| 148 | + firewallchain { 'FORWARD:filter:IPv4': |
| 149 | + policy => 'drop', |
| 150 | + } |
| 151 | + PUPPETCODE |
| 152 | + it 'applies cleanly' do |
| 153 | + # Run it twice and test for idempotency |
| 154 | + idempotent_apply(pp8) |
| 155 | + end |
| 156 | + |
| 157 | + it 'finds the chain' do |
| 158 | + run_shell('iptables-save') do |r| |
| 159 | + expect(r.stdout).to match(%r{FORWARD DROP}) |
| 160 | + end |
| 161 | + end |
| 162 | + end |
| 163 | + end |
| 164 | +end |
0 commit comments