01949b60ec8dcdc57a268744aac285d87eba543e
[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   ].each do |param_set|
34     describe "when #{param_set == {} ? "using default" : "specifying"} class parameters" do
35       let :param_hash do
36         default_params.merge(param_set)
37       end
38
39       let :params do
40         param_set
41       end
42
43       let :filename do
44         "/etc/apt/sources.list.d/#{title}.list"
45       end
46
47       let :content do
48         content = "# #{title}"
49         content << "\ndeb #{param_hash[:location]} #{param_hash[:release]} #{param_hash[:repos]}\n"
50         if param_hash[:include_src]
51           content << "deb-src #{param_hash[:location]} #{param_hash[:release]} #{param_hash[:repos]}\n"
52         end
53         content
54       end
55
56       it { should contain_apt__params }
57
58       it { should contain_file("#{title}.list").with({
59           'path' => filename,
60           'ensure' => "file",
61           'owner'   => "root",
62           'group'     => "root",
63           'mode'      => 644,
64           'content'   => content
65         })
66       }
67
68       it {
69         if param_hash[:pin]
70           should contain_apt__pin(param_hash[:release]).with({
71             "priority"  => param_hash[:pin],
72             "before"    => "File[#{title}.list]"
73           })
74         else
75           should_not contain_apt__pin(param_hash[:release]).with({
76             "priority"  => param_hash[:pin],
77             "before"    => "File[#{title}.list]"
78           })
79         end
80       }
81
82       it {
83         should contain_exec("#{title} apt update").with({
84           "command"     => "/usr/bin/apt-get update",
85           "subscribe"   => "File[#{title}.list]",
86           "refreshonly" => true
87         })
88       }
89
90       it {
91         if param_hash[:required_packages]
92           should contain_exec("/usr/bin/apt-get -y install #{param_hash[:required_packages]}").with({
93             "subscribe"   => "File[#{title}.list]",
94             "refreshonly" => true
95           })
96         else
97           should_not contain_exec("/usr/bin/apt-get -y install #{param_hash[:required_packages]}").with({
98             "subscribe"   => "File[#{title}.list]",
99             "refreshonly" => true
100           })
101         end
102       }
103
104       it {
105         if param_hash[:key]
106           if param_hash[:key_content]
107             should contain_exec("Add key: #{param_hash[:key]} from content").with({
108               "command" => "/bin/echo '#{param_hash[:key_content]}' | /usr/bin/apt-key add -",
109               "unless"  => "/usr/bin/apt-key list | /bin/grep '#{param_hash[:key]}'",
110               "before"  => "File[#{title}.list]"
111             })
112             should_not contain_exec("/usr/bin/apt-key adv --keyserver #{param_hash[:key_server]} --recv-keys #{param_hash[:key]}").with({
113               "unless"  => "/usr/bin/apt-key list | /bin/grep #{param_hash[:key]}",
114               "before"  => "File[#{title}.list]"
115             })
116
117           else
118             should contain_exec("/usr/bin/apt-key adv --keyserver #{param_hash[:key_server]} --recv-keys #{param_hash[:key]}").with({
119                 "unless"  => "/usr/bin/apt-key list | /bin/grep #{param_hash[:key]}",
120                 "before"  => "File[#{title}.list]"
121               })
122             should_not contain_exec("Add key: #{param_hash[:key]} from content").with({
123                 "command" => "/bin/echo '#{param_hash[:key_content]}' | /usr/bin/apt-key add -",
124                 "unless"  => "/usr/bin/apt-key list | /bin/grep '#{param_hash[:key]}'",
125                 "before"  => "File[#{title}.list]"
126               })
127           end
128         else
129           should_not contain_exec("Add key: #{param_hash[:key]} from content").with({
130             "command"   => "/bin/echo '#{param_hash[:key_content]}' | /usr/bin/apt-key add -",
131             "unless"    => "/usr/bin/apt-key list | /bin/grep '#{param_hash[:key]}'",
132             "before"    => "File[#{title}.list]"
133           })
134           should_not contain_exec("/usr/bin/apt-key adv --keyserver #{param_hash[:key_server]} --recv-keys #{param_hash[:key]}").with({
135               "unless"  => "/usr/bin/apt-key list | /bin/grep #{param_hash[:key]}",
136               "before"  => "File[#{title}.list]"
137           })
138
139         end
140       }
141     end
142   end
143 end
144