From 6ebe6bf46e93640b5d1341c51a3d1c37cd90bcad Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Mon, 11 Sep 2017 18:47:17 +0100 Subject: [PATCH] Start setting up new-style unit tests; convert get() to new API --- lib/puppet/provider/apt_key2/apt_key2.rb | 42 ++++++------- spec/spec_helper_new.rb | 41 +++++++++++++ .../puppet/provider/apt_key2/apt_key2_spec.rb | 60 +++++++++++++++++++ 3 files changed, 122 insertions(+), 21 deletions(-) create mode 100644 spec/spec_helper_new.rb create mode 100644 spec/unit/puppet/provider/apt_key2/apt_key2_spec.rb diff --git a/lib/puppet/provider/apt_key2/apt_key2.rb b/lib/puppet/provider/apt_key2/apt_key2.rb index b5066f0..4f5c1ba 100644 --- a/lib/puppet/provider/apt_key2/apt_key2.rb +++ b/lib/puppet/provider/apt_key2/apt_key2.rb @@ -7,7 +7,7 @@ class Puppet::Provider::AptKey2::AptKey2 def initialize @apt_key_cmd = Puppet::ResourceApi::Command.new 'apt-key' - @gpg_cmd = Puppet::ResourceApi::Command.new '/usr/bin/gpg' + @gpg_cmd = Puppet::ResourceApi::Command.new '/usr/bin/gpg' end def canonicalize(resources) @@ -20,35 +20,35 @@ class Puppet::Provider::AptKey2::AptKey2 end end - def get(_names = []) - cli_args = %w[adv --list-keys --with-colons --fingerprint --fixed-list-mode] - key_output = apt_key_lines(*cli_args) + def get(context) pub_line = nil fpr_line = nil - result = key_output.map { |line| - line = line.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '') - 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.start_read(context, %w[adv --list-keys --with-colons --fingerprint --fixed-list-mode]) do |handle| + handle.stdout.each_line.map { |line| + line = line.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '') + 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 = key_line_to_hash(pub_line, fpr_line) + hash = 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! + hash + }.compact! - result + result + end end def self.key_line_to_hash(pub_line, fpr_line) diff --git a/spec/spec_helper_new.rb b/spec/spec_helper_new.rb new file mode 100644 index 0000000..6ae38e4 --- /dev/null +++ b/spec/spec_helper_new.rb @@ -0,0 +1,41 @@ +if ENV['COVERAGE'] == 'yes' + require 'coveralls' + require 'simplecov' + require 'simplecov-console' + + SimpleCov.formatters = [ + SimpleCov::Formatter::HTMLFormatter, + # SimpleCov::Formatter::Console, + Coveralls::SimpleCov::Formatter, + ] + SimpleCov.start do + track_files 'lib/**/*.rb' + + add_filter '/spec' + + # do not track vendored files + add_filter '/vendor' + add_filter '/.vendor' + + # do not track gitignored files + # this adds about 4 seconds to the coverage check + # this could definitely be optimized + add_filter do |f| + # system returns true if exit status is 0, which with git-check-ignore means file is ignored + system("git check-ignore --quiet #{f.filename}") + end + end +end + +#This file is generated by ModuleSync, do not edit. +require 'puppetlabs_spec_helper/module_spec_helper' + +# put local configuration and setup into spec_helper_local +begin + require 'spec_helper_local' +rescue LoadError +end + +RSpec.configure do |c| + c.mock_with :rspec +end \ No newline at end of file diff --git a/spec/unit/puppet/provider/apt_key2/apt_key2_spec.rb b/spec/unit/puppet/provider/apt_key2/apt_key2_spec.rb new file mode 100644 index 0000000..b80c485 --- /dev/null +++ b/spec/unit/puppet/provider/apt_key2/apt_key2_spec.rb @@ -0,0 +1,60 @@ +require 'spec_helper_new' + +# TODO: needs some cleanup/helper to avoid this misery +module Puppet::Provider::AptKey2; end +require 'puppet/provider/apt_key2/apt_key2' + +RSpec.describe Puppet::Provider::AptKey2::AptKey2 do + subject(:provider) { described_class.new } + let(:context) { instance_double('context') } + + describe '#canonicalize(resources)' do + it('works with empty inputs') { expect(provider.canonicalize([])).to eq [] } + it('cleans up 0x hex numbers') { expect(provider.canonicalize([{ id: '0xabcd' }])).to eq [{ id: 'ABCD' }] } + it('upcases bare hex numbers alone') { expect(provider.canonicalize([{ id: 'abcd' }])).to eq [{ id: 'ABCD' }] } + it('leaves bare upper case hex numbers alone') { expect(provider.canonicalize([{ id: 'ABCD' }])).to eq [{ id: 'ABCD' }] } + it('handles multiple inputs') do + expect(provider.canonicalize([{ id: '0xabcd' }, + { id: 'abcd' }, + { id: 'ABCD' }])) + .to eq [{ id: 'ABCD' }, + { id: 'ABCD' }, + { id: 'ABCD' }] + end + it('handles invalid inputs') do + expect { provider.canonicalize([{ id: 'not a hex number' }]) }.not_to raise_error + end + end + + describe '#get' do + let(:apt_key_cmd) { instance_double('Puppet::ResourceApi::Command') } + let(:handle) { instance_double('handle') } + let(:stdout) do + StringIO.new(<: +pub:-:4096:1:9D6D8F6BC857C906:1416604417:1668892417::-:::scSC::::::: +rvk:::1::::::FBFABDB541B5DC955BD9BA6EDB16CF5BB12525C4:80: +rvk:::1::::::309911BEA966D0613053045711B4E5FF15B0FD82:80: +rvk:::1::::::80E976F14A508A48E9CA3FE9BC372252CA1CF964:80: +fpr:::::::::D21169141CECD440F2EB8DDA9D6D8F6BC857C906: +uid:-::::1416604417::088FA6B00E33BCC6F6EB4DFEFAC591F9940E06F0::Debian Security Archive Automatic Signing Key (8/jessie) : +EOS + ) + end + + it 'processes input' do + allow(Puppet::ResourceApi::Command).to receive(:new).and_return(apt_key_cmd) + expect(apt_key_cmd).to receive(:start_read).with(context, any_args).and_yield(handle) + expect(handle).to receive(:stdout).and_yield(stdout) + + expect { provider.get(context) }.not_to raise_error + end + end +end -- 2.45.2