]> review.fuel-infra Code Review - puppet-modules/puppetlabs-apt.git/commitdiff
(#14308) Add ensure=>absent for define resource.
authorNan Liu <nan@puppetlabs.com>
Thu, 3 May 2012 23:59:13 +0000 (16:59 -0700)
committerNan Liu <nan@puppetlabs.com>
Thu, 3 May 2012 23:59:13 +0000 (16:59 -0700)
Several apt::* define resource type does not support ensurable. This
update allows ensure=>absent to support the removal of these
configuration files.

* apt::conf
* apt::pin
* apt::source

manifests/conf.pp
manifests/pin.pp
manifests/source.pp
spec/defines/conf_spec.rb
spec/defines/pin_spec.rb
spec/defines/source_spec.rb

index fa7f97d435f6001c3bea06cf2f50e921f2a3f4fa..03aab8e00270fb40ca699d1e4e286a8dd949bb71 100644 (file)
@@ -1,4 +1,5 @@
 define apt::conf (
+  $ensure   = present,
   $priority = '50',
   $content
 ) {
@@ -8,7 +9,7 @@ define apt::conf (
   $apt_conf_d = $apt::params::apt_conf_d
 
   file { "${apt_conf_d}/${priority}${name}":
-    ensure  => file,
+    ensure  => $ensure,
     content => $content,
     owner   => root,
     group   => root,
index 2899fce77a568fad54f418f6943d7c5678269da0..b69e230ea077f1c31e16fa5791349ed196daf6f0 100644 (file)
@@ -2,6 +2,7 @@
 # pin a release in apt, useful for unstable repositories
 
 define apt::pin(
+  $ensure   = present,
   $packages = '*',
   $priority = 0
 ) {
@@ -11,7 +12,7 @@ define apt::pin(
   $preferences_d = $apt::params::preferences_d
 
   file { "${name}.pref":
-    ensure  => file,
+    ensure  => $ensure,
     path    => "${preferences_d}/${name}",
     owner   => root,
     group   => root,
index 17d2d8e5a2a2ad61f8bf58f5287edfd487dde70f..c63fc2fb2282d5bd2dc457a256442683747b6b13 100644 (file)
@@ -2,6 +2,7 @@
 # add an apt source
 
 define apt::source(
+  $ensure = present,
   $location = '',
   $release = $lsbdistcodename,
   $repos = 'main',
@@ -24,7 +25,7 @@ define apt::source(
   }
 
   file { "${name}.list":
-    ensure  => file,
+    ensure  => $ensure,
     path    => "${sources_list_d}/${name}.list",
     owner   => root,
     group   => root,
@@ -32,7 +33,7 @@ define apt::source(
     content => template("${module_name}/source.list.erb"),
   }
 
-  if $pin != false {
+  if ($pin != false) and ($ensure == 'present') {
     apt::pin { $release:
       priority => $pin,
       before   => File["${name}.list"]
@@ -45,7 +46,7 @@ define apt::source(
     refreshonly => true,
   }
 
-  if $required_packages != false {
+  if ($required_packages != false) and ($ensure == 'present') {
     exec { "Required packages: '${required_packages}' for ${name}":
       command     => "${provider} -y install ${required_packages}",
       subscribe   => File["${name}.list"],
@@ -53,7 +54,8 @@ define apt::source(
     }
   }
 
-  if $key != false {
+  # We do not want to remove keys when the source is absent.
+  if ($key != false) and ($ensure == 'present') {
     apt::key { "Add key: ${key} from Apt::Source ${title}":
       ensure      => present,
       key         => $key,
index 7c04db1c12883a53aa3b2a7e23e6c472723e10ba..5a81b5148c7d7bf489dabdef47012abe8d58a995 100644 (file)
@@ -23,7 +23,7 @@ describe 'apt::conf', :type => :define do
     }
 
     it { should contain_file(filename).with({
-          'ensure'    => 'file',
+          'ensure'    => 'present',
           'content'   => "Apt::Install-Recommends 0;\nApt::AutoRemove::InstallRecommends 1;\n",
           'owner'     => 'root',
           'group'     => 'root',
@@ -31,4 +31,27 @@ describe 'apt::conf', :type => :define do
         })
       }
   end
+
+  describe "when removing an apt preference" do
+    let :params do
+      {
+        :ensure   => 'absent',
+        :priority => '00',
+        :content  => "Apt::Install-Recommends 0;\nApt::AutoRemove::InstallRecommends 1;\n"
+      }
+    end
+
+    let :filename do
+      "/etc/apt/apt.conf.d/00norecommends"
+    end
+
+    it { should contain_file(filename).with({
+        'ensure'    => 'absent',
+        'content'   => "Apt::Install-Recommends 0;\nApt::AutoRemove::InstallRecommends 1;\n",
+        'owner'     => 'root',
+        'group'     => 'root',
+        'mode'      => '0644',
+      })
+    }
+  end
 end
index dbc595c568000caf7a6536356c9395fe17f3f617..c5d3fd91af03f3c159c1c25316943860e68e9e3c 100644 (file)
@@ -4,13 +4,19 @@ describe 'apt::pin', :type => :define do
 
   let :default_params do
     {
+      :ensure   => 'present',
       :packages => '*',
       :priority => '0'
     }
   end
 
-  [{},
-   {
+  [ {},
+    {
+      :packages  => 'apache',
+      :priority  => '1'
+    },
+    {
+      :ensure    => 'absent',
       :packages  => 'apache',
       :priority  => '1'
     }
@@ -27,7 +33,7 @@ describe 'apt::pin', :type => :define do
       it { should include_class("apt::params") }
 
       it { should contain_file("#{title}.pref").with({
-          'ensure'  => 'file',
+          'ensure'  => param_hash[:ensure],
           'path'    => "/etc/apt/preferences.d/#{title}",
           'owner'   => 'root',
           'group'   => 'root',
index 284342940b445d161e25851ea2dac321cb5dd679..02bc45fcbebd3256b0f8256b292be68611828adc 100644 (file)
@@ -6,6 +6,7 @@ describe 'apt::source', :type => :define do
 
   let :default_params do
     {
+      :ensure             => 'present',
       :location           => '',
       :release            => 'karmic',
       :repos              => 'main',
@@ -35,6 +36,12 @@ describe 'apt::source', :type => :define do
       :key                => 'key_name',
       :key_server         => 'keyserver.debian.com',
       :key_content        => false,
+    },
+    {
+      :ensure             => 'absent',
+      :location           => 'somewhere',
+      :release            => 'precise',
+      :repos              => 'security',
     }
   ].each do |param_set|
     describe "when #{param_set == {} ? "using default" : "specifying"} class parameters" do
@@ -66,7 +73,7 @@ describe 'apt::source', :type => :define do
       it { should contain_apt__params }
 
       it { should contain_file("#{title}.list").with({
-          'ensure'    => 'file',
+          'ensure'    => param_hash[:ensure],
           'path'      => filename,
           'owner'     => 'root',
           'group'     => 'root',