Merge pull request #282 from laurenrother/security
[puppet-modules/puppetlabs-apt.git] / spec / defines / source_spec.rb
1 require 'spec_helper'
2
3 describe 'apt::source', :type => :define do
4   let(:facts) { { :lsbdistid => 'Debian' } }
5   GPG_KEY_ID = '4BD6EC30'
6
7   let :title do
8     'my_source'
9   end
10
11   let :default_params do
12     {
13       :ensure             => 'present',
14       :location           => '',
15       :release            => 'karmic',
16       :repos              => 'main',
17       :include_src        => true,
18       :required_packages  => false,
19       :key                => false,
20       :key_server         => false,
21       :key_content        => false,
22       :key_source         => false,
23       :pin                => false
24     }
25   end
26
27   [{},
28    {
29       :location           => 'http://example.com',
30       :release            => 'precise',
31       :repos              => 'security',
32       :include_src        => false,
33       :required_packages  => 'apache',
34       :key                => GPG_KEY_ID,
35       :key_server         => 'keyserver.debian.com',
36       :pin                => '600',
37       :key_content        => 'ABCD1234'
38     },
39     {
40       :key                => GPG_KEY_ID,
41       :key_server         => 'keyserver.debian.com',
42     },
43     {
44       :ensure             => 'absent',
45       :location           => 'http://example.com',
46       :release            => 'precise',
47       :repos              => 'security',
48     },
49     {
50       :release            => '',
51     },
52     {
53       :release            => 'custom',
54     },
55     {
56       :architecture       => 'amd64',
57     }
58   ].each do |param_set|
59     describe "when #{param_set == {} ? "using default" : "specifying"} class parameters" do
60       let :param_hash do
61         default_params.merge(param_set)
62       end
63
64       let :facts do
65         {:lsbdistcodename => 'karmic', :lsbdistid => 'Ubuntu'}
66       end
67
68       let :params do
69         param_set
70       end
71
72       let :filename do
73         "/etc/apt/sources.list.d/#{title}.list"
74       end
75
76       let :content do
77         content = "# #{title}"
78         if param_hash[:architecture]
79           arch = "[arch=#{param_hash[:architecture]}] "
80         end
81         content << "\ndeb #{arch}#{param_hash[:location]} #{param_hash[:release]} #{param_hash[:repos]}\n"
82
83         if param_hash[:include_src]
84           content << "deb-src #{arch}#{param_hash[:location]} #{param_hash[:release]} #{param_hash[:repos]}\n"
85         end
86         content
87       end
88
89       it { should contain_apt__params }
90
91       it { should contain_file("#{title}.list").with({
92           'ensure'    => param_hash[:ensure],
93           'path'      => filename,
94           'owner'     => 'root',
95           'group'     => 'root',
96           'mode'      => '0644',
97           'content'   => content,
98         })
99       }
100
101       it {
102         if param_hash[:pin]
103           should contain_apt__pin(title).with({
104             "priority"  => param_hash[:pin],
105             "before"    => "File[#{title}.list]"
106           })
107         else
108           should_not contain_apt__pin(title).with({
109             "priority"  => param_hash[:pin],
110             "before"    => "File[#{title}.list]"
111           })
112         end
113       }
114
115       it {
116         should contain_exec("apt_update").with({
117           "command"     => "/usr/bin/apt-get update",
118           "refreshonly" => true
119         })
120       }
121
122       it {
123         if param_hash[:required_packages]
124           should contain_exec("Required packages: '#{param_hash[:required_packages]}' for #{title}").with({
125             "command" => "/usr/bin/apt-get -y install #{param_hash[:required_packages]}",
126             "subscribe"   => "File[#{title}.list]",
127             "refreshonly" => true,
128             "before"      => 'Exec[apt_update]',
129           })
130         else
131           should_not contain_exec("Required packages: '#{param_hash[:required_packages]}' for #{title}").with({
132             "command"     => "/usr/bin/apt-get -y install #{param_hash[:required_packages]}",
133             "subscribe"   => "File[#{title}.list]",
134             "refreshonly" => true
135           })
136         end
137       }
138
139       it {
140         key_server  = param_hash[:key_server]  || nil
141         key_content = param_hash[:key_content] || nil
142         key_source  = param_hash[:key_source]  || nil
143         if param_hash[:key]
144           should contain_apt__key("Add key: #{param_hash[:key]} from Apt::Source #{title}").with({
145             "key"         => param_hash[:key],
146             "ensure"      => :present,
147             "key_server"  => key_server,
148             "key_content" => key_content,
149             "key_source"  => key_source,
150             "before"      => "File[#{title}.list]"
151           })
152         else
153           should_not contain_apt__key("Add key: #{param_hash[:key]} from Apt::Source #{title}").with({
154             "key"         => param_hash[:key],
155             "ensure"      => :present,
156             "key_server"  => param_hash[:key_server],
157             "key_content" => param_hash[:key_content],
158             "key_source"  => param_hash[:key_source],
159             "before"      => "File[#{title}.list]"
160           })
161         end
162       }
163     end
164   end
165   describe "without release should raise a Puppet::Error" do
166     let(:default_params) { Hash.new }
167     let(:facts) { Hash.new }
168     it { expect { should raise_error(Puppet::Error) } }
169     let(:facts) { { :lsbdistcodename => 'lucid', :lsbdistid => 'Ubuntu' } }
170     it { should contain_apt__source(title) }
171   end
172 end