简体   繁体   English

Rspec puppet 模块测试:访问包含类的自定义事实

[英]Rspec puppet module testing: access to included classes' custom facts

Is there a standard pattern for writing spec tests for a puppet module, that correctly creates the custom facts that are required by the modules that the tested module includes?是否有用于为 puppet 模块编写规范测试的标准模式,该模式可以正确创建被测试模块包含的模块所需的自定义事实

I have a module whose class includes puppetlabs/mongodb , which uses the custom fact ::root_home (which is created by puppetlabs/stdlib).我有一个模块,其类包含puppetlabs/mongodb ,它使用自定义事实 ::root_home (由 puppetlabs/stdlib 创建)。

Looking at the test code for mongodb, (in particular spec_helper_local.rb ), I see code that creates the :root_home fact for testing.查看 mongodb 的测试代码(特别是spec_helper_local.rb ),我看到了创建 :root_home 事实以进行测试的代码。

But, in my own module, unless I do something to create that fact in my own test code, my test fails with "Evaluation Error: Unknown variable," which makes perfect since since nothing in the test suite is creating that fact.但是,在我自己的模块中,除非我在自己的测试代码中执行某些操作来创建该事实,否则我的测试将失败并显示“评估错误:未知变量”,这是完美的,因为测试套件中没有任何内容创建该事实。

Now I could just create the fact in the spec_helper_local.rb file for my module, but that just kicks the problem upstairs to whoever includes my module in theirs.现在我可以在我的模块的 spec_helper_local.rb 文件中创建事实,但这只会将问题踢到楼上的任何将我的模块包含在他们的模块中的人。

How should I deal with this?我该如何处理? Has anyone already written code that recursively descends into included modules and creates the required facts for testing?有没有人已经编写了递归下降到包含的模块并创建测试所需的事实的代码?

Adding the custom fact to your test suite setup is exactly what you should be doing.将自定义事实添加到您的测试套件设置中正是您应该做的。

As you correctly assert, this will not help out the downstream users of your module, but that misses the point of a test suite: fully describing the environment in which a module is evaluated.正如您正确断言的那样,这不会帮助您模块的下游用户,但这错过了测试套件的重点:完全描述评估模块的环境。

You can look into rspec-puppet-facts to setup default facts for all your tests (and gain some other nifty capabilities along the way).您可以查看rspec-puppet-facts为所有测试设置默认事实(并在此过程中获得一些其他漂亮的功能)。

I agree, it is very annoying to have to add mocking to make realistic tests of your use of third party code.我同意,必须添加模拟来对第三方代码的使用进行实际测试是非常烦人的。 It is especially annoying since your tests can fail at any time due to changes in the internals of that code, eg, a sudden introduction of the root_home fact from stdlib.这尤其令人讨厌,因为您的测试随时可能因代码内部的变化而失败,例如,从 stdlib 中突然引入root_home事实。

I have added at the top of my spec/spec_helper.rb :我在我的spec/spec_helper.rb的顶部添加了:

if ENV.include? 'MODULEPATH'
  top_path = File.dirname(File.dirname(__FILE__))
  ENV['FACTERLIB'] = ENV['MODULEPATH'].split(/:/).map do |p|
    p.start_with?('/') ? p : File.join(top_path, p)
  end.map do |p|
    Dir.glob(File.join(p, '*/lib/facter'))
  end.flatten.join(':')
end

When the Facter library loads, it will honour the FACTERLIB environment variable and evaluate all the custom facts supplied by the modules in your environment.当 Facter 库加载时,它将遵循 FACTERLIB 环境变量并评估您环境中模块提供的所有自定义事实。

A typical value for MODULEPATH is "design:modules" when testing environments, and "spec/fixtures/modules" when testing modules. MODULEPATH的典型值在测试环境时是“design:modules”,在测试模块时是“spec/fixtures/modules”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM