puppetlabs / pdk

The shortest path to better modules: Puppet Development Kit; Download:
https://puppet.com/download-puppet-development-kit
Apache License 2.0
225 stars 106 forks source link

PDK Function mocking breaks in version 2.6.1 templates and above #1239

Closed rnelson0 closed 8 months ago

rnelson0 commented 1 year ago

Describe the bug When updating pdk-templates to 2.6.1 or newer (including the most recent 2.7.1 update), our mock for the function vault_lookup() throws errors during unit testing. We have made no other changes other than pdk update, and using PDK 2.7.1 with pdk-templates 2.5.0 works fine, it is tied to the update of pdk-templates.

To Reproduce On a repo with pdk-templates v2.5.0 and a mocked function, run pdk update to 2.6.1 or newer, then run pdk test unit again.

Expected behavior The tests would continue to behave the same way, pass or fail, as they did with the old pdk templates, and not generate failures based on the function.

Additional context

The mocked function is defined in spec/spec_helper_local.rb:

require 'rspec-puppet-utils'

RSpec.configure do |c|
  c.before :each do
    if ENV['PUPPET_DEBUG']
      Puppet::Util::Log.level = :debug
      Puppet::Util::Log.newdestination(:console)
    end

    MockFunction.new('vault_lookup') do |f|
      f.stubbed.returns(domain: 'prod',
                        username: 'test',
                        password: 'test')
    end

    allow_any_instance_of(Puppet::Type.type(:acl).provider(:windows)).to receive(:validate)                                              # rubocop:disable RSpec/AnyInstance
    allow_any_instance_of(Puppet::Type.type(:acl).provider(:windows)).to receive(:respond_to?).with(:get_account_name).and_return(false) # rubocop:disable RSpec/AnyInstance
    allow_any_instance_of(Puppet::Type.type(:acl).provider(:windows)).to receive(:respond_to?).with(:get_group_name).and_return(false)   # rubocop:disable RSpec/AnyInstance
    allow_any_instance_of(Puppet::Type.type(:acl).provider(:windows)).to receive(:respond_to?).with(:validate).and_return(true)          # rubocop:disable RSpec/AnyInstance
  end
end

Running pdk test unit after the pdk update results in this error over and over:

Failures:

  1) ahchocolatey on windows-2012 R2-x86_64 
     Failure/Error:
       f.stubbed.returns(domain: 'prod',
                         username: 'test',
                         password: 'test')

     NoMethodError:
       undefined method `stubs' for #<RSpecPuppetUtils::MockFunction:0x000055e3ffeff2c0>
       Did you mean?  stub
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-puppet-utils-3.4.0/lib/rspec_puppet_utils/mock_function.rb:38:in `stubbed'
     # /var/lib/jenkins/workspace/EXSRE_puppet-controlrepo_pdk261@2/spec/spec_helper_local.rb:12:in `block (3 levels) in <top (required)>'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-puppet-utils-3.4.0/lib/rspec_puppet_utils/mock_function.rb:26:in `initialize'
     # /var/lib/jenkins/workspace/EXSRE_puppet-controlrepo_pdk261@2/spec/spec_helper_local.rb:11:in `new'
     # /var/lib/jenkins/workspace/EXSRE_puppet-controlrepo_pdk261@2/spec/spec_helper_local.rb:11:in `block (2 levels) in <top (required)>'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/example.rb:457:in `instance_exec'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/example.rb:457:in `instance_exec'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/hooks.rb:365:in `run'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/hooks.rb:529:in `block in run_owned_hooks_for'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/hooks.rb:528:in `each'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/hooks.rb:528:in `run_owned_hooks_for'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/hooks.rb:615:in `block in run_example_hooks_for'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/hooks.rb:614:in `reverse_each'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/hooks.rb:614:in `run_example_hooks_for'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/hooks.rb:484:in `run'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/example.rb:505:in `run_before_example'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/example.rb:261:in `block in run'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/example.rb:511:in `block in with_around_and_singleton_context_hooks'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/example.rb:468:in `block in with_around_example_hooks'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/hooks.rb:486:in `block in run'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/hooks.rb:624:in `run_around_example_hooks_for'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/hooks.rb:486:in `run'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/example.rb:468:in `with_around_example_hooks'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/example.rb:511:in `with_around_and_singleton_context_hooks'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/example.rb:259:in `run'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/example_group.rb:646:in `block in run_examples'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/example_group.rb:642:in `map'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/example_group.rb:642:in `run_examples'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/example_group.rb:607:in `run'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/example_group.rb:608:in `block in run'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/example_group.rb:608:in `map'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/example_group.rb:608:in `run'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/runner.rb:121:in `block (3 levels) in run_specs'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/runner.rb:121:in `map'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/runner.rb:121:in `block (2 levels) in run_specs'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/configuration.rb:2070:in `with_suite_hooks'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/runner.rb:116:in `block in run_specs'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/reporter.rb:74:in `report'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/runner.rb:115:in `run_specs'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/runner.rb:89:in `run'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/runner.rb:71:in `run'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/lib/rspec/core/runner.rb:45:in `invoke'
     # /tmp/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.12.1/exe/rspec:4:in `<main>'
     # 
     #   Showing full backtrace because every line was filtered out.
     #   See docs for RSpec::Configuration#backtrace_exclusion_patterns and
     #   RSpec::Configuration#backtrace_inclusion_patterns for more information.

<repeat a few thousand times>

We have tried changing the code to f.stub, f.stubs, and a few other permutations I've forgotten now and receive the same errors every time. Are we mocking the function incorrectly, or is there some other issue going on? We are quite happy to change the mock rather than anything else, as long as it allows unit tests to function properly. Thank you!

david22swan commented 1 year ago

@rnelson0 From what I can tell it seems likely that the issue has arisen from a change in the rspec version that is being brought in. Could you compare the version that is installed prior the the update and the version that is installed after and see what changes are listed as having happened between them. This brings to mind an issue we had had ourselves with the way in which stubs work.

rnelson0 commented 8 months ago

@david22swan It's been a while since I reported this but it wasn't really related to the PDK and has been resolved.