Created
August 22, 2016 08:38
-
-
Save vStone/4e269150f6fca2727ecbc501877878d6 to your computer and use it in GitHub Desktop.
Use external facter for unit testing facts: spec/helpers/external_facter_helper.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'yaml' | |
module RSpecHelper | |
module SystemFacter | |
def self.resolve_fact (fact, stubs = {}, facter_path = (ENV['FACTER_PATH'] || '/usr/bin/facter')) | |
env_values = {} | |
command = [ facter_path, '--yaml'] | |
facts_path = File.expand_path(File.dirname(__FILE__) + '/../../lib/facter') | |
stubs.each do |key,value| | |
env_values["FACTER_#{key}"] = value | |
end | |
env_values['FACTERLIB'] = facts_path | |
command += [ fact ] | |
result_yaml = '' | |
IO.popen(env_values, command.flatten) do |io| | |
result_yaml = io.read | |
end | |
result_hash = YAML.load(result_yaml) | |
result_hash[fact] | |
end | |
end | |
end | |
if RSpecHelper::SystemFacter.resolve_fact('facterversion') !~ /^3/ | |
raise "Wrong facter system version detected: 'RSpecHelper::SystemFacter.resolve_fact('facterversion')" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment