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