postfix_spec.rb 14.7 KB
Newer Older
1
2
3
require 'spec_helper'

describe 'postfix' do
Raphaël Pinson's avatar
Raphaël Pinson committed
4
5
6
  context 'when using defaults' do
    context 'when on Debian' do
      let (:facts) { {
Mickaël Canévet's avatar
Mickaël Canévet committed
7
        :lsbdistcodename => 'wheezy',
Raphaël Pinson's avatar
Raphaël Pinson committed
8
9
10
        :operatingsystem => 'Debian',
        :osfamily        => 'Debian',
        :fqdn            => 'fqdn.example.com',
11
        :path            => '/foo/bar',
Raphaël Pinson's avatar
Raphaël Pinson committed
12
13
      } }

Raphaël Pinson's avatar
Raphaël Pinson committed
14
15
      it { is_expected.to contain_package('postfix') }
      it { is_expected.to contain_package('mailx') }
Raphaël Pinson's avatar
Raphaël Pinson committed
16

Raphaël Pinson's avatar
Raphaël Pinson committed
17
18
19
20
21
      it { is_expected.to contain_file('/etc/mailname').without('seltype').with_content("fqdn.example.com\n") }
      it { is_expected.to contain_file('/etc/aliases').without('seltype').with_content("# file managed by puppet\n") }
      it { is_expected.to contain_exec('newaliases').with_refreshonly('true') }
      it { is_expected.to contain_file('/etc/postfix/master.cf').without('seltype') }
      it { is_expected.to contain_file('/etc/postfix/main.cf').without('seltype') }
Raphaël Pinson's avatar
Raphaël Pinson committed
22

Raphaël Pinson's avatar
Raphaël Pinson committed
23
24
25
      it { is_expected.to contain_postfix__config('myorigin').with_value('fqdn.example.com') }
      it { is_expected.to contain_postfix__config('alias_maps').with_value('hash:/etc/aliases') }
      it { is_expected.to contain_postfix__config('inet_interfaces').with_value('all') }
Raphaël Pinson's avatar
Raphaël Pinson committed
26

Raphaël Pinson's avatar
Raphaël Pinson committed
27
      it { is_expected.to contain_mailalias('root').with_recipient('nobody') }
Raphaël Pinson's avatar
Raphaël Pinson committed
28
29

      it {
Raphaël Pinson's avatar
Raphaël Pinson committed
30
        is_expected.to contain_service('postfix').with(
Raphaël Pinson's avatar
Raphaël Pinson committed
31
32
33
34
35
36
37
38
39
          :ensure    => 'running',
          :enable    => 'true',
          :hasstatus => 'true',
          :restart   => '/etc/init.d/postfix reload'
      ) }
    end

    context 'when on RedHat' do
      let (:facts) { {
Ben Hocker's avatar
Ben Hocker committed
40
        :lsbdistcodename           => 'Maipo',
Mickaël Canévet's avatar
Mickaël Canévet committed
41
42
43
44
45
        :fqdn                      => 'fqdn.example.com',
        :operatingsystem           => 'RedHat',
        :operatingsystemmajrelease => '7',
        :osfamily                  => 'RedHat',
        :path                      => '/foo/bar',
Raphaël Pinson's avatar
Raphaël Pinson committed
46
47
      } }

Raphaël Pinson's avatar
Raphaël Pinson committed
48
49
      it { is_expected.to contain_package('postfix') }
      it { is_expected.to contain_package('mailx') }
Raphaël Pinson's avatar
Raphaël Pinson committed
50

Raphaël Pinson's avatar
Raphaël Pinson committed
51
52
53
54
55
      it { is_expected.to contain_file('/etc/mailname').with_seltype('postfix_etc_t').with_content("fqdn.example.com\n") }
      it { is_expected.to contain_file('/etc/aliases').with_seltype('postfix_etc_t').with_content("# file managed by puppet\n") }
      it { is_expected.to contain_exec('newaliases').with_refreshonly('true') }
      it { is_expected.to contain_file('/etc/postfix/master.cf').with_seltype('postfix_etc_t') }
      it { is_expected.to contain_file('/etc/postfix/main.cf').with_seltype('postfix_etc_t') }
