Skip to content

Commit

Permalink
(puppetlabsGH-121) Add tests for Puppet 4 API functions
Browse files Browse the repository at this point in the history
This commit adds the test fixtures, and tests, to assert that the sidecar does
indeed load Puppet 4 API functions appropriately and that the feature flag does
not affect the loading of other assets.

Note that function documentation is not available yet and will come in later
commits.
  • Loading branch information
glennsarti committed Apr 10, 2019
1 parent e4b3daa commit 01d125d
Show file tree
Hide file tree
Showing 14 changed files with 140 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 pup4api' do
describe 'PuppetLanguageServerSidecar with Feature Flag pup4api', :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 @@ -81,6 +81,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 +159,27 @@ def with_temporary_file(content)
deserial = PuppetLanguageServer::Sidecar::Protocol::PuppetFunctionList.new()
expect { deserial.from_json!(result) }.to_not raise_error

# 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
expect(deserial).to_not contain_child_with_key(:fixture_pup4_badfile)
expect(deserial).to_not 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 be_nil # Currently we can't get the documentation for v4 functions
expect(func.source).to match(/valid_module_workspace/)
end
end

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

expect(deserial).to_not contain_child_with_key(:pup4_env_badfile)
expect(deserial).to_not 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 be_nil # Currently we can't get the documentation for v4 functions
expect(func.source).to match(/valid_environment_workspace/)
end
end

Expand Down

0 comments on commit 01d125d

Please sign in to comment.