Merge pull request #24 from haus/fix_source_key_tests
[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 :params do
45         param_set
46       end
47
48       let :filename do
49         "/etc/apt/sources.list.d/#{title}.list"
50       end
51
52       let :content do
53         content = "# #{title}"
54         content << "\ndeb #{param_hash[:location]} #{param_hash[:release]} #{param_hash[:repos]}\n"
55         if param_hash[:include_src]
56           content << "deb-src #{param_hash[:location]} #{param_hash[:release]} #{param_hash[:repos]}\n"
57         end
58         content
59       end
60
61       it { should contain_apt__params }
62
63       it { should contain_file("#{title}.list").with({
64           'path'      => filename,
65           'ensure'    => "file",
66           'owner'     => "root",
67           'group'     => "root",
68           'mode'      => 644,
69           'content'   => content
70         })
71       }
72
73       it {
74         if param_hash[:pin]
75           should contain_apt__pin(param_hash[:release]).with({
76             "priority"  => param_hash[:pin],
77             "before"    => "File[#{title}.list]"
78           })
79         else
80           should_not contain_apt__pin(param_hash[:release]).with({
81             "priority"  => param_hash[:pin],
82             "before"    => "File[#{title}.list]"
83           })
84         end
85       }
86
87       it {
88         should contain_exec("#{title} apt update").with({
89           "command"     => "/usr/bin/apt-get update",
90           "subscribe"   => "File[#{title}.list]",
91           "refreshonly" => true
92         })
93       }
94
95       it {
96         if param_hash[:required_packages]
97           should contain_exec("/usr/bin/apt-get -y install #{param_hash[:required_packages]}").with({
98             "subscribe"   => "File[#{title}.list]",
99             "refreshonly" => true
100           })
101         else
102           should_not contain_exec("/usr/bin/apt-get -y install #{param_hash[:required_packages]}").with({
103             "subscribe"   => "File[#{title}.list]",
104             "refreshonly" => true
105           })
106         end
107       }
108
109       it {
110         if param_hash[:key]
111           if param_hash[:key_content]
112             should contain_exec("Add key: #{param_hash[:key]} from content for #{title}").with({
113               "command" => "/bin/echo '#{param_hash[:key_content]}' | /usr/bin/apt-key add -",
114               "unless"  => "/usr/bin/apt-key list | /bin/grep '#{param_hash[:key]}'",
115               "before"  => "File[#{title}.list]"
116             })
117           else
118             should_not 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           end
124         else
125           should_not contain_exec("Add key: #{param_hash[:key]} from content for #{title}").with({
126             "command"   => "/bin/echo '#{param_hash[:key_content]}' | /usr/bin/apt-key add -",
127             "unless"    => "/usr/bin/apt-key list | /bin/grep '#{param_hash[:key]}'",
128             "before"    => "File[#{title}.list]"
129           })
130         end
131       }
132
133       it {
134         if param_hash[:key]
135           if param_hash[:key_content]
136             should_not contain_exec("Add key: #{param_hash[:key]} from #{param_hash[:key_server]} for #{title}").with({
137                 "command" => "/usr/bin/apt-key adv --keyserver #{param_hash[:key_server]} --recv-keys #{param_hash[:key]}",
138                 "unless"  => "/usr/bin/apt-key list | /bin/grep #{param_hash[:key]}",
139                 "before"  => "File[#{title}.list]"
140             })
141           else
142             should 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           end
148         else
149           should_not contain_exec("Add key: #{param_hash[:key]} from #{param_hash[:key_server]} for #{title}").with({
150               "command" => "/usr/bin/apt-key adv --keyserver #{param_hash[:key_server]} --recv-keys #{param_hash[:key]}",
151               "unless"  => "/usr/bin/apt-key list | /bin/grep #{param_hash[:key]}",
152               "before"  => "File[#{title}.list]"
153           })
154         end
155       }
156     end
157   end
158 end
159