]> review.fuel-infra Code Review - puppet-modules/puppetlabs-apt.git/commitdiff
Fix more glitchy tests
authorDavid Schmitt <david.schmitt@puppet.com>
Mon, 25 Sep 2017 09:17:31 +0000 (10:17 +0100)
committerDavid Schmitt <david.schmitt@puppet.com>
Mon, 25 Sep 2017 12:30:43 +0000 (13:30 +0100)
spec/acceptance/apt_key_provider_spec.rb
spec/unit/puppet/provider/apt_key2/apt_key2_spec.rb

index 5df5e0aa3714f37c52886c38735bf6d183e120c7..0deeb154177398984bb4e1ee72b174b5d055f9b7 100644 (file)
@@ -26,6 +26,10 @@ def check_key(fingerprint)
   expect(shell_ex("apt-key adv --list-keys --with-colons --fingerprint | grep #{fingerprint}").exit_code).to eq 0
 end
 
+def check_key_absent(fingerprint)
+  expect(shell_ex("apt-key adv --list-keys --with-colons --fingerprint | grep #{fingerprint}").exit_code).not_to eq 0
+end
+
 %w[apt_key2 apt_key].each do |typename|
   describe typename do
     fedora = {
@@ -60,6 +64,21 @@ end
         puppet_resource_should_show('size', '4096')
         puppet_resource_should_show('type', ':?rsa')
       end
+
+      it 'can be removed' do
+        pp = <<-EOS
+          #{typename} { 'fedora':
+            id     => '#{fedora[:fingerprint]}',
+            ensure => 'absent',
+          }
+        EOS
+
+        # Time to remove it using Puppet
+        execute_manifest(pp, trace: true, catch_failures: true)
+        check_key_absent(fedora[:fingerprint])
+        execute_manifest(pp, trace: true, catch_changes: true)
+        check_key_absent(fedora[:fingerprint])
+      end
     end
 
     describe 'default options' do
@@ -103,115 +122,40 @@ end
       end
     end
 
-    describe 'ensure =>' do
-      context 'absent' do
-        it 'is removed' do
-          pp = <<-EOS
-        #{typename} { 'centos':
-          id     => '#{CENTOS_GPG_KEY_LONG_ID}',
-          ensure => 'absent',
-        }
-        EOS
-
-          # Install the key first (retry because key pool may timeout)
-          retry_on_error_matching(MAX_TIMEOUT_RETRY, TIMEOUT_RETRY_WAIT, TIMEOUT_ERROR_MATCHER) do
-            shell_ex("apt-key adv --keyserver hkps.pool.sks-keyservers.net \
-                --recv-keys #{CENTOS_GPG_KEY_FINGERPRINT}")
-          end
-          shell_ex(CENTOS_KEY_CHECK_COMMAND)
-
-          # Time to remove it using Puppet
-          apply_manifest(pp, catch_failures: true)
-          apply_manifest(pp, catch_changes: true)
-
-          shell_ex(CENTOS_KEY_CHECK_COMMAND,
-                   acceptable_exit_codes: [1])
-
-          # Re-Install the key (retry because key pool may timeout)
-          retry_on_error_matching(MAX_TIMEOUT_RETRY, TIMEOUT_RETRY_WAIT, TIMEOUT_ERROR_MATCHER) do
-            shell_ex("apt-key adv --keyserver hkps.pool.sks-keyservers.net \
-                  --recv-keys #{CENTOS_GPG_KEY_FINGERPRINT}")
-          end
-        end
-      end
-
-      context 'absent, added with long key' do
-        it 'is removed' do
-          pp = <<-EOS
-        #{typename} { 'puppetlabs':
-          id     => '#{PUPPETLABS_GPG_KEY_LONG_ID}',
-          ensure => 'absent',
-        }
-        EOS
-
-          # Install the key first (retry because key pool may timeout)
-          retry_on_error_matching(MAX_TIMEOUT_RETRY, TIMEOUT_RETRY_WAIT, TIMEOUT_ERROR_MATCHER) do
-            shell_ex("apt-key adv --keyserver hkps.pool.sks-keyservers.net \
-                  --recv-keys #{PUPPETLABS_GPG_KEY_LONG_ID}")
-          end
-
-          shell_ex(PUPPETLABS_KEY_CHECK_COMMAND)
-
-          # Time to remove it using Puppet
-          apply_manifest(pp, catch_failures: true)
-          apply_manifest(pp, catch_changes: true)
-
-          shell_ex(PUPPETLABS_KEY_CHECK_COMMAND,
-                   acceptable_exit_codes: [1])
-        end
-      end
-    end
-
-    describe 'content =>' do
-      context 'puppetlabs gpg key' do
-        it 'works' do
-          pp = <<-EOS
-          #{typename} { 'puppetlabs':
-            id      => '#{PUPPETLABS_GPG_KEY_FINGERPRINT}',
-            ensure  => 'present',
-            content => "#{my_fixture_read('puppetlabs_key.gpg')}",
-          }
-        EOS
-
-          # Apply the manifest (Retry if timeout error is received from key pool)
-          retry_on_error_matching(MAX_TIMEOUT_RETRY, TIMEOUT_RETRY_WAIT, TIMEOUT_ERROR_MATCHER) do
-            apply_manifest(pp, catch_failures: true)
-          end
-
-          apply_manifest(pp, catch_changes: true)
-          shell_ex(PUPPETLABS_KEY_CHECK_COMMAND)
+    fdescribe 'content =>' do
+      context 'multiple keys' do
+        after(:each) do
+          shell_ex("apt-key del #{PUPPETLABS_GPG_KEY_FINGERPRINT} > /dev/null")
         end
