7c7ae399e84768fe3d51daca47c67a49c35b8855
[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 = "#file generated by puppet\n"
78         if param_hash[:comment]
79           content << "# #{comment}"
80         else
81           content << "# #{title}"
82         end
83         if param_hash[:architecture]
84           arch = "[arch=#{param_hash[:architecture]}] "
85         end
86         content << "\ndeb #{arch}#{param_hash[:location]} #{param_hash[:release]} #{param_hash[:repos]}\n"
87
88         if param_hash[:include_src]
89           content << "deb-src #{arch}#{param_hash[:location]} #{param_hash[:release]} #{param_hash[:repos]}\n"
90         end
91         content
92       end
93
94       it { should contain_apt__params }
95
96       it { should contain_file("#{title}.list").with({
97           'ensure'    => param_hash[:ensure],
98           'path'      => filename,
99           'owner'     => 'root',
100           'group'     => 'root',
101           'mode'      => '0644',
102           'content'   => content,
103         })
104       }
105
106       it {
107         if param_hash[:pin]
108           should contain_apt__pin(title).with({
109             "priority"  => param_hash[:pin],
110             "before"    => "File[#{title}.list]"
111           })
112         else
113           should_not contain_apt__pin(title).with({
114             "priority"  => param_hash[:pin],
115             "before"    => "File[#{title}.list]"
116           })
117         end
118       }
119
120       it {
121         should contain_exec("apt_update").with({
122           "command"     => "/usr/bin/apt-get update",
123           "refreshonly" => true
124         })
125       }
126
127       it {
128         if param_hash[:required_packages]
129           should contain_exec("Required packages: '#{param_hash[:required_packages]}' for #{title}").with({
130             "command" => "/usr/bin/apt-get -y install #{param_hash[:required_packages]}",
131             "subscribe"   => "File[#{title}.list]",
132             "refreshonly" => true,
133             "before"      => 'Exec[apt_update]',
134           })
135         else
136           should_not contain_exec("Required packages: '#{param_hash[:required_packages]}' for #{title}").with({
137             "command"     => "/usr/bin/apt-get -y install #{param_hash[:required_packages]}",
138             "subscribe"   => "File[#{title}.list]",
139             "refreshonly" => true
140           })
141         end
142       }
143
144       it {
145         key_server  = param_hash[:key_server]  || nil
146         key_content = param_hash[:key_content] || nil
147         key_source  = param_hash[:key_source]  || nil
148         if param_hash[:key]
149           should contain_apt__key("Add key: #{param_hash[:key]} from Apt::Source #{title}").with({
150             "key"         => param_hash[:key],
151             "ensure"      => :present,
152             "key_server"  => key_server,
153             "key_content" => key_content,
154             "key_source"  => key_source,
155             "before"      => "File[#{title}.list]"
156           })
157         else
158           should_not contain_apt__key("Add key: #{param_hash[:key]} from Apt::Source #{title}").with({
159             "key"         => param_hash[:key],
160             "ensure"      => :present,
161             "key_server"  => param_hash[:key_server],
162             "key_content" => param_hash[:key_content],
163             "key_source"  => param_hash[:key_source],
164             "before"      => "File[#{title}.list]"
165           })
166         end
167       }
168     end
169   end
170   describe "without release should raise a Puppet::Error" do
171     let(:default_params) { Hash.new }
172     let(:facts) { Hash.new }
173     it { expect { should raise_error(Puppet::Error) } }
174     let(:facts) { { :lsbdistcodename => 'lucid', :lsbdistid => 'Ubuntu' } }
175     it { should contain_apt__source(title) }
176   end
177 end