From 28e745c40f48622de9df1ba0f903eb2da54d72cd Mon Sep 17 00:00:00 2001 From: Tobias Urdin Date: Tue, 30 Oct 2018 15:52:16 +0100 Subject: [PATCH] Use puppet4 functions-api Change-Id: I0877c6b94787ee2dd5c52c3b5e7ee2d525f660f0 --- lib/puppet/functions/get_ext_net_name.rb | 9 ++ .../parser/functions/get_ext_net_name.rb | 9 -- spec/functions/get_ext_net_name_spec.rb | 108 +++++++----------- 3 files changed, 52 insertions(+), 74 deletions(-) create mode 100644 lib/puppet/functions/get_ext_net_name.rb delete mode 100644 lib/puppet/parser/functions/get_ext_net_name.rb diff --git a/lib/puppet/functions/get_ext_net_name.rb b/lib/puppet/functions/get_ext_net_name.rb new file mode 100644 index 0000000..257ae18 --- /dev/null +++ b/lib/puppet/functions/get_ext_net_name.rb @@ -0,0 +1,9 @@ +Puppet::Functions.create_function(:get_ext_net_name) do + def get_ext_net_name(*args) + networks = args.first + raise(Puppet::ParseError, 'get_ext_net_name(): No network data provided!') unless networks.is_a? Hash + ext_net_array = networks.find { |_, value| value.fetch('L2', {})['router_ext'] } + return nil unless ext_net_array + ext_net_array.first + end +end diff --git a/lib/puppet/parser/functions/get_ext_net_name.rb b/lib/puppet/parser/functions/get_ext_net_name.rb deleted file mode 100644 index 2c29a2d..0000000 --- a/lib/puppet/parser/functions/get_ext_net_name.rb +++ /dev/null @@ -1,9 +0,0 @@ -module Puppet::Parser::Functions - newfunction(:get_ext_net_name, :type => :rvalue) do |args| - networks = args.first - fail 'No network data provided!' unless networks.is_a? Hash - ext_net_array = networks.find { |_, value| value.fetch('L2', {})['router_ext'] } - break unless ext_net_array - ext_net_array.first - end -end diff --git a/spec/functions/get_ext_net_name_spec.rb b/spec/functions/get_ext_net_name_spec.rb index 767734f..293ad6a 100644 --- a/spec/functions/get_ext_net_name_spec.rb +++ b/spec/functions/get_ext_net_name_spec.rb @@ -1,85 +1,63 @@ require 'spec_helper' describe 'get_ext_net_name' do - let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + it 'exists' do + is_expected.not_to eq(nil) + end - it 'should exist' do - expect(Puppet::Parser::Functions.function('get_ext_net_name')).to eq('function_get_ext_net_name') + it 'fails with no arguments' do + is_expected.to run.with_params.and_raise_error(Puppet::ParseError) end it 'should return the network name that has router_ext enabled' do - expect(scope.function_get_ext_net_name( - [ - { - "net04" => - { - "L2" => - { - "router_ext" => false, - } - }, - "net04_ext" => - { - "L2" => - { - "router_ext" => true, - } - } + param = { + "net04" => { + "L2" => { + "router_ext" => false, } - ] - )).to eq 'net04_ext' + }, + "net04_ext" => { + "L2" => { + "router_ext" => true, + } + } + } + + is_expected.to run.with_params(param).and_return('net04_ext') end it 'should return nil if router_ext is not enabled' do - expect(scope.function_get_ext_net_name( - [ - { - "net04" => - { - "L2" => - { - "router_ext" => false, - } - }, - "net04_ext" => - { - "L2" => - { - "router_ext" => false, - } - } + param = { + "net04" => { + "L2" => { + "router_ext" => false, } - ], - )).to be_nil + }, + "net04_ext" => { + "L2" => { + "router_ext" => false, + } + } + } + + is_expected.to run.with_params(param).and_return(nil) end it 'should return nil if there is no router_ext' do - expect(scope.function_get_ext_net_name( - [ - { - "net04" => - { - "L2" => - { - } - }, - "net04_ext" => - { - "L2" => - { - } - } - } - ] - )).to be_nil + param = { + "net04" => { + "L2" => {} + }, + "net04_ext" => { + "L2" => {} + } + } + + is_expected.to run.with_params(param).and_return(nil) end it 'should return nil with empty network data' do - expect(scope.function_get_ext_net_name( - [ - {} - ] - )).to be_nil + param = {} + is_expected.to run.with_params(param).and_return(nil) end - end