Skip to content

Commit

Permalink
(puppetlabsGH-121) Add tests for Puppet API 3 and 4 loading
Browse files Browse the repository at this point in the history
This commit adds test fixtures for Puppet 4 API style functions and modifies the
integration tests to expect these new fixtures.
  • Loading branch information
glennsarti committed May 22, 2019
1 parent 514e9c0 commit a6653a6
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Example function using the Puppet 4 API
# This should be loaded
Puppet::Functions.create_function(:default_pup4_function) do
# @return [Array<String>]
def default_pup4_function
'default_pup4_function result'
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Example function using the Puppet 4 API
# This should be loaded in the environment namespace
Puppet::Functions.create_function(:'environment::default_env_pup4_function') do
# @return [Array<String>]
def default_env_pup4_function
'default_env_pup4_function result'
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Example function using the Puppet 4 API
# This should be loaded in the module called 'modname' namespace
Puppet::Functions.create_function(:'modname::default_mod_pup4_function') do
# @return [Array<String>]
def default_mod_pup4_function
'default_env_pup4_function result'
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Example function using the Puppet 4 API in a module
# This should not be loaded in the environment namespace
Puppet::Functions.create_function(:'badname::pup4_function') do
# @return [Array<String>]
def pup4_function
'pup4_function result'
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Example function using the Puppet 4 API in a module
# This should be loaded in the module namespace
Puppet::Functions.create_function(:'profile::pup4_envprofile_function') do
# @return [Array<String>]
def pup4_envprofile_function
'pup4_envprofile_function result'
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'a_bad_gem_that_does_not_exist'

# Example function using the Puppet 4 API in a module
# This should not be loaded
Puppet::Functions.create_function(:pup4_env_badfile) do
# @return [Array<String>]
def pup4_env_badfile
'pup4_env_badfile result'
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Example function using the Puppet 4 API in a module
# This should be loaded but never actually successfully invoke
Puppet::Functions.create_function(:pup4_env_badfunction) do
# @return [Array<String>]
def pup4_env_badfunction
require 'a_bad_gem_that_does_not_exist'
'pup4_env_badfunction result'
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Example function using the Puppet 4 API in a module
# ??? This should be loaded as global namespace function
Puppet::Functions.create_function(:pup4_env_function) do
# @return [Array<String>]
def pup4_env_function
'pup4_env_function result'
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Example function using the Puppet 4 API in a module
# This should not be loaded in the module namespace
Puppet::Functions.create_function(:'badname::fixture_pup4_badname_function') do
# @return [Array<String>]
def fixture_pup4_badname_function
'fixture_pup4_badname_function result'
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'a_bad_gem_that_does_not_exist'

# Example function using the Puppet 4 API in a module
# This should not be loaded
Puppet::Functions.create_function(:fixture_pup4_badfile) do
# @return [Array<String>]
def fixture_pup4_badfile
'fixture_pup4_badfile result'
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Example function using the Puppet 4 API in a module
# This should be loaded but never actually successfully invoke
Puppet::Functions.create_function(:fixture_pup4_badfunction) do
# @return [Array<String>]
def fixture_pup4_badfunction
require 'a_bad_gem_that_does_not_exist'
'fixture_pup4_badfunction result'
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Example function using the Puppet 4 API in a module
# This should be loaded as global namespace function
Puppet::Functions.create_function(:fixture_pup4_function) do
# @return [Array<String>]
def fixture_pup4_function
'fixture_pup4_function result'
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Example function using the Puppet 4 API in a module
# This should be loaded in the module namespace
Puppet::Functions.create_function(:'valid::fixture_pup4_mod_function') do
# @return [Array<String>]
def fixture_pup4_mod_function
'fixture_pup4_mod_function result'
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'open3'
require 'tempfile'

describe 'PuppetLanguageServerSidecar with Feature Flag puppetstrings' do
describe 'PuppetLanguageServerSidecar with Feature Flag puppetstrings', :if => Gem::Version.new(Puppet.version) >= Gem::Version.new('6.0.0') do
def run_sidecar(cmd_options)
cmd_options << '--no-cache'

