Fixed tests for cf04f76
[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     {
53       :architecture       => 'amd64',
54     }
55   ].each do |param_set|
56     describe "when #{param_set == {} ? "using default" : "specifying"} class parameters" do
57       let :param_hash do
58         default_params.merge(param_set)
59       end
60
61       let :facts do
62         {:lsbdistcodename => 'karmic'}
63       end
64
65       let :params do
66         param_set
67       end
68
69       let :filename do
70         "/etc/apt/sources.list.d/#{title}.list"
71       end
72
73       let :content do
74         content = "# #{title}"
75         if param_hash[:architecture]
76           arch = "[arch=#{param_hash[:architecture]}]"
77         end
78         content << "\ndeb #{arch}#{param_hash[:location]} #{param_hash[:release]} #{param_hash[:repos]}\n"
79
80         if param_hash[:include_src]
81           content << "deb-src #{arch}#{param_hash[:location]} #{param_hash[:release]} #{param_hash[:repos]}\n"
82         end
83         content
84       end
85
86       it { should contain_apt__params }
87
88       it { should contain_file("#{title}.list").with({
89           'ensure'    => param_hash[:ensure],
90           'path'      => filename,
91           'owner'     => 'root',
92           'group'     => 'root',
93           'mode'      => '0644',
94           'content'   => content,
95         })
96       }
97
98       it {
99         if param_hash[:pin]
100           should contain_apt__pin(title).with({
101             "priority"  => param_hash[:pin],
102             "before"    => "File[#{title}.list]"
103           })
104         else
105           should_not contain_apt__pin(title).with({
106             "priority"  => param_hash[:pin],
107             "before"    => "File[#{title}.list]"
108           })
109         end
110       }
111
112       it {
113         should contain_exec("apt_update").with({
114           "command"     => "/usr/bin/apt-get update",
115           "refreshonly" => true
116         })
117       }
118
119       it {
120         if param_hash[:required_packages]
121           should 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             "before"      => 'Exec[apt_update]',
126           })
127         else
128           should_not contain_exec("Required packages: '#{param_hash[:required_packages]}' for #{title}").with({
129             "command"     => "/usr/bin/apt-get -y install #{param_hash[:required_packages]}",
130             "subscribe"   => "File[#{title}.list]",
131             "refreshonly" => true
132           })
133         end
134       }
135
136       it {
137         if param_hash[:key]
138           should contain_apt__key("Add key: #{param_hash[:key]} from Apt::Source #{title}").with({
139             "key"         => param_hash[:key],
140             "ensure"      => :present,
141             "key_server"  => param_hash[:key_server],
142             "key_content" => param_hash[:key_content],
143             "key_source"  => param_hash[:key_source],
144             "before"      => "File[#{title}.list]"
145           })
146         else
147           should_not contain_apt__key("Add key: #{param_hash[:key]} from Apt::Source #{title}").with({
148             "key"         => param_hash[:key],
149             "ensure"      => :present,
150             "key_server"  => param_hash[:key_server],
151             "key_content" => param_hash[:key_content],
152             "key_source"  => param_hash[:key_source],
153             "before"      => "File[#{title}.list]"
154           })
155         end
156       }
157     end
158   end
159   describe "without release should raise a Puppet::Error" do
160     let(:default_params) { Hash.new }
161     let(:facts) { Hash.new }
162     it { expect { should raise_error(Puppet::Error) } }
163     let(:facts) { { :lsbdistcodename => 'lucid' } }
164     it { should contain_apt__source(title) }
165   end
166 end