Don't stub puppetversion
[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', :puppetversion   => Puppet.version} }
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').with({
105       :content => nil,
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         :puppetversion   => Puppet.version,
136       }
137     end
138     let(:params) { { :sources => {
139       'debian_unstable' => {
140         'location'          => 'http://debian.mirror.iweb.ca/debian/',
141         'release'           => 'unstable',
142         'repos'             => 'main contrib non-free',
143         'key'               => { 'id' => '150C8614919D8446E01E83AF9AA38DCD55BE302B', 'server' => 'subkeys.pgp.net' },
144         'pin'               => '-10',
145         'include'           => {'src' => true,},
146       },
147       'puppetlabs' => {
148         'location'   => 'http://apt.puppetlabs.com',
149         'repos'      => 'main',
150         'key'        => { 'id' => '47B320EB4C7C375AA9DAE1A01054B7A24BD6EC30', 'server' => 'pgp.mit.edu' },
151       }
152     } } }
153
154     it {
155       is_expected.to contain_apt__setting('list-debian_unstable').with({
156         :ensure => 'present',
157       })
158     }
159
160     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$/) }
161     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$/) }
162
163     it {
164       is_expected.to contain_apt__setting('list-puppetlabs').with({
165         :ensure => 'present',
166       })
167     }
168
169     it { is_expected.to contain_file('/etc/apt/sources.list.d/puppetlabs.list').with_content(/^deb http:\/\/apt.puppetlabs.com precise main$/) }
170   end
171
172   context 'with keys defined on valid osfamily' do
173     let :facts do
174       { :osfamily        => 'Debian',
175         :lsbdistcodename => 'precise',
176         :lsbdistid       => 'Debian',
177         :puppetversion   => Puppet.version,
178       }
179     end
180     let(:params) { { :keys => {
181       '55BE302B' => {
182         'server' => 'subkeys.pgp.net',
183       },
184       '4BD6EC30' => {
185         'server' => 'pgp.mit.edu',
186       }
187     } } }
188
189     it { is_expected.to contain_apt__key('55BE302B').with({
190         :server => 'subkeys.pgp.net',
191     })}
192
193     it { is_expected.to contain_apt__key('4BD6EC30').with({
194         :server => 'pgp.mit.edu',
195     })}
196   end
197
198   context 'with ppas defined on valid osfamily' do
199     let :facts do
200       { :osfamily        => 'Debian',
201         :lsbdistcodename => 'precise',
202         :lsbdistid       => 'ubuntu',
203         :puppetversion   => Puppet.version,
204       }
205     end
206     let(:params) { { :ppas => {
207       'ppa:drizzle-developers/ppa' => {},
208       'ppa:nginx/stable' => {},
209     } } }
210
211     it { is_expected.to contain_apt__ppa('ppa:drizzle-developers/ppa')}
212     it { is_expected.to contain_apt__ppa('ppa:nginx/stable')}
213   end
214
215   context 'with settings defined on valid osfamily' do
216     let :facts do
217       { :osfamily        => 'Debian',
218         :lsbdistcodename => 'precise',
219         :lsbdistid       => 'Debian',
220         :puppetversion   => Puppet.version,
221       }
222     end
223     let(:params) { { :settings => {
224       'conf-banana' => { 'content' => 'banana' },
225       'pref-banana' => { 'content' => 'banana' },
226     } } }
227
228     it { is_expected.to contain_apt__setting('conf-banana')}
229     it { is_expected.to contain_apt__setting('pref-banana')}
230   end
231
232   describe 'failing tests' do
233     context "purge['sources.list']=>'banana'" do
234       let(:params) { { :purge => { 'sources.list' => 'banana' }, } }
235       it do
236         expect {
237           subject.call
238         }.to raise_error(Puppet::Error)
239       end
240     end
241
242     context "purge['sources.list.d']=>'banana'" do
243       let(:params) { { :purge => { 'sources.list.d' => 'banana' }, } }
244       it do
245         expect {
246           subject.call
247         }.to raise_error(Puppet::Error)
248       end
249     end
250
251     context "purge['preferences']=>'banana'" do
252       let(:params) { { :purge => { 'preferences' => 'banana' }, } }
253       it do
254         expect {
255           subject.call
256         }.to raise_error(Puppet::Error)
257       end
258     end
259
260     context "purge['preferences.d']=>'banana'" do
261       let(:params) { { :purge => { 'preferences.d' => 'banana' }, } }
262       it do
263         expect {
264           subject.call
265         }.to raise_error(Puppet::Error)
266       end
267     end
268
269     context 'with unsupported osfamily' do
270       let :facts do
271         { :osfamily => 'Darwin', :puppetversion   => Puppet.version,}
272       end
273
274       it do
275         expect {
276           subject.call
277         }.to raise_error(Puppet::Error, /This module only works on Debian or derivatives like Ubuntu/)
278       end
279     end
280   end
281 end