]> review.fuel-infra Code Review - puppet-modules/puppetlabs-apt.git/commitdiff
Undo nested context experiment
authorDavid Schmitt <david.schmitt@puppet.com>
Mon, 25 Sep 2017 09:53:15 +0000 (10:53 +0100)
committerDavid Schmitt <david.schmitt@puppet.com>
Mon, 25 Sep 2017 12:30:43 +0000 (13:30 +0100)
lib/puppet/provider/apt_key2/apt_key2.rb
spec/unit/puppet/provider/apt_key2/apt_key2_spec.rb

index 4fe469260d45269b9f311834d6d05264f2bcc1a4..ff810b42ba094ab6ec4e6c14c8d8ed9fb818876b 100644 (file)
@@ -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
index bc984c344d13bf55219898ebaafae016f9d56c2e..5b4aaf449f021d36a564f0d3b3dd26f885368714 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('Puppet::ResourceApi::BaseContext') }
+  let(:context) { instance_double('Puppet::ResourceApi::BaseContext', 'context') }
   let(:key_list) do
     <<EOS
 Executing: /tmp/apt-key-gpghome.4VkaIao1Ca/gpg.1.sh --list-keys --with-colons --fingerprint --fixed-list-mode
@@ -170,11 +170,9 @@ EOS
     end
 
     context 'when fetching a key from the keyserver' do
-      let(:creating_ctx) { instance_double('Puppet::ResourceApi::BaseContext', 'creating_ctx') }
-
       it 'updates the system' do
-        expect(context).to receive(:creating).with(fingerprint).and_yield(creating_ctx)
-        expect(apt_key_cmd).to receive(:run).with(creating_ctx, 'adv', '--keyserver', 'keyserver.example.com', '--recv-keys', fingerprint, noop: false).and_return 0
+        expect(context).to receive(:creating).with(fingerprint).and_yield
+        expect(apt_key_cmd).to receive(:run).with(context, 'adv', '--keyserver', 'keyserver.example.com', '--recv-keys', fingerprint, noop: false).and_return 0
         provider.set(context, fingerprint =>
         {
           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: {