(#12823) Add apt::key defined type and modify apt::source to use it
[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       :key_content        => false,
17       :key_source         => false,
18       :pin                => false
19     }
20   end
21
22   [{},
23    {
24       :location           => 'somewhere',
25       :release            => 'precise',
26       :repos              => 'security',
27       :include_src        => false,
28       :required_packages  => 'apache',
29       :key                => 'key_name',
30       :key_server         => 'keyserver.debian.com',
31       :pin                => '600',
32       :key_content        => 'ABCD1234'
33     },
34     {
35       :key                => 'key_name',
36       :key_server         => 'keyserver.debian.com',
37       :key_content        => false,
38     }
39   ].each do |param_set|
40     describe "when #{param_set == {} ? "using default" : "specifying"} class parameters" do
41       let :param_hash do
42         default_params.merge(param_set)
43       end
44
45       let :params do
46         param_set
47       end
48
49       let :filename do
50         "/etc/apt/sources.list.d/#{title}.list"
51       end
52
53       let :content do
54         content = "# #{title}"
55         content << "\ndeb #{param_hash[:location]} #{param_hash[:release]} #{param_hash[:repos]}\n"
56         if param_hash[:include_src]
57           content << "deb-src #{param_hash[:location]} #{param_hash[:release]} #{param_hash[:repos]}\n"
58         end
59         content
60       end
61
62       it { should contain_apt__params }
63
64       it { should contain_file("#{title}.list").with({
65           'path'      => filename,
66           'ensure'    => "file",
67           'owner'     => "root",
68           'group'     => "root",
69           'mode'      => 644,
70           'content'   => content
71         })
72       }
73
74       it {
75         if param_hash[:pin]
76           should contain_apt__pin(param_hash[:release]).with({
77             "priority"  => param_hash[:pin],
78             "before"    => "File[#{title}.list]"
79           })
80         else
81           should_not contain_apt__pin(param_hash[:release]).with({
82             "priority"  => param_hash[:pin],
83             "before"    => "File[#{title}.list]"
84           })
85         end
86       }
87
88       it {
89         should contain_exec("#{title} apt update").with({
90           "command"     => "/usr/bin/apt-get update",
91           "subscribe"   => "File[#{title}.list]",
92           "refreshonly" => true
93         })
94       }
95
96       it {
97         if param_hash[:required_packages]
98           should contain_exec("Required packages: '#{param_hash[:required_packages]}' for #{title}").with({
99             "command" => "/usr/bin/apt-get -y install #{param_hash[:required_packages]}",
100             "subscribe"   => "File[#{title}.list]",
101             "refreshonly" => true
102           })
103         else
104           should_not contain_exec("Required packages: '#{param_hash[:required_packages]}' for #{title}").with({
105             "command"     => "/usr/bin/apt-get -y install #{param_hash[:required_packages]}",
106             "subscribe"   => "File[#{title}.list]",
107             "refreshonly" => true
108           })
109         end
110       }
111
112       it {
113         if param_hash[:key]
114           should contain_apt__key("Add key: #{param_hash[:key]} from Apt::Source #{title}").with({
115             "key"         => param_hash[:key],
116             "ensure"      => :present,
117             "key_server"  => param_hash[:key_server],
118             "key_content" => param_hash[:key_content],
119             "key_source"  => param_hash[:key_source],
120             "before"      => "File[#{title}.list]"
121           })
122         else
123           should_not contain_apt__key("Add key: #{param_hash[:key]} from Apt::Source #{title}").with({
124             "key"         => param_hash[:key],
125             "ensure"      => :present,
126             "key_server"  => param_hash[:key_server],
127             "key_content" => param_hash[:key_content],
128             "key_source"  => param_hash[:key_source],
129             "before"      => "File[#{title}.list]"
130           })
131         end
132       }
133     end
134   end
135 end
136