]> review.fuel-infra Code Review - puppet-modules/puppetlabs-apt.git/commitdiff
Start setting up new-style unit tests; convert get() to new API
authorDavid Schmitt <david.schmitt@puppet.com>
Mon, 11 Sep 2017 17:47:17 +0000 (18:47 +0100)
committerDavid Schmitt <david.schmitt@puppet.com>
Mon, 11 Sep 2017 17:47:17 +0000 (18:47 +0100)
lib/puppet/provider/apt_key2/apt_key2.rb
spec/spec_helper_new.rb [new file with mode: 0644]
spec/unit/puppet/provider/apt_key2/apt_key2_spec.rb [new file with mode: 0644]

index b5066f0bc802bb2f5a52b5ba52dc00a7d72d40af..4f5c1ba01e0f155c5363768bccdcf3377d42dd1f 100644 (file)
@@ -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 (file)
index 0000000..6ae38e4
--- /dev/null
@@ -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 (file)
index 0000000..b80c485
--- /dev/null
@@ -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(<<EOS
+Executing: /tmp/apt-key-gpghome.4VkaIao1Ca/gpg.1.sh --list-keys --with-colons --fingerprint --fixed-list-mode
+tru:t:1:1505150630:0:3:1:5
+pub:-:4096:1:7638D0442B90D010:1416603673:1668891673::-:::scSC:::::::
+rvk:::1::::::309911BEA966D0613053045711B4E5FF15B0FD82:80:
+rvk:::1::::::FBFABDB541B5DC955BD9BA6EDB16CF5BB12525C4:80:
+rvk:::1::::::80E976F14A508A48E9CA3FE9BC372252CA1CF964:80:
+fpr:::::::::126C0D24BD8A2942CC7DF8AC7638D0442B90D010:
+uid:-::::1416603673::15C761B84F0C9C293316B30F007E34BE74546B48::Debian Archive Automatic Signing Key (8/jessie) <ftpmaster@debian.org>:
+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) <ftpmaster@debian.org>:
+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