Merge pull request #515 from mhaskel/merge_2.0.x_to_master
[puppet-modules/puppetlabs-apt.git] / spec / defines / source_spec.rb
1 require 'spec_helper'
2
3 describe 'apt::source' do
4   GPG_KEY_ID = '47B320EB4C7C375AA9DAE1A01054B7A24BD6EC30'
5
6   let :pre_condition do
7     'class { "apt": }'
8   end
9
10   let :title do
11     'my_source'
12   end
13
14   context 'defaults' do
15     context 'without location' do
16       let :facts do
17         {
18           :lsbdistid       => 'Debian',
19           :lsbdistcodename => 'wheezy',
20           :osfamily        => 'Debian',
21           :puppetversion   => '3.5.0',
22         }
23       end
24       it do
25         expect {
26           subject.call
27         }.to raise_error(Puppet::Error, /source entry without specifying a location/)
28       end
29     end
30     context 'with location' do
31       let :facts do
32         {
33           :lsbdistid       => 'Debian',
34           :lsbdistcodename => 'wheezy',
35           :osfamily        => 'Debian',
36           :puppetversion   => '3.5.0',
37         }
38       end
39       let(:params) { { :location => 'hello.there', } }
40
41       it { is_expected.to contain_apt__setting('list-my_source').with({
42         :ensure  => 'present',
43       }).without_content(/# my_source\ndeb-src hello.there wheezy main\n/)
44       }
45     end
46   end
47
48   describe 'no defaults' do
49     let :facts do
50       {
51         :lsbdistid       => 'Debian',
52         :lsbdistcodename => 'wheezy',
53         :osfamily        => 'Debian',
54         :puppetversion   => '3.5.0',
55       }
56     end
57
58     context 'with complex pin' do
59       let :params do
60         {
61           :location => 'hello.there',
62           :pin      => { 'release'     => 'wishwash',
63                          'explanation' => 'wishwash',
64                          'priority'    => 1001, },
65         }
66       end
67
68       it { is_expected.to contain_apt__setting('list-my_source').with({
69         :ensure => 'present',
70       }).with_content(/hello.there wheezy main\n/)
71       }
72
73       it { is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with({
74         :ensure       => 'present',
75         :priority     => 1001,
76         :explanation  => 'wishwash',
77         :release      => 'wishwash',
78       })
79       }
80     end
81
82     context 'with simple key' do
83       let :params do
84         {
85           :comment           => 'foo',
86           :location          => 'http://debian.mirror.iweb.ca/debian/',
87           :release           => 'sid',
88           :repos             => 'testing',
89           :key               => GPG_KEY_ID,
90           :pin               => '10',
91           :architecture      => 'x86_64',
92           :allow_unsigned    => true,
93         }
94       end
95
96       it { is_expected.to contain_apt__setting('list-my_source').with({
97         :ensure => 'present',
98       }).with_content(/# foo\ndeb \[arch=x86_64 trusted=yes\] http:\/\/debian\.mirror\.iweb\.ca\/debian\/ sid testing\n/).without_content(/deb-src/)
99       }
100
101       it { is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with({
102         :ensure   => 'present',
103         :priority => '10',
104         :origin   => 'debian.mirror.iweb.ca',
105       })
106       }
107
108       it { is_expected.to contain_apt__key("Add key: #{GPG_KEY_ID} from Apt::Source my_source").that_comes_before('Apt::Setting[list-my_source]').with({
109         :ensure  => 'present',
110         :id      => GPG_KEY_ID,
111       })
112       }
113     end
114
115     context 'with complex key' do
116       let :params do
117         {
118           :comment           => 'foo',
119           :location          => 'http://debian.mirror.iweb.ca/debian/',
120           :release           => 'sid',
121           :repos             => 'testing',
122           :key               => { 'id' => GPG_KEY_ID, 'server' => 'pgp.mit.edu',
123                                   'content' => 'GPG key content',
124                                   'source'  => 'http://apt.puppetlabs.com/pubkey.gpg',},
125           :pin               => '10',
126           :architecture      => 'x86_64',
127           :allow_unsigned    => true,
128         }
129       end
130
131       it { is_expected.to contain_apt__setting('list-my_source').with({
132         :ensure => 'present',
133       }).with_content(/# foo\ndeb \[arch=x86_64 trusted=yes\] http:\/\/debian\.mirror\.iweb\.ca\/debian\/ sid testing\n/).without_content(/deb-src/)
134       }
135
136       it { is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with({
137         :ensure   => 'present',
138         :priority => '10',
139         :origin   => 'debian.mirror.iweb.ca',
140       })
141       }
142
143       it { is_expected.to contain_apt__key("Add key: #{GPG_KEY_ID} from Apt::Source my_source").that_comes_before('Apt::Setting[list-my_source]').with({
144         :ensure  => 'present',
145         :id      => GPG_KEY_ID,
146         :server  => 'pgp.mit.edu',
147         :content => 'GPG key content',
148         :source  => 'http://apt.puppetlabs.com/pubkey.gpg',
149       })
150       }
151     end
152
153     context 'with simple key' do
154       let :params do
155         {
156           :comment        => 'foo',
157           :location       => 'http://debian.mirror.iweb.ca/debian/',
158           :release        => 'sid',
159           :repos          => 'testing',
160           :key            => GPG_KEY_ID,
161           :pin            => '10',
162           :architecture   => 'x86_64',
163           :allow_unsigned => true,
164         }
165       end
166
167       it { is_expected.to contain_apt__setting('list-my_source').with({
168         :ensure => 'present',
169       }).with_content(/# foo\ndeb \[arch=x86_64 trusted=yes\] http:\/\/debian\.mirror\.iweb\.ca\/debian\/ sid testing\n/).without_content(/deb-src/)
170       }
171
172       it { is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with({
173         :ensure   => 'present',
174         :priority => '10',
175         :origin   => 'debian.mirror.iweb.ca',
176       })
177       }
178
179       it { is_expected.to contain_apt__key("Add key: #{GPG_KEY_ID} from Apt::Source my_source").that_comes_before('Apt::Setting[list-my_source]').with({
180         :ensure  => 'present',
181         :id      => GPG_KEY_ID,
182       })
183       }
184     end
185   end
186
187   context 'allow_unsigned true' do
188     let :facts do
189       {
190         :lsbdistid       => 'Debian',
191         :lsbdistcodename => 'wheezy',
192         :osfamily        => 'Debian',
193         :puppetversion   => '3.5.0',
194       }
195     end
196     let :params do
197       {
198         :location       => 'hello.there',
199         :allow_unsigned => true,
200       }
201     end
202
203     it { is_expected.to contain_apt__setting('list-my_source').with({
204       :ensure => 'present',
205     }).with_content(/# my_source\ndeb \[trusted=yes\] hello.there wheezy main\n/)
206     }
207   end
208
209   context 'architecture equals x86_64' do
210     let :facts do
211       {
212         :lsbdistid       => 'Debian',
213         :lsbdistcodename => 'wheezy',
214         :osfamily        => 'Debian',
215         :puppetversion   => '3.5.0',
216       }
217     end
218     let :params do
219       {
220         :location     => 'hello.there',
221         :include      => {'deb' => false, 'src' => true,},
222         :architecture => 'x86_64',
223       }
224     end
225
226     it { is_expected.to contain_apt__setting('list-my_source').with({
227       :ensure => 'present',
228     }).with_content(/# my_source\ndeb-src \[arch=x86_64 \] hello.there wheezy main\n/)
229     }
230   end
231
232   context 'ensure => absent' do
233     let :facts do
234       {
235         :lsbdistid       => 'Debian',
236         :lsbdistcodename => 'wheezy',
237         :osfamily        => 'Debian',
238         :puppetversion   => '3.5.0',
239       }
240     end
241     let :params do
242       {
243         :ensure => 'absent',
244       }
245     end
246
247     it { is_expected.to contain_apt__setting('list-my_source').with({
248       :ensure => 'absent'
249     })
250     }
251   end
252
253   describe 'validation' do
254     context 'no release' do
255       let :facts do
256         {
257           :lsbdistid       => 'Debian',
258           :osfamily        => 'Debian',
259           :puppetversion   => '3.5.0',
260         }
261       end
262       let(:params) { { :location => 'hello.there', } }
263
264       it do
265         expect {
266           subject.call
267         }.to raise_error(Puppet::Error, /lsbdistcodename fact not available: release parameter required/)
268       end
269     end
270
271     context 'invalid pin' do
272       let :facts do
273         {
274           :lsbdistid       => 'Debian',
275           :lsbdistcodename => 'wheezy',
276           :osfamily        => 'Debian',
277           :puppetversion   => '3.5.0',
278         }
279       end
280       let :params do
281         {
282           :location => 'hello.there',
283           :pin      => true,
284         }
285       end
286
287       it do
288         expect {
289           subject.call
290         }.to raise_error(Puppet::Error, /invalid value for pin/)
291       end
292     end
293
294   end
295 end