Get rid of unattended upgrades and cleanup unused templates
[puppet-modules/puppetlabs-apt.git] / spec / classes / apt_spec.rb
1 require 'spec_helper'
2 describe 'apt', :type => :class do
3   let(:facts) { { :lsbdistid => 'Debian', :osfamily => 'Debian' } }
4
5   context 'defaults' do
6     it { should contain_file('sources.list').that_notifies('Exec[apt_update]').only_with({
7       'ensure' => 'present',
8       'path'   => '/etc/apt/sources.list',
9       'owner'  => 'root',
10       'group'  => 'root',
11       'mode'   => '0644',
12       'notify' => 'Exec[apt_update]',
13     })}
14
15     it { should contain_file('sources.list.d').that_notifies('Exec[apt_update]').only_with({
16       'ensure'  => 'directory',
17       'path'    => '/etc/apt/sources.list.d',
18       'owner'   => 'root',
19       'group'   => 'root',
20       'purge'   => false,
21       'recurse' => false,
22       'notify'  => 'Exec[apt_update]',
23     })}
24
25     it { should contain_file('preferences.d').only_with({
26       'ensure'  => 'directory',
27       'path'    => '/etc/apt/preferences.d',
28       'owner'   => 'root',
29       'group'   => 'root',
30       'purge'   => false,
31       'recurse' => false,
32     })}
33
34     it 'should lay down /etc/apt/apt.conf.d/15update-stamp' do
35       should contain_file('/etc/apt/apt.conf.d/15update-stamp').with({
36         'group' => 'root',
37         'mode'  => '0644',
38         'owner' => 'root',
39       }).with_content(/APT::Update::Post-Invoke-Success \{"touch \/var\/lib\/apt\/periodic\/update-success-stamp 2>\/dev\/null \|\| true";\};/)
40     end
41
42     it { should contain_exec('apt_update').with({
43       'refreshonly' => 'true',
44     })}
45   end
46
47   context 'lots of non-defaults' do
48     let :params do
49       {
50         :always_apt_update    => true,
51         :purge_sources_list   => true,
52         :purge_sources_list_d => true,
53         :purge_preferences    => true,
54         :purge_preferences_d  => true,
55         :update_timeout       => '1',
56         :update_tries         => '3',
57       }
58     end
59
60     it { should contain_file('sources.list').with({
61       'content' => "# Repos managed by puppet.\n"
62     })}
63
64     it { should contain_file('sources.list.d').with({
65       'purge'   => 'true',
66       'recurse' => 'true',
67     })}
68
69     it { should contain_file('apt-preferences').only_with({
70       'ensure' => 'absent',
71       'path'   => '/etc/apt/preferences',
72     })}
73
74     it { should contain_file('preferences.d').with({
75       'purge'   => 'true',
76       'recurse' => 'true',
77     })}
78
79     it { should contain_exec('apt_update').with({
80       'refreshonly' => 'false',
81       'timeout'     => '1',
82       'tries'       => '3',
83     })}
84
85   end
86
87   context 'with sources defined on valid osfamily' do
88     let :facts do
89       { :osfamily        => 'Debian',
90         :lsbdistcodename => 'precise',
91         :lsbdistid       => 'Debian',
92       }
93     end
94     let(:params) { { :sources => {
95       'debian_unstable' => {
96         'location'          => 'http://debian.mirror.iweb.ca/debian/',
97         'release'           => 'unstable',
98         'repos'             => 'main contrib non-free',
99         'required_packages' => 'debian-keyring debian-archive-keyring',
100         'key'               => '55BE302B',
101         'key_server'        => 'subkeys.pgp.net',
102         'pin'               => '-10',
103         'include_src'       => true
104       },
105       'puppetlabs' => {
106         'location'   => 'http://apt.puppetlabs.com',
107         'repos'      => 'main',
108         'key'        => '4BD6EC30',
109         'key_server' => 'pgp.mit.edu',
110       }
111     } } }
112
113     it {
114       should contain_file('debian_unstable.list').with({
115         'ensure'  => 'present',
116         'path'    => '/etc/apt/sources.list.d/debian_unstable.list',
117         'owner'   => 'root',
118         'group'   => 'root',
119         'mode'    => '0644',
120         'notify'  => 'Exec[apt_update]',
121       })
122     }
123
124     it { should contain_file('debian_unstable.list').with_content(/^deb http:\/\/debian.mirror.iweb.ca\/debian\/ unstable main contrib non-free$/) }
125     it { should contain_file('debian_unstable.list').with_content(/^deb-src http:\/\/debian.mirror.iweb.ca\/debian\/ unstable main contrib non-free$/) }
126
127     it {
128       should contain_file('puppetlabs.list').with({
129         'ensure'  => 'present',
130         'path'    => '/etc/apt/sources.list.d/puppetlabs.list',
131         'owner'   => 'root',
132         'group'   => 'root',
133         'mode'    => '0644',
134         'notify'  => 'Exec[apt_update]',
135       })
136     }
137
138     it { should contain_file('puppetlabs.list').with_content(/^deb http:\/\/apt.puppetlabs.com precise main$/) }
139     it { should contain_file('puppetlabs.list').with_content(/^deb-src http:\/\/apt.puppetlabs.com precise main$/) }
140   end
141
142   describe 'failing tests' do
143     context 'bad purge_sources_list' do
144       let :params do
145         {
146           'purge_sources_list' => 'foo'
147         }
148       end
149       it do
150         expect {
151           should compile
152         }.to raise_error(Puppet::Error)
153       end
154     end
155
156     context 'bad purge_sources_list_d' do
157       let :params do
158         {
159           'purge_sources_list_d' => 'foo'
160         }
161       end
162       it do
163         expect {
164           should compile
165         }.to raise_error(Puppet::Error)
166       end
167     end
168
169     context 'bad purge_preferences' do
170       let :params do
171         {
172           'purge_preferences' => 'foo'
173         }
174       end
175       it do
176         expect {
177           should compile
178         }.to raise_error(Puppet::Error)
179       end
180     end
181
182     context 'bad purge_preferences_d' do
183       let :params do
184         {
185           'purge_preferences_d' => 'foo'
186         }
187       end
188       it do
189         expect {
190           should compile
191         }.to raise_error(Puppet::Error)
192       end
193     end
194
195     context 'with unsupported osfamily' do
196       let :facts do
197         { :osfamily => 'Darwin', }
198       end
199
200       it do
201         expect {
202           should compile
203         }.to raise_error(Puppet::Error, /This module only works on Debian or derivatives like Ubuntu/)
204       end
205     end
206   end
207 end