Use puppet4 functions-api

Change-Id: Id124ec4c598c7b6bcb7c8134d75825fd01d938d2
This commit is contained in:
Tobias Urdin 2018-10-30 16:03:37 +01:00
parent 61b26933f7
commit 37bb654424
12 changed files with 236 additions and 15 deletions

View File

@ -5,7 +5,7 @@ Puppet::Functions.create_function(:convert_cert_to_string) do
def convert_cert_to_string(cert_file)
unless File.file?(cert_file)
raise puppet::ParseError, "Certificate file not found: #{cert_file}"
raise Puppet::ParseError, "Certificate file not found: #{cert_file}"
end
text=File.readlines(cert_file)
cert_string = ''

View File

@ -1,5 +1,5 @@
module Puppet::Parser::Functions
newfunction(:convert_to_json_string, :type => :rvalue) do |args|
Puppet::Functions.create_function(:convert_to_json_string) do
def convert_to_json_string(*args)
require 'json'
value = args[0]
if (value.kind_of? Array) && value.all? {|x| x.include? ":"}

View File

@ -18,9 +18,8 @@
#
# Advanced validation for VLAN configuration
#
module Puppet::Parser::Functions
newfunction(:validate_network_vlan_ranges) do |args|
Puppet::Functions.create_function(:validate_network_vlan_ranges) do
def validate_network_vlan_ranges(*args)
value = args[0]
if not value.kind_of?(Array)
value = [value]

View File

@ -18,9 +18,8 @@
#
# Advanced validation when using GRE
#
module Puppet::Parser::Functions
newfunction(:validate_tunnel_id_ranges) do |args|
Puppet::Functions.create_function(:validate_tunnel_id_ranges) do
def validate_tunnel_id_ranges(*args)
value = args[0]
if not value.kind_of?(Array)
value = [value]

View File

@ -18,9 +18,8 @@
#
# Advanced validation when using VXLAN
#
module Puppet::Parser::Functions
newfunction(:validate_vni_ranges) do |args|
Puppet::Functions.create_function(:validate_vni_ranges) do
def validate_vni_ranges(*args)
value = args[0]
if not value.kind_of?(Array)
value = [value]

View File

@ -18,9 +18,8 @@
#
# Advanced validation for VXLAN UDP port configuration
#
module Puppet::Parser::Functions
newfunction(:validate_vxlan_udp_port) do |args|
Puppet::Functions.create_function(:validate_vxlan_udp_port) do
def validate_vxlan_udp_port(*args)
value = Integer(args[0])
# check if port is either default value or one of the private ports

View File

@ -0,0 +1,39 @@
require 'spec_helper'
describe 'convert_cert_to_string' do
it 'exists' do
is_expected.not_to eq(nil)
end
it 'fails with no arguments' do
is_expected.to run.with_params.and_raise_error(ArgumentError)
end
it 'fails when arg is not a string' do
is_expected.to run.with_params(123).and_raise_error(ArgumentError)
end
context 'when file does not exist' do
it 'fails when cert file doesnt exist' do
File.stubs(:file?).with('/etc/ssl/certs/test.pem').returns(false)
is_expected.to run.with_params('/etc/ssl/certs/test.pem').and_raise_error(Puppet::ParseError)
end
end
context 'with certificate that doesnt need strip' do
it 'should return proper value' do
File.stubs(:file?).with('/etc/ssl/certs/test.pem').returns(true)
File.stubs(:readlines).with('/etc/ssl/certs/test.pem').returns(['----- BEGIN CERTIFICATE -----', 'abc123data', '----- END CERTIFICATE -----'])
is_expected.to run.with_params('/etc/ssl/certs/test.pem').and_return('abc123data')
end
end
context 'with certificate that requires strip' do
it 'should return proper value' do
File.stubs(:file?).with('/etc/ssl/certs/test.pem').returns(true)
# NOTE(tobias-urdin): There is spacing in the return data here on purpose to test the ruby string strip.
File.stubs(:readlines).with('/etc/ssl/certs/test.pem').returns(['----- BEGIN CERTIFICATE -----', ' abc321 ', '----- END CERTIFICATE -----'])
is_expected.to run.with_params('/etc/ssl/certs/test.pem').and_return('abc321')
end
end
end

View File

@ -0,0 +1,22 @@
require 'spec_helper'
describe 'convert_to_json_string' do
it 'exists' do
is_expected.not_to eq(nil)
end
it 'hash to json string' do
data = {:some => "data"}
is_expected.to run.with_params(data).and_return('{"some":"data"}')
end
it 'array of strings with kv to json string' do
data = ['mykey:myvalue', 'key2:val2']
is_expected.to run.with_params(data).and_return('{"mykey":"myvalue","key2":"val2"}')
end
it 'array of strings to json strings' do
data = ['val1', 'val2']
is_expected.to run.with_params(data).and_return('["val1","val2"]')
end
end

View File

@ -0,0 +1,43 @@
require 'spec_helper'
describe 'validate_network_vlan_ranges' do
it 'exists' do
is_expected.not_to eq(nil)
end
it 'fails with invalid first id max' do
is_expected.to run.with_params('4095:4096').and_raise_error(Puppet::Error)
end
it 'fails with valid first id but invalid second id' do
is_expected.to run.with_params('1024:4096').and_raise_error(Puppet::Error)
end
it 'fails with first range valid and second invalid' do
is_expected.to run.with_params(['1024:1050', '4095:4096']).and_raise_error(Puppet::Error)
end
it 'fails with invalid vlan range' do
is_expected.to run.with_params('2048:2000').and_raise_error(Puppet::Error)
end
it 'fails with invalid vlan range in array' do
is_expected.to run.with_params(['2048:2000']).and_raise_error(Puppet::Error)
end
it 'works with valid vlan range' do
is_expected.to run.with_params('1024:1048')
end
it 'works with valid vlan range in array' do
is_expected.to run.with_params(['1024:1048', '1050:1060'])
end
it 'works with a physical net name' do
is_expected.to run.with_params('physnet1')
end
it 'works with a single vlan' do
is_expected.to run.with_params('1024')
end
end

View File

@ -0,0 +1,39 @@
require 'spec_helper'
describe 'validate_tunnel_id_ranges' do
it 'exists' do
is_expected.not_to eq(nil)
end
it 'fails with invalid input' do
is_expected.to run.with_params('??!!!').and_raise_error(Puppet::Error)
end
it 'fails with invalid range' do
is_expected.to run.with_params('4096').and_raise_error(Puppet::Error)
end
it 'fails with invalid range in array' do
is_expected.to run.with_params(['4096']).and_raise_error(Puppet::Error)
end
it 'fails with invalid range max' do
is_expected.to run.with_params('2048:1024').and_raise_error(Puppet::Error)
end
it 'fails with invalid range max in array' do
is_expected.to run.with_params(['2048:1024']).and_raise_error(Puppet::Error)
end
it 'fails when range is too large' do
is_expected.to run.with_params('10:1100000').and_raise_error(Puppet::Error)
end
it 'works with valid range' do
is_expected.to run.with_params('1024:2048')
end
it 'works with valid array of ranges' do
is_expected.to run.with_params(['1024:2048', '4096:8296'])
end
end

View File

@ -0,0 +1,43 @@
require 'spec_helper'
describe 'validate_vni_ranges' do
it 'exists' do
is_expected.not_to eq(nil)
end
it 'fails with invalid input' do
is_expected.to run.with_params('??!!!').and_raise_error(Puppet::Error)
end
it 'fails with invalid range' do
is_expected.to run.with_params('4096').and_raise_error(Puppet::Error)
end
it 'fails with invalid range in array' do
is_expected.to run.with_params(['4096']).and_raise_error(Puppet::Error)
end
it 'fails with invalid range max' do
is_expected.to run.with_params('2048:1024').and_raise_error(Puppet::Error)
end
it 'fails with invalid range max in array' do
is_expected.to run.with_params(['2048:1024']).and_raise_error(Puppet::Error)
end
it 'fails when range is too large on first' do
is_expected.to run.with_params('16777315:10').and_raise_error(Puppet::Error)
end
it 'fails when range is too large on second' do
is_expected.to run.with_params('10:16777315').and_raise_error(Puppet::Error)
end
it 'works with valid range' do
is_expected.to run.with_params('1024:2048')
end
it 'works with valid array of ranges' do
is_expected.to run.with_params(['1024:2048', '4096:8296'])
end
end

View File

@ -0,0 +1,39 @@
require 'spec_helper'
describe 'validate_vxlan_udp_port' do
it 'exists' do
is_expected.not_to eq(nil)
end
it 'fails with port too high integer' do
is_expected.to run.with_params(65536).and_raise_error(Puppet::Error)
end
it 'fails with a well known port integer' do
is_expected.to run.with_params(90).and_raise_error(Puppet::Error)
end
it 'fails with a well known port string' do
is_expected.to run.with_params('90').and_raise_error(Puppet::Error)
end
it 'fails with port too high string' do
is_expected.to run.with_params('65536').and_raise_error(Puppet::Error)
end
it 'works with default port integer' do
is_expected.to run.with_params(4789)
end
it 'works with default port string' do
is_expected.to run.with_params('4789')
end
it 'works with a private port integer' do
is_expected.to run.with_params(49155)
end
it 'works with a private port string' do
is_expected.to run.with_params('49155')
end
end