Refactor facts to improve performance:
[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}-lts";/
65           ).with_content(
66             /"\${distro_id} \${distro_codename}-security";/
67           ).with_content(
68             /"\${distro_id} oldstable";/
69           )
70         }
71       end
72       context 'wheezy' do
73         let :facts do default_facts.merge({:lsbdistcodename => 'wheezy'}) end
74         it {
75           should contain_file(file_unattended).with_content(
76             /^Unattended-Upgrade::Origins-Pattern/
77           ).with_content(
78             /"origin=Debian,archive=stable,label=Debian-Security";/
79           )
80         }
81       end
82     end
83
84     describe 'on Ubuntu' do
85       default_facts = { :lsbdistid => 'Ubuntu' }
86       context 'default' do
87         let :facts do default_facts end
88         it {
89           should contain_file(file_unattended).with_content(
90             /^Unattended-Upgrade::Allowed-Origins/
91           ).with_content(
92             /"\${distro_id}\:\${distro_codename}-security";/
93           )
94         }
95       end
96       context 'lucid' do
97         let :facts do default_facts.merge({:lsbdistcodename => 'lucid'}) end
98         it {
99           should contain_file(file_unattended).with_content(
100             /^Unattended-Upgrade::Allowed-Origins/
101           ).with_content(
102             /"\${distro_id} \${distro_codename}-security";/
103           )
104         }
105       end
106       context 'precise' do
107         let :facts do default_facts.merge({:lsbdistcodename => 'precise'}) end
108         it {
109           should contain_file(file_unattended).with_content(
110             /^Unattended-Upgrade::Allowed-Origins/
111           ).with_content(
112             /"\${distro_id}\:\${distro_codename}-security";/
113           )
114         }
115       end
116       context 'trusty' do
117         let :facts do default_facts.merge({:lsbdistcodename => 'trusty'}) end
118         it {
119           should contain_file(file_unattended).with_content(
120             /^Unattended-Upgrade::Allowed-Origins/
121           ).with_content(
122             /"\${distro_id}\:\${distro_codename}-security";/
123           )
124         }
125       end
126     end
127   end
128
129   describe "blacklist" do
130     describe "with param defaults" do
131       let(:params) {{ }}
132       it { should contain_file(file_unattended).with_content(/^Unattended-Upgrade::Package-Blacklist \{\n\};$/) }
133     end
134
135     describe "with blacklist => []" do
136       let :params do
137         { :blacklist => ['libc6', 'libc6-dev'] }
138       end
139       it { should contain_file(file_unattended).with_content(/^Unattended-Upgrade::Package-Blacklist \{\n\t"libc6";\n\t"libc6-dev";\n\};$/) }
140     end
141   end
142
143   describe "with update => 2" do
144     let :params do
145       { :update => "2" }
146     end
147     it { should contain_file(file_periodic).with_content(/^APT::Periodic::Update-Package-Lists "2";$/) }
148   end
149
150   describe "with download => 2" do
151     let :params do
152       { :download => "2" }
153     end
154     it { should contain_file(file_periodic).with_content(/^APT::Periodic::Download-Upgradeable-Packages "2";$/) }
155   end
156
157   describe "with upgrade => 2" do
158     let :params do
159       { :upgrade => "2" }
160     end
161     it { should contain_file(file_periodic).with_content(/^APT::Periodic::Unattended-Upgrade "2";$/) }
162   end
163
164   describe "with autoclean => 2" do
165     let :params do
166       { :autoclean => "2" }
167     end
168     it { should contain_file(file_periodic).with_content(/^APT::Periodic::AutocleanInterval "2";$/) }
169   end
170
171   describe "with auto_fix => false" do
172     let :params do
173       { :auto_fix => false }
174     end
175     it { should contain_file(file_unattended).with_content(/^Unattended-Upgrade::AutoFixInterruptedDpkg "false";$/) }
176   end
177
178   describe "with minimal_steps => true" do
179     let :params do
180       { :minimal_steps => true }
181     end
182     it { should contain_file(file_unattended).with_content(/^Unattended-Upgrade::MinimalSteps "true";$/) }
183   end
184
185   describe "with install_on_shutdown => true" do
186     let :params do
187       { :install_on_shutdown => true }
188     end
189     it { should contain_file(file_unattended).with_content(/^Unattended-Upgrade::InstallOnShutdown "true";$/) }
190   end
191
192   describe "mail_to" do
193     describe "param defaults" do
194       let(:params) {{ }}
195       it { should_not contain_file(file_unattended).with_content(/^Unattended-Upgrade::Mail /) }
196       it { should_not contain_file(file_unattended).with_content(/^Unattended-Upgrade::MailOnlyOnError /) }
197     end
198
199     describe "with mail_to => user@website, mail_only_on_error => true" do
200       let :params do
201         { :mail_to => "user@website",
202           :mail_only_on_error => true }
203       end
204       it { should contain_file(file_unattended).with_content(/^Unattended-Upgrade::Mail "user@website";$/) }
205       it { should contain_file(file_unattended).with_content(/^Unattended-Upgrade::MailOnlyOnError "true";$/) }
206     end
207   end
208
209   describe "with remove_unused => false" do
210     let :params do
211       { :remove_unused => false }
212     end
213     it { should contain_file(file_unattended).with_content(/^Unattended-Upgrade::Remove-Unused-Dependencies "false";$/) }
214   end
215
216   describe "with auto_reboot => true" do
217     let :params do
218       { :auto_reboot => true }
219     end
220     it { should contain_file(file_unattended).with_content(/^Unattended-Upgrade::Automatic-Reboot "true";$/) }
221   end
222
223   describe "dl_limit" do
224     describe "param defaults" do
225       let(:params) {{ }}
226       it { should_not contain_file(file_unattended).with_content(/^Acquire::http::Dl-Limit /) }
227     end
228
229     describe "with dl_limit => 70" do
230       let :params do
231         { :dl_limit => "70" }
232       end
233       it { should contain_file(file_unattended).with_content(/^Acquire::http::Dl-Limit "70";$/) }
234     end
235   end
236
237   describe "with enable => 0" do
238     let :params do
239       { :enable => "0" }
240     end
241     it { should contain_file(file_periodic).with_content(/^APT::Periodic::Enable "0";$/) }
242   end
243
244   describe "with backup_interval => 1" do
245     let :params do
246       { :backup_interval => "1" }
247     end
248     it { should contain_file(file_periodic).with_content(/^APT::Periodic::BackUpArchiveInterval "1";$/) }
249   end
250
251   describe "with backup_level => 0" do
252     let :params do
253       { :backup_level => "0" }
254     end
255     it { should contain_file(file_periodic).with_content(/^APT::Periodic::BackUpLevel "0";$/) }
256   end
257
258   describe "with max_age => 1" do
259     let :params do
260       { :max_age => "1" }
261     end
262     it { should contain_file(file_periodic).with_content(/^APT::Periodic::MaxAge "1";$/) }
263   end
264
265   describe "with min_age => 1" do
266     let :params do
267       { :min_age => "1" }
268     end
269     it { should contain_file(file_periodic).with_content(/^APT::Periodic::MinAge "1";$/) }
270   end
271
272   describe "with max_size => 1" do
273     let :params do
274       { :max_size => "1" }
275     end
276     it { should contain_file(file_periodic).with_content(/^APT::Periodic::MaxSize "1";$/) }
277   end
278
279   describe "with download_delta => 2" do
280     let :params do
281       { :download_delta => "2" }
282     end
283     it { should contain_file(file_periodic).with_content(/^APT::Periodic::Download-Upgradeable-Packages-Debdelta "2";$/) }
284   end
285
286   describe "with verbose => 2" do
287     let :params do
288       { :verbose => "2" }
289     end
290     it { should contain_file(file_periodic).with_content(/^APT::Periodic::Verbose "2";$/) }
291   end
292
293 end