]> review.fuel-infra Code Review - puppet-modules/puppetlabs-apt.git/blob - spec/classes/apt_spec.rb
Cleanup for `apt::source`
[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 lay down /etc/apt/apt.conf.d/15update-stamp' do
35       should 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 { should contain_exec('apt_update').with({
43       'refreshonly' => 'true',
44     })}
45   end
46
47   context 'lots of non-defaults' do
48     let :params do
49       {
50         :always_apt_update    => true,
51         :purge_sources_list   => true,
52         :purge_sources_list_d => true,
53         :purge_preferences    => true,
54         :purge_preferences_d  => true,
55         :update_timeout       => '1',
56         :update_tries         => '3',
57       }
58     end
59
60     it { should contain_file('sources.list').with({
61       'content' => "# Repos managed by puppet.\n"
62     })}
63
64     it { should contain_file('sources.list.d').with({
65       'purge'   => 'true',
66       'recurse' => 'true',
67     })}
68
69     it { should contain_file('apt-preferences').only_with({
70       'ensure' => 'absent',
71       'path'   => '/etc/apt/preferences',
72     })}
73
74     it { should contain_file('preferences.d').with({
75       'purge'   => 'true',
76       'recurse' => 'true',
77     })}
78
79     it { should contain_exec('apt_update').with({
80       'refreshonly' => 'false',
81       'timeout'     => '1',
82       'tries'       => '3',
83     })}
84
85   end
86
87   context 'with sources defined on valid osfamily' do
88     let :facts do
89       { :osfamily        => 'Debian',
90         :lsbdistcodename => 'precise',
91         :lsbdistid       => 'Debian',
92       }
93     end
94     let(:params) { { :sources => {
95       'debian_unstable' => {
96         'location'          => 'http://debian.mirror.iweb.ca/debian/',
97         'release'           => 'unstable',
98         'repos'             => 'main contrib non-free',
99         'key'               => '55BE302B',
100         'key_server'        => 'subkeys.pgp.net',
101         'pin'               => '-10',
102         'include_src'       => true
103       },
104       'puppetlabs' => {
105         'location'   => 'http://apt.puppetlabs.com',
106         'repos'      => 'main',
107         'key'        => '4BD6EC30',
108         'key_server' => 'pgp.mit.edu',
109       }
110     } } }
111
112     it {
113       should contain_file('debian_unstable.list').with({
114         'ensure'  => 'present',
115         'path'    => '/etc/apt/sources.list.d/debian_unstable.list',
116         'owner'   => 'root',
117         'group'   => 'root',
118         'mode'    => '0644',
119         'notify'  => 'Exec[apt_update]',
120       })
121     }
122
123     it { should contain_file('debian_unstable.list').with_content(/^deb http:\/\/debian.mirror.iweb.ca\/debian\/ unstable main contrib non-free$/) }
124     it { should contain_file('debian_unstable.list').with_content(/^deb-src http:\/\/debian.mirror.iweb.ca\/debian\/ unstable main contrib non-free$/) }
125
126     it {
127       should contain_file('puppetlabs.list').with({
128         'ensure'  => 'present',
129         'path'    => '/etc/apt/sources.list.d/puppetlabs.list',
130         'owner'   => 'root',
131         'group'   => 'root',
132         'mode'    => '0644',
133         'notify'  => 'Exec[apt_update]',
134       })
135     }
136
137     it { should contain_file('puppetlabs.list').with_content(/^deb http:\/\/apt.puppetlabs.com precise main$/) }
138   end
139
140   describe 'failing tests' do
141     context 'bad purge_sources_list' do
142       let :params do
143         {
144           'purge_sources_list' => 'foo'
145         }
146       end
147       it do
148         expect {
149           should compile
150         }.to raise_error(Puppet::Error)
151       end
152     end
153
154     context 'bad purge_sources_list_d' do
155       let :params do
156         {
157           'purge_sources_list_d' => 'foo'
158         }
159       end
160       it do
161         expect {
162           should compile
163         }.to raise_error(Puppet::Error)
164       end
165     end
166
167     context 'bad purge_preferences' do
168       let :params do
169         {
170           'purge_preferences' => 'foo'
171         }
172       end
173       it do
174         expect {
175           should compile
176         }.to raise_error(Puppet::Error)
177       end
178     end
179
180     context 'bad purge_preferences_d' do
181       let :params do
182         {
183           'purge_preferences_d' => 'foo'
184         }
185       end
186       it do
187         expect {
188           should compile
189         }.to raise_error(Puppet::Error)
190       end
191     end
192
193     context 'with unsupported osfamily' do
194       let :facts do
195         { :osfamily => 'Darwin', }
196       end
197
198       it do
199         expect {
200           should compile
201         }.to raise_error(Puppet::Error, /This module only works on Debian or derivatives like Ubuntu/)
202       end
203     end
204   end
205 end