Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow testing environment functions #661

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

asharpe
Copy link

@asharpe asharpe commented Mar 8, 2018

This is probably the wrong way to achieve the outcome, it's just what I did to get it working and figured it should be shared.

Scenario

As per https://puppet.com/docs/puppet/5.3/functions_ruby_overview.html#location I have an "environment function" named environment::upcase and I want to test it. My test code looks like

require 'spec_helper'

describe 'environment::upcase' do
 it {
   should run.with_params("a").and_return("A")
 }
end

Issue

Running the tests as-is fails with

     NoMethodError:
       undefined method `func_name' for nil:NilClass
     # ./vendor/gems/ruby/gems/rspec-puppet-2.6.11/lib/rspec-puppet/matchers/run.rb:109:in `func_name'
     # ./vendor/gems/ruby/gems/rspec-puppet-2.6.11/lib/rspec-puppet/matchers/run.rb:135:in `failure_message_generic'
     # ./vendor/gems/ruby/gems/rspec-puppet-2.6.11/lib/rspec-puppet/matchers/run.rb:92:in `failure_message'
     # ./spec/functions/environment__upcase_spec.rb:25:in `block (2 levels) in <top (required)>'

Workaround

This MR allows the test to succeed

# bundle exec rspec
....

Finished in 0.51518 seconds (files took 8.41 seconds to load)
1 examples, 0 failures

@coveralls
Copy link

coveralls commented Mar 8, 2018

Coverage Status

Coverage decreased (-1.5%) to 84.551% when pulling 6e15572 on asharpe:environment-functions into 5cc25d0 on rodjek:master.

@rodjek rodjek added this to the 2.7.x milestone Mar 9, 2018
@scotje scotje added the parser label Oct 15, 2020
@DavidS
Copy link
Collaborator

DavidS commented Oct 26, 2020

Given the pain this is causing to test, and that I see no good reason for environment functions that could not also be addressed by having an environment module for this, I'll bring up the topic of deprecating/removing environment functions with the puppet coremunity team.

Internal ref: https://puppet.slack.com/archives/CF466J67L/p1603713702132700 , https://puppet.slack.com/archives/CF418Q1M2/p1603738590085200

@DavidS
Copy link
Collaborator

DavidS commented Oct 27, 2020

After tracing through the tickets and code changes, I'm still not 100% clear on the history of "environment functions". The best I could determine is they were probably introduced as a side-effect to loader changes for the Puppet 4 function API (see puppetlabs/puppet#2546 puppetlabs/puppet#5455) and then someone decided to document it.

My suggestion of moving those functions to a module called environment is not workable, since that is a keyword in the puppet language, and thus the module can't be called that.

I'm still having a hard time coming up with a use-case where this capability is useful for a production server/agent setup in a way that couldn't also be addressed by having said functions in a module (even if it is not called "environment"). Therefore I'm still in the camp of "it's not worth jumping through all the hoops for getting the test moons aligned just right".

@scotje scotje removed this from the 2.8.x milestone Oct 27, 2020
@scotje
Copy link
Collaborator

scotje commented Oct 27, 2020

Bumping this from the 2.8.x release since it sounds like it may just not get implemented.

@DavidS DavidS self-assigned this Nov 9, 2020
@logicminds
Copy link

logicminds commented Dec 23, 2020

I was having a similar issue with a namespace function. It appears that testing a function only works when the function is in a directory with the module name. Which in this case is: <module>/lib/puppet/functions/environment/upcase.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants