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