Skip to content

Commit

Permalink
Merge pull request voxpupuli#327 from awelzel/setpolicyints
Browse files Browse the repository at this point in the history
set_policy: convert known parameters to integers
  • Loading branch information
hunner committed Apr 21, 2015
2 parents 2c3fd7e + 4fcc6a1 commit fa40a00
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/puppet/type/rabbitmq_policy.rb
Expand Up @@ -43,6 +43,9 @@
validate do |value|
resource.validate_definition(value)
end
munge do |value|
resource.munge_definition(value)
end
end

newproperty(:priority) do
Expand Down Expand Up @@ -72,5 +75,27 @@ def validate_definition(definition)
raise ArgumentError, "Invalid definition"
end
end
if definition['ha-mode'] == 'exactly'
ha_params = definition['ha-params']
unless ha_params.to_i.to_s == ha_params
raise ArgumentError, "Invalid ha-params '#{ha_params}' for ha-mode 'exactly'"
end
end
if definition.key? 'expires'
expires_val = definition['expires']
unless expires_val.to_i.to_s == expires_val
raise ArgumentError, "Invalid expires value '#{expires_val}'"
end
end
end

def munge_definition(definition)
if definition['ha-mode'] == 'exactly'
definition['ha-params'] = definition['ha-params'].to_i
end
if definition.key? 'expires'
definition['expires'] = definition['expires'].to_i
end
definition
end
end
28 changes: 28 additions & 0 deletions spec/unit/puppet/type/rabbitmq_policy_spec.rb
Expand Up @@ -88,4 +88,32 @@
}.to raise_error(Puppet::Error, /Invalid value/)
end
end

it 'should accept and convert ha-params for ha-mode exactly' do
definition = {'ha-mode' => 'exactly', 'ha-params' => '2'}
@policy[:definition] = definition
@policy[:definition]['ha-params'].should be_a(Fixnum)
@policy[:definition]['ha-params'].should == 2
end

it 'should not accept non-numeric ha-params for ha-mode exactly' do
definition = {'ha-mode' => 'exactly', 'ha-params' => 'nonnumeric'}
expect {
@policy[:definition] = definition
}.to raise_error(Puppet::Error, /Invalid ha-params.*nonnumeric.*exactly/)
end

it 'should accept and convert the expires value' do
definition = {'expires' => '1800000'}
@policy[:definition] = definition
@policy[:definition]['expires'].should be_a(Fixnum)
@policy[:definition]['expires'].should == 1800000
end

it 'should not accept non-numeric expires value' do
definition = {'expires' => 'future'}
expect {
@policy[:definition] = definition
}.to raise_error(Puppet::Error, /Invalid expires value.*future/)
end
end

0 comments on commit fa40a00

Please sign in to comment.