684a6f06561da45a2751b9694b4580e78f632821
[puppet-modules/puppetlabs-apt.git] / spec / defines / source_spec.rb
1 require 'spec_helper'
2
3 describe 'apt::source', :type => :define do
4   GPG_KEY_ID = '47B320EB4C7C375AA9DAE1A01054B7A24BD6EC30'
5
6   let :pre_condition do
7     'class { "apt": }'
8   end
9
10   let :title do
11     'my_source'
12   end
13
14   context 'mostly defaults' do
15     let :facts do
16       {
17         :lsbdistid       => 'Debian',
18         :lsbdistcodename => 'wheezy',
19         :osfamily        => 'Debian'
20       }
21     end
22
23     let :params do
24       {
25         'include_deb' => false,
26       }
27     end
28
29     it { is_expected.to contain_file('my_source.list').that_notifies('Exec[apt_update]').with({
30       'ensure' => 'present',
31       'path'   => '/etc/apt/sources.list.d/my_source.list',
32       'owner'  => 'root',
33       'group'  => 'root',
34       'mode'   => '0644',
35     }).with_content(/# my_source\ndeb-src  wheezy main\n/)
36     }
37   end
38
39   context 'no defaults' do
40     let :facts do
41       {
42         :lsbdistid       => 'Debian',
43         :lsbdistcodename => 'wheezy',
44         :osfamily        => 'Debian'
45       }
46     end
47     let :params do
48       {
49         'comment'           => 'foo',
50         'location'          => 'http://debian.mirror.iweb.ca/debian/',
51         'release'           => 'sid',
52         'repos'             => 'testing',
53         'include_src'       => false,
54         'required_packages' => 'vim',
55         'key'               => GPG_KEY_ID,
56         'key_server'        => 'pgp.mit.edu',
57         'key_content'       => 'GPG key content',
58         'key_source'        => 'http://apt.puppetlabs.com/pubkey.gpg',
59         'pin'               => '10',
60         'architecture'      => 'x86_64',
61         'trusted_source'    => true,
62       }
63     end
64
65     it { is_expected.to contain_file('my_source.list').that_notifies('Exec[apt_update]').with({
66       'ensure' => 'present',
67       'path'   => '/etc/apt/sources.list.d/my_source.list',
68       'owner'  => 'root',
69       'group'  => 'root',
70       'mode'   => '0644',
71     }).with_content(/# foo\ndeb \[arch=x86_64 trusted=yes\] http:\/\/debian\.mirror\.iweb\.ca\/debian\/ sid testing\n/).without_content(/deb-src/)
72     }
73
74     it { is_expected.to contain_apt__pin('my_source').that_comes_before('File[my_source.list]').with({
75       'ensure'   => 'present',
76       'priority' => '10',
77       'origin'   => 'debian.mirror.iweb.ca',
78     })
79     }
80
81     it { is_expected.to contain_exec("Required packages: 'vim' for my_source").that_comes_before('Exec[apt_update]').that_subscribes_to('File[my_source.list]').with({
82       'command'     => '/usr/bin/apt-get -y install vim',
83       'logoutput'   => 'on_failure',
84       'refreshonly' => true,
85       'tries'       => '3',
86       'try_sleep'   => '1',
87     })
88     }
89
90     it { is_expected.to contain_apt__key("Add key: #{GPG_KEY_ID} from Apt::Source my_source").that_comes_before('File[my_source.list]').with({
91       'ensure' => 'present',
92         'key'  => GPG_KEY_ID,
93         'key_server' => 'pgp.mit.edu',
94         'key_content' => 'GPG key content',
95         'key_source' => 'http://apt.puppetlabs.com/pubkey.gpg',
96     })
97     }
98   end
99
100   context 'trusted_source true' do
101     let :facts do
102       {
103         :lsbdistid       => 'Debian',
104         :lsbdistcodename => 'wheezy',
105         :osfamily        => 'Debian'
106       }
107     end
108     let :params do
109       {
110         'include_src'    => false,
111         'trusted_source' => true,
112       }
113     end
114
115     it { is_expected.to contain_file('my_source.list').that_notifies('Exec[apt_update]').with({
116       'ensure' => 'present',
117       'path'   => '/etc/apt/sources.list.d/my_source.list',
118       'owner'  => 'root',
119       'group'  => 'root',
120       'mode'   => '0644',
121     }).with_content(/# my_source\ndeb \[trusted=yes\]  wheezy main\n/)
122     }
123   end
124
125   context 'architecture equals x86_64' do
126     let :facts do
127       {
128         :lsbdistid       => 'Debian',
129         :lsbdistcodename => 'wheezy',
130         :osfamily        => 'Debian'
131       }
132     end
133     let :params do
134       {
135         'include_deb'  => false,
136         'architecture' => 'x86_64',
137       }
138     end
139
140     it { is_expected.to contain_file('my_source.list').that_notifies('Exec[apt_update]').with({
141       'ensure' => 'present',
142       'path'   => '/etc/apt/sources.list.d/my_source.list',
143       'owner'  => 'root',
144       'group'  => 'root',
145       'mode'   => '0644',
146     }).with_content(/# my_source\ndeb-src \[arch=x86_64 \]  wheezy main\n/)
147     }
148   end
149  
150   context 'ensure => absent' do
151     let :facts do
152       {
153         :lsbdistid       => 'Debian',
154         :lsbdistcodename => 'wheezy',
155         :osfamily        => 'Debian'
156       }
157     end
158     let :params do
159       {
160         'ensure' => 'absent',
161       }
162     end
163
164     it { is_expected.to contain_file('my_source.list').that_notifies('Exec[apt_update]').with({
165       'ensure' => 'absent'
166     })
167     }
168   end
169
170   describe 'validation' do
171     context 'no release' do
172       let :facts do
173         {
174           :lsbdistid       => 'Debian',
175           :osfamily        => 'Debian'
176         }
177       end
178
179       it do
180         expect {
181           should compile
182         }.to raise_error(Puppet::Error, /lsbdistcodename fact not available: release parameter required/)
183       end
184     end
185   end
186 end