]> review.fuel-infra Code Review - puppet-modules/puppetlabs-apt.git/commitdiff
Finish `get` method unit testing
authorDavid Schmitt <david.schmitt@puppet.com>
Tue, 12 Sep 2017 13:42:42 +0000 (14:42 +0100)
committerDavid Schmitt <david.schmitt@puppet.com>
Tue, 12 Sep 2017 13:42:42 +0000 (14:42 +0100)
lib/puppet/provider/apt_key2/apt_key2.rb
spec/unit/puppet/provider/apt_key2/apt_key2_spec.rb

index 4f5c1ba01e0f155c5363768bccdcf3377d42dd1f..1ee95781110a8883c611bd57cf215924cbeb5f84 100644 (file)
@@ -24,9 +24,9 @@ class Puppet::Provider::AptKey2::AptKey2
     pub_line   = nil
     fpr_line   = nil
 
-    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: '')
+    result = @apt_key_cmd.start_read(context, *%w[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')
@@ -38,7 +38,7 @@ class Puppet::Provider::AptKey2::AptKey2
 
         # puts "debug: key_line_to_hash"
 
-        hash = key_line_to_hash(pub_line, fpr_line)
+        hash = self.class.key_line_to_hash(pub_line, fpr_line)
 
         # reset scanning
         pub_line = nil
@@ -46,8 +46,6 @@ class Puppet::Provider::AptKey2::AptKey2
 
         hash
       }.compact!
-
-      result
     end
   end
 
@@ -80,8 +78,8 @@ class Puppet::Provider::AptKey2::AptKey2
       short:       fingerprint[-8..-1], # last 8 characters of fingerprint
       size:        pub_split[2].to_i,
       type:        key_type,
-      created:     Time.at(pub_split[5].to_i),
-      expiry:      expiry,
+      created:     Time.at(pub_split[5].to_i).to_s,
+      expiry:      expiry.to_s,
       expired:     !!(expiry && Time.now >= expiry),
     }
   end
index b80c4859791f792bd03195bd821afbf5c10595b5..9400de7b39e1f7f343f8c9860d78e162751fbbda 100644 (file)
@@ -6,7 +6,8 @@ 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('context') }
 
   describe '#canonicalize(resources)' do
     it('works with empty inputs') { expect(provider.canonicalize([])).to eq [] }
@@ -26,11 +27,37 @@ RSpec.describe Puppet::Provider::AptKey2::AptKey2 do
     end
   end
 
+  describe '.key_line_to_hash(pub, fpr)' do
+    subject(:result) { described_class.key_line_to_hash(pub, fpr) }
+
+    let(:pub) { "pub:-:4096:#{key_type}:7638D0442B90D010:1416603673:1668891673::-:::scSC:::::::" }
+    let(:fpr) { "fpr:::::::::#{id}:" }
+
+    let(:key_type) { :foo }
+
+    let(:short) { 'a' * 8 }
+    let(:long) { ('1' * 8) + short }
+    let(:id) { 'f' * (40 - 16) + long }
+
+    it('returns the id') { expect(result[:id]).to eq id }
+    it('returns the long id') { expect(result[:long]).to eq long }
+    it('returns the short id') { expect(result[:short]).to eq short }
+
+    [[1, :rsa], [17, :dsa], [18, :ecc], [19, :ecdsa], [:foo, :unrecognized]].each do |key_type, value|
+      context "with a key type of #{key_type}" do
+        let(:key_type) { key_type }
+
+        it("returns #{value.inspect} as key type") { expect(result[:type]).to eq value }
+      end
+    end
+  end
+
   describe '#get' do
     let(:apt_key_cmd) { instance_double('Puppet::ResourceApi::Command') }
-    let(:handle) { instance_double('handle') }
+    let(:process) { instance_double('ChildProcess::AbstractProcess') }
+    let(:io) { instance_double('ChildProcess::AbstractIO') }
     let(:stdout) do
-      StringIO.new(<<EOS
+      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:::::::
@@ -46,15 +73,38 @@ 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
+    before(:each) 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)
+      allow(process).to receive(:io).and_return(io)
+    end
 
-      expect { provider.get(context) }.not_to raise_error
+    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)
+      expect(provider.get(context)).to eq [
+        { ensure: 'present',
+          id: '126C0D24BD8A2942CC7DF8AC7638D0442B90D010',
+          fingerprint: '126C0D24BD8A2942CC7DF8AC7638D0442B90D010',
+          long: '7638D0442B90D010',
+          short: '2B90D010',
+          size: 4096,
+          type: :rsa,
+          created: '2014-11-21 21:01:13 +0000',
+          expiry: '2022-11-19 21:01:13 +0000',
+          expired: false },
+        { ensure: 'present',
+          id: 'D21169141CECD440F2EB8DDA9D6D8F6BC857C906',
+          fingerprint: 'D21169141CECD440F2EB8DDA9D6D8F6BC857C906',
+          long: '9D6D8F6BC857C906',
+          short: 'C857C906',
+          size: 4096,
+          type: :rsa,
+          created: '2014-11-21 21:13:37 +0000',
+          expiry: '2022-11-19 21:13:37 +0000',
+          expired: false },
+      ]
     end
   end
 end