Use puppet4 functions-api

Change-Id: I0877c6b94787ee2dd5c52c3b5e7ee2d525f660f0
This commit is contained in:
Tobias Urdin 2018-10-30 15:52:16 +01:00
parent 3d60f890b4
commit 28e745c40f
3 changed files with 52 additions and 74 deletions

View File

@ -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

View File

@ -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

View File

@ -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