Merge pull request #375 from raphink/dev/facts_perfs
authorDaniele Sluijters <daenney@users.noreply.github.com>
Thu, 16 Oct 2014 08:07:25 +0000 (10:07 +0200)
committerDaniele Sluijters <daenney@users.noreply.github.com>
Thu, 16 Oct 2014 08:07:25 +0000 (10:07 +0200)
Refactor facts to improve performance.

lib/facter/apt_package_updates.rb [deleted file]
lib/facter/apt_security_updates.rb [deleted file]
lib/facter/apt_updates.rb
spec/unit/facter/apt_has_updates_spec.rb [new file with mode: 0644]
spec/unit/facter/apt_package_updates_spec.rb
spec/unit/facter/apt_security_updates_spec.rb
spec/unit/facter/apt_updates_spec.rb

diff --git a/lib/facter/apt_package_updates.rb b/lib/facter/apt_package_updates.rb
deleted file mode 100644 (file)
index 97c75c6..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-Facter.add("apt_package_updates") do
-  confine :osfamily => 'Debian'
-  setcode do
-    if File.executable?("/usr/lib/update-notifier/apt-check")
-      packages = Facter::Util::Resolution.exec('/usr/lib/update-notifier/apt-check -p 2>&1')
-      packages = packages.split("\n")
-      if Facter.version < '2.0.0'
-        packages = packages.join(',')
-      end
-      packages
-    end
-  end
-end
diff --git a/lib/facter/apt_security_updates.rb b/lib/facter/apt_security_updates.rb
deleted file mode 100644 (file)
index 19bae75..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-Facter.add("apt_security_updates") do
-  confine :osfamily => 'Debian'
-  setcode do
-    if File.executable?("/usr/lib/update-notifier/apt-check")
-      updates = Facter::Util::Resolution.exec('/usr/lib/update-notifier/apt-check 2>&1')
-      Integer(updates.strip.split(';')[1])
-    end
-  end
-end
index ee177380c231939a12f0d22fd33dbacd4acd3f0c..75670bc39767280c0d8fbd2ddfa7121ded3ffe30 100644 (file)
@@ -1,9 +1,37 @@
-Facter.add("apt_updates") do
+apt_package_updates = nil
+Facter.add("apt_has_updates") do
   confine :osfamily => 'Debian'
+  if File.executable?("/usr/lib/update-notifier/apt-check")
+    apt_package_updates = Facter::Util::Resolution.exec('/usr/lib/update-notifier/apt-check 2>&1').split(';')
+  end
+
+  setcode do
+    apt_package_updates != ['0', '0'] unless apt_package_updates.nil?
+  end
+end
+
+Facter.add("apt_package_updates") do
+  confine :apt_has_updates => true
   setcode do
-    if File.executable?("/usr/lib/update-notifier/apt-check")
-      updates = Facter::Util::Resolution.exec('/usr/lib/update-notifier/apt-check 2>&1')
-      Integer(updates.strip.split(';')[0])
+    packages = Facter::Util::Resolution.exec('/usr/lib/update-notifier/apt-check -p 2>&1').split("\n")
+    if Facter.version < '2.0.0'
+      packages.join(',')
+    else
+      packages
     end
   end
 end
+
+Facter.add("apt_updates") do
+  confine :apt_has_updates => true
+  setcode do
+    Integer(apt_package_updates[0])
+  end
+end
+
+Facter.add("apt_security_updates") do
+  confine :apt_has_updates => true
+  setcode do
+    Integer(apt_package_updates[1])
+  end
+end
diff --git a/spec/unit/facter/apt_has_updates_spec.rb b/spec/unit/facter/apt_has_updates_spec.rb
new file mode 100644 (file)
index 0000000..9a444db
--- /dev/null
@@ -0,0 +1,34 @@
+require 'spec_helper'
+
+describe 'apt_has_updates fact' do
+  subject { Facter.fact(:apt_has_updates).value }
+  after(:each) { Facter.clear }
+
+  describe 'on non-Debian distro' do
+    before {
+      Facter.fact(:osfamily).expects(:value).returns 'RedHat'
+    }
+    it { should be_nil }
+  end
+
+  describe 'on Debian based distro missing update-notifier-common' do
+    before {
+      Facter.fact(:osfamily).expects(:value).returns 'Debian'
+      File.stubs(:executable?) # Stub all other calls
+      File.expects(:executable?).with('/usr/lib/update-notifier/apt-check').returns false
+    }
+    it { should be_nil }
+  end
+
+  describe 'on Debian based distro' do
+    before {
+      Facter.fact(:osfamily).expects(:value).returns 'Debian'
+      File.stubs(:executable?) # Stub all other calls
+      Facter::Util::Resolution.stubs(:exec) # Catch all other calls
+      File.expects(:executable?).with('/usr/lib/update-notifier/apt-check').returns true
+      Facter::Util::Resolution.expects(:exec).with('/usr/lib/update-notifier/apt-check 2>&1').returns "4;3"
+    }
+    it { should be true }
+  end
+end
+
index dfa09270eb59d050a08812d4984846c040494efd..5c7a624f4d7f2688ca367b62676870cfc35b70ce 100644 (file)
@@ -4,26 +4,28 @@ describe 'apt_package_updates fact' do
   subject { Facter.fact(:apt_package_updates).value }
   after(:each) { Facter.clear }
 
