Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / spec / classes / ip_nat_spec.rb @ master

Historique | Voir | Annoter | Télécharger (9,47 ko)

1
# frozen_string_literal: true
2

    
3
require 'spec_helper'
4

    
5
describe 'nftables' do
6
  let(:pre_condition) { 'Exec{path => "/bin"}' }
7

    
8
  on_supported_os.each do |os, os_facts|
9
    context "on #{os}" do
10
      let(:facts) { os_facts }
11

    
12
      nft_mode = case os_facts[:os]['family']
13
                 when 'RedHat'
14
                   '0600'
15
                 else
16
                   '0640'
17
                 end
18

    
19
      it { is_expected.to compile }
20

    
21
      it {
22
        expect(subject).to contain_concat('nftables-ip-nat').with(
23
          path: '/etc/nftables/puppet-preflight/ip-nat.nft',
24
          ensure: 'present',
25
          owner: 'root',
26
          group: 'root',
27
          mode: nft_mode
28
        )
29
      }
30

    
31
      it {
32
        expect(subject).to contain_concat__fragment('nftables-ip-nat-header').with(
33
          target: 'nftables-ip-nat',
34
          content: %r{^table ip nat \{$},
35
          order: '00'
36
        )
37
      }
38

    
39
      it {
40
        expect(subject).to contain_concat__fragment('nftables-ip-nat-body').with(
41
          target: 'nftables-ip-nat',
42
          content: %r{^\s+include "ip-nat-chain-\*\.nft"$},
43
          order: '98'
44
        )
45
      }
46

    
47
      it {
48
        expect(subject).to contain_concat__fragment('nftables-ip-nat-footer').with(
49
          target: 'nftables-ip-nat',
50
          content: %r{^\}$},
51
          order: '99'
52
        )
53
      }
54

    
55
      it {
56
        expect(subject).to contain_concat('nftables-ip6-nat').with(
57
          path: '/etc/nftables/puppet-preflight/ip6-nat.nft',
58
          ensure: 'present',
59
          owner: 'root',
60
          group: 'root',
61
          mode: nft_mode
62
        )
63
      }
64

    
65
      it {
66
        expect(subject).to contain_concat__fragment('nftables-ip6-nat-header').with(
67
          target: 'nftables-ip6-nat',
68
          content: %r{^table ip6 nat \{$},
69
          order: '00'
70
        )
71
      }
72

    
73
      it {
74
        expect(subject).to contain_concat__fragment('nftables-ip6-nat-body').with(
75
          target: 'nftables-ip6-nat',
76
          content: %r{^\s+include "ip6-nat-chain-\*\.nft"$},
77
          order: '98'
78
        )
79
      }
80

    
81
      it {
82
        expect(subject).to contain_concat__fragment('nftables-ip6-nat-footer').with(
83
          target: 'nftables-ip6-nat',
84
          content: %r{^\}$},
85
          order: '99'
86
        )
87
      }
88

    
89
      context 'table ip nat chain prerouting' do
90
        it {
91
          expect(subject).to contain_concat('nftables-ip-nat-chain-PREROUTING').with(
92
            path: '/etc/nftables/puppet-preflight/ip-nat-chain-PREROUTING.nft',
93
            owner: 'root',
94
            group: 'root',
95
            mode: nft_mode,
96
            ensure_newline: true
97
          )
98
        }
99

    
100
        it {
101
          expect(subject).to contain_concat__fragment('nftables-ip-nat-chain-PREROUTING-header').with(
102
            target: 'nftables-ip-nat-chain-PREROUTING',
103
            content: %r{^chain PREROUTING \{$},
104
            order: '00'
105
          )
106
        }
107

    
108
        it {
109
          expect(subject).to contain_concat__fragment('nftables-ip-nat-chain-PREROUTING-rule-type').with(
110
            target: 'nftables-ip-nat-chain-PREROUTING',
111
            content: %r{^  type nat hook prerouting priority -100$},
112
            order: '01-nftables-ip-nat-chain-PREROUTING-rule-type-b'
113
          )
114
        }
115

    
116
        it {
117
          expect(subject).to contain_concat__fragment('nftables-ip-nat-chain-PREROUTING-rule-policy').with(
118
            target: 'nftables-ip-nat-chain-PREROUTING',
119
            content: %r{^  policy accept$},
120
            order: '02-nftables-ip-nat-chain-PREROUTING-rule-policy-b'
121
          )
122
        }
123

    
124
        it {
125
          expect(subject).to contain_concat__fragment('nftables-ip-nat-chain-PREROUTING-footer').with(
126
            target: 'nftables-ip-nat-chain-PREROUTING',
127
            content: %r{^\}$},
128
            order: '99'
129
          )
130
        }
131
      end
132

    
133
      context 'table ipv4 nat chain postrouting' do
134
        it {
135
          expect(subject).to contain_concat('nftables-ip-nat-chain-POSTROUTING').with(
136
            path: '/etc/nftables/puppet-preflight/ip-nat-chain-POSTROUTING.nft',
137
            owner: 'root',
138
            group: 'root',
139
            mode: nft_mode,
140
            ensure_newline: true
141
          )
142
        }
143

    
144
        it {
145
          expect(subject).to contain_concat__fragment('nftables-ip-nat-chain-POSTROUTING-header').with(
146
            target: 'nftables-ip-nat-chain-POSTROUTING',
147
            content: %r{^chain POSTROUTING \{$},
148
            order: '00'
149
          )
150
        }
151

    
152
        it {
153
          expect(subject).to contain_concat__fragment('nftables-ip-nat-chain-POSTROUTING-rule-type').with(
154
            target: 'nftables-ip-nat-chain-POSTROUTING',
155
            content: %r{^  type nat hook postrouting priority 100$},
156
            order: '01-nftables-ip-nat-chain-POSTROUTING-rule-type-b'
157
          )
158
        }
159

    
160
        it {
161
          expect(subject).to contain_concat__fragment('nftables-ip-nat-chain-POSTROUTING-rule-policy').with(
162
            target: 'nftables-ip-nat-chain-POSTROUTING',
163
            content: %r{^  policy accept$},
164
            order: '02-nftables-ip-nat-chain-POSTROUTING-rule-policy-b'
165
          )
166
        }
167

    
168
        it {
169
          expect(subject).to contain_concat__fragment('nftables-ip-nat-chain-POSTROUTING-footer').with(
170
            target: 'nftables-ip-nat-chain-POSTROUTING',
171
            content: %r{^\}$},
172
            order: '99'
173
          )
174
        }
175
      end
176

    
177
      context 'table ip6 nat chain prerouting' do
178
        it {
179
          expect(subject).to contain_concat('nftables-ip6-nat-chain-PREROUTING6').with(
180
            path: '/etc/nftables/puppet-preflight/ip6-nat-chain-PREROUTING6.nft',
181
            owner: 'root',
182
            group: 'root',
183
            mode: nft_mode,
184
            ensure_newline: true
185
          )
186
        }
187

    
188
        it {
189
          expect(subject).to contain_concat__fragment('nftables-ip6-nat-chain-PREROUTING6-header').with(
190
            target: 'nftables-ip6-nat-chain-PREROUTING6',
191
            content: %r{^chain PREROUTING6 \{$},
192
            order: '00'
193
          )
194
        }
195

    
196
        it {
197
          expect(subject).to contain_concat__fragment('nftables-ip6-nat-chain-PREROUTING6-rule-type').with(
198
            target: 'nftables-ip6-nat-chain-PREROUTING6',
199
            content: %r{^  type nat hook prerouting priority -100$},
200
            order: '01-nftables-ip6-nat-chain-PREROUTING6-rule-type-b'
201
          )
202
        }
203

    
204
        it {
205
          expect(subject).to contain_concat__fragment('nftables-ip6-nat-chain-PREROUTING6-rule-policy').with(
206
            target: 'nftables-ip6-nat-chain-PREROUTING6',
207
            content: %r{^  policy accept$},
208
            order: '02-nftables-ip6-nat-chain-PREROUTING6-rule-policy-b'
209
          )
210
        }
211

    
212
        it {
213
          expect(subject).to contain_concat__fragment('nftables-ip6-nat-chain-PREROUTING6-footer').with(
214
            target: 'nftables-ip6-nat-chain-PREROUTING6',
215
            content: %r{^\}$},
216
            order: '99'
217
          )
218
        }
