Update functions to fix unit tests

With the release of 5.5.7, some of the legacy function definitions no
longer pass in unit tests. This change updates the functions that are
failing in the tests to the modern style (4.x) for function
declarations.  Additionally we're removing teh lookup_hiera_hash
function which is failing but not actually consumed by our code base.

There will be a followup patch to migrate the rest of the parser
functions to the new format, but this patch should unblock the gates.

NOTE: git thinks some of these files have been added/deleted rather than
git move due to the large amount of changes between the two versions of
the file.

Change-Id: Ie7316fd422bd4a5eb91f94016977e5d8d76c27bc
Closes-Bug: #1799786
This commit is contained in:
Alex Schultz 2018-10-25 10:13:42 -06:00 committed by Bogdan Dobrelya
parent fcadda0f8d
commit 8d889af7d4
7 changed files with 39 additions and 69 deletions

View File

@ -5,12 +5,13 @@ require 'ipaddr'
# For example from "172.17.0.16" to {172,17,0,16}
# See http://erlang.org/doc/man/kernel_app.html and http://erlang.org/doc/man/inet.html
# for more information.
module Puppet::Parser::Functions
newfunction(:ip_to_erl_format, :type => :rvalue, :doc => "Convert an IP address to the erlang inet format.") do |arg|
if arg[0].class != String
raise Puppet::ParseError, "Syntax error: #{arg[0]} must be a String"
end
ip = IPAddr.new arg[0]
Puppet::Functions.create_function(:ip_to_erl_format) do
dispatch :ip_to_erl_format do
param 'String', :ip_addr
end
def ip_to_erl_format(ip_addr)
ip = IPAddr.new(ip_addr)
output = '{'
if ip.ipv6?
split_char = ':'

View File

@ -3,15 +3,19 @@ require 'ipaddr'
# Custom function to verify if the parameter is a string representing an ip address
# or an array of strings representing an ip address
# Returns true if all elements are proper ip addresses and false otherwise
module Puppet::Parser::Functions
newfunction(:is_ip_addresses, :type => :rvalue, :doc => "Verify if a string or an array of strings are all IP addresses.") do |arg|
if arg[0].class != String and arg[0].class != Array
raise Puppet::ParseError, "Syntax error: #{arg[0]} must be a String or an Array"
Puppet::Functions.create_function(:is_ip_addresses) do
dispatch :is_ip_addresses do
param 'Variant[Array, String, Undef]', :ip_addr
end
def is_ip_addresses(ip_addr)
if not ip_addr
return false
end
if arg[0].class == String
ips = [arg[0]]
if ip_addr.class == String
ips = [ip_addr]
else
ips = arg[0]
ips = ip_addr
end
ips.each do |ip|
begin

View File

@ -0,0 +1,15 @@
# Custom function to transform netmask from IP notation to
# CIDR format. Input is an IP address, output a CIDR:
# 255.255.255.0 = 24
# The CIDR formated netmask is needed for some
# Contrail configuration files
require 'ipaddr'
Puppet::Functions.create_function(:netmask_to_cidr) do
dispatch :netmask_to_cidr do
param 'String', :netmask
end
def netmask_to_cidr(netmask)
IPAddr.new(netmask).to_i.to_s(2).count("1")
end
end

View File

@ -1,22 +0,0 @@
module Puppet::Parser::Functions
newfunction(:lookup_hiera_hash, :arity => 2, :type => :rvalue,
:doc => "Lookup a key->value from a Hiera hash") do |args|
hash_name = args[0]
key_name = args[1]
unless hash_name.is_a?(String) and key_name.is_a?(String)
raise Puppet::ParseError, "The hash name and the key name must be given as strings."
end
if defined? call_function
hash = call_function('hiera', [hash_name])
else
hash = function_hiera([hash_name])
end
unless hash.is_a?(Hash)
raise Puppet::ParseError, "The value Hiera returned for #{hash_name} is not a Hash."
end
unless hash.key?(key_name)
raise Puppet::ParseError, "The Hiera hash #{hash_name} does not contain key #{key_name}."
end
return hash[key_name]
end
end

View File

@ -1,14 +0,0 @@
# Custom function to transform netmask from IP notation to
# CIDR format. Input is an IP address, output a CIDR:
# 255.255.255.0 = 24
# The CIDR formated netmask is needed for some
# Contrail configuration files
require 'ipaddr'
module Puppet::Parser::Functions
newfunction(:netmask_to_cidr, :type => :rvalue) do |args|
if args[0].class != String
raise Puppet::ParseError, "Syntax error: #{args[0]} must be a String"
end
IPAddr.new(args[0]).to_i.to_s(2).count("1")
end
end

View File

@ -21,6 +21,7 @@ describe 'tripleo::keepalived' do
shared_examples_for 'tripleo::keeplived' do
# needed for puppet 4.x
before(:each) do
# mock interface_for_ip function
Puppet::Parser::Functions.newfunction(:interface_for_ip, :type => :rvalue) do |arg|
@ -28,6 +29,11 @@ describe 'tripleo::keepalived' do
end
end
# needed for puppet 5.5.7+
let (:pre_condition) do
'function interface_for_ip($a) { return "br-foo" }'
end
let :default_params do
{
:controller_virtual_ip => '10.0.0.1',

View File

@ -1,20 +0,0 @@
require 'spec_helper'
require 'puppet'
# puppet 4.0 call_function() has no visibility of 3.x functions and will fail anyway
unless Puppet.version =~ /^4\.0/
describe 'lookup_hiera_hash' do
# working version
it { should run.with_params('my_hash', 'network').and_return('127.0.0.1') }
# raise if key does not exist
it { should run.with_params('my_hash', 'not_network').and_raise_error(Puppet::ParseError) }
# raise if hash value returned by hiera is not a hash
it { should run.with_params('not_hash', 'key').and_raise_error(Puppet::ParseError) }
# raise if arguments are not two
it { should run.with_params('hash', 'key', 'unexpected').and_raise_error(ArgumentError) }
it { should run.with_params('hash').and_raise_error(ArgumentError) }
# raise if arguments are not strings
it { should run.with_params({}, 'key').and_raise_error(Puppet::ParseError) }
it { should run.with_params('hash', true).and_raise_error(Puppet::ParseError) }
end
end