【问题标题】:Undefined local variable or Method - Testing Puppet Modules using Beaker未定义的局部变量或方法 - 使用 Beaker 测试 Puppet 模块
【发布时间】:2018-03-11 05:30:41
【问题描述】:

我对这一切都很陌生。我正在尝试使用 Beaker 测试一个 puppet 模块。我不断收到这个:

 NoMethodError: undefined method `describe' for 
 #Beaker::TestCase:0x007fd6f95e6460
 /Users/user1/beaker/Puppet/puppet-files/spec/classes/unit_spec.rb:3  
 /Users/user1/.rvm/gems/ruby-2.2.7/gems/beaker-3.24.0/bin/beaker:9  
 /Users/user1/.rvm/gems/ruby-2.2.7/bin/ruby_executable_hooks:15  
 /Users/user1/.rvm/gems/ruby-2.2.7/bin/ruby_executable_hooks:15.
 This is the command that I'm running - "beaker --hosts myhost.yaml --pre-suite spec".

我的 unit_spec.rb 包含这个:

  require 'puppetlabs_spec_helper/rake_tasks'

  describe 'application' do
  context 'applied to supported operating system' do
  on_supported_os.each do |os, facts|
  context "#{os}" do
   let(:facts) do
     facts
   end

   context "without any parameters" do
     let(:params) {{ }}

     it { is_expected.to compile.with_all_deps }
     it { is_expected.to contain_class('files') }
     end
    end
   end
  end


    context 'applied to unsupported operating system' do
    describe 'ubuntu-14-x86_64' do
    let(:facts) {{
     :osfamily        => 'Debian',
     :operatingsystem => 'Ubuntu'
      }}

    it { is_expected.to raise_error(Puppet::Error, /Ubuntu not supported/) }
    end
   end
  end

任何帮助将不胜感激!顺便说一句,我正在使用 'puppetlabs_spec_helper/rake_tasks',因为当我刚刚使用 'spec_helper' 时,它给了我一个错误,即使它在那里也无法“加载此类文件”。

另外,我也试过了

RSpec.Describe

这也没有解决问题。我收到以下错误 -

 NameError: undefined local variable or method `on_supported_os' for #Class:0x007f92a61d5e58

我意识到这可能是一个 RSpec Puppet 问题,因为这个模块之前是通过 puppet RSpec 测试的,但是现在我正在尝试使用 Beaker 进行测试,但不太确定如何完全实现这一点!

【问题讨论】:

  • 尝试添加require 'rspec'
  • 我试过了,它仍然给我 - NameError: undefined local variable or method "on_supported_os" for #Class:0x007f92a61d5e58
  • on_supported_os 定义在哪里?您没有在上面的代码中向我们展示。
  • 在快速谷歌之后,我找到了this -- 你用的是这个吗?按照安装说明进行操作;即在某个地方(可能,但不一定,在spec_helper.rb)你需要require 'rspec-puppet-facts'include RspecPuppetFacts
  • 我不知道,就是这样。我正在测试的模块已经包含了 unit_spec.rb 中的所有内容。我什至不确定它是否正确定义。如果我确实需要定义它,我不知道在哪里做。

标签: ruby testing rspec puppet beaker


【解决方案1】:

您将基于单元的测试和基于 VM 的测试混为一谈。

  • rspec-puppet(和rspec-puppet-facts'on_supported_os)用于基于目录的单元测试。它们不需要 VM,并且可以为您提供有关模块语法和逻辑的快速反馈。

  • beaker 和推荐的 beaker-rspec 插件提供完整的端到端测试功能,使用实际 VM 并测试完整的堆栈部署(如您的测试中所定义)。

现有模块的主要入口点通常是 rake。使用rake -T 查看模块中现有的 rake 任务。在编写良好的模块中,它应该同时具有 rspec-puppet(通常称为 spec)和烧杯(通常称为 beakeracceptance)的任务。

如果它是您自己的模块,您可能还想查看新的 Puppet Development Kit 以在单个安装程序中获取最重要的工具。

【讨论】:

  • 感谢您的反馈!我一定会调查的。
猜你喜欢
  • 1970-01-01
  • 2015-10-02
  • 1970-01-01
  • 1970-01-01
  • 2017-01-26
  • 1970-01-01
相关资源
最近更新 更多