运行 Puppet 模块的单元测试



我想将RSpec单元测试添加到某些Puppet 4模块中。为了开始并确认基本功能,我首先想运行标准Puppet模块(如puppetlabs/apt(附带的单元测试。

如果我尝试像这样运行特定的单元测试

cd modules/apt
rspec spec/classes/apt_spec.rb

我只得到失败,并且某些诊断输出似乎表明Puppet运行时环境的一部分(例如定义函数merge的模块stdlib(未正确拾取:

Failures:
1) apt defaults should contain File[sources.list] that notifies Class[Apt::Update]
Failure/Error:
it { is_expected.to contain_file('sources.list').that_notifies('Class[Apt::Update]').only_with({
:ensure  => 'file',
:path    => '/etc/apt/sources.list',
:owner   => 'root',
:group   => 'root',
:mode    => '0644',
:notify  => 'Class[Apt::Update]',
})}
Puppet::PreformattedError:
Evaluation Error: Unknown function: 'merge'. at /home/MY_USER/modules/apt/spec/fixtures/modules/apt/manifests/init.pp:50:14 on node MY_HOST

如果我尝试像这样运行所有单元测试

rake spec

我收到此错误消息:

rake aborted!
NameError: uninitialized constant Bundler
/home/MY_USER/modules/apt/Rakefile:3:in `<top (required)>'
(See full trace by running task with --trace)

在这些尝试之前,我已经采取了这些步骤来准备运行时环境(在 Debian 主机上(:

mkdir modules
cd modules
puppet module --modulepath=. install puppetlabs-apt # installs also stdlib
sudo gem install rspec-puppet
sudo gem install puppet
sudo gem install puppetlabs_spec_helper
sudo apt-get install rake

我的问题是:如何正确准备运行时环境并成功运行 Puppet Forge 的 Puppet 模块附带的单元测试,例如puppetlabs/apt

要做到这一点,你需要首先确保你安装了Ruby Gems。然后确保你已经安装了bundler,这通常只意味着输入:

$ gem install bundler

接下来,我假设您已经克隆了 apt 模块,因此 cd 到您克隆它的目录中,然后安装您的捆绑包:

$ bundle install

现在,您选择的特定模块存在问题。当我尝试运行捆绑安装时,我得到:

Could not find gem 'puppet-module-posix-default-r2.0' in any of the gem sources listed in your Gemfile or available on this machine.

我目前不确定这意味着什么,有人可能需要提出一个错误。

我希望能够通过以下方式解决它:

$ bundle install --without development system_tests

即使这样也行不通,所以我只是简单地注释掉了这些组。

接下来,使用以下方法运行测试:

$ bundle exec rake spec

这通常会运行测试。但是,我认为Puppet已经打破了公司外部人员的Rspec测试。

我将跟进此事,然后回来更新我已经弄清楚发生了什么的答案。

我将把这个答案留在这里,因为它仍然记录了正确的过程。

另请参阅我关于此主题的博客文章,尽管现在可能有点过时了。如果有什么需要更新的,请告诉我。

最新更新