From 79481df8ef866ad299814783c205f207964ee047 Mon Sep 17 00:00:00 2001 From: Tobias Urdin Date: Tue, 30 Oct 2018 15:21:01 +0100 Subject: [PATCH] Use puppet4 functions-api Depends-On: https://review.openstack.org/#/c/614782/ Change-Id: I7bb50c4e591585b80661fe0d41c7cc2dfec5fdf8 --- lib/puppet/functions/validate_yum_hash.rb | 58 ++++++++++++++++++ .../parser/functions/validate_yum_hash.rb | 59 ------------------- spec/functions/validate_yum_hash_spec.rb | 31 ++++++++++ 3 files changed, 89 insertions(+), 59 deletions(-) create mode 100644 lib/puppet/functions/validate_yum_hash.rb delete mode 100644 lib/puppet/parser/functions/validate_yum_hash.rb create mode 100644 spec/functions/validate_yum_hash_spec.rb diff --git a/lib/puppet/functions/validate_yum_hash.rb b/lib/puppet/functions/validate_yum_hash.rb new file mode 100644 index 0000000..e7dd1cf --- /dev/null +++ b/lib/puppet/functions/validate_yum_hash.rb @@ -0,0 +1,58 @@ +Puppet::Functions.create_function(:validate_yum_hash) do + def validate_yum_hash(*args) + yumrepo_arguments = [ + 'name', + 'ensure', + 'baseurl', + 'cost', + 'descr', + 'enabled', + 'enablegroups', + 'exclude', + 'failovermethod', + 'gpgcheck', + 'gpgkey', + 'http_caching', + 'include', + 'includepkgs', + 'keepalive', + 'metadata_expire', + 'metalink', + 'mirrorlist', + 'priority', + 'protect', + 'provider', + 'proxy', + 'proxy_password', + 'proxy_username', + 'repo_gpgcheck', + 's3_enabled', + 'skip_if_unavailable', + 'sslcacert', + 'sslclientcert', + 'sslclientkey', + 'sslverify', + 'target', + 'timeout' + ] + + if args.size > 1 + raise Puppet::Error, "validate_yum_hash takes only a single argument, #{args.size} provided" + end + arg = args[0] + + if not arg.kind_of?(Hash) + raise Puppet::Error, "non-hash argument provided to validate_yum_hash" + end + + if arg.size > 0 + arg.each do |title, params| + params.each do |param, value| + if ! yumrepo_arguments.include?(param) + raise Puppet::Error, "Parameter #{param} is not valid for the yumrepo type" + end + end + end + end + end +end diff --git a/lib/puppet/parser/functions/validate_yum_hash.rb b/lib/puppet/parser/functions/validate_yum_hash.rb deleted file mode 100644 index ea5cc77..0000000 --- a/lib/puppet/parser/functions/validate_yum_hash.rb +++ /dev/null @@ -1,59 +0,0 @@ -module Puppet::Parser::Functions - - yumrepo_arguments = [ - 'name', - 'ensure', - 'baseurl', - 'cost', - 'descr', - 'enabled', - 'enablegroups', - 'exclude', - 'failovermethod', - 'gpgcheck', - 'gpgkey', - 'http_caching', - 'include', - 'includepkgs', - 'keepalive', - 'metadata_expire', - 'metalink', - 'mirrorlist', - 'priority', - 'protect', - 'provider', - 'proxy', - 'proxy_password', - 'proxy_username', - 'repo_gpgcheck', - 's3_enabled', - 'skip_if_unavailable', - 'sslcacert', - 'sslclientcert', - 'sslclientkey', - 'sslverify', - 'target', - 'timeout' - ] - - newfunction(:validate_yum_hash) do |args| - if args.size > 1 - raise Puppet::Error, "validate_yum_hash takes only a single argument, #{args.size} provided" - end - arg = args[0] - - if not arg.kind_of?(Hash) - raise Puppet::Error, "non-hash argument provided to validate_yum_hash" - end - - if arg.size > 0 - arg.each do |title, params| - params.each do |param, value| - if ! yumrepo_arguments.include?(param) - raise Puppet::Error, "Parameter #{param} is not valid for the yumrepo type" - end - end - end - end - end -end diff --git a/spec/functions/validate_yum_hash_spec.rb b/spec/functions/validate_yum_hash_spec.rb new file mode 100644 index 0000000..df0281d --- /dev/null +++ b/spec/functions/validate_yum_hash_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe 'validate_yum_hash' do + it 'exists' do + is_expected.not_to eq(nil) + end + + it 'throws error with more than one argument' do + is_expected.to run.with_params({'title' => {'key1' => 'value1'}, 'title2' => {'key2' => 'value2'}}).and_raise_error(Puppet::Error) + end + + it 'fails with no arguments' do + is_expected.to run.with_params.and_raise_error(Puppet::Error) + end + + it 'fails with invalid hash' do + is_expected.to run.with_params({'title' => {'invalid' => 'val'}}).and_raise_error(Puppet::Error) + end + + it 'fails with invalid hash with multiple entries' do + is_expected.to run.with_params({'title' => {'baseurl' => 'placeholder', 'invalid' => 'val'}}).and_raise_error(Puppet::Error) + end + + it 'works with valid entry' do + is_expected.to run.with_params({'title' => {'baseurl' => 'placeholder'}}) + end + + it 'works with multiple valid entries' do + is_expected.to run.with_params({'title' => {'baseurl' => 'placeholder', 'timeout' => 30}}) + end +end