Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix apt_has_updates fact not parsing apt-check output correctly #403

Merged
merged 1 commit into from
Jan 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/facter/apt_updates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@
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>/dev/null').split(';')
apt_check_result = Facter::Util::Resolution.exec('/usr/lib/update-notifier/apt-check 2>&1')
if not apt_check_result.nil? and apt_check_result =~ /^\d+;\d+$/
apt_package_updates = apt_check_result.split(';')
end
end

setcode do
apt_package_updates != ['0', '0'] unless apt_package_updates.nil?
if not apt_package_updates.nil? and apt_package_updates.length == 2
apt_package_updates != ['0', '0']
end
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>/dev/null').split("\n")
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
Expand Down
30 changes: 26 additions & 4 deletions spec/unit/facter/apt_has_updates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,49 @@

describe 'on non-Debian distro' do
before {
Facter.fact(:osfamily).expects(:value).returns 'RedHat'
Facter.fact(:osfamily).expects(:value).at_least(1).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'
Facter.fact(:osfamily).expects(:value).at_least(1).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 with broken packages' do
before {
Facter.fact(:osfamily).expects(:value).at_least(1).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 "E: Error: BrokenCount > 0"
}
it { should be_nil }
end

describe 'on Debian based distro with unknown error with semicolons' do
before {
Facter.fact(:osfamily).expects(:value).at_least(1).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 "E: Unknown Error: 'This error contains something that could be parsed like 4;3' (10)"
}
it { should be_nil }
end

describe 'on Debian based distro' do
before {
Facter.fact(:osfamily).expects(:value).returns 'Debian'
Facter.fact(:osfamily).expects(:value).at_least(1).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>/dev/null').returns "4;3"
Facter::Util::Resolution.expects(:exec).with('/usr/lib/update-notifier/apt-check 2>&1').returns "4;3"
}
it { should be true }
end
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/facter/apt_package_updates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
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>/dev/null').returns "1;2"
Facter::Util::Resolution.expects(:exec).with('/usr/lib/update-notifier/apt-check -p 2>/dev/null').returns "puppet-common\nlinux-generic\nlinux-image-generic"
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'
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/facter/apt_security_updates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
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>/dev/null').returns "14;7"
Facter::Util::Resolution.expects(:exec).with('/usr/lib/update-notifier/apt-check 2>&1').returns "14;7"
}
it { should == 7 }
end
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/facter/apt_updates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
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>/dev/null').returns "14;7"
Facter::Util::Resolution.expects(:exec).with('/usr/lib/update-notifier/apt-check 2>&1').returns "14;7"
}
it { should == 14 }
end
Expand Down