Merge pull request #375 from raphink/dev/facts_perfs

Refactor facts to improve performance.
This commit is contained in:
Daniele Sluijters 2014-10-16 10:07:25 +02:00
commit 5d96da0c6a
7 changed files with 109 additions and 65 deletions

View file

@ -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

View file

@ -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

View file

@ -1,9 +1,37 @@
Facter.add("apt_updates") do apt_package_updates = nil
Facter.add("apt_has_updates") do
confine :osfamily => 'Debian' confine :osfamily => 'Debian'
setcode do
if File.executable?("/usr/lib/update-notifier/apt-check") if File.executable?("/usr/lib/update-notifier/apt-check")
updates = Facter::Util::Resolution.exec('/usr/lib/update-notifier/apt-check 2>&1') apt_package_updates = Facter::Util::Resolution.exec('/usr/lib/update-notifier/apt-check 2>&1').split(';')
Integer(updates.strip.split(';')[0]) 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
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 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

View file

@ -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

View file

@ -4,19 +4,21 @@ describe 'apt_package_updates fact' do
subject { Facter.fact(:apt_package_updates).value } subject { Facter.fact(:apt_package_updates).value }
after(:each) { Facter.clear } after(:each) { Facter.clear }
describe 'on Debian based distro missing update-notifier-common' do describe 'when apt has no updates' do
before { before {
Facter.fact(:osfamily).stubs(:value).returns 'Debian' Facter.fact(:apt_has_updates).stubs(:value).returns false
File.stubs(:executable?).returns false
} }
it { should == nil } it { should be nil }
end end
describe 'on Debian based distro' do describe 'when apt has updates' do
before { before {
Facter.fact(:osfamily).stubs(:value).returns 'Debian' Facter.fact(:osfamily).stubs(:value).returns 'Debian'
File.stubs(:executable?).returns true File.stubs(:executable?) # Stub all other calls
Facter::Util::Resolution.stubs(:exec).returns "puppet-common\nlinux-generic\nlinux-image-generic" 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 { it {
if Facter.version < '2.0.0' if Facter.version < '2.0.0'

View file

@ -4,19 +4,20 @@ describe 'apt_security_updates fact' do
subject { Facter.fact(:apt_security_updates).value } subject { Facter.fact(:apt_security_updates).value }
after(:each) { Facter.clear } after(:each) { Facter.clear }
describe 'on Debian based distro missing update-notifier-common' do describe 'when apt has no updates' do
before { before {
Facter.fact(:osfamily).stubs(:value).returns 'Debian' Facter.fact(:apt_has_updates).stubs(:value).returns false
File.stubs(:executable?).returns false
} }
it { should == nil } it { should be nil }
end end
describe 'on Debian based distro' do describe 'when apt has security updates' do
before { before {
Facter.fact(:osfamily).stubs(:value).returns 'Debian' Facter.fact(:osfamily).stubs(:value).returns 'Debian'
File.stubs(:executable?).returns true File.stubs(:executable?) # Stub all other calls
Facter::Util::Resolution.stubs(:exec).returns '14;7' 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 } it { should == 7 }
end end

View file

@ -4,19 +4,20 @@ describe 'apt_updates fact' do
subject { Facter.fact(:apt_updates).value } subject { Facter.fact(:apt_updates).value }
after(:each) { Facter.clear } after(:each) { Facter.clear }
describe 'on Debian based distro missing update-notifier-common' do describe 'when apt has no updates' do
before { before {
Facter.fact(:osfamily).stubs(:value).returns 'Debian' Facter.fact(:apt_has_updates).stubs(:value).returns false
File.stubs(:executable?).returns false
} }
it { should == nil } it { should be nil }
end end
describe 'on Debian based distro' do describe 'when apt has updates' do
before { before {
Facter.fact(:osfamily).stubs(:value).returns 'Debian' Facter.fact(:osfamily).stubs(:value).returns 'Debian'
File.stubs(:executable?).returns true File.stubs(:executable?) # Stub all other calls
Facter::Util::Resolution.stubs(:exec).returns '14;7' 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 } it { should == 14 }
end end