-      end
 
-      context 'multiple keys' do
         it 'runs without errors' do
           pp = <<-EOS
-          #{typename} { 'puppetlabs':
-            id      => '#{PUPPETLABS_GPG_KEY_FINGERPRINT}',
-            ensure  => 'present',
-            content => "#{my_fixture_read('puppetlabs_multiple.gpg')}",
-          }
-        EOS
+            #{typename} { 'puppetlabs':
+              id      => '#{PUPPETLABS_GPG_KEY_FINGERPRINT}',
+              ensure  => 'present',
+              content => "#{my_fixture_read('puppetlabs_multiple.gpg')}",
+            }
+          EOS
 
-          apply_manifest(pp, catch_failures: true)
-          apply_manifest(pp, catch_changes: true)
-          shell_ex(PUPPETLABS_KEY_CHECK_COMMAND)
+          execute_manifest(pp, trace: true, catch_failures: true)
+          check_key(PUPPETLABS_GPG_KEY_FINGERPRINT)
+          execute_manifest(pp, trace: true, catch_changes: true)
+          check_key(PUPPETLABS_GPG_KEY_FINGERPRINT)
         end
       end
 
       context 'bogus key' do
         it 'fails' do
           pp = <<-EOS
-        #{typename} { 'puppetlabs':
-          id      => '#{PUPPETLABS_GPG_KEY_LONG_ID}',
-          ensure  => 'present',
-          content => 'For posterity: such content, much bogus, wow',
-        }
-        EOS
+            #{typename} { 'puppetlabs':
+              id      => '#{PUPPETLABS_GPG_KEY_LONG_ID}',
+              ensure  => 'present',
+              content => 'For posterity: such content, much bogus, wow',
+            }
+          EOS
 
-          apply_manifest(pp, expect_failures: true) do |r|
-            expect(r.stderr).to match(%r{no valid OpenPGP data found})
-          end
+          result = execute_manifest(pp, trace: true, expect_failures: true)
+          expect(result.stderr).to match(%r{no valid OpenPGP data found})
         end
       end
     end
index 1b1b378f5a9b974fc2a92cfc181174d0f9203edd..619541c5f023014d96bcfe9ccce2cca737b54b66 100644 (file)
@@ -196,6 +196,7 @@ EOS
         allow(key_tempfile).to receive(:path).with(no_args).and_return('tempfilename')
         allow(key_tempfile).to receive(:close)
         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')