Cleaned up unit tests.
[puppet-modules/puppetlabs-apt.git] / spec / classes / apt_spec.rb
1 require 'spec_helper'
2 describe 'apt', :type => :class do
3   let(:facts) { { :lsbdistid => 'Debian', :osfamily => 'Debian' } }
4
5   context 'defaults' do
6     it { should contain_file('sources.list').that_notifies('Exec[apt_update]').only_with({
7       'ensure' => 'present',
8       'path'   => '/etc/apt/sources.list',
9       'owner'  => 'root',
10       'group'  => 'root',
11       'mode'   => '0644',
12       'notify' => 'Exec[apt_update]',
13     })}
14
15     it { should 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       'purge'   => false,
21       'recurse' => false,
22       'notify'  => 'Exec[apt_update]',
23     })}
24
25     it { should contain_file('preferences.d').only_with({
26       'ensure'  => 'directory',
27       'path'    => '/etc/apt/preferences.d',
28       'owner'   => 'root',
29       'group'   => 'root',
30       'purge'   => false,
31       'recurse' => false,
32     })}
33
34     it { should contain_file('01proxy').that_notifies('Exec[apt_update]').only_with({
35       'ensure' => 'absent',
36       'path'   => '/etc/apt/apt.conf.d/01proxy',
37       'notify' => 'Exec[apt_update]',
38     })}
39
40     it { should contain_file('old-proxy-file').that_notifies('Exec[apt_update]').only_with({
41       'ensure' => 'absent',
42       'path'   => '/etc/apt/apt.conf.d/proxy',
43       'notify' => 'Exec[apt_update]',
44     })}
45
46     it { should contain_exec('apt_update').with({
47       'refreshonly' => 'true',
48     })}
49   end
50
51   context 'lots of non-defaults' do
52     let :params do
53       {
54         :always_apt_update    => true,
55         :disable_keys         => true,
56         :proxy_host           => 'foo',
57         :proxy_port           => '9876',
58         :purge_sources_list   => true,
59         :purge_sources_list_d => true,
60         :purge_preferences    => true,
61         :purge_preferences_d  => true,
62         :update_timeout       => '1',
63         :update_tries         => '3',
64         :fancy_progress       => true,
65       }
66     end
67
68     it { should contain_file('sources.list').with({
69       'content' => "# Repos managed by puppet.\n"
70     })}
71
72     it { should contain_file('sources.list.d').with({
73       'purge'   => 'true',
74       'recurse' => 'true',
75     })}
76
77     it { should contain_file('apt-preferences').only_with({
78       'ensure' => 'absent',
79       'path'   => '/etc/apt/preferences',
80     })}
81
82     it { should contain_file('preferences.d').with({
83       'purge'   => 'true',
84       'recurse' => 'true',
85     })}
86
87     it { should contain_file('99progressbar').only_with({
88       'ensure'  => 'present',
89       'content' => 'Dpkg::Progress-Fancy "1";',
90       'path'    => '/etc/apt/apt.conf.d/99progressbar',
91     })}
92
93     it { should contain_file('99unauth').only_with({
94       'ensure'  => 'present',
95       'content' => "APT::Get::AllowUnauthenticated 1;\n",
96       'path'    => '/etc/apt/apt.conf.d/99unauth',
97     })}
98
99     it { should contain_file('01proxy').that_notifies('Exec[apt_update]').only_with({
100       'ensure'  => 'present',
101       'path'    => '/etc/apt/apt.conf.d/01proxy',
102       'content' => "Acquire::http::Proxy \"http://foo:9876\";\n",
103       'notify'  => 'Exec[apt_update]',
104       'mode'    => '0644',
105       'owner'   => 'root',
106       'group'   => 'root'
107     })}
108
109     it { should contain_exec('apt_update').with({
110       'refreshonly' => 'false',
111       'timeout'     => '1',
112       'tries'       => '3',
113     })}
114
115   end
116
117   context 'more non-default' do
118     let :params do
119       {
120         :fancy_progress => false,
121         :disable_keys   => false,
122       }
123     end
124
125     it { should contain_file('99progressbar').only_with({
126       'ensure'  => 'absent',
127       'path'    => '/etc/apt/apt.conf.d/99progressbar',
128     })}
129
130     it { should contain_file('99unauth').only_with({
131       'ensure'  => 'absent',
132       'path'    => '/etc/apt/apt.conf.d/99unauth',
133     })}
134
135   end
136
137   context 'with sources defined on valid osfamily' do
138     let :facts do
139       { :osfamily        => 'Debian',
140         :lsbdistcodename => 'precise',
141         :lsbdistid       => 'Debian',
142       }
143     end
144     let(:params) { { :sources => {
145       'debian_unstable' => {
146         'location'          => 'http://debian.mirror.iweb.ca/debian/',
147         'release'           => 'unstable',
148         'repos'             => 'main contrib non-free',
149         'required_packages' => 'debian-keyring debian-archive-keyring',
150         'key'               => '55BE302B',
151         'key_server'        => 'subkeys.pgp.net',
152         'pin'               => '-10',
153         'include_src'       => true
154       },
155       'puppetlabs' => {
156         'location'   => 'http://apt.puppetlabs.com',
157         'repos'      => 'main',
158         'key'        => '4BD6EC30',
159         'key_server' => 'pgp.mit.edu',
160       }
161     } } }
162
163     it {
164       should contain_file('debian_unstable.list').with({
165         'ensure'  => 'present',
166         'path'    => '/etc/apt/sources.list.d/debian_unstable.list',
167         'owner'   => 'root',
168         'group'   => 'root',
169         'mode'    => '0644',
170         'notify'  => 'Exec[apt_update]',
171       })
172     }
173
174     it { should contain_file('debian_unstable.list').with_content(/^deb http:\/\/debian.mirror.iweb.ca\/debian\/ unstable main contrib non-free$/) }
175     it { should contain_file('debian_unstable.list').with_content(/^deb-src http:\/\/debian.mirror.iweb.ca\/debian\/ unstable main contrib non-free$/) }
176
177     it {
178       should contain_file('puppetlabs.list').with({
179         'ensure'  => 'present',
180         'path'    => '/etc/apt/sources.list.d/puppetlabs.list',
181         'owner'   => 'root',
182         'group'   => 'root',
183         'mode'    => '0644',
184         'notify'  => 'Exec[apt_update]',
185       })
186     }
187
188     it { should contain_file('puppetlabs.list').with_content(/^deb http:\/\/apt.puppetlabs.com precise main$/) }
189     it { should contain_file('puppetlabs.list').with_content(/^deb-src http:\/\/apt.puppetlabs.com precise main$/) }
190   end
191
192   describe 'failing tests' do
193     context 'bad purge_sources_list' do
194       let :params do
195         {
196           'purge_sources_list' => 'foo'
197         }
198       end
199       it do
200         expect {
201           should compile
202         }.to raise_error(Puppet::Error)
203       end
204     end
205
206     context 'bad purge_sources_list_d' do
207       let :params do
208         {
209           'purge_sources_list_d' => 'foo'
210         }
211       end
212       it do
213         expect {
214           should compile
215         }.to raise_error(Puppet::Error)
216       end
217     end
218
219     context 'bad purge_preferences' do
220       let :params do
221         {
222           'purge_preferences' => 'foo'
223         }
224       end
225       it do
226         expect {
227           should compile
228         }.to raise_error(Puppet::Error)
229       end
230     end
231
232     context 'bad purge_preferences_d' do
233       let :params do
234         {
235           'purge_preferences_d' => 'foo'
236         }
237       end
238       it do
239         expect {
240           should compile
241         }.to raise_error(Puppet::Error)
242       end
243     end
244
245     context 'with unsupported osfamily' do
246       let :facts do
247         { :osfamily => 'Darwin', }
248       end
249
250       it do
251         expect {
252           should compile
253         }.to raise_error(Puppet::Error, /This module only works on Debian or derivatives like Ubuntu/)
254       end
255     end
256   end
257 end