(#13125) Apt keys should be case insensitive
[puppet-modules/puppetlabs-apt.git] / spec / defines / key_spec.rb
1 require 'spec_helper'
2 describe 'apt::key', :type => :define do
3   let :title do
4     '8347A27F'
5   end
6
7   let :default_params do
8     {
9       :key         => title,
10       :ensure      => 'present',
11       :key_server  => "keyserver.ubuntu.com",
12       :key_source  => false,
13       :key_content => false
14     }
15   end
16
17   [{},
18     {
19       :ensure  => 'absent'
20     },
21     {
22       :ensure  => 'random'
23     },
24     {
25       :key_source => 'ftp://ftp.example.org/key',
26     },
27     {
28       :key_content => 'deadbeef',
29     }
30   ].each do |param_set|
31
32     let :param_hash do
33       param_hash = default_params.merge(param_set)
34       param_hash[:key].upcase! if param_hash[:key]
35       param_hash
36     end
37
38     let :params do
39       param_set
40     end
41
42     let :digest do
43       str = String.new
44       str << param_hash[:key].to_s         << '/'
45       str << param_hash[:key_content].to_s << '/'
46       str << param_hash[:key_source].to_s  << '/'
47       str << param_hash[:key_server].to_s  << '/'
48       Digest::SHA1.hexdigest(str)
49     end
50
51     describe "when #{param_set == {} ? "using default" : "specifying"} define parameters" do
52
53       it {
54         if [:present, 'present', :absent, 'absent'].include? param_hash[:ensure]
55           should contain_apt__params
56         end
57       }
58
59       it {
60         if [:present, 'present'].include? param_hash[:ensure]
61           should_not contain_exec("apt::key #{param_hash[:key]} absent")
62           should contain_anchor("apt::key #{param_hash[:key]} present")
63           should contain_exec(digest).with({
64             "path"    => "/bin:/usr/bin",
65             "unless"  => "/usr/bin/apt-key list | /bin/grep '#{param_hash[:key]}'"
66           })
67         elsif [:absent, 'absent'].include? param_hash[:ensure]
68           should_not contain_anchor("apt::key #{param_hash[:key]} present")
69           should contain_exec("apt::key #{param_hash[:key]} absent").with({
70             "path"    => "/bin:/usr/bin",
71             "onlyif"  => "apt-key list | grep '#{param_hash[:key]}'",
72             "command" => "apt-key del '#{param_hash[:key]}'"
73           })
74         else
75           expect { should raise_error(Puppet::Error) }
76         end
77       }
78
79       it {
80         if [:present, 'present'].include? param_hash[:ensure]
81           if param_hash[:key_content]
82             should contain_exec(digest).with({
83               "command" => "echo '#{param_hash[:key_content]}' | /usr/bin/apt-key add -"
84             })
85           elsif param_hash[:key_source]
86             should contain_exec(digest).with({
87               "command" => "wget -q '#{param_hash[:key_source]}' -O- | apt-key add -"
88             })
89           elsif param_hash[:key_server]
90             should contain_exec(digest).with({
91               "command" => "apt-key adv --keyserver '#{param_hash[:key_server]}' --recv-keys '#{param_hash[:key]}'"
92             })
93           end
94         end
95       }
96
97     end
98   end
99
100   [{ :ensure => 'present' }, { :ensure => 'absent' }].each do |param_set|
101     describe "should correctly handle duplicate definitions" do
102
103       let :pre_condition do
104         "apt::key { 'duplicate': key => '#{title}'; }"
105       end
106
107       let(:params) { param_set }
108
109       it {
110         if param_set[:ensure] == 'present'
111           should contain_anchor("apt::key #{title} present")
112           should contain_apt__key(title)
113           should contain_apt__key("duplicate")
114         elsif param_set[:ensure] == 'absent'
115           expect { should raise_error(Puppet::Error) }
116         end
117       }
118
119     end
120   end
121
122 end
123