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

Conversation

adamleff
Copy link
Contributor

If a repo did not exist, running matchers against it (such as exist)
were failing due to a bug in #to_s when fetching the repo name. The
info method would return nil and we'd still try to treat it as a hash.

This change ensures that info is always a hash, possibly empty if the
repo doesn't exist, and uses the repo name provided by the user rather
than shortening it to be consistent with our other resources which don't
manipulate the user input in the formatter.

Also added a method_missing to allow users to interrogate repo options,
such as baseurl or gpgcheck.

Fixes #1553

@@ -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 the configuration of Yum repositories.'
Copy link
Contributor

Choose a reason for hiding this comment

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

This is missing the word "test" and should be:

desc 'Use the yum InSpec audit resource to test the configuration of Yum repositories.'

Sorry for being pedantic :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pedants welcome. I'm the club president.

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

def method_missing(name)
Copy link
Contributor

Choose a reason for hiding this comment

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

We try to avoid method_missing whenever possible. Do we know all possible properties? Maybe we use something like:

    %w{
      baseurl
    }.each do |m|
      define_method m.to_sym do |*args|
        info[m.to_s]
      end
    end

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

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 👍

If a repo did not exist, running matchers against it (such as `exist`)
were failing due to a bug in `#to_s` when fetching the repo name. The
`info` method would return nil and we'd still try to treat it as a hash.

This change ensures that info is always a hash, possibly empty if the
repo doesn't exist, and uses the repo name provided by the user rather
than shortening it to be consistent with our other resources which don't
manipulate the user input in the formatter.

Also added a method_missing to allow users to interrogate repo options,
such as baseurl or gpgcheck.

Signed-off-by: Adam Leff <adam@leff.co>
Instead of method_missing, methods for each output item from
`yum repolist` are provided.

Signed-off-by: Adam Leff <adam@leff.co>
Copy link
Contributor

@arlimus arlimus left a comment

Choose a reason for hiding this comment

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

Great update and more usefull methods here, kudos @adamleff

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

@arlimus arlimus merged commit af7dffa into master Mar 29, 2017
@arlimus arlimus deleted the adamleff/yum-fix branch March 29, 2017 08:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants