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

Yum resource fix for non-existent repos and repo info #1593

Merged
merged 2 commits into from
Mar 29, 2017
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
8 changes: 8 additions & 0 deletions docs/resources/yum.md.erb
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,11 @@ The following examples show how to use this InSpec audit resource.
it { should exist }
it { should be_enabled }
end

### Test a particular repository configuration, such as its Base URL

describe yum.repo('mycompany-artifacts') do
it { should exist }
it { should be_enabled }
its('baseurl') { should include 'mycompany.biz' }
end
30 changes: 22 additions & 8 deletions lib/resources/yum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# author: Christoph Hartmann
# author: Dominik Richter

require 'resources/file'

# Usage:
# describe yum do
# its('repos') { should exist }
Expand All @@ -22,6 +20,7 @@
# describe yum.repo('epel') do
# it { should exist }
# it { should be_enabled }
# its('baseurl') { should include 'mycompany.biz' }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be baseurl or ruby style base_url?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key in the yum output and in the *.repo files is baseurl so I vote for keeping it consistent.

# end
#
# deprecated:
Expand All @@ -33,7 +32,7 @@
module Inspec::Resources
class Yum < Inspec.resource(1)
name 'yum'
desc 'Use the yum InSpec audit resource to test packages in the Yum repository.'
desc 'Use the yum InSpec audit resource to test the configuration of Yum repositories.'
example "
describe yum.repo('name') do
it { should exist }
Expand Down Expand Up @@ -121,22 +120,37 @@ def shortname(id)
def info
return @cache if defined?(@cache)
selection = @yum.repositories.select { |e| e['id'] == @reponame || shortname(e['id']) == @reponame }
@cache = selection[0] if !selection.nil? && selection.length == 1
@cache = selection.empty? ? {} : selection.first
@cache
end

def exist?
!info.nil?
!info.empty?
end

def enabled?
repo = info
return false if repo.nil?
return false unless exist?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice simplification

info['status'] == 'enabled'
end

# provide a method for each of the repo metadata items we know about
[
:baseurl,
:expire,
:filename,
:mirrors,
:pkgs,
:size,
:status,
:updated,
].each do |key|
define_method key do
info[key.to_s]
end
end

def to_s
"YumRepo #{shortname(info['id'])}"
"YumRepo #{@reponame}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you 👍

end
end

Expand Down
17 changes: 16 additions & 1 deletion test/unit/resources/yum_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,30 @@
extras = resource.repo('extras/7/x86_64')
_(extras.exist?).must_equal true
_(extras.enabled?).must_equal true
_(extras.baseurl).must_include 'informatik'
# test enabled extra repo with short name
extras = resource.repo('extras')
_(extras.exist?).must_equal true
_(extras.enabled?).must_equal true
_(extras.baseurl).must_include 'informatik'
# test disabled extra-source repo
extras = resource.repo('base-debuginfo/x86_64')
_(extras.exist?).must_equal true
_(extras.enabled?).must_equal false
_(extras.to_s).must_equal 'YumRepo base-debuginfo'
_(extras.to_s).must_equal 'YumRepo base-debuginfo/x86_64'
end

it 'provides methods for retrieving per-repo information' do
resource = MockLoader.new(:centos7).load_resource('yum')
repo = resource.repo('base/7/x86_64')
_(repo.baseurl).must_equal 'http://ftp.hosteurope.de/mirror/centos.org/7.1.1503/os/x86_64/ (9 more)'
_(repo.expire).must_equal '21600 second(s) (last: Sun Sep 6 10:20:46 2015)'
_(repo.filename).must_equal '/etc/yum.repos.d/CentOS-Base.repo'
_(repo.mirrors).must_equal 'http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock'
_(repo.pkgs).must_equal '8652'
_(repo.size).must_equal '6.3 G'
_(repo.status).must_equal 'enabled'
_(repo.updated).must_equal 'Tue Mar 31 22:50:46 2015'
end

it 'test enabled extra repo (serverspec backwards comptability)' do
Expand Down