Merge pull request #446 from puppetlabs/daenney/apt-proxy
[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 => '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 { 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       :purge   => false,
21       :recurse => false,
22       :notify  => 'Exec[apt_update]',
23     })}
24
25     it { is_expected.to 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 lay down /etc/apt/apt.conf.d/15update-stamp' do
35       is_expected.to contain_file('/etc/apt/apt.conf.d/15update-stamp').with({
36         :group => 'root',
37         :mode  => '0644',
38         :owner => 'root',
39       }).with_content(/APT::Update::Post-Invoke-Success \{"touch \/var\/lib\/apt\/periodic\/update-success-stamp 2>\/dev\/null \|\| true";\};/)
40     end
41
42     it { is_expected.to contain_exec('apt_update').with({
43       :refreshonly => 'true',
44     })}
45
46     it { is_expected.not_to contain_apt__setting('conf-proxy') }
47   end
48
49   describe 'proxy=' do
50     context 'host=localhost' do
51       let(:params) { { :proxy => { 'host' => 'localhost'} } }
52       it { is_expected.to contain_apt__setting('conf-proxy').with({
53         :priority => '01',
54       }).with_content(
55         /Acquire::http::proxy "http:\/\/localhost:8080\/";/
56       ).without_content(
57         /Acquire::https::proxy/
58       )}
59     end
60
61     context 'host=localhost and port=8180' do
62       let(:params) { { :proxy => { 'host' => 'localhost', 'port' => 8180} } }
63       it { is_expected.to contain_apt__setting('conf-proxy').with({
64         :priority => '01',
65       }).with_content(
66         /Acquire::http::proxy "http:\/\/localhost:8180\/";/
67       ).without_content(
68         /Acquire::https::proxy/
69       )}
70     end
71
72     context 'host=localhost and https=true' do
73       let(:params) { { :proxy => { 'host' => 'localhost', 'https' => true} } }
74       it { is_expected.to contain_apt__setting('conf-proxy').with({
75         :priority => '01',
76       }).with_content(
77         /Acquire::http::proxy "http:\/\/localhost:8080\/";/
78       ).with_content(
79         /Acquire::https::proxy "https:\/\/localhost:8080\/";/
80       )}
81     end
82   end
83   context 'lots of non-defaults' do
84     let :params do
85       {
86         :always_apt_update    => true,
87         :purge_sources_list   => true,
88         :purge_sources_list_d => true,
89         :purge_preferences    => true,
90         :purge_preferences_d  => true,
91         :update_timeout       => '1',
92         :update_tries         => '3',
93       }
94     end
95
96     it { is_expected.to contain_file('sources.list').with({
97       :content => "# Repos managed by puppet.\n"
98     })}
99
100     it { is_expected.to contain_file('sources.list.d').with({
101       :purge   => 'true',
102       :recurse => 'true',
103     })}
104
105     it { is_expected.to contain_file('apt-preferences').only_with({
106       :ensure => 'absent',
107       :path   => '/etc/apt/preferences',
108     })}
109
110     it { is_expected.to contain_file('preferences.d').with({
111       :purge   => 'true',
112       :recurse => 'true',
113     })}
114
115     it { is_expected.to contain_exec('apt_update').with({
116       :refreshonly => 'false',
117       :timeout     => '1',
118       :tries       => '3',
119     })}
120
121   end
122
123   context 'with sources defined on valid osfamily' do
124     let :facts do
125       { :osfamily        => 'Debian',
126         :lsbdistcodename => 'precise',
127         :lsbdistid       => 'Debian',
128       }
129     end
130     let(:params) { { :sources => {
131       'debian_unstable' => {
132         'location'          => 'http://debian.mirror.iweb.ca/debian/',
133         'release'           => 'unstable',
134         'repos'             => 'main contrib non-free',
135         'key'               => '55BE302B',
136         'key_server'        => 'subkeys.pgp.net',
137         'pin'               => '-10',
138         'include_src'       => true,
139       },
140       'puppetlabs' => {
141         'location'   => 'http://apt.puppetlabs.com',
142         'repos'      => 'main',
143         'key'        => '4BD6EC30',
144         'key_server' => 'pgp.mit.edu',
145       }
146     } } }
147
148     it {
149       is_expected.to contain_apt__setting('list-debian_unstable').with({
150         :ensure => 'present',
151       })
152     }
153
154     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$/) }
155     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$/) }
156
157     it {
158       is_expected.to contain_apt__setting('list-puppetlabs').with({
159         :ensure => 'present',
160       })
161     }
162
163     it { is_expected.to contain_file('/etc/apt/sources.list.d/puppetlabs.list').with_content(/^deb http:\/\/apt.puppetlabs.com precise main$/) }
164   end
165
166   describe 'failing tests' do
167     context 'bad purge_sources_list' do
168       let :params do
169         {
170           :purge_sources_list => 'foo'
171         }
172       end
173       it do
174         expect {
175           is_expected.to compile
176         }.to raise_error(Puppet::Error)
177       end
178     end
179
180     context 'bad purge_sources_list_d' do
181       let :params do
182         {
183           :purge_sources_list_d => 'foo'
184         }
185       end
186       it do
187         expect {
188           is_expected.to compile
189         }.to raise_error(Puppet::Error)
190       end
191     end
192
193     context 'bad purge_preferences' do
194       let :params do
195         {
196           :purge_preferences => 'foo'
197         }
198       end
199       it do
200         expect {
201           is_expected.to compile
202         }.to raise_error(Puppet::Error)
203       end
204     end
205
206     context 'bad purge_preferences_d' do
207       let :params do
208         {
209           :purge_preferences_d => 'foo'
210         }
211       end
212       it do
213         expect {
214           is_expected.to compile
215         }.to raise_error(Puppet::Error)
216       end
217     end
218
219     context 'with unsupported osfamily' do
220       let :facts do
221         { :osfamily => 'Darwin', }
222       end
223
224       it do
225         expect {
226           is_expected.to compile
227         }.to raise_error(Puppet::Error, /This module only works on Debian or derivatives like Ubuntu/)
228       end
229     end
230   end
231 end