Merge pull request #447 from puppetlabs/daenney/hulk-smash-2
[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' } }
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       :content => "# Repos managed by puppet.\n",
13       :notify  => 'Exec[apt_update]',
14     })}
15
16     it { is_expected.to contain_file('sources.list.d').that_notifies('Exec[apt_update]').only_with({
17       :ensure  => 'directory',
18       :path    => '/etc/apt/sources.list.d',
19       :owner   => 'root',
20       :group   => 'root',
21       :mode    => '0644',
22       :purge   => true,
23       :recurse => true,
24       :notify  => 'Exec[apt_update]',
25     })}
26
27     it { is_expected.to contain_file('preferences').that_notifies('Exec[apt_update]').only_with({
28       :ensure  => 'absent',
29       :path    => '/etc/apt/preferences',
30       :owner   => 'root',
31       :group   => 'root',
32       :mode    => '0644',
33       :notify  => 'Exec[apt_update]',
34     })}
35
36     it { is_expected.to contain_file('preferences.d').that_notifies('Exec[apt_update]').only_with({
37       :ensure  => 'directory',
38       :path    => '/etc/apt/preferences.d',
39       :owner   => 'root',
40       :group   => 'root',
41       :mode    => '0644',
42       :purge   => true,
43       :recurse => true,
44       :notify  => 'Exec[apt_update]',
45     })}
46
47     it 'should lay down /etc/apt/apt.conf.d/15update-stamp' do
48       is_expected.to contain_file('/etc/apt/apt.conf.d/15update-stamp').with({
49         :group => 'root',
50         :mode  => '0644',
51         :owner => 'root',
52       }).with_content(/APT::Update::Post-Invoke-Success \{"touch \/var\/lib\/apt\/periodic\/update-success-stamp 2>\/dev\/null \|\| true";\};/)
53     end
54
55     it { is_expected.to contain_exec('apt_update').with({
56       :refreshonly => 'true',
57     })}
58
59     it { is_expected.not_to contain_apt__setting('conf-proxy') }
60   end
61
62   describe 'proxy=' do
63     context 'host=localhost' do
64       let(:params) { { :proxy => { 'host' => 'localhost'} } }
65       it { is_expected.to contain_apt__setting('conf-proxy').with({
66         :priority => '01',
67       }).with_content(
68         /Acquire::http::proxy "http:\/\/localhost:8080\/";/
69       ).without_content(
70         /Acquire::https::proxy/
71       )}
72     end
73
74     context 'host=localhost and port=8180' do
75       let(:params) { { :proxy => { 'host' => 'localhost', 'port' => 8180} } }
76       it { is_expected.to contain_apt__setting('conf-proxy').with({
77         :priority => '01',
78       }).with_content(
79         /Acquire::http::proxy "http:\/\/localhost:8180\/";/
80       ).without_content(
81         /Acquire::https::proxy/
82       )}
83     end
84
85     context 'host=localhost and https=true' do
86       let(:params) { { :proxy => { 'host' => 'localhost', 'https' => true} } }
87       it { is_expected.to contain_apt__setting('conf-proxy').with({
88         :priority => '01',
89       }).with_content(
90         /Acquire::http::proxy "http:\/\/localhost:8080\/";/
91       ).with_content(
92         /Acquire::https::proxy "https:\/\/localhost:8080\/";/
93       )}
94     end
95   end
96   context 'lots of non-defaults' do
97     let :params do
98       {
99         :update => { 'always' => true, 'timeout' => 1, 'tries' => 3 },
100         :purge  => { 'sources.list' => false, 'sources.list.d' => false,
101                      'preferences' => false, 'preferences.d' => false, },
102       }
103     end
104
105     it { is_expected.to contain_file('sources.list').without({
106       :content => "# Repos managed by puppet.\n",
107     })}
108
109     it { is_expected.to contain_file('sources.list.d').with({
110       :purge   => false,
111       :recurse => false,
112     })}
113
114     it { is_expected.to contain_file('preferences').with({
115       :ensure => 'file',
116     })}
117
118     it { is_expected.to contain_file('preferences.d').with({
119       :purge   => false,
120       :recurse => false,
121     })}
122
123     it { is_expected.to contain_exec('apt_update').with({
124       :refreshonly => false,
125       :timeout     => 1,
126       :tries       => 3,
127     })}
128
129   end
130
131   context 'with sources defined on valid osfamily' do
132     let :facts do
133       { :osfamily        => 'Debian',
134         :lsbdistcodename => 'precise',
135         :lsbdistid       => 'Debian',
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' => '55BE302B', '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' => '4BD6EC30', '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       }
178     end
179     let(:params) { { :keys => {
180       '55BE302B' => {
181         'server' => 'subkeys.pgp.net',
182       },
183       '4BD6EC30' => {
184         'server' => 'pgp.mit.edu',
185       }
186     } } }
187
188     it { is_expected.to contain_apt__key('55BE302B').with({
189         :server => 'subkeys.pgp.net',
190     })}
191
192     it { is_expected.to contain_apt__key('4BD6EC30').with({
193         :server => 'pgp.mit.edu',
194     })}
195   end
196
197   context 'with ppas defined on valid osfamily' do
198     let :facts do
199       { :osfamily        => 'Debian',
200         :lsbdistcodename => 'precise',
201         :lsbdistid       => 'ubuntu',
202       }
203     end
204     let(:params) { { :ppas => {
205       'ppa:drizzle-developers/ppa' => {},
206       'ppa:nginx/stable' => {},
207     } } }
208
209     it { is_expected.to contain_apt__ppa('ppa:drizzle-developers/ppa')}
210     it { is_expected.to contain_apt__ppa('ppa:nginx/stable')}
211   end
212
213   context 'with settings defined on valid osfamily' do
214     let :facts do
215       { :osfamily        => 'Debian',
216         :lsbdistcodename => 'precise',
217         :lsbdistid       => 'Debian',
218       }
219     end
220     let(:params) { { :settings => {
221       'conf-banana' => { 'content' => 'banana' },
222       'pref-banana' => { 'content' => 'banana' },
223     } } }
224
225     it { is_expected.to contain_apt__setting('conf-banana')}
226     it { is_expected.to contain_apt__setting('pref-banana')}
227   end
228
229   describe 'failing tests' do
230     context "purge['sources.list']=>'banana'" do
231       let(:params) { { :purge => { 'sources.list' => 'banana' }, } }
232       it do
233         expect {
234           is_expected.to compile
235         }.to raise_error(Puppet::Error)
236       end
237     end
238
239     context "purge['sources.list.d']=>'banana'" do
240       let(:params) { { :purge => { 'sources.list.d' => 'banana' }, } }
241       it do
242         expect {
243           is_expected.to compile
244         }.to raise_error(Puppet::Error)
245       end
246     end
247
248     context "purge['preferences']=>'banana'" do
249       let(:params) { { :purge => { 'preferences' => 'banana' }, } }
250       it do
251         expect {
252           is_expected.to compile
253         }.to raise_error(Puppet::Error)
254       end
255     end
256
257     context "purge['preferences.d']=>'banana'" do
258       let(:params) { { :purge => { 'preferences.d' => 'banana' }, } }
259       it do
260         expect {
261           is_expected.to compile
262         }.to raise_error(Puppet::Error)
263       end
264     end
265
266     context 'with unsupported osfamily' do
267       let :facts do
268         { :osfamily => 'Darwin', }
269       end
270
271       it do
272         expect {
273           is_expected.to compile
274         }.to raise_error(Puppet::Error, /This module only works on Debian or derivatives like Ubuntu/)
275       end
276     end
277   end
278 end