Skip to content

Commit

Permalink
(puppetlabsGH-141) Add tests for Puppet Language Functions
Browse files Browse the repository at this point in the history
Previously the function loader was modified for V4 Function API however there
were no tests for the Puppet based Functions, as opposed to Ruby.  This commit
adds the test fixtures and tests to ensure that Puppet based Functions are
loaded correctly.
  • Loading branch information
glennsarti committed Jun 13, 2019
1 parent 5299cbd commit 7d603fd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# An example puppet function, as opposed to a ruby custom function
#
# @example Declaring the class
# $test = defaultmodule::puppetfunc('true')
#
# @param arg The first parameter for this function.
function defaultmodule::puppetfunc(Variant[String, Boolean] $arg) >> String {
case $arg {
false, undef, /(?i:false)/ : { 'Off' }
true, /(?i:true)/ : { 'On' }
default : { "$arg" }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# An example puppet function in a module, as opposed to a ruby custom function
#
# @example Declaring the class
# $test = valid::modulefunc('true')
#
# @param p1 The first parameter for this function.
function valid::modulefunc(Variant[String, Boolean] $p1) >> String {
case $p1 {
false, undef, /(?i:false)/ : { 'Off' }
true, /(?i:true)/ : { 'On' }
default : { "$p1" }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def should_not_contain_default_functions(deserial)
expect(deserial).to_not contain_child_with_key(:default_pup4_function)
expect(deserial).to_not contain_child_with_key(:'environment::default_env_pup4_function')
expect(deserial).to_not contain_child_with_key(:'modname::default_mod_pup4_function')
expect(deserial).to_not contain_child_with_key(:'defaultmodule::puppetfunc')
end

before(:each) do
Expand Down Expand Up @@ -160,6 +161,8 @@ def expect_same_array_content(a, b)
expect(deserial).to contain_child_with_key(:'environment::default_env_pup4_function')
# These are defined in the fixtures/real_agent/cache/lib/puppet/functions/modname (module namespaced function)
expect(deserial).to contain_child_with_key(:'modname::default_mod_pup4_function')
# These are defined in the fixtures/real_agent/environments/testfixtures/modules/defaultmodules/functions/puppetfunc.pp
expect(deserial).to contain_child_with_key(:'defaultmodule::puppetfunc')

# Now run using cached information
expect_populated_cache
Expand Down Expand Up @@ -271,6 +274,7 @@ def expect_same_array_content(a, b)
expect(deserial).to contain_child_with_key(:fixture_pup4_function)
expect(deserial).to contain_child_with_key(:'valid::fixture_pup4_mod_function')
expect(deserial).to contain_child_with_key(:fixture_pup4_badfunction)
expect(deserial).to contain_child_with_key(:'valid::modulefunc')

# Make sure the function has the right properties
func = child_with_key(deserial, :fixture_function)
Expand All @@ -281,6 +285,13 @@ def expect_same_array_content(a, b)
func = child_with_key(deserial, :fixture_pup4_function)
expect(func.doc).to match(/Example function using the Puppet 4 API in a module/)
expect(func.source).to match(/valid_module_workspace/)

# Make sure the function has the right properties
func = child_with_key(deserial, :'valid::modulefunc')
expect(func.function_version).to eq(4) # Puppet Langauge functions are V4
expect(func.doc).to match(/An example puppet function in a module, as opposed to a ruby custom function/)
expect(func.source).to match(/valid_module_workspace/)
expect(func.signatures.count).to be > 0
end
end

Expand Down

0 comments on commit 7d603fd

Please sign in to comment.