Skip to content

Commit 17597bf

Browse files
author
Ashley Penney
committed
Merge pull request #282 from mediatemple/fix/master/merge_function
Fixes issue #274 by using recursive hash merge.
2 parents 069fedc + 1b4a486 commit 17597bf

File tree

4 files changed

+140
-1
lines changed

4 files changed

+140
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
module Puppet::Parser::Functions
2+
newfunction(:mysql_deepmerge, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
3+
Recursively merges two or more hashes together and returns the resulting hash.
4+
5+
For example:
6+
7+
$hash1 = {'one' => 1, 'two' => 2, 'three' => { 'four' => 4 } }
8+
$hash2 = {'two' => 'dos', 'three' => { 'five' => 5 } }
9+
$merged_hash = mysql_deepmerge($hash1, $hash2)
10+
# The resulting hash is equivalent to:
11+
# $merged_hash = { 'one' => 1, 'two' => 'dos', 'three' => { 'four' => 4, 'five' => 5 } }
12+
13+
When there is a duplicate key that is a hash, they are recursively merged.
14+
When there is a duplicate key that is not a hash, the key in the rightmost hash will "win."
15+
16+
ENDHEREDOC
17+
18+
if args.length < 2
19+
raise Puppet::ParseError, ("mysql_deepmerge(): wrong number of arguments (#{args.length}; must be at least 2)")
20+
end
21+
22+
result = Hash.new
23+
args.each do |arg|
24+
next if arg.is_a? String and arg.empty? # empty string is synonym for puppet's undef
25+
# If the argument was not a hash, skip it.
26+
unless arg.is_a?(Hash)
27+
raise Puppet::ParseError, "mysql_deepmerge: unexpected argument type #{arg.class}, only expects hash arguments"
28+
end
29+
30+
# Now we have to traverse our hash assigning our non-hash values
31+
# to the matching keys in our result while following our hash values
32+
# and repeating the process.
33+
overlay( result, arg )
34+
end
35+
return( result )
36+
end
37+
end
38+
39+
def overlay( hash1, hash2 )
40+
hash2.each do |key, value|
41+
if( value.is_a?(Hash) )
42+
if( ! hash1.has_key?( key ) or ! hash1[key].is_a?(Hash))
43+
hash1[key] = value
44+
else
45+
overlay( hash1[key], value )
46+
end
47+
else
48+
hash1[key] = value
49+
end
50+
end
51+
end
52+

manifests/server.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
}
3838

3939
# Create a merged together set of options. Rightmost hashes win over left.
40-
$options = merge($mysql::params::default_options, $override_options)
40+
$options = mysql_deepmerge($mysql::params::default_options, $override_options)
4141

4242
Class['mysql::server::root_password'] -> Mysql::Db <| |>
4343

spec/classes/mysql_server_spec.rb

+10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99
it { should contain_class('mysql::server::root_password') }
1010
end
1111

12+
# make sure that overriding the mysqld settings keeps the defaults for everything else
13+
context 'with overrides' do
14+
let(:params) {{ :override_options => { 'mysqld' => { 'socket' => '/var/lib/mysql/mysql.sock' } } }}
15+
it do
16+
should contain_file('/etc/my.cnf').with({
17+
:mode => '0644',
18+
}).with_content(/basedir/)
19+
end
20+
end
21+
1222
context 'with remove_default_accounts set' do
1323
let (:params) {{ :remove_default_accounts => true }}
1424
it { should contain_class('mysql::server::account_security') }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#! /usr/bin/env ruby -S rspec
2+
3+
require 'spec_helper'
4+
5+
describe Puppet::Parser::Functions.function(:mysql_deepmerge) do
6+
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
7+
8+
describe 'when calling mysql_deepmerge from puppet' do
9+
it "should not compile when no arguments are passed" do
10+
pending("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
11+
Puppet[:code] = '$x = mysql_deepmerge()'
12+
expect {
13+
scope.compiler.compile
14+
}.to raise_error(Puppet::ParseError, /wrong number of arguments/)
15+
end
16+
17+
it "should not compile when 1 argument is passed" do
18+
pending("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
19+
Puppet[:code] = "$my_hash={'one' => 1}\n$x = mysql_deepmerge($my_hash)"
20+
expect {
21+
scope.compiler.compile
22+
}.to raise_error(Puppet::ParseError, /wrong number of arguments/)
23+
end
24+
end
25+
26+
describe 'when calling mysql_deepmerge on the scope instance' do
27+
it 'should require all parameters are hashes' do
28+
expect { new_hash = scope.function_mysql_deepmerge([{}, '2'])}.to raise_error(Puppet::ParseError, /unexpected argument type String/)
29+
expect { new_hash = scope.function_mysql_deepmerge([{}, 2])}.to raise_error(Puppet::ParseError, /unexpected argument type Fixnum/)
30+
end
31+
32+
it 'should accept empty strings as puppet undef' do
33+
expect { new_hash = scope.function_mysql_deepmerge([{}, ''])}.not_to raise_error(Puppet::ParseError, /unexpected argument type String/)
34+
end
35+
36+
it 'should be able to mysql_deepmerge two hashes' do
37+
new_hash = scope.function_mysql_deepmerge([{'one' => '1', 'two' => '1'}, {'two' => '2', 'three' => '2'}])
38+
new_hash['one'].should == '1'
39+
new_hash['two'].should == '2'
40+
new_hash['three'].should == '2'
41+
end
42+
43+
it 'should mysql_deepmerge multiple hashes' do
44+
hash = scope.function_mysql_deepmerge([{'one' => 1}, {'one' => '2'}, {'one' => '3'}])
45+
hash['one'].should == '3'
46+
end
47+
48+
it 'should accept empty hashes' do
49+
scope.function_mysql_deepmerge([{},{},{}]).should == {}
50+
end
51+
52+
it 'should mysql_deepmerge subhashes' do
53+
hash = scope.function_mysql_deepmerge([{'one' => 1}, {'two' => 2, 'three' => { 'four' => 4 } }])
54+
hash['one'].should == 1
55+
hash['two'].should == 2
56+
hash['three'].should == { 'four' => 4 }
57+
end
58+
59+
it 'should append to subhashes' do
60+
hash = scope.function_mysql_deepmerge([{'one' => { 'two' => 2 } }, { 'one' => { 'three' => 3 } }])
61+
hash['one'].should == { 'two' => 2, 'three' => 3 }
62+
end
63+
64+
it 'should append to subhashes 2' do
65+
hash = scope.function_mysql_deepmerge([{'one' => 1, 'two' => 2, 'three' => { 'four' => 4 } }, {'two' => 'dos', 'three' => { 'five' => 5 } }])
66+
hash['one'].should == 1
67+
hash['two'].should == 'dos'
68+
hash['three'].should == { 'four' => 4, 'five' => 5 }
69+
end
70+
71+
it 'should append to subhashes 3' do
72+
hash = scope.function_mysql_deepmerge([{ 'key1' => { 'a' => 1, 'b' => 2 }, 'key2' => { 'c' => 3 } }, { 'key1' => { 'b' => 99 } }])
73+
hash['key1'].should == { 'a' => 1, 'b' => 99 }
74+
hash['key2'].should == { 'c' => 3 }
75+
end
76+
end
77+
end

0 commit comments

Comments
 (0)