Raphaël Pinson's avatar
Raphaël Pinson committed
56

Raphaël Pinson's avatar
Raphaël Pinson committed
57
58
59
60
61
62
      it { is_expected.to contain_postfix__config('myorigin').with_value('fqdn.example.com') }
      it { is_expected.to contain_postfix__config('alias_maps').with_value('hash:/etc/aliases') }
      it { is_expected.to contain_postfix__config('inet_interfaces').with_value('all') }
      it { is_expected.to contain_postfix__config('sendmail_path') }
      it { is_expected.to contain_postfix__config('newaliases_path') }
      it { is_expected.to contain_postfix__config('mailq_path') }
Raphaël Pinson's avatar
Raphaël Pinson committed
63

Raphaël Pinson's avatar
Raphaël Pinson committed
64
      it { is_expected.to contain_mailalias('root').with_recipient('nobody') }
65

Raphaël Pinson's avatar
Raphaël Pinson committed
66
      it {
Raphaël Pinson's avatar
Raphaël Pinson committed
67
        is_expected.to contain_service('postfix').with(
Raphaël Pinson's avatar
Raphaël Pinson committed
68
69
70
71
72
73
          :ensure    => 'running',
          :enable    => 'true',
          :hasstatus => 'true',
          :restart   => '/etc/init.d/postfix reload'
      ) }
    end
74
  end
Raphaël Pinson's avatar
Raphaël Pinson committed
75
76
77
78
79

  context 'when setting parameters' do
    context 'when on Debian' do
      context "when setting smtp_listen to 'all'" do
        let (:facts) { {
Mickaël Canévet's avatar
Mickaël Canévet committed
80
          :lsbdistcodename => 'wheezy',
Raphaël Pinson's avatar
Raphaël Pinson committed
81
82
83
          :operatingsystem => 'Debian',
          :osfamily        => 'Debian',
          :fqdn            => 'fqdn.example.com',
84
          :path            => '/foo/bar',
Raphaël Pinson's avatar
Raphaël Pinson committed
85
86
87
88
89
        } }

        let (:params) { {
          :smtp_listen         => 'all',
          :root_mail_recipient => 'foo',
90
91
92
93
          :use_amavisd         => true,
          :use_dovecot_lda     => true,
          :use_schleuder       => true,
          :use_sympa           => true,
Raphaël Pinson's avatar
Raphaël Pinson committed
94
95
96
          :mail_user           => 'bar',
          :myorigin            => 'localhost',
          :inet_interfaces     => 'localhost2',
97
98
99
100
          :master_smtp         => "smtp      inet  n       -       -       -       -       smtpd
    -o smtpd_client_restrictions=check_client_access,hash:/etc/postfix/access,reject",
          :master_smtps        => 'smtps     inet  n       -       -       -       -       smtpd',
          :master_submission   => 'submission inet n       -       -       -       -       smtpd',
Raphaël Pinson's avatar
Raphaël Pinson committed
101
102
        } }

Raphaël Pinson's avatar
Raphaël Pinson committed
103
104
        it { is_expected.to contain_package('postfix') }
        it { is_expected.to contain_package('mailx') }
Raphaël Pinson's avatar
Raphaël Pinson committed
105

Raphaël Pinson's avatar
Raphaël Pinson committed
106
107
108
        it { is_expected.to contain_file('/etc/mailname').without('seltype').with_content("fqdn.example.com\n") }
        it { is_expected.to contain_file('/etc/aliases').without('seltype').with_content("# file managed by puppet\n") }
        it { is_expected.to contain_exec('newaliases').with_refreshonly('true') }
Raphaël Pinson's avatar
Raphaël Pinson committed
109
        it {
Raphaël Pinson's avatar
Raphaël Pinson committed
110
          is_expected.to contain_file('/etc/postfix/master.cf').without('seltype').with_content(
Raphaël Pinson's avatar
Raphaël Pinson committed
111
            /smtp      inet  n       -       -       -       -       smtpd/
112
113
114
115
116
117
118
119
          ).with_content(
            /amavis unix/
          ).with_content(
            /dovecot.*\n.* user=bar:bar /
          ).with_content(
            /schleuder/
          ).with_content(
            /sympa/
Raphaël Pinson's avatar
Raphaël Pinson committed
120
121
          ).with_content(
            /user=bar/
122
123
124
125
126
127
          ).with_content(
            /^smtp.*\n.*smtpd_client_restrictions=check_client_access,hash:/
          ).with_content(
            /^smtps     inet  n/
          ).with_content(
            /^submission inet n/
128
129
          )
        }
Raphaël Pinson's avatar
Raphaël Pinson committed
130
        it { is_expected.to contain_file('/etc/postfix/main.cf').without('seltype') }
Raphaël Pinson's avatar
Raphaël Pinson committed
131

Raphaël Pinson's avatar
Raphaël Pinson committed
132
133
134
        it { is_expected.to contain_postfix__config('myorigin').with_value('localhost') }
        it { is_expected.to contain_postfix__config('alias_maps').with_value('hash:/etc/aliases') }
        it { is_expected.to contain_postfix__config('inet_interfaces').with_value('localhost2') }
Raphaël Pinson's avatar
Raphaël Pinson committed
135

Raphaël Pinson's avatar
Raphaël Pinson committed
136
        it { is_expected.to contain_mailalias('root').with_recipient('foo') }
Raphaël Pinson's avatar
Raphaël Pinson committed
137
138

        it {
Raphaël Pinson's avatar
Raphaël Pinson committed
139
          is_expected.to contain_service('postfix').with(
Raphaël Pinson's avatar
Raphaël Pinson committed
140
141
142
143
144
145
146
            :ensure    => 'running',
            :enable    => 'true',
            :hasstatus => 'true',
            :restart   => '/etc/init.d/postfix reload'
        ) }
      end
    end
147
148
    context 'when on RedHat' do
      let (:facts) { {
Mickaël Canévet's avatar
Mickaël Canévet committed
149
150
        :augeasversion   => '1.2.0',
        :lsbdistcodename => 'wheezy',
151
152
        :operatingsystem => 'Debian',
        :osfamily        => 'Debian',
Mickaël Canévet's avatar
Mickaël Canévet committed
153
        :rubyversion     => '1.9.7',
154
        :fqdn            => 'fqdn.example.com',
155
        :path            => '/foo/bar',
156
157
158
159
160
161
      } }
      context 'when specifying inet_interfaces' do
        let (:params) { {
          :inet_interfaces => 'localhost2'
        } }
        it 'should create a postfix::config defined type with inet_interfaces specified properly' do
Raphaël Pinson's avatar
Raphaël Pinson committed
162
          is_expected.to contain_postfix__config('inet_interfaces').with_value('localhost2')
163
164
165
        end
      end
      context 'when enabling ldap' do
Ben Hocker's avatar
Ben Hocker committed
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
        context 'when on Debian' do
          let (:params) { {
            :ldap            => true,
            :ldap_base       => 'dc=example,dc=com',
            :ldap_host       => 'host.example.com',
          } }

          let (:facts) { {
            :operatingsystem => 'Debian',
            :osfamily        => 'Debian',
            :fqdn            => 'fqdn.example.com',
            :lsbdistcodename => 'wheezy',
          } }

          it { should contain_package('postfix-ldap') }

          it { should contain_file('/etc/postfix/ldap-aliases.cf').with(
            :owner => 'root',
            :group => 'postfix' ).with_content(/search_base = /)
          }
        end

        context 'when on RedHat' do
          let (:params) { {
            :ldap => true,
            :ldap_base => 'dc=example,dc=com',
            :ldap_host => 'host.example.com',
          } }

          let (:facts) { {
            :operatingsystem           => 'RedHat',
            :osfamily                  => 'RedHat',
            :operatingsystemmajrelease => '7',
            :lsbdistcodename           => 'Maipo',
            :fqdn                      => 'fqdn.example.com',
          } }

          it do
            expect {
              should_not contain_package('postfix-ldap')
            }
          end

          it { should contain_file('/etc/postfix/ldap-aliases.cf').with(
            :owner => 'root',
            :group => 'postfix').with_content(/search_base = /)
          }
213
214
215
216
217
218
219
        end
      end
      context 'when a custom mail_user is specified' do
        let (:params) { {
          :mail_user => 'bar'
        } }
        it 'should adjust the content of /etc/postfix/master.cf specifying the user' do
Raphaël Pinson's avatar
Raphaël Pinson committed
220
          is_expected.to contain_file('/etc/postfix/master.cf').without('seltype').with_content(/user=bar/)
221
222
223
224
225
226
227
        end
      end
      context 'when mailman is true' do
        let (:params) { {
          :mailman => true
        } }
        it 'should do stuff' do
228
          skip 'need to write this still'
229
230
231
232
233
234
235
        end
      end
      context 'when specifying a custom mastercf_source' do
        let (:params) { {
          :mastercf_source => 'testy'
        } }
        it 'should do stuff' do
236
          skip 'need to write this still'
237
238
239
240
241
242
243
        end
      end
      context 'when specifying a custom master_smtp' do
        let (:params) { {
          :master_smtp         => "smtp      inet  n       -       -       -       -       smtpd
    -o smtpd_client_restrictions=check_client_access,hash:/etc/postfix/access,reject",
        } }
Ben Hocker's avatar
Ben Hocker committed
244
        it 'should update master.cf with the specified flags to smtp' do
Raphaël Pinson's avatar
Raphaël Pinson committed
245
          is_expected.to contain_file('/etc/postfix/master.cf').without('seltype').with_content(
246
247
248
249
250
251
252
253
254
255
            /smtp      inet  n       -       -       -       -       smtpd/).with_content(
            /^smtp.*\n.*smtpd_client_restrictions=check_client_access,hash:/
          )
        end
      end
      context 'when specifying a custom master_smtps' do
        let (:params) { {
          :master_smtps        => 'smtps     inet  n       -       -       -       -       smtpd'
        } }
        it 'should update master.cf with the specified flags to smtps' do
Raphaël Pinson's avatar
Raphaël Pinson committed
256
          is_expected.to contain_file('/etc/postfix/master.cf').with_content(/^smtps     inet  n/)
257
258
259
260
261
        end
      end
      context 'when mta is enabled' do
        let (:params) { { :mta => true, :mydestination => '1.2.3.4', :relayhost => '2.3.4.5' } }
        it 'should configure postfix as a minimal MTA, delivering mail to the mydestination param' do
Raphaël Pinson's avatar
Raphaël Pinson committed
262
263
264
265
266
          is_expected.to contain_postfix__config('mydestination').with_value('1.2.3.4')
          is_expected.to contain_postfix__config('mynetworks').with_value('127.0.0.0/8')
          is_expected.to contain_postfix__config('relayhost').with_value('2.3.4.5')
          is_expected.to contain_postfix__config('virtual_alias_maps').with_value('hash:/etc/postfix/virtual')
          is_expected.to contain_postfix__config('transport_maps').with_value('hash:/etc/postfix/transport')
267
        end
Raphaël Pinson's avatar
Raphaël Pinson committed
268
        it { is_expected.to contain_class('postfix::mta') }
269
270
271
        context 'and satellite is also enabled' do
          let (:params) { { :mta => true, :satellite => true, :mydestination => '1.2.3.4', :relayhost => '2.3.4.5' } }
          it 'should fail' do
272
            expect { should compile }.to raise_error(/Please disable one/)
273
274
275
276
277
          end
        end
      end
      context 'when specifying mydesitination' do
        it 'should do stuff' do
278
          skip 'need to write this still'
279
280
281
282
        end
      end
      context 'when specifying mynetworks' do
        it 'should do stuff' do
