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