]> review.fuel-infra Code Review - puppet-modules/puppetlabs-apt.git/commitdiff
apt: Allow managing of preferences file.
authorDaniele Sluijters <github@daenney.net>
Sat, 22 Feb 2014 09:56:50 +0000 (10:56 +0100)
committerDaniele Sluijters <github@daenney.net>
Sat, 22 Feb 2014 10:00:22 +0000 (11:00 +0100)
We already had a feature to manage and purge entries in preferences.d
but not the preferences file in /etc/apt. This commit adds that
capability.

Fixes #199

manifests/init.pp
spec/acceptance/apt_spec.rb
spec/classes/apt_spec.rb

index b106ad490979ce67522382eb02f54a9ce7a1a15d..2de6aa056ff24740e7588146251e95665643c1ef 100644 (file)
@@ -29,6 +29,7 @@ class apt(
   $proxy_port           = '8080',
   $purge_sources_list   = false,
   $purge_sources_list_d = false,
+  $purge_preferences    = false,
   $purge_preferences_d  = false,
   $update_timeout       = undef
 ) {
@@ -36,13 +37,21 @@ class apt(
   include apt::params
   include apt::update
 
-  validate_bool($purge_sources_list, $purge_sources_list_d, $purge_preferences_d)
+  validate_bool($purge_sources_list, $purge_sources_list_d,
+                $purge_preferences, $purge_preferences_d)
 
   $sources_list_content = $purge_sources_list ? {
     false => undef,
     true  => "# Repos managed by puppet.\n",
   }
 
+  $preferences_content = $purge_preferences ? {
+    false => undef,
+    true  => "Explanation: Preferences managed by Puppet\n
+Explanation: We need a bogus package line because of Debian Bug #732746\n
+Package: bogus-package\n",
+  }
+
   if $always_apt_update == true {
     Exec <| title=='apt_update' |> {
       refreshonly => false,
@@ -75,6 +84,15 @@ class apt(
     notify  => Exec['apt_update'],
   }
 
+  file { 'apt-preferences':
+    ensure  => present,
+    path    => "${root}/preferences",
+    owner   => root,
+    group   => root,
+    mode    => '0644',
+    content => $preferences_content,
+  }
+
   file { 'preferences.d':
     ensure  => directory,
     path    => $preferences_d,
index 35a0d3e70b20ae17f3ed1e26413da5f502ba8245..336161cc6d8b8a21a2baed6b732427cc9661caa7 100644 (file)
@@ -175,6 +175,50 @@ describe 'apt class' do
     end
   end
 
+  context 'purge_preferences' do
+    context 'false' do
+      it 'creates a preferences file' do
+        shell("echo 'original' > /etc/apt/preferences")
+      end
+
+      it 'should work with no errors' do
+        pp = <<-EOS
+        class { 'apt': purge_preferences => false }
+        EOS
+
+        apply_manifest(pp, :catch_failures => true)
+      end
+
+      describe file('/etc/apt/preferences') do
+        it { should be_file }
+        it 'is not managed by Puppet' do
+          shell("grep 'original' /etc/apt/preferences", {:acceptable_exit_codes => 0})
+        end
+      end
+    end
+
+    context 'true' do
+      it 'creates a preferences file' do
+        shell('touch /etc/apt/preferences')
+      end
+
+      it 'should work with no errors' do
+        pp = <<-EOS
+        class { 'apt': purge_preferences => true }
+        EOS
+
+        apply_manifest(pp, :catch_failures => true)
+      end
+
+      describe file('/etc/apt/preferences') do
+        it { should be_file }
+        it 'is managed by Puppet' do
+          shell("grep 'Explanation' /etc/apt/preferences", {:acceptable_exit_codes => 0})
+        end
+      end
+    end
+  end
+
   context 'purge_preferences_d' do
     context 'false' do
       it 'creates a preferences file' do
index 6d87cc64dccecb4e5aa2071c89bcbfd300e27756..5d8a597b9088ba3df2e5be02f5b2df29cc09d4a3 100644 (file)
@@ -18,6 +18,10 @@ describe 'apt', :type => :class do
       :purge_sources_list => true,
       :purge_sources_list_d => true,
     },
+    {
+      :purge_preferences   => true,
+      :purge_preferences_d => true,
+    },
     {
       :disable_keys => false
     }
@@ -85,6 +89,49 @@ describe 'apt', :type => :class do
           })
         end
       }
+      it {
+        if param_hash[:purge_preferences]
+          should create_file('apt-preferences').with({
+            :ensure  => 'present',
+            :path    => '/etc/apt/preferences',
+            :owner   => 'root',
+            :group   => 'root',
+            :mode    => '0644',
+            :content => /Explanation/,
+          })
+        else
+          should create_file('apt-preferences').with({
+            :ensure  => 'present',
+            :path    => '/etc/apt/preferences',
+            :owner   => 'root',
+            :group   => 'root',
+            :mode    => '0644',
+            :content => nil,
+          })
+        end
+      }
+
+      it {
+        if param_hash[:purge_preferences_d]
+          should create_file("preferences.d").with({
+            'path'    => "/etc/apt/preferences.d",
+            'ensure'  => "directory",
+            'owner'   => "root",
+            'group'   => "root",
+            'purge'   => true,
+            'recurse' => true,
+          })
+        else
+          should create_file("preferences.d").with({
+            'path'    => "/etc/apt/preferences.d",
+            'ensure'  => "directory",
+            'owner'   => "root",
+            'group'   => "root",
+            'purge'   => false,
+            'recurse' => false,
+          })
+        end
+      }
 
       it {
         should contain_exec("apt_update").with({