]> review.fuel-infra Code Review - puppet-modules/puppetlabs-apt.git/commitdiff
source: Support complex pin, like key does.
authorDaniele Sluijters <daenney@users.noreply.github.com>
Mon, 6 Apr 2015 22:02:58 +0000 (18:02 -0400)
committerMorgan Haskel <morgan@puppetlabs.com>
Thu, 9 Apr 2015 22:44:20 +0000 (15:44 -0700)
This adds support for passing in a full pin declaration as the pin
parameter on a source declaration.

It keeps the old behaviour intact, you can still simply do `pin => '10'`
and it will pin on origin with a priority of 10.

Should that not be what you want you can now pass in a full pin
declaration instead. We make no assumptions here, whatever you pass in
will be passed through to pin as-is with the exception of the values for
`ensure` and `before` which are always overridden by us to ensure
everything keeps working as designed.

manifests/source.pp
spec/defines/source_spec.rb

index 163a411bb21f426f1ae1f43aeeffc4cd4c188464..24d0ae4b8dff795d874ee1b588170ed544e0eced 100644 (file)
@@ -8,7 +8,7 @@ define apt::source(
   $repos          = 'main',
   $include        = {},
   $key            = undef,
-  $pin            = false,
+  $pin            = undef,
   $architecture   = undef,
   $allow_unsigned = false,
 ) {
@@ -44,17 +44,22 @@ define apt::source(
     content => template('apt/_header.erb', 'apt/source.list.erb'),
   }
 
-  if ($pin != false) {
-    # Get the host portion out of the url so we can pin to origin
-    $url_split = split($location, '/')
-    $host      = $url_split[2]
-
-    apt::pin { $name:
-      ensure   => $ensure,
-      priority => $pin,
-      before   => $_before,
-      origin   => $host,
+  if $pin {
+    if is_hash($pin) {
+      $_pin = merge($pin, { 'ensure' => $ensure, 'before' => $_before })
+    } elsif (is_numeric($pin) or is_string($pin)) {
+      $url_split = split($location, '/')
+      $host      = $url_split[2]
+      $_pin = {
+        'ensure'   => $ensure,
+        'priority' => $pin,
+        'before'   => $_before,
+        'origin'   => $host,
+      }
+    } else {
+      fail('Received invalid value for pin parameter')
     }
+    create_resources('apt::pin', { $name => $_pin })
   }
 
   # We do not want to remove keys when the source is absent.
index 7fd86b56ff0ff35e98ff8a4e5abd90fdfb31c13b..3900158a1612cc48b8995195c40e5dae655515d0 100644 (file)
@@ -51,6 +51,31 @@ describe 'apt::source' do
         :osfamily        => 'Debian'
       }
     end
+
+    context 'with complex pin' do
+      let :params do
+        {
+          :location => 'hello.there',
+          :pin      => { 'release'     => 'wishwash',
+                         'explanation' => 'wishwash',
+                         'priority'    => 1001, },
+        }
+      end
+
+      it { is_expected.to contain_apt__setting('list-my_source').with({
+        :ensure => 'present',
+      }).with_content(/hello.there wheezy main\n/)
+      }
+
+      it { is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with({
+        :ensure       => 'present',
+        :priority     => 1001,
+        :explanation  => 'wishwash',
+        :release      => 'wishwash',
+      })
+      }
+    end
+
     context 'with simple key' do
       let :params do
         {
@@ -235,5 +260,28 @@ describe 'apt::source' do
         }.to raise_error(Puppet::Error, /lsbdistcodename fact not available: release parameter required/)
       end
     end
+
+    context 'invalid pin' do
+      let :facts do
+        {
+          :lsbdistid       => 'Debian',
+          :lsbdistcodename => 'wheezy',
+          :osfamily        => 'Debian'
+        }
+      end
+      let :params do
+        {
+          :location => 'hello.there',
+          :pin      => true,
+        }
+      end
+
+      it do
+        expect {
+          is_expected.to compile
+        }.to raise_error(Puppet::Error, /invalid value for pin/)
+      end
+    end
+
   end
 end