Skip to content

Commit

Permalink
add unit tests for voxpupuli#225
Browse files Browse the repository at this point in the history
  • Loading branch information
lbetz authored and n00by committed Apr 26, 2018
1 parent bc0c049 commit 641f9ab
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/puppet_x/icinga2/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,19 @@ def self.process_array(items, indent=2)

def self.process_hash(attrs, indent=2, level=3, prefix=' '*indent)
result = ''

attrs.each do |attr, value|
if value.is_a?(Hash)
result += case level
when 1 then process_hash(value, indent, 2, "%s%s" % [ prefix, attr ])
when 2 then "%s[\"%s\"] = {\n%s%s}\n" % [ prefix, attr, process_hash(value, indent), ' ' * (indent-2) ]
else "%s%s = {\n%s%s}\n" % [ prefix, attribute_types(attr), process_hash(value, indent+2), ' ' * indent ]
if value.empty?
result = "%s%s = {}\n" % [ prefix, attribute_types(attr) ]
else
result += case level
when 1 then process_hash(value, indent, 2, "%s%s" % [ prefix, attr ])
when 2 then "%s[\"%s\"] = {\n%s%s}\n" % [ prefix, attr, process_hash(value, indent), ' ' * (indent-2) ]
else "%s%s = {\n%s%s}\n" % [ prefix, attribute_types(attr), process_hash(value, indent+2), ' ' * indent ]
end
end
elsif value.is_a?(Array)
#result += "%s%s = [ %s]\n" % [ prefix, attribute_types(attr), process_array(value) ]
result += case level
when 2 then "%s[\"%s\"] = [ %s]\n" % [ prefix, attribute_types(attr), process_array(value) ]
else "%s%s = [ %s]\n" % [ prefix, attribute_types(attr), process_array(value) ]
Expand Down
48 changes: 48 additions & 0 deletions spec/defines/object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,22 @@
end


context "#{os} with attrs => { vars => { bar => {} } }" do
let(:params) { {:attrs => { 'vars' => { 'bar' => {} } }, :object_type => 'foo', :target => '/bar/baz', :order => '10'} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/vars.bar = \{\}/) }
end


context "#{os} with attrs => { vars => { bar => [] } }" do
let(:params) { {:attrs => { 'vars' => { 'bar' => [] } }, :object_type => 'foo', :target => '/bar/baz', :order => '10'} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/vars.bar = \[\s+\]/) }
end


context "#{os} with attrs => { vars => {key1 => 4247, key2 => value2} }" do
let(:params) { {:attrs => { 'vars' => {'key1' => '4247', 'key2' => 'value2'} }, :object_type => 'foo', :target => '/bar/baz', :order => '10'} }

Expand All @@ -206,6 +222,14 @@
end


context "#{os} with attrs => { vars => {foo => {bar => [4247, value2]}} }" do
let(:params) { {:attrs => { 'vars' => {'foo' => { 'bar' => ['4247', 'value2']}} }, :object_type => 'foo', :target => '/bar/baz', :order => '10'} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/vars.foo\["bar"\] = \[ 4247, "value2", \]/) }
end


context "#{os} with attrs => { foo => {{ unparsed string }} }" do
let(:params) { {:attrs => { 'foo' => '{{ unparsed string }}' }, :object_type => 'foo', :target => '/bar/baz', :order => '10'} }

Expand Down Expand Up @@ -405,6 +429,22 @@
end


context "Windows 2012 R2 with attrs => { vars => { bar => {} } }" do
let(:params) { {:attrs => { 'vars' => { 'bar' => {} } }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10'} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/vars.bar = \{\}/) }
end


context "Windows 2012 R2 with attrs => { vars => { bar => [] } }" do
let(:params) { {:attrs => { 'vars' => { 'bar' => [] } }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10'} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/vars.bar = \[\s+\]/) }
end


context "Windows 2012 R2 with attrs => { vars => {key1 => 4247, key2 => value2} }" do
let(:params) { {:attrs => { 'vars' => {'key1' => '4247', 'key2' => 'value2'} }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10'} }

Expand All @@ -431,6 +471,14 @@
end


context "Windows 2012 R2 with attrs => { vars => {foo => {bar => [4247, value2]}} }" do
let(:params) { {:attrs => { 'vars' => {'foo' => { 'bar' => ['4247', 'value2']}} }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10'} }

it { is_expected.to contain_concat__fragment('bar')
.with_content(/vars.foo\["bar"\] = \[ 4247, "value2", \]/) }
end


context "Windows 2012 R2 with attrs => { foo => {{ unparsed string }} }" do
let(:params) { {:attrs => { 'foo' => '{{ unparsed string }}' }, :object_type => 'foo', :target => 'C:/bar/baz', :order => '10'} }

Expand Down

0 comments on commit 641f9ab

Please sign in to comment.