(maint) reduce run time for acceptance testing
[puppet-modules/puppetlabs-apt.git] / spec / unit / puppet / provider / apt_key_spec.rb
1 require 'spec_helper'
2
3 describe Puppet::Type.type(:apt_key).provider(:apt_key) do
4   describe 'instances' do
5     it 'has an instance method' do
6       expect(described_class).to respond_to :instances
7     end
8   end
9
10   describe 'prefetch' do
11     it 'has a prefetch method' do
12       expect(described_class).to respond_to :prefetch
13     end
14   end
15
16   context 'self.instances no key' do
17     before :each do
18       allow(described_class).to receive(:apt_key).with(
19         ['adv', '--no-tty', '--list-keys', '--with-colons', '--fingerprint', '--fixed-list-mode'],
20       ).and_return('uid:-::::1284991450::07BEBE04F4AE4A8E885A761325717D8509D9C1DC::Ubuntu Extras Archive Automatic Signing Key <ftpmaster@ubuntu.com>::::::::::0:')
21     end
22     it 'returns no resources' do
23       expect(described_class.instances.size).to eq(0)
24     end
25   end
26
27   context 'self.instances multiple keys' do
28     before :each do
29       command_output = <<-OUTPUT
30 Executing: gpg --ignore-time-conflict --no-options --no-default-keyring --homedir /tmp/tmp.DU0GdRxjmE --no-auto-check-trustdb --trust-model always --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg --keyring /etc/apt/trusted.gpg.d/puppetlabs-pc1-keyring.gpg --no-tty --list-keys --with-colons --fingerprint --fixed-list-mode
31 tru:t:1:1549900774:0:3:1:5
32 pub:-:1024:17:40976EAF437D05B5:1095016255:::-:::scESC:
33 fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:
34 uid:-::::1095016255::B84AE656F4F5A826C273A458512EF8E282754CE1::Ubuntu Archive Automatic Signing Key <ftpmaster@ubuntu.com>:
35 sub:-:2048:16:251BEFF479164387:1095016263::::::e:
36 pub:-:1024:17:46181433FBB75451:1104433784:::-:::scSC:
37 fpr:::::::::C5986B4F1257FFA86632CBA746181433FBB75451:
38 OUTPUT
39       allow(described_class).to receive(:apt_key).with(
40         ['adv', '--no-tty', '--list-keys', '--with-colons', '--fingerprint', '--fixed-list-mode'],
41       ).and_return(command_output)
42     end
43     it 'returns 2 resources' do
44       expect(described_class.instances.size).to eq(2)
45       expect(described_class.instances[0].name).to eq('630239CC130E1A7FD81A27B140976EAF437D05B5')
46       expect(described_class.instances[0].id).to eq('40976EAF437D05B5')
47       expect(described_class.instances[1].name).to eq('C5986B4F1257FFA86632CBA746181433FBB75451')
48       expect(described_class.instances[1].id).to eq('46181433FBB75451')
49     end
50   end
51
52   context 'create apt_key resource' do
53     it 'apt_key with content set and source nil' do
54       expect(described_class).to receive(:apt_key).with(['adv', '--no-tty',
55                                                          '--keyserver',
56                                                          :"keyserver.ubuntu.com",
57                                                          '--recv-keys',
58                                                          'C105B9DE'])
59       resource = Puppet::Type::Apt_key.new(name: 'source and content nil',
60                                            id: 'C105B9DE',
61                                            ensure: 'present')
62
63       provider = described_class.new(resource)
64       expect(provider).not_to be_exist
65       provider.create
66       expect(provider).to be_exist
67     end
68
69     it 'apt_key content and source nil, options set' do
70       expect(described_class).to receive(:apt_key).with(['adv', '--no-tty',
71                                                          '--keyserver',
72                                                          :"keyserver.ubuntu.com",
73                                                          '--keyserver-options',
74                                                          'jimno',
75                                                          '--recv-keys',
76                                                          'C105B9DE'])
77       resource = Puppet::Type::Apt_key.new(name: 'source and content nil',
78                                            id: 'C105B9DE',
79                                            options: 'jimno',
80                                            ensure: 'present')
81
82       provider = described_class.new(resource)
83       expect(provider).not_to be_exist
84       provider.create
85       expect(provider).to be_exist
86     end
87
88     it 'apt_key with content set' do
89       expect(described_class).to receive(:apt_key).with(array_including('add', kind_of(String)))
90       resource = Puppet::Type::Apt_key.new(name: 'gsd',
91                                            id: 'C105B9DE',
92                                            content: 'asad',
93                                            ensure: 'present')
94
95       provider = described_class.new(resource)
96       expect(provider).not_to be_exist
97       expect(provider).to receive(:tempfile).and_return(Tempfile.new('foo'))
98       provider.create
99       expect(provider).to be_exist
100     end
101
102     it 'apt_key with source set' do
103       expect(described_class).to receive(:apt_key).with(array_including('add', kind_of(String)))
104       resource = Puppet::Type::Apt_key.new(name: 'gsd',
105                                            id: 'C105B9DE',
106                                            source: 'ftp://bla/herpderp.gpg',
107                                            ensure: 'present')
108
109       provider = described_class.new(resource)
110       expect(provider).not_to be_exist
111       expect(provider).to receive(:source_to_file).and_return(Tempfile.new('foo'))
112       provider.create
113       expect(provider).to be_exist
114     end
115
116     describe 'different valid id keys' do
117       hash_of_keys = {
118         '32bit key id' => 'EF8D349F',
119         '64bit key id' => '7F438280EF8D349F',
120         '160bit key fingerprint' => '6F6B15509CF8E59E6E469F327F438280EF8D349F',
121         '32bit key id lowercase' =>   'EF8D349F'.downcase,
122         '64bit key id lowercase' =>   '7F438280EF8D349F'.downcase,
123         '160bit key fingerprint lowercase' => '6F6B15509CF8E59E6E469F327F438280EF8D349F'.downcase,
124         '32bit key id 0x formatted' =>   '0xEF8D349F',
125         '64bit key id 0x formatted' =>   '0x7F438280EF8D349F',
126         '160bit key fingerprint 0x formatted' => '0x6F6B15509CF8E59E6E469F327F438280EF8D349F',
127       }
128       hash_of_keys.each do |key_type, value|
129         it "#{key_type} #{value} is valid" do
130           expect(described_class).to receive(:apt_key).with(array_including('adv', '--no-tty',
131                                                                             '--keyserver',
132                                                                             :"keyserver.ubuntu.com",
133                                                                             '--recv-keys'))
134           resource = Puppet::Type::Apt_key.new(name: 'source and content nil',
135                                                id: value,
136                                                ensure: 'present')
137
138           provider = described_class.new(resource)
139           expect(provider).not_to be_exist
140           provider.create
141           expect(provider).to be_exist
142         end
143       end
144     end
145
146     it 'apt_key with invalid key length' do
147       expect {
148         Puppet::Type::Apt_key.new(name: 'source and content nil',
149                                   id: '1',
150                                   ensure: 'present')
151       }.to raise_error(Puppet::ResourceError, %r{Parameter id failed on Apt_key})
152     end
153   end
154
155   context 'key_line_hash function' do
156     it 'matches rsa' do
157       expect(described_class.key_line_hash('pub:-:1024:1:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
158         key_expiry: nil,
159         key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
160         key_long: '40976EAF437D05B5',
161         key_short: '437D05B5',
162         key_size: '1024',
163         key_type: :rsa,
164       )
165     end
166
167     it 'matches dsa' do
168       expect(described_class.key_line_hash('pub:-:1024:17:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
169         key_expiry: nil,
170         key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
171         key_long: '40976EAF437D05B5',
172         key_short: '437D05B5',
173         key_size: '1024',
174         key_type: :dsa,
175       )
176     end
177
178     it 'matches ecc' do
179       expect(described_class.key_line_hash('pub:-:1024:18:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
180         key_expiry: nil,
181         key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
182         key_long: '40976EAF437D05B5',
183         key_short: '437D05B5',
184         key_size: '1024',
185         key_type: :ecc,
186       )
187     end
188
189     it 'matches ecdsa' do
190       expect(described_class.key_line_hash('pub:-:1024:19:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
191         key_expiry: nil,
192         key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
193         key_long: '40976EAF437D05B5',
194         key_short: '437D05B5',
195         key_size: '1024',
196         key_type: :ecdsa,
197       )
198     end
199   end
200 end