]> review.fuel-infra Code Review - puppet-modules/puppetlabs-apt.git/commitdiff
Change get to not use the Commands API for now
authorDavid Schmitt <david.schmitt@puppet.com>
Thu, 21 Sep 2017 10:05:16 +0000 (11:05 +0100)
committerDavid Schmitt <david.schmitt@puppet.com>
Thu, 21 Sep 2017 10:05:16 +0000 (11:05 +0100)
lib/puppet/provider/apt_key2/apt_key2.rb
spec/unit/puppet/provider/apt_key2/apt_key2_spec.rb

index 5d0e740af864e8694d5e98c0418edcf76e804c19..3b6cd911fd2e5cf51bdac19dd920858ae60816a3 100644 (file)
@@ -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)
index 5175bd000e162df8a36eed90b84f34025847575f..f98e03148dfe949f1d93138ddffc57ed896b21e6 100644 (file)
@@ -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',