Merge pull request #32 from pdxcat/convert_apt__key_to_use_anchors
[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       default_params.merge(param_set)
34     end
35
36     let :params do
37       param_set
38     end
39
40     let :digest do
41       str = String.new
42       str << param_hash[:key].to_s         << '/'
43       str << param_hash[:key_content].to_s << '/'
44       str << param_hash[:key_source].to_s  << '/'
45       str << param_hash[:key_server].to_s  << '/'
46       Digest::SHA1.hexdigest(str)
47     end
48
49     describe "when #{param_set == {} ? "using default" : "specifying"} define parameters" do
50
51       it {
52         if [:present, 'present', :absent, 'absent'].include? param_hash[:ensure]
53           should contain_apt__params
54         end
55       }
56
57       it {
58         if [:present, 'present'].include? param_hash[:ensure]
59           should_not contain_exec("apt::key #{param_hash[:key]} absent")
60           should contain_anchor("apt::key #{param_hash[:key]} present")
61           should contain_exec(digest).with({
62             "path"    => "/bin:/usr/bin",
63             "unless"  => "/usr/bin/apt-key list | /bin/grep '#{param_hash[:key]}'"
64           })
65         elsif [:absent, 'absent'].include? param_hash[:ensure]
66           should_not contain_anchor("apt::key #{param_hash[:key]} present")
67           should contain_exec("apt::key #{param_hash[:key]} absent").with({
68             "path"    => "/bin:/usr/bin",
69             "onlyif"  => "apt-key list | grep '#{param_hash[:key]}'",
70             "command" => "apt-key del '#{param_hash[:key]}'"
71           })
72         else
73           expect { should raise_error(Puppet::Error) }
74         end
75       }
76
77       it {
78         if [:present, 'present'].include? param_hash[:ensure]
79           if param_hash[:key_content]
80             should contain_exec(digest).with({
81               "command" => "echo '#{param_hash[:key_content]}' | /usr/bin/apt-key add -"
82             })
83           elsif param_hash[:key_source]
84             should contain_exec(digest).with({
85               "command" => "wget -q '#{param_hash[:key_source]}' -O- | apt-key add -"
86             })
87           elsif param_hash[:key_server]
88             should contain_exec(digest).with({
89               "command" => "apt-key adv --keyserver '#{param_hash[:key_server]}' --recv-keys '#{param_hash[:key]}'"
90             })
91           end
92         end
93       }
94
95     end
96   end
97
98   [{ :ensure => 'present' }, { :ensure => 'absent' }].each do |param_set|
99     describe "should correctly handle duplicate definitions" do
100
101       let :pre_condition do
102         "apt::key { 'duplicate': key => '#{title}'; }"
103       end
104
105       let(:params) { param_set }
106
107       it {
108         if param_set[:ensure] == 'present'
109           should contain_anchor("apt::key #{title} present")
110           should contain_apt__key(title)
111           should contain_apt__key("duplicate")
112         elsif param_set[:ensure] == 'absent'
113           expect { should raise_error(Puppet::Error) }
114         end
115       }
116
117     end
118   end
119
120 end
121