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