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

Add mocked custom facts based on metadata #19

Merged
merged 1 commit into from
Dec 15, 2020
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
63 changes: 63 additions & 0 deletions lib/voxpupuli/test/facts.rb
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
Expand Down Expand Up @@ -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' }
Copy link
Member

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.

Copy link
Member Author

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 in modules/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.

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
Copy link
Member

Choose a reason for hiding this comment

The 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 puppet/caddy looks different.

Copy link
Member Author

Choose a reason for hiding this comment

The 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
2 changes: 0 additions & 2 deletions lib/voxpupuli/test/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ def suggest_facter_version

require 'voxpupuli/test/facts'
require 'puppetlabs_spec_helper/module_spec_helper'
require 'rspec-puppet-facts'
include RspecPuppetFacts

# Generating facts is slow - this memoizes the facts between multiple classes.
# Marshalling is used to get unique instances which helps when tests overrides
Expand Down