Merge pull request #441 from mhaskel/use_setting
[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         'notify'  => 'Exec[apt_update]',
116       })
117     }
118
119     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$/) }
120     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$/) }
121
122     it {
123       is_expected.to contain_apt__setting('list-puppetlabs').with({
124         'ensure'  => 'present',
125         'notify'  => 'Exec[apt_update]',
126       })
127     }
128
129     it { is_expected.to contain_file('/etc/apt/sources.list.d/puppetlabs.list').with_content(/^deb http:\/\/apt.puppetlabs.com precise main$/) }
130   end
131
132   describe 'failing tests' do
133     context 'bad purge_sources_list' do
134       let :params do
135         {
136           'purge_sources_list' => 'foo'
137         }
138       end
139       it do
140         expect {
141           is_expected.to compile
142         }.to raise_error(Puppet::Error)
143       end
144     end
145
146     context 'bad purge_sources_list_d' do
147       let :params do
148         {
149           'purge_sources_list_d' => 'foo'
150         }
151       end
152       it do
153         expect {
154           is_expected.to compile
155         }.to raise_error(Puppet::Error)
156       end
157     end
158
159     context 'bad purge_preferences' do
160       let :params do
161         {
162           'purge_preferences' => 'foo'
163         }
164       end
165       it do
166         expect {
167           is_expected.to compile
168         }.to raise_error(Puppet::Error)
169       end
170     end
171
172     context 'bad purge_preferences_d' do
173       let :params do
174         {
175           'purge_preferences_d' => 'foo'
176         }
177       end
178       it do
179         expect {
180           is_expected.to compile
181         }.to raise_error(Puppet::Error)
182       end
183     end
184
185     context 'with unsupported osfamily' do
186       let :facts do
187         { :osfamily => 'Darwin', }
188       end
189
190       it do
191         expect {
192           is_expected.to compile
193         }.to raise_error(Puppet::Error, /This module only works on Debian or derivatives like Ubuntu/)
194       end
195     end
196   end
197 end