Expand Down Expand Up @@ -48,6 +48,17 @@ def with_temporary_file(content)
end
end

def should_not_contain_default_functions(deserial)
# These functions should not appear in the deserialised list as they're part
# of the default function set, not the workspace.
#
# They are defined in the fixtures/real_agent/cache/lib/...
expect(deserial).to_not contain_child_with_key(:default_cache_function)
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')
end

describe 'when running default_classes action' do
let (:cmd_options) { ['--action', 'default_classes'] }

Expand Down Expand Up @@ -81,6 +92,12 @@ def with_temporary_file(content)

# These are defined in the fixtures/real_agent/cache/lib/puppet/parser/functions
expect(deserial).to contain_child_with_key(:default_cache_function)
# These are defined in the fixtures/real_agent/cache/lib/puppet/functions
expect(deserial).to contain_child_with_key(:default_pup4_function)
# These are defined in the fixtures/real_agent/cache/lib/puppet/functions/environment (Special environent namespace)
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')
end
end

Expand Down Expand Up @@ -153,14 +170,35 @@ def with_temporary_file(content)
deserial = PuppetLanguageServer::Sidecar::Protocol::PuppetFunctionList.new()
expect { deserial.from_json!(result) }.to_not raise_error

# TODO Turn this in a shared example thingy or at least a function
# These are defined in the fixtures/real_agent/cache/lib/...
should_not_contain_default_functions(deserial)

# Puppet 3 API Functions
expect(deserial).to_not contain_child_with_key(:badfile)
expect(deserial).to contain_child_with_key(:bad_function)
expect(deserial).to contain_child_with_key(:fixture_function)

# Puppet 4 API Functions
# The strings based parser will still see 'fixture_pup4_badfile' because it's never _actually_ loaded
# in Puppet therefore it will never error
expect(deserial).to contain_child_with_key(:fixture_pup4_badfile)
# The strings based parser will still see 'badname::fixture_pup4_badname_function' because it's never _actually_ loaded
# in Puppet therefore it will never error
expect(deserial).to contain_child_with_key(:'badname::fixture_pup4_badname_function')
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)

# Make sure the function has the right properties
func = child_with_key(deserial, :fixture_function)
expect(func.doc).to eq('doc_fixture_function')
expect(func.source).to match(/valid_module_workspace/)

# Make sure the function has the right properties
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/)
end
end

Expand Down Expand Up @@ -246,12 +284,28 @@ def with_temporary_file(content)
deserial = PuppetLanguageServer::Sidecar::Protocol::PuppetFunctionList.new()
expect { deserial.from_json!(result) }.to_not raise_error

should_not_contain_default_functions(deserial)

# The strings based parser will still see 'pup4_env_badfile' because it's never _actually_ loaded
# in Puppet therefore it will never error
expect(deserial).to contain_child_with_key(:pup4_env_badfile)
# The strings based parser will still see 'badname::pup4_function' because it's never _actually_ loaded
# in Puppet therefore it will never error
expect(deserial).to contain_child_with_key(:'badname::pup4_function')
expect(deserial).to contain_child_with_key(:env_function)
expect(deserial).to contain_child_with_key(:pup4_env_function)
expect(deserial).to contain_child_with_key(:pup4_env_badfunction)
expect(deserial).to contain_child_with_key(:'profile::pup4_envprofile_function')

# Make sure the function has the right properties
func = child_with_key(deserial, :env_function)
expect(func.doc).to eq('doc_env_function')
expect(func.source).to match(/valid_environment_workspace/)

# Make sure the function has the right properties
func = child_with_key(deserial, :pup4_env_function)
expect(func.doc).to match(/Example function using the Puppet 4 API in a module/)
expect(func.source).to match(/valid_environment_workspace/)
end
end

Expand Down

0 comments on commit a6653a6

Please sign in to comment.