b8ff37ea312caee1153a9c574c73b2520c9b4723
[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 { 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   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 { is_expected.to contain_file('sources.list').with({
61       'content' => "# Repos managed by puppet.\n"
62     })}
63
64     it { is_expected.to contain_file('sources.list.d').with({
65       'purge'   => 'true',
66       'recurse' => 'true',
67     })}
68
69     it { is_expected.to contain_file('apt-preferences').only_with({
70       'ensure' => 'absent',
71       'path'   => '/etc/apt/preferences',
72     })}
73
74     it { is_expected.to contain_file('preferences.d').with({
75       'purge'   => 'true',
76       'recurse' => 'true',
77     })}
78
79     it { is_expected.to 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       is_expected.to contain_apt__setting('list-debian_unstable').with({
114         'ensure'  => 'present',
115       })
116     }
117
118     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$/) }
119     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$/) }
120
121     it {
122       is_expected.to contain_apt__setting('list-puppetlabs').with({
123         'ensure'  => 'present',
124       })
125     }
126
127     it { is_expected.to contain_file('/etc/apt/sources.list.d/puppetlabs.list').with_content(/^deb http:\/\/apt.puppetlabs.com precise main$/) }
128   end
129
130   describe 'failing tests' do
131     context 'bad purge_sources_list' do
132       let :params do
133         {
134           'purge_sources_list' => 'foo'
135         }
136       end
137       it do
138         expect {
139           is_expected.to compile
140         }.to raise_error(Puppet::Error)
141       end
142     end
143
144     context 'bad purge_sources_list_d' do
145       let :params do
146         {
147           'purge_sources_list_d' => 'foo'
148         }
149       end
150       it do
151         expect {
152           is_expected.to compile
153         }.to raise_error(Puppet::Error)
154       end
155     end
156
157     context 'bad purge_preferences' do
158       let :params do
159         {
160           'purge_preferences' => 'foo'
161         }
162       end
163       it do
164         expect {
165           is_expected.to compile
166         }.to raise_error(Puppet::Error)
167       end
168     end
169
170     context 'bad purge_preferences_d' do
171       let :params do
172         {
173           'purge_preferences_d' => 'foo'
174         }
175       end
176       it do
177         expect {
178           is_expected.to compile
179         }.to raise_error(Puppet::Error)
180       end
181     end
182
183     context 'with unsupported osfamily' do
184       let :facts do
185         { :osfamily => 'Darwin', }
186       end
187
188       it do
189         expect {
190           is_expected.to compile
191         }.to raise_error(Puppet::Error, /This module only works on Debian or derivatives like Ubuntu/)
192       end
193     end
194   end
195 end