-
Notifications
You must be signed in to change notification settings - Fork 193
fix: Do not hard code package name in puppet_agent_end_run.rb #761
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
base: main
Are you sure you want to change the base?
Conversation
2000bc5
to
4eb240d
Compare
Not sure how to handle the tests, as they do not seem to ensure that the puppet_agent class is in the catalog. Most likely adding to catalog in puppet_agent_end_run_spec? And also adding other "bogus" packages to ensure it works properly? |
# Latest version might be undefined, e.G. if we're about to install a different named | ||
# package than the currently running one. In that case, we'll leave desired_version empty. | ||
latest_version = @resource.catalog.resource('package', package_name).parameters[:ensure].latest | ||
desired_version = latest_version.match(%r{^(?:[0-9]:)?(\d+\.\d+(\.\d+)?(?:\.\d+))?}).captures.first unless latest_version.nil? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If latest_version
is nil, then desired_version
will be too, and the versioncmp
function will raise:
❯ bundle exec ruby -rpuppet -e 'Puppet::Util::Package.versioncmp(nil, "1.2.3")'
/home/josh/work/puppet-private/lib/puppet/util/package.rb:12:in `versioncmp': undefined method `scan' for nil:NilClass (NoMethodError)
Maybe use empty string instead?
desired_version = if latest_version.nil?
""
else
latest_version.match(%r{^(?:[0-9]:)?(\d+\.\d+(\.\d+)?(?:\.\d+))?}).captures.first
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If latest_version is nil then desired_version will still be 'latest' - But I think it makes sense to be more explicit here.
latest_version = @resource.catalog.resource('package', 'puppet-agent').parameters[:ensure].latest | ||
desired_version = latest_version.match(%r{^(?:[0-9]:)?(\d+\.\d+(\.\d+)?(?:\.\d+))?}).captures.first | ||
# Package name might be different to puppet-agent, hence we need to look it up. | ||
package_name = @resource.catalog.resource('class', 'puppet_agent')[:package_name] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If package_name
can be nil (not sure that's possible?), then it'd be good to set a default:
package_name = @resource.catalog.resource('class', 'puppet_agent')[:package_name] | |
package_name = @resource.catalog.resource('class', 'puppet_agent')[:package_name] || 'puppet-agent' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it can (realistically) be nil - but having a sane default still seems like a good idea.
The tests are failing because they "manually" create a catalog and add the Puppet::Type.type(:package).new(name: 'puppet-agent', ensure: 'latest', provider: :yum)
end
+ let(:puppet_agent_class) do
+ Puppet::Type.type(:component).new(name: 'puppet_agent', package_name: 'puppet-agent')
+ end
+
before(:each) do
+ catalog.add_resource(puppet_agent_class)
catalog.add_resource(agent_latest_package)
resource.catalog = catalog
end |
Thanks a lot! I'll enhance/fix the tests and will include your previous review comments early next week. |
When using this module to install puppet-agent compatible packages with a different name (such as
openvox-agent
) callingpuppet_agent_end_run
withlatest
will fail, as the package name is hard coded.This patch replaces the hard coded
puppet-agent
with a dynamic lookup for the package name. It also considers that (during migration) thelatest_version
lookup will not work.How to reproduce the error:
Results in: