Merge pull request #502 from mhaskel/merge_2.0.x_to_master
[puppet-modules/puppetlabs-apt.git] / spec / classes / apt_spec.rb
1 require 'spec_helper'
2 describe 'apt' do
3   let(:facts) { { :lsbdistid => 'Debian', :osfamily => 'Debian', :lsbdistcodename => 'wheezy'} }
4
5   context 'defaults' do
6     it { is_expected.to contain_file('sources.list').that_notifies('Exec[apt_update]').only_with({
7       :ensure  => 'file',
8       :path    => '/etc/apt/sources.list',
9       :owner   => 'root',
10       :group   => 'root',
11       :mode    => '0644',
12       :notify  => 'Exec[apt_update]',
13     })}
14
15     it { is_expected.to 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       :mode    => '0644',
21       :purge   => false,
22       :recurse => false,
23       :notify  => 'Exec[apt_update]',
24     })}
25
26     it { is_expected.to contain_file('preferences').that_notifies('Exec[apt_update]').only_with({
27       :ensure  => 'file',
28       :path    => '/etc/apt/preferences',
29       :owner   => 'root',
30       :group   => 'root',
31       :mode    => '0644',
32       :notify  => 'Exec[apt_update]',
33     })}
34
35     it { is_expected.to contain_file('preferences.d').that_notifies('Exec[apt_update]').only_with({
36       :ensure  => 'directory',
37       :path    => '/etc/apt/preferences.d',
38       :owner   => 'root',
39       :group   => 'root',
40       :mode    => '0644',
41       :purge   => false,
42       :recurse => false,
43       :notify  => 'Exec[apt_update]',
44     })}
45
46     it 'should lay down /etc/apt/apt.conf.d/15update-stamp' do
47       is_expected.to contain_file('/etc/apt/apt.conf.d/15update-stamp').with({
48         :group => 'root',
49         :mode  => '0644',
50         :owner => 'root',
51       }).with_content(/APT::Update::Post-Invoke-Success \{"touch \/var\/lib\/apt\/periodic\/update-success-stamp 2>\/dev\/null \|\| true";\};/)
52     end
53
54     it { is_expected.to contain_exec('apt_update').with({
55       :refreshonly => 'true',
56     })}
57
58     it { is_expected.not_to contain_apt__setting('conf-proxy') }
59   end
60
61   describe 'proxy=' do
62     context 'host=localhost' do
63       let(:params) { { :proxy => { 'host' => 'localhost'} } }
64       it { is_expected.to contain_apt__setting('conf-proxy').with({
65         :priority => '01',
66       }).with_content(
67         /Acquire::http::proxy "http:\/\/localhost:8080\/";/
68       ).without_content(
69         /Acquire::https::proxy/
70       )}
71     end
72
73     context 'host=localhost and port=8180' do
74       let(:params) { { :proxy => { 'host' => 'localhost', 'port' => 8180} } }
75       it { is_expected.to contain_apt__setting('conf-proxy').with({
76         :priority => '01',
77       }).with_content(
78         /Acquire::http::proxy "http:\/\/localhost:8180\/";/
79       ).without_content(
80         /Acquire::https::proxy/
81       )}
82     end
83
84     context 'host=localhost and https=true' do
85       let(:params) { { :proxy => { 'host' => 'localhost', 'https' => true} } }
86       it { is_expected.to contain_apt__setting('conf-proxy').with({
87         :priority => '01',
88       }).with_content(
89         /Acquire::http::proxy "http:\/\/localhost:8080\/";/
90       ).with_content(
91         /Acquire::https::proxy "https:\/\/localhost:8080\/";/
92       )}
93     end
94   end
95   context 'lots of non-defaults' do
96     let :params do
97       {
98         :update => { 'frequency' => 'always', 'timeout' => 1, 'tries' => 3 },
99         :purge  => { 'sources.list' => false, 'sources.list.d' => false,
100                      'preferences' => false, 'preferences.d' => false, },
101       }
102     end
103
104     it { is_expected.to contain_file('sources.list').without({
105       :content => "# Repos managed by puppet.\n",
106     })}
107
108     it { is_expected.to contain_file('sources.list.d').with({
109       :purge   => false,
110       :recurse => false,
111     })}
112
113     it { is_expected.to contain_file('preferences').with({
114       :ensure => 'file',
115     })}
116
117     it { is_expected.to contain_file('preferences.d').with({
118       :purge   => false,
119       :recurse => false,
120     })}
121
122     it { is_expected.to contain_exec('apt_update').with({
123       :refreshonly => false,
124       :timeout     => 1,
125       :tries       => 3,
126     })}
127
128   end
129
130   context 'with sources defined on valid osfamily' do
131     let :facts do
132       { :osfamily        => 'Debian',
133         :lsbdistcodename => 'precise',
134         :lsbdistid       => 'Debian',
135       }
136     end
137     let(:params) { { :sources => {
138       'debian_unstable' => {
139         'location'          => 'http://debian.mirror.iweb.ca/debian/',
140         'release'           => 'unstable',
141         'repos'             => 'main contrib non-free',
142         'key'               => { 'id' => '150C8614919D8446E01E83AF9AA38DCD55BE302B', 'server' => 'subkeys.pgp.net' },
143         'pin'               => '-10',
144         'include'           => {'src' => true,},
145       },
146       'puppetlabs' => {
147         'location'   => 'http://apt.puppetlabs.com',
148         'repos'      => 'main',
149         'key'        => { 'id' => '47B320EB4C7C375AA9DAE1A01054B7A24BD6EC30', 'server' => 'pgp.mit.edu' },
150       }
151     } } }
152
153     it {
154       is_expected.to contain_apt__setting('list-debian_unstable').with({
155         :ensure => 'present',
156       })
157     }
158
159     it { is_expected.to contain_file('/etc/apt/sources.list.d/debian_unstable.list').with_content(/^deb http:\/\/debian.mirror.iweb.ca\/debian\/ unstable main contrib non-free$/) }
160     it { is_expected.to contain_file('/etc/apt/sources.list.d/debian_unstable.list').with_content(/^deb-src http:\/\/debian.mirror.iweb.ca\/debian\/ unstable main contrib non-free$/) }
161
162     it {
163       is_expected.to contain_apt__setting('list-puppetlabs').with({
164         :ensure => 'present',
165       })
166     }
167
168     it { is_expected.to contain_file('/etc/apt/sources.list.d/puppetlabs.list').with_content(/^deb http:\/\/apt.puppetlabs.com precise main$/) }
169   end
170
171   context 'with keys defined on valid osfamily' do
172     let :facts do
173       { :osfamily        => 'Debian',
174         :lsbdistcodename => 'precise',
175         :lsbdistid       => 'Debian',
176       }
177     end
178     let(:params) { { :keys => {
179       '55BE302B' => {
180         'server' => 'subkeys.pgp.net',
181       },
182       '4BD6EC30' => {
183         'server' => 'pgp.mit.edu',
184       }
185     } } }
186
187     it { is_expected.to contain_apt__key('55BE302B').with({
188         :server => 'subkeys.pgp.net',
189     })}
190
191     it { is_expected.to contain_apt__key('4BD6EC30').with({
192         :server => 'pgp.mit.edu',
193     })}
194   end
195
196   context 'with ppas defined on valid osfamily' do
197     let :facts do
198       { :osfamily        => 'Debian',
199         :lsbdistcodename => 'precise',
200         :lsbdistid       => 'ubuntu',
201       }
202     end
203     let(:params) { { :ppas => {
204       'ppa:drizzle-developers/ppa' => {},
205       'ppa:nginx/stable' => {},
206     } } }
207
208     it { is_expected.to contain_apt__ppa('ppa:drizzle-developers/ppa')}
209     it { is_expected.to contain_apt__ppa('ppa:nginx/stable')}
210   end
211
212   context 'with settings defined on valid osfamily' do
213     let :facts do
214       { :osfamily        => 'Debian',
215         :lsbdistcodename => 'precise',
216         :lsbdistid       => 'Debian',
217       }
218     end
219     let(:params) { { :settings => {
220       'conf-banana' => { 'content' => 'banana' },
221       'pref-banana' => { 'content' => 'banana' },
222     } } }
223
224     it { is_expected.to contain_apt__setting('conf-banana')}
225     it { is_expected.to contain_apt__setting('pref-banana')}
226   end
227
228   describe 'failing tests' do
229     context "purge['sources.list']=>'banana'" do
230       let(:params) { { :purge => { 'sources.list' => 'banana' }, } }
231       it do
232         expect {
233           is_expected.to compile
234         }.to raise_error(Puppet::Error)
235       end
236     end
237
238     context "purge['sources.list.d']=>'banana'" do
239       let(:params) { { :purge => { 'sources.list.d' => 'banana' }, } }
240       it do
241         expect {
242           is_expected.to compile
243         }.to raise_error(Puppet::Error)
244       end
245     end
246
247     context "purge['preferences']=>'banana'" do
248       let(:params) { { :purge => { 'preferences' => 'banana' }, } }
249       it do
250         expect {
251           is_expected.to compile
252         }.to raise_error(Puppet::Error)
253       end
254     end
255
256     context "purge['preferences.d']=>'banana'" do
257       let(:params) { { :purge => { 'preferences.d' => 'banana' }, } }
258       it do
259         expect {
260           is_expected.to compile
261         }.to raise_error(Puppet::Error)
262       end
263     end
264
265     context 'with unsupported osfamily' do
266       let :facts do
267         { :osfamily => 'Darwin', }
268       end
269
270       it do
271         expect {
272           is_expected.to compile
273         }.to raise_error(Puppet::Error, /This module only works on Debian or derivatives like Ubuntu/)
274       end
275     end
276   end
277 end