]> review.fuel-infra Code Review - puppet-modules/puppetlabs-apt.git/blob - spec/classes/apt_spec.rb
apt: Change how purging is managed.
[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         :always_apt_update    => true,
100         :purge                => { 'sources.list' => false, 'sources.list.d' => false,
101                                    'preferences' => false, 'preferences.d' => false, },
102         :update_timeout       => '1',
103         :update_tries         => '3',
104       }
105     end
106
107     it { is_expected.to contain_file('sources.list').without({
108       :content => "# Repos managed by puppet.\n",
109     })}
110
111     it { is_expected.to contain_file('sources.list.d').with({
112       :purge   => false,
113       :recurse => false,
114     })}
115
116     it { is_expected.to contain_file('preferences').with({
117       :ensure => 'file',
118     })}
119
120     it { is_expected.to contain_file('preferences.d').with({
121       :purge   => false,
122       :recurse => false,
123     })}
124
125     it { is_expected.to contain_exec('apt_update').with({
126       :refreshonly => 'false',
127       :timeout     => '1',
128       :tries       => '3',
129     })}
130
131   end
132
133   context 'with sources defined on valid osfamily' do
134     let :facts do
135       { :osfamily        => 'Debian',
136         :lsbdistcodename => 'precise',
137         :lsbdistid       => 'Debian',
138       }
139     end
140     let(:params) { { :sources => {
141       'debian_unstable' => {
142         'location'          => 'http://debian.mirror.iweb.ca/debian/',
143         'release'           => 'unstable',
144         'repos'             => 'main contrib non-free',
145         'key'               => '55BE302B',
146         'key_server'        => 'subkeys.pgp.net',
147         'pin'               => '-10',
148         'include_src'       => true,
149       },
150       'puppetlabs' => {
151         'location'   => 'http://apt.puppetlabs.com',
152         'repos'      => 'main',
153         'key'        => '4BD6EC30',
154         'key_server' => 'pgp.mit.edu',
155       }
156     } } }
157
158     it {
159       is_expected.to contain_apt__setting('list-debian_unstable').with({
160         :ensure => 'present',
161       })
162     }
163
164     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$/) }
165     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$/) }
166
167     it {
168       is_expected.to contain_apt__setting('list-puppetlabs').with({
169         :ensure => 'present',
170       })
171     }
172
173     it { is_expected.to contain_file('/etc/apt/sources.list.d/puppetlabs.list').with_content(/^deb http:\/\/apt.puppetlabs.com precise main$/) }
174   end
175
176   describe 'failing tests' do
177     context "purge['sources.list']=>'banana'" do
178       let(:params) { { :purge => { 'sources.list' => 'banana' }, } }
179       it do
180         expect {
181           is_expected.to compile
182         }.to raise_error(Puppet::Error)
183       end
184     end
185
186     context "purge['sources.list.d']=>'banana'" do
187       let(:params) { { :purge => { 'sources.list.d' => 'banana' }, } }
188       it do
189         expect {
190           is_expected.to compile
191         }.to raise_error(Puppet::Error)
192       end
193     end
194
195     context "purge['preferences']=>'banana'" do
196       let(:params) { { :purge => { 'preferences' => 'banana' }, } }
197       it do
198         expect {
199           is_expected.to compile
200         }.to raise_error(Puppet::Error)
201       end
202     end
203
204     context "purge['preferences.d']=>'banana'" do
205       let(:params) { { :purge => { 'preferences.d' => 'banana' }, } }
206       it do
207         expect {
208           is_expected.to compile
209         }.to raise_error(Puppet::Error)
210       end
211     end
212
213     context 'with unsupported osfamily' do
214       let :facts do
215         { :osfamily => 'Darwin', }
216       end
217
218       it do
219         expect {
220           is_expected.to compile
221         }.to raise_error(Puppet::Error, /This module only works on Debian or derivatives like Ubuntu/)
222       end
223     end
224   end
225 end