-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Override facts | ||
# | ||
# This doesn't use deep_merge because that's highly unpredictable. It can merge | ||
# nested hashes in place, modifying the original. It's also unable to override | ||
# true to false. | ||
# | ||
# A deep copy is obtained by using Marshal so it can be modified in place. Then | ||
# it recursively overrides values. If the result is a hash, it's recursed into. | ||
# | ||
# A typical example: | ||
# | ||
# let(:facts) do | ||
# override_facts(super(), os: {'selinux' => {'enabled' => false}}) | ||
# end | ||
def override_facts(base_facts, **overrides) | ||
facts = Marshal.load(Marshal.dump(base_facts)) | ||
apply_overrides!(facts, overrides, false) | ||
facts | ||
end | ||
|
||
# A private helper to override_facts | ||
def apply_overrides!(facts, overrides, enforce_strings) | ||
overrides.each do |key, value| | ||
# Nested facts are strings | ||
key = key.to_s if enforce_strings | ||
|
||
if value.is_a?(Hash) | ||
facts[key] = {} unless facts.key?(key) | ||
apply_overrides!(facts[key], value, true) | ||
else | ||
facts[key] = value | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
require 'spec_helper' | ||
require 'voxpupuli/test/facts' | ||
|
||
describe 'override_facts' do | ||
let(:base_facts) do | ||
{ | ||
os: { | ||
'family' => 'RedHat', | ||
'name' => 'CentOS', | ||
'release' => { | ||
'full' => '7.7.1908', | ||
'major' => '7', | ||
'minor' => '7' | ||
}, | ||
} | ||
} | ||
end | ||
|
||
describe 'no overrides' do | ||
let(:expected) do | ||
{ | ||
os: { | ||
'family' => 'RedHat', | ||
'name' => 'CentOS', | ||
'release' => { | ||
'full' => '7.7.1908', | ||
'major' => '7', | ||
'minor' => '7' | ||
}, | ||
} | ||
} | ||
end | ||
|
||
it { expect(override_facts(base_facts)).to eq(expected) } | ||
end | ||
|
||
describe 'with addition at the top level' do | ||
let(:expected) do | ||
{ | ||
os: { | ||
'family' => 'RedHat', | ||
'name' => 'CentOS', | ||
'release' => { | ||
'full' => '7.7.1908', | ||
'major' => '7', | ||
'minor' => '7' | ||
}, | ||
}, | ||
ruby: { | ||
'sitedir' => '/usr/local/share/ruby/site_ruby', | ||
} | ||
} | ||
end | ||
|
||
it { expect(override_facts(base_facts, ruby: {sitedir: '/usr/local/share/ruby/site_ruby'})).to eq(expected) } | ||
end | ||
|
||
describe 'with deep merging' do | ||
let(:expected) do | ||
{ | ||
os: { | ||
'family' => 'RedHat', | ||
'name' => 'CentOS', | ||
'release' => { | ||
'full' => '7.7.1908', | ||
'major' => '7', | ||
'minor' => '8' | ||
}, | ||
} | ||
} | ||
end | ||
|
||
it { expect(override_facts(base_facts, os: {release: {minor: '8'}})).to eq(expected) } | ||
end | ||
|
||
describe 'with strings' do | ||
let(:expected) do | ||
{ | ||
os: { | ||
'family' => 'RedHat', | ||
'name' => 'CentOS', | ||
'release' => { | ||
'full' => '7.7.1908', | ||
'major' => '7', | ||
'minor' => '8' | ||
}, | ||
} | ||
} | ||
end | ||
|
||
it { expect(override_facts(base_facts, os: {'release' => {minor: '8'}})).to eq(expected) } | ||
end | ||
end |