require 'puppet/resource_api'
require 'tempfile'
+# Implementation for the apt_key type using the Resource API.
class Puppet::Provider::AptKey2::AptKey2
def initialize
@apt_key_cmd = Puppet::ResourceApi::Command.new 'apt-key'
end
end
- def get(context)
+ def get(_context)
pub_line = nil
fpr_line = nil
- @apt_key_cmd.start_read(context, 'adv', '--list-keys', '--with-colons', '--fingerprint', '--fixed-list-mode') do |process|
- process.io.stdout.each_line.map { |line|
- line = line.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '').strip
- if line.start_with?('pub')
- pub_line = line
- elsif line.start_with?('fpr')
- fpr_line = line
- end
- # puts "debug: parsing #{line}; fpr: #{fpr_line.inspect}; pub: #{pub_line.inspect}"
+ # result = @apt_key_cmd.run(
+ # context,
+ # 'adv', '--list-keys', '--with-colons', '--fingerprint', '--fixed-list-mode',
+ # stdout_destination: :capture,
+ # stderr_destination: :discard
+ # )
+ # lines = result.stdout
+ `apt-key adv --list-keys --with-colons --fingerprint --fixed-list-mode 2>/dev/null`.each_line.map { |line|
+ line = line.strip
+ if line.start_with?('pub')
+ pub_line = line
+ elsif line.start_with?('fpr')
+ fpr_line = line
+ end
+ # puts "debug: parsing #{line}; fpr: #{fpr_line.inspect}; pub: #{pub_line.inspect}"
- next unless pub_line && fpr_line
+ next unless pub_line && fpr_line
- # puts "debug: key_line_to_hash"
+ # puts "debug: key_line_to_hash"
- hash = self.class.key_line_to_hash(pub_line, fpr_line)
+ hash = self.class.key_line_to_hash(pub_line, fpr_line)
- # reset scanning
- pub_line = nil
- fpr_line = nil
+ # reset scanning
+ pub_line = nil
+ fpr_line = nil
- hash
- }.compact!
- end
+ hash
+ }.compact!
end
def self.key_line_to_hash(pub_line, fpr_line)
RSpec.describe Puppet::Provider::AptKey2::AptKey2 do
subject(:provider) { described_class.new }
- let(:context) { instance_double('context') }
+ let(:context) { instance_double('Puppet::ResourceApi::BaseContext') }
describe '#canonicalize(resources)' do
it('works with empty inputs') { expect(provider.canonicalize([])).to eq [] }
allow(process).to receive(:io).and_return(io)
end
- it 'processes input' do # rubocop:ignore RSpec/ExampleLength
- expect(apt_key_cmd).to receive(:start_read).with(context, any_args).and_yield(process)
- expect(io).to receive(:stdout).and_return(stdout)
+ it 'processes input' do
+ # expect(apt_key_cmd).to receive(:run).with(context, any_args).and_yield(process)
+ # expect(io).to receive(:stdout).and_return(stdout)
+ expect(provider).to receive(:`).with('apt-key adv --list-keys --with-colons --fingerprint --fixed-list-mode 2>/dev/null').and_return(stdout) # rubocop:disable RSpec/SubjectStub
expect(provider.get(context)).to eq [
{ ensure: 'present',
id: '126C0D24BD8A2942CC7DF8AC7638D0442B90D010',