apt(::key|_key) Remove the `key.*_` prefixes.
[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'               => '55BE302B',
144         'key_server'        => 'subkeys.pgp.net',
145         'pin'               => '-10',
146         'include_src'       => true,
147       },
148       'puppetlabs' => {
149         'location'   => 'http://apt.puppetlabs.com',
150         'repos'      => 'main',
151         'key'        => '4BD6EC30',
152         'key_server' => 'pgp.mit.edu',
153       }
154     } } }
155
156     it {
157       is_expected.to contain_apt__setting('list-debian_unstable').with({
158         :ensure => 'present',
159       })
160     }
161
162     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$/) }
163     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$/) }
164
165     it {
166       is_expected.to contain_apt__setting('list-puppetlabs').with({
167         :ensure => 'present',
168       })
169     }
170
171     it { is_expected.to contain_file('/etc/apt/sources.list.d/puppetlabs.list').with_content(/^deb http:\/\/apt.puppetlabs.com precise main$/) }
172   end
173
174   context 'with keys defined on valid osfamily' do
175     let :facts do
176       { :osfamily        => 'Debian',
177         :lsbdistcodename => 'precise',
178         :lsbdistid       => 'Debian',
179       }
180     end
181     let(:params) { { :keys => {
182       '55BE302B' => {
183         'server' => 'subkeys.pgp.net',
184       },
185       '4BD6EC30' => {
186         'server' => 'pgp.mit.edu',
187       }
188     } } }
189
190     it { is_expected.to contain_apt__key('55BE302B').with({
191         :server => 'subkeys.pgp.net',
192     })}
193
194     it { is_expected.to contain_apt__key('4BD6EC30').with({
195         :server => 'pgp.mit.edu',
196     })}
197   end
198
199   context 'with ppas defined on valid osfamily' do
200     let :facts do
201       { :osfamily        => 'Debian',
202         :lsbdistcodename => 'precise',
203         :lsbdistid       => 'ubuntu',
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       }
221     end
222     let(:params) { { :settings => {
223       'conf-banana' => { 'content' => 'banana' },
224       'pref-banana' => { 'content' => 'banana' },
225     } } }
226
227     it { is_expected.to contain_apt__setting('conf-banana')}
228     it { is_expected.to contain_apt__setting('pref-banana')}
229   end
230
231   describe 'failing tests' do
232     context "purge['sources.list']=>'banana'" do
233       let(:params) { { :purge => { 'sources.list' => 'banana' }, } }
234       it do
235         expect {
236           is_expected.to compile
237         }.to raise_error(Puppet::Error)
238       end
239     end
240
241     context "purge['sources.list.d']=>'banana'" do
242       let(:params) { { :purge => { 'sources.list.d' => 'banana' }, } }
243       it do
244         expect {
245           is_expected.to compile
246         }.to raise_error(Puppet::Error)
247       end
248     end
249
250     context "purge['preferences']=>'banana'" do
251       let(:params) { { :purge => { 'preferences' => 'banana' }, } }
252       it do
253         expect {
254           is_expected.to compile
255         }.to raise_error(Puppet::Error)
256       end
257     end
258
259     context "purge['preferences.d']=>'banana'" do
260       let(:params) { { :purge => { 'preferences.d' => 'banana' }, } }
261       it do
262         expect {
263           is_expected.to compile
264         }.to raise_error(Puppet::Error)
265       end
266     end
267
268     context 'with unsupported osfamily' do
269       let :facts do
270         { :osfamily => 'Darwin', }
271       end
272
273       it do
274         expect {
275           is_expected.to compile
276         }.to raise_error(Puppet::Error, /This module only works on Debian or derivatives like Ubuntu/)
277       end
278     end
279   end
280 end