-
Notifications
You must be signed in to change notification settings - Fork 6
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
Add mocked custom facts based on metadata #19
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
require 'rspec-puppet-facts' | ||
include RspecPuppetFacts | ||
|
||
# Override facts | ||
# | ||
# This doesn't use deep_merge because that's highly unpredictable. It can merge | ||
|
@@ -32,3 +35,63 @@ def apply_overrides!(facts, overrides, enforce_strings) | |
end | ||
end | ||
end | ||
|
||
# Add mocked facts based on the metadata present in the module | ||
# | ||
# This means that for some module there are hardcoded mocks, such as stdlib. | ||
# When stdlib is present in metadata.json, facts like service_provider are | ||
# mocked and return the correct value according to the OS facts. | ||
def add_mocked_facts! | ||
add_facts_for_metadata(RspecPuppetFacts.metadata) | ||
end | ||
|
||
def add_facts_for_metadata(metadata) | ||
return unless metadata && metadata['dependencies'] | ||
|
||
metadata['dependencies'].each do |dependency| | ||
case normalize_module_name(dependency['name']) | ||
when 'camptocamp/systemd' | ||
add_custom_fact :systemd, ->(os, facts) { facts[:service_provider] == 'systemd' } | ||
when 'puppetlabs/stdlib' | ||
add_stdlib_facts | ||
end | ||
end | ||
end | ||
|
||
def normalize_module_name(name) | ||
return unless name | ||
name.sub('-', '/') | ||
end | ||
|
||
def add_stdlib_facts | ||
add_custom_fact :puppet_environmentpath, '/etc/puppetlabs/code/environments' | ||
add_custom_fact :puppet_vardir, '/opt/puppetlabs/puppet/cache' | ||
add_custom_fact :root_home, '/root' | ||
|
||
# Rough conversion of grepping in the puppet source: | ||
# grep defaultfor lib/puppet/provider/service/*.rb | ||
add_custom_fact :service_provider, ->(os, facts) do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rubocop was complaining that I should use the lambda syntax, that is the reason why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I know. As you can see by the date on the commit this has been around for a while. This repo probably should also gain rubocop. |
||
case facts[:osfamily].downcase | ||
when 'archlinux' | ||
'systemd' | ||
when 'darwin' | ||
'launchd' | ||
when 'debian' | ||
'systemd' | ||
when 'freebsd' | ||
'freebsd' | ||
when 'gentoo' | ||
'openrc' | ||
when 'openbsd' | ||
'openbsd' | ||
when 'redhat' | ||
facts[:operatingsystemrelease].to_i >= 7 ? 'systemd' : 'redhat' | ||
when 'suse' | ||
facts[:operatingsystemmajrelease].to_i >= 12 ? 'systemd' : 'redhat' | ||
when 'windows' | ||
'windows' | ||
else | ||
'init' | ||
end | ||
end | ||
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.
We should also add the fact
systemd_version
. Sometimes a parameter requires a specific version of Systemd.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'm not opposed, but at this point I'm very busy since the Foreman is getting close to the 2.1 release cycle and a lot has to be done on my end. My goal of this PR was to share my intent so people don't develop the same thing again. I'm happy it did, but right now I don't really have time to test it and push for the major cleanup.
If someone wants to do so, I'd be happy if they could confirm. What I'd normally do is use modulesync to check out all modules, then grep for
service_provider
inmodules/vopupuli/puppet-*/spec
and see where it is already mocked. Update those modules to use this change, see if tests pass. If they do, get this merged, released and do a modulesync.