From 0361da7d5647340b999dc4e4df0e8bc3f03b24cc Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Thu, 21 Sep 2017 11:05:16 +0100 Subject: [PATCH] Change get to not use the Commands API for now --- lib/puppet/provider/apt_key2/apt_key2.rb | 44 +++++++++++-------- .../puppet/provider/apt_key2/apt_key2_spec.rb | 9 ++-- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/lib/puppet/provider/apt_key2/apt_key2.rb b/lib/puppet/provider/apt_key2/apt_key2.rb index 5d0e740..3b6cd91 100644 --- a/lib/puppet/provider/apt_key2/apt_key2.rb +++ b/lib/puppet/provider/apt_key2/apt_key2.rb @@ -3,6 +3,7 @@ require 'open-uri' 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' @@ -19,33 +20,38 @@ class Puppet::Provider::AptKey2::AptKey2 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) diff --git a/spec/unit/puppet/provider/apt_key2/apt_key2_spec.rb b/spec/unit/puppet/provider/apt_key2/apt_key2_spec.rb index 5175bd0..f98e031 100644 --- a/spec/unit/puppet/provider/apt_key2/apt_key2_spec.rb +++ b/spec/unit/puppet/provider/apt_key2/apt_key2_spec.rb @@ -7,7 +7,7 @@ require 'puppet/provider/apt_key2/apt_key2' 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 [] } @@ -80,9 +80,10 @@ EOS 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', -- 2.45.2