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