-  describe 'on Debian based distro missing update-notifier-common' do
+  describe 'when apt has no updates' do
     before { 
-    Facter.fact(:osfamily).stubs(:value).returns 'Debian'
-    File.stubs(:executable?).returns false
-  }
-  it { should == nil }
+      Facter.fact(:apt_has_updates).stubs(:value).returns false
+    }
+    it { should be nil }
   end
 
-  describe 'on Debian based distro' do
+  describe 'when apt has updates' do
     before { 
-    Facter.fact(:osfamily).stubs(:value).returns 'Debian'
-    File.stubs(:executable?).returns true
-    Facter::Util::Resolution.stubs(:exec).returns "puppet-common\nlinux-generic\nlinux-image-generic"
-  }
-  it {
-    if Facter.version < '2.0.0'
-      should == 'puppet-common,linux-generic,linux-image-generic'
-    else
-      should == ['puppet-common', 'linux-generic', 'linux-image-generic']
-    end
-  }
+      Facter.fact(:osfamily).stubs(:value).returns 'Debian'
+      File.stubs(:executable?) # Stub all other calls
+      Facter::Util::Resolution.stubs(:exec) # Catch all other calls
+      File.expects(:executable?).with('/usr/lib/update-notifier/apt-check').returns true
+      Facter::Util::Resolution.expects(:exec).with('/usr/lib/update-notifier/apt-check 2>&1').returns "1;2"
+      Facter::Util::Resolution.expects(:exec).with('/usr/lib/update-notifier/apt-check -p 2>&1').returns "puppet-common\nlinux-generic\nlinux-image-generic"
+    }
+    it {
+      if Facter.version < '2.0.0'
+        should == 'puppet-common,linux-generic,linux-image-generic'
+      else
+        should == ['puppet-common', 'linux-generic', 'linux-image-generic']
+      end
+    }
   end
 end
index 999603b6124d410d76882b4f2771523752e1411e..4bc760f7898ca406a20053fcb61917aa6879063f 100644 (file)
@@ -4,21 +4,22 @@ describe 'apt_security_updates fact' do
   subject { Facter.fact(:apt_security_updates).value }
   after(:each) { Facter.clear }
 
-  describe 'on Debian based distro missing update-notifier-common' do
+  describe 'when apt has no updates' do
     before { 
-    Facter.fact(:osfamily).stubs(:value).returns 'Debian'
-    File.stubs(:executable?).returns false
-  }
-  it { should == nil }
+      Facter.fact(:apt_has_updates).stubs(:value).returns false
+    }
+    it { should be nil }
   end
 
-  describe 'on Debian based distro' do
+  describe 'when apt has security updates' do
     before { 
-    Facter.fact(:osfamily).stubs(:value).returns 'Debian'
-    File.stubs(:executable?).returns true
-    Facter::Util::Resolution.stubs(:exec).returns '14;7'
-  }
-  it { should == 7 }
+      Facter.fact(:osfamily).stubs(:value).returns 'Debian'
+      File.stubs(:executable?) # Stub all other calls
+      Facter::Util::Resolution.stubs(:exec) # Catch all other calls
+      File.expects(:executable?).with('/usr/lib/update-notifier/apt-check').returns true
+      Facter::Util::Resolution.expects(:exec).with('/usr/lib/update-notifier/apt-check 2>&1').returns "14;7"
+    }
+    it { should == 7 }
   end
 
 end
index 2f343bd8a10dc1bca214fc1acd6a9e30da913ce2..7e9b77f84f695cecb108c9cc9897ea7254d411a6 100644 (file)
@@ -4,21 +4,22 @@ describe 'apt_updates fact' do
   subject { Facter.fact(:apt_updates).value }
   after(:each) { Facter.clear }
 
-  describe 'on Debian based distro missing update-notifier-common' do
+  describe 'when apt has no updates' do
     before { 
-    Facter.fact(:osfamily).stubs(:value).returns 'Debian'
-    File.stubs(:executable?).returns false
-  }
-  it { should == nil }
+      Facter.fact(:apt_has_updates).stubs(:value).returns false
+    }
+    it { should be nil }
   end
 
-  describe 'on Debian based distro' do
+  describe 'when apt has updates' do
     before { 
-    Facter.fact(:osfamily).stubs(:value).returns 'Debian'
-    File.stubs(:executable?).returns true
-    Facter::Util::Resolution.stubs(:exec).returns '14;7'
-  }
-  it { should == 14 }
+      Facter.fact(:osfamily).stubs(:value).returns 'Debian'
+      File.stubs(:executable?) # Stub all other calls
+      Facter::Util::Resolution.stubs(:exec) # Catch all other calls
+      File.expects(:executable?).with('/usr/lib/update-notifier/apt-check').returns true
+      Facter::Util::Resolution.expects(:exec).with('/usr/lib/update-notifier/apt-check 2>&1').returns "14;7"
+    }
+    it { should == 14 }
   end
 
 end