283
          skip 'need to write this still'
284
285
286
287
288
        end
      end
      context 'when specifying myorigin' do
        let (:params) { { :myorigin => 'localhost'} }
        it 'should create a postfix::config defined type with myorigin specified properly' do
Raphaël Pinson's avatar
Raphaël Pinson committed
289
          is_expected.to contain_postfix__config('myorigin').with_value('localhost')
290
291
292
293
        end
      end
      context 'when specifying relayhost' do
        it 'should do stuff' do
294
          skip 'need to write this still'
295
296
297
298
299
        end
      end
      context 'when specifying a root_mail_recipient' do
        let (:params) { { :root_mail_recipient => 'foo'} }
        it 'should contain a Mailalias resource directing roots mail to the required user' do
Raphaël Pinson's avatar
Raphaël Pinson committed
300
          is_expected.to contain_mailalias('root').with_recipient('foo')
301
302
303
304
        end
      end
      context 'when specifying satellite' do
        let (:params) { { :satellite => true, :mydestination => '1.2.3.4', :relayhost => '2.3.4.5' } }
Mickaël Canévet's avatar
Mickaël Canévet committed
305
306
307
        let :pre_condition do
          "class { 'augeas': }"
        end
308
        it 'should configure all local email to be forwarded to $root_mail_recipient delivered through $relayhost' do
Raphaël Pinson's avatar
Raphaël Pinson committed
309
310
311
312
313
          is_expected.to contain_postfix__config('mydestination').with_value('1.2.3.4')
          is_expected.to contain_postfix__config('mynetworks').with_value('127.0.0.0/8')
          is_expected.to contain_postfix__config('relayhost').with_value('2.3.4.5')
          is_expected.to contain_postfix__config('virtual_alias_maps').with_value('hash:/etc/postfix/virtual')
          is_expected.to contain_postfix__config('transport_maps').with_value('hash:/etc/postfix/transport')
314
315
316
317
        end
        context 'and mta is also enabled' do
          let (:params) { { :mta => true, :satellite => true, :mydestination => '1.2.3.4', :relayhost => '2.3.4.5' } }
          it 'should fail' do
318
            expect { should compile }.to raise_error(/Please disable one/)
319
320
321
322
323
324
          end
        end
      end
      context 'when specifying smtp_listen' do
        let (:params) { { :smtp_listen => 'all' } }
        it 'should do stuff' do
325
          skip 'need to write this still'
326
327
328
329
330
        end
      end
      context 'when use_amavisd is true' do
        let (:params) { { :use_amavisd => true } }
        it 'should update master.cf with the specified flags to amavis' do
Raphaël Pinson's avatar
Raphaël Pinson committed
331
          is_expected.to contain_file('/etc/postfix/master.cf').with_content(/amavis unix/)
332
333
334
335
336
        end
      end
      context 'when use_dovecot_lda is true' do
        let (:params) { { :use_dovecot_lda => true } }
        it 'should update master.cf with the specified flags to dovecot' do
Raphaël Pinson's avatar
Raphaël Pinson committed
337
          is_expected.to contain_file('/etc/postfix/master.cf').with_content(/dovecot.*\n.* user=vmail:vmail /)
338
339
340
341
342
        end
      end
      context 'when use_schleuder is true' do
        let (:params) { { :use_schleuder => true } }
        it 'should update master.cf with the specified flags to schleuder' do
Raphaël Pinson's avatar
Raphaël Pinson committed
343
          is_expected.to contain_file('/etc/postfix/master.cf').with_content(/schleuder/)
344
345
346
347
348
        end
      end
      context 'when use_sympa is true' do
        let (:params) { { :use_sympa => true } }
        it 'should update master.cf to include sympa' do
Raphaël Pinson's avatar
Raphaël Pinson committed
349
          is_expected.to contain_file('/etc/postfix/master.cf').with_content(/sympa/)
350
351
352
        end
      end
    end
Raphaël Pinson's avatar
Raphaël Pinson committed
353
  end
354
end