219
      end
220

    
221
      context 'table ipv6 nat chain postrouting' do
222
        it {
223
          expect(subject).to contain_concat('nftables-ip6-nat-chain-POSTROUTING6').with(
224
            path: '/etc/nftables/puppet-preflight/ip6-nat-chain-POSTROUTING6.nft',
225
            owner: 'root',
226
            group: 'root',
227
            mode: nft_mode,
228
            ensure_newline: true
229
          )
230
        }
231

    
232
        it {
233
          expect(subject).to contain_concat__fragment('nftables-ip6-nat-chain-POSTROUTING6-header').with(
234
            target: 'nftables-ip6-nat-chain-POSTROUTING6',
235
            content: %r{^chain POSTROUTING6 \{$},
236
            order: '00'
237
          )
238
        }
239

    
240
        it {
241
          expect(subject).to contain_concat__fragment('nftables-ip6-nat-chain-POSTROUTING6-rule-type').with(
242
            target: 'nftables-ip6-nat-chain-POSTROUTING6',
243
            content: %r{^  type nat hook postrouting priority 100$},
244
            order: '01-nftables-ip6-nat-chain-POSTROUTING6-rule-type-b'
245
          )
246
        }
247

    
248
        it {
249
          expect(subject).to contain_concat__fragment('nftables-ip6-nat-chain-POSTROUTING6-rule-policy').with(
250
            target: 'nftables-ip6-nat-chain-POSTROUTING6',
251
            content: %r{^  policy accept$},
252
            order: '02-nftables-ip6-nat-chain-POSTROUTING6-rule-policy-b'
253
          )
254
        }
255

    
256
        it {
257
          expect(subject).to contain_concat__fragment('nftables-ip6-nat-chain-POSTROUTING6-footer').with(
258
            target: 'nftables-ip6-nat-chain-POSTROUTING6',
259
            content: %r{^\}$},
260
            order: '99'
261
          )
262
        }
263
      end
264

    
265
      context 'custom ip nat table name' do
266
        let(:params) do
267
          {
268
            'nat_table_name' => 'mycustomtablename',
269
          }
270
        end
271

    
272
        it { is_expected.to compile }
273

    
274
        it {
275
          expect(subject).to contain_concat('nftables-ip-mycustomtablename').with(
276
            path: '/etc/nftables/puppet-preflight/ip-mycustomtablename.nft',
277
            ensure: 'present',
278
            owner: 'root',
279
            group: 'root',
280
            mode: nft_mode
281
          )
282
        }
283
      end
284

    
285
      context 'all nat tables disabled' do
286
        let(:params) do
287
          {
288
            'nat' => false,
289
          }
290
        end
291

    
292
        it { is_expected.not_to contain_class('nftables::ip_nat') }
293
        it { is_expected.not_to contain_nftables__config('ip-nat') }
294
        it { is_expected.not_to contain_nftables__config('ip6-nat') }
295
        it { is_expected.not_to contain_nftables__chain('PREROUTING') }
296
        it { is_expected.not_to contain_nftables__chain('POSTROUTING') }
297
        it { is_expected.not_to contain_nftables__chain('PREROUTING6') }
298
        it { is_expected.not_to contain_nftables__chain('POSTROUTING6') }
299
      end
300
    end
301
  end
302
end