From 7d2e1cf1c74fbc1a0b359f8138b921c26c6ab045 Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Mon, 25 Sep 2017 10:53:15 +0100 Subject: [PATCH] Undo nested context experiment --- lib/puppet/provider/apt_key2/apt_key2.rb | 24 +++++++++---------- .../puppet/provider/apt_key2/apt_key2_spec.rb | 21 +++++++--------- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/lib/puppet/provider/apt_key2/apt_key2.rb b/lib/puppet/provider/apt_key2/apt_key2.rb index 4fe4692..ff810b4 100644 --- a/lib/puppet/provider/apt_key2/apt_key2.rb +++ b/lib/puppet/provider/apt_key2/apt_key2.rb @@ -145,8 +145,8 @@ class Puppet::Provider::AptKey2::AptKey2 # end end - def create(global_context, title, should, noop = false) - global_context.creating(title) do |context| + def create(context, name, should, noop = false) + context.creating(name) do if should[:source].nil? && should[:content].nil? # Breaking up the command like this is needed because it blows up # if --recv-keys isn't the last argument. @@ -157,7 +157,7 @@ class Puppet::Provider::AptKey2::AptKey2 args.push('--recv-keys', should[:name]) @apt_key_cmd.run(context, *args, noop: noop) elsif should[:content] - temp_key_file(context, title, should[:content]) do |key_file| + temp_key_file(context, name, should[:content]) do |key_file| # @apt_key_cmd.run(context, 'add', key_file, noop: noop) # require'pry';binding.pry # puts key_file @@ -169,33 +169,33 @@ class Puppet::Provider::AptKey2::AptKey2 apt_key('add', key_file.path, noop: noop) # In case we really screwed up, better safe than sorry. else - context.fail("an unexpected condition occurred while trying to add the key: #{title} (content: #{should[:content].inspect}, source: #{should[:source].inspect})") + context.fail("an unexpected condition occurred while trying to add the key: #{name} (content: #{should[:content].inspect}, source: #{should[:source].inspect})") end end end - def delete(global_context, title, noop = false) - global_context.deleting(title) do |context| - @apt_key_cmd.run(context, 'del', title, noop: noop) + def delete(context, name, noop = false) + context.deleting(name) do + @apt_key_cmd.run(context, 'del', name, noop: noop) end end # This method writes out the specified contents to a temporary file and # confirms that the fingerprint from the file, matches the long key that is in the manifest - def temp_key_file(context, title, content) + def temp_key_file(context, name, content) file = Tempfile.new('apt_key') begin file.write content file.close if File.executable? '/usr/bin/gpg' - extracted_keys = `/usr/bin/gpg --with-fingerprint --with-colons #{file.path}`.each_line.select { |line| line =~ %r{^fpr:} }.map { |fpr| fpr.split(':')[9] } + extracted_keys = `/usr/bin/gpg --with-fingerprint --with-colons #{file.path}`.each_line.select { |line| line =~ %r{^fpr:} }.map { |fpr| fpr.strip.split(':')[9] } - if extracted_keys.include? title + if extracted_keys.include? name context.debug('Fingerprint verified against extracted key') - elsif extracted_keys.any? { |k| k =~ %r{#{title}$} } + elsif extracted_keys.any? { |k| k =~ %r{#{name}$} } context.debug('Fingerprint matches the extracted key') else - raise ArgumentError, "The fingerprint in your manifest (#{title}) and the fingerprint from content/source (#{extracted_keys.inspect}) do not match. "\ + raise ArgumentError, "The fingerprint in your manifest (#{name}) and the fingerprint from content/source (#{extracted_keys.inspect}) do not match. "\ ' Please check there is not an error in the name or check the content/source is legitimate.' end else 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 bc984c3..5b4aaf4 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('Puppet::ResourceApi::BaseContext') } + let(:context) { instance_double('Puppet::ResourceApi::BaseContext', 'context') } let(:key_list) do < { is: nil, @@ -188,11 +186,10 @@ EOS end context 'when adding a key from a string' do - let(:creating_ctx) { instance_double('Puppet::ResourceApi::BaseContext', 'creating_ctx') } let(:key_tempfile) { instance_double('Tempfile') } it 'updates the system' do - expect(context).to receive(:creating).with(fingerprint).and_yield(creating_ctx) + expect(context).to receive(:creating).with(fingerprint).and_yield expect(Tempfile).to receive(:new).with('apt_key').and_return(key_tempfile) expect(key_tempfile).to receive(:write).with('public gpg key block') allow(key_tempfile).to receive(:path).with(no_args).and_return('tempfilename') @@ -200,9 +197,9 @@ EOS expect(key_tempfile).to receive(:unlink) expect(File).to receive(:executable?).with('/usr/bin/gpg').and_return(true) expect(provider).to receive(:`).with('/usr/bin/gpg --with-fingerprint --with-colons tempfilename').and_return("\nfpr:::::::::#{fingerprint}:\n") # rubocop:disable RSpec/SubjectStub - expect(creating_ctx).to receive(:debug).with('Fingerprint verified against extracted key') + expect(context).to receive(:debug).with('Fingerprint verified against extracted key') - # expect(apt_key_cmd).to receive(:run).with(creating_ctx, 'add', 'tempfilename', noop: false).and_return 0 + # expect(apt_key_cmd).to receive(:run).with(context, 'add', 'tempfilename', noop: false).and_return 0 expect(provider).to receive(:system).with('apt-key add tempfilename') # rubocop:disable RSpec/SubjectStub provider.set(context, fingerprint => { @@ -217,11 +214,9 @@ EOS end context 'when deleting a key' do - let(:deleting_ctx) { instance_double('Puppet::ResourceApi::BaseContext', 'deleting_ctx') } - it 'updates the system' do - expect(context).to receive(:deleting).with(fingerprint).and_yield(deleting_ctx) - expect(apt_key_cmd).to receive(:run).with(deleting_ctx, 'del', fingerprint, noop: false).and_return 0 + expect(context).to receive(:deleting).with(fingerprint).and_yield + expect(apt_key_cmd).to receive(:run).with(context, 'del', fingerprint, noop: false).and_return 0 provider.set(context, fingerprint => { is: { -- 2.45.2