Merge pull request #319 from cloudbuy/master
[puppet-modules/puppetlabs-apt.git] / spec / classes / unattended_upgrades_spec.rb
1 require 'spec_helper'
2 describe 'apt::unattended_upgrades', :type => :class do
3   let(:file_unattended) { '/etc/apt/apt.conf.d/50unattended-upgrades' }
4   let(:file_periodic) { '/etc/apt/apt.conf.d/10periodic' }
5   let(:facts) { { :lsbdistid => 'Debian' } }
6
7   it { should contain_package("unattended-upgrades") }
8
9   it {
10     should create_file("/etc/apt/apt.conf.d/50unattended-upgrades").with({
11       "owner"   => "root",
12       "group"   => "root",
13       "mode"    => "0644",
14       "require" => "Package[unattended-upgrades]",
15     })
16   }
17
18   it {
19     should create_file("/etc/apt/apt.conf.d/10periodic").with({
20       "owner"   => "root",
21       "group"   => "root",
22       "mode"    => "0644",
23       "require" => "Package[unattended-upgrades]",
24     })
25   }
26
27   describe "origins" do
28     describe 'on Debian' do
29       default_facts = { :lsbdistid => 'Debian' }
30       context 'defaults' do
31         let :facts do default_facts end
32         it {
33           should contain_file(file_unattended).with_content(
34             /^Unattended-Upgrade::Origins-Pattern/
35           ).with_content(
36             /"origin=Debian,archive=stable,label=Debian-Security";/
37           )
38         }
39       end
40       context 'defaults with custom origin' do
41         let :facts do default_facts end
42         let :params do { :origins => ['bananana']} end
43         it {
44           should contain_file(file_unattended).with_content(
45             /^Unattended-Upgrade::Origins-Pattern/
46           ).with_content(
47             /"bananana";/
48           )
49         }
50       end
51       context 'defaults with invalid origin' do
52         let :facts do default_facts end
53         let :params do { :origins => 'bananana'} end
54         it {
55           expect {subject}.to raise_error(/is not an Array/)
56         }
57       end
58       context 'squeeze' do
59         let :facts do default_facts.merge({:lsbdistcodename => 'squeeze'}) end
60         it {
61           should contain_file(file_unattended).with_content(
62             /^Unattended-Upgrade::Allowed-Origins/
63           ).with_content(
64             /"\${distro_id} \${distro_codename}-security";/
65           ).with_content(
66             /"\${distro_id} oldstable";/
67           )
68         }
69       end
70       context 'wheezy' do
71         let :facts do default_facts.merge({:lsbdistcodename => 'wheezy'}) end
72         it {
73           should contain_file(file_unattended).with_content(
74             /^Unattended-Upgrade::Origins-Pattern/
75           ).with_content(
76             /"origin=Debian,archive=stable,label=Debian-Security";/
77           )
78         }
79       end
80     end
81
82     describe 'on Ubuntu' do
83       default_facts = { :lsbdistid => 'Ubuntu' }
84       context 'default' do
85         let :facts do default_facts end
86         it {
87           should contain_file(file_unattended).with_content(
88             /^Unattended-Upgrade::Allowed-Origins/
89           ).with_content(
90             /"\${distro_id}\:\${distro_codename}-security";/
91           )
92         }
93       end
94       context 'lucid' do
95         let :facts do default_facts.merge({:lsbdistcodename => 'lucid'}) end
96         it {
97           should contain_file(file_unattended).with_content(
98             /^Unattended-Upgrade::Allowed-Origins/
99           ).with_content(
100             /"\${distro_id} \${distro_codename}-security";/
101           )
102         }
103       end
104       context 'precise' do
105         let :facts do default_facts.merge({:lsbdistcodename => 'precise'}) end
106         it {
107           should contain_file(file_unattended).with_content(
108             /^Unattended-Upgrade::Allowed-Origins/
109           ).with_content(
110             /"\${distro_id}\:\${distro_codename}-security";/
111           )
112         }
113       end
114       context 'trusty' do
115         let :facts do default_facts.merge({:lsbdistcodename => 'trusty'}) end
116         it {
117           should contain_file(file_unattended).with_content(
118             /^Unattended-Upgrade::Allowed-Origins/
119           ).with_content(
120             /"\${distro_id}\:\${distro_codename}-security";/
121           )
122         }
123       end
124     end
125   end
126
127   describe "blacklist" do
128     describe "with param defaults" do
129       let(:params) {{ }}
130       it { should contain_file(file_unattended).with_content(/^Unattended-Upgrade::Package-Blacklist \{\n\};$/) }
131     end
132
133     describe "with blacklist => []" do
134       let :params do
135         { :blacklist => ['libc6', 'libc6-dev'] }
136       end
137       it { should contain_file(file_unattended).with_content(/^Unattended-Upgrade::Package-Blacklist \{\n\t"libc6";\n\t"libc6-dev";\n\};$/) }
138     end
139   end
140
141   describe "with update => 2" do
142     let :params do
143       { :update => "2" }
144     end
145     it { should contain_file(file_periodic).with_content(/^APT::Periodic::Update-Package-Lists "2";$/) }
146   end
147
148   describe "with download => 2" do
149     let :params do
150       { :download => "2" }
151     end
152     it { should contain_file(file_periodic).with_content(/^APT::Periodic::Download-Upgradeable-Packages "2";$/) }
153   end
154
155   describe "with upgrade => 2" do
156     let :params do
157       { :upgrade => "2" }
158     end
159     it { should contain_file(file_periodic).with_content(/^APT::Periodic::Unattended-Upgrade "2";$/) }
160   end
161
162   describe "with autoclean => 2" do
163     let :params do
164       { :autoclean => "2" }
165     end
166     it { should contain_file(file_periodic).with_content(/^APT::Periodic::AutocleanInterval "2";$/) }
167   end
168
169   describe "with auto_fix => false" do
170     let :params do
171       { :auto_fix => false }
172     end
173     it { should contain_file(file_unattended).with_content(/^Unattended-Upgrade::AutoFixInterruptedDpkg "false";$/) }
174   end
175
176   describe "with minimal_steps => true" do
177     let :params do
178       { :minimal_steps => true }
179     end
180     it { should contain_file(file_unattended).with_content(/^Unattended-Upgrade::MinimalSteps "true";$/) }
181   end
182
183   describe "with install_on_shutdown => true" do
184     let :params do
185       { :install_on_shutdown => true }
186     end
187     it { should contain_file(file_unattended).with_content(/^Unattended-Upgrade::InstallOnShutdown "true";$/) }
188   end
189
190   describe "mail_to" do
191     describe "param defaults" do
192       let(:params) {{ }}
193       it { should_not contain_file(file_unattended).with_content(/^Unattended-Upgrade::Mail /) }
194       it { should_not contain_file(file_unattended).with_content(/^Unattended-Upgrade::MailOnlyOnError /) }
195     end
196
197     describe "with mail_to => user@website, mail_only_on_error => true" do
198       let :params do
199         { :mail_to => "user@website",
200           :mail_only_on_error => true }
201       end
202       it { should contain_file(file_unattended).with_content(/^Unattended-Upgrade::Mail "user@website";$/) }
203       it { should contain_file(file_unattended).with_content(/^Unattended-Upgrade::MailOnlyOnError "true";$/) }
204     end
205   end
206
207   describe "with remove_unused => false" do
208     let :params do
209       { :remove_unused => false }
210     end
211     it { should contain_file(file_unattended).with_content(/^Unattended-Upgrade::Remove-Unused-Dependencies "false";$/) }
212   end
213
214   describe "with auto_reboot => true" do
215     let :params do
216       { :auto_reboot => true }
217     end
218     it { should contain_file(file_unattended).with_content(/^Unattended-Upgrade::Automatic-Reboot "true";$/) }
219   end
220
221   describe "dl_limit" do
222     describe "param defaults" do
223       let(:params) {{ }}
224       it { should_not contain_file(file_unattended).with_content(/^Acquire::http::Dl-Limit /) }
225     end
226
227     describe "with dl_limit => 70" do
228       let :params do
229         { :dl_limit => "70" }
230       end
231       it { should contain_file(file_unattended).with_content(/^Acquire::http::Dl-Limit "70";$/) }
232     end
233   end
234
235   describe "with enable => 0" do
236     let :params do
237       { :enable => "0" }
238     end
239     it { should contain_file(file_periodic).with_content(/^APT::Periodic::Enable "0";$/) }
240   end
241
242   describe "with backup_interval => 1" do
243     let :params do
244       { :backup_interval => "1" }
245     end
246     it { should contain_file(file_periodic).with_content(/^APT::Periodic::BackUpArchiveInterval "1";$/) }
247   end
248
249   describe "with backup_level => 0" do
250     let :params do
251       { :backup_level => "0" }
252     end
253     it { should contain_file(file_periodic).with_content(/^APT::Periodic::BackUpLevel "0";$/) }
254   end
255
256   describe "with max_age => 1" do
257     let :params do
258       { :max_age => "1" }
259     end
260     it { should contain_file(file_periodic).with_content(/^APT::Periodic::MaxAge "1";$/) }
261   end
262
263   describe "with min_age => 1" do
264     let :params do
265       { :min_age => "1" }
266     end
267     it { should contain_file(file_periodic).with_content(/^APT::Periodic::MinAge "1";$/) }
268   end
269
270   describe "with max_size => 1" do
271     let :params do
272       { :max_size => "1" }
273     end
274     it { should contain_file(file_periodic).with_content(/^APT::Periodic::MaxSize "1";$/) }
275   end
276
277   describe "with download_delta => 2" do
278     let :params do
279       { :download_delta => "2" }
280     end
281     it { should contain_file(file_periodic).with_content(/^APT::Periodic::Download-Upgradeable-Packages-Debdelta "2";$/) }
282   end
283
284   describe "with verbose => 2" do
285     let :params do
286       { :verbose => "2" }
287     end
288     it { should contain_file(file_periodic).with_content(/^APT::Periodic::Verbose "2";$/) }
289   end
290
291 end