puppet - 如何使用 rspec 在 puppet 中模拟 puppet validate_cmd?

标签 puppet rspec-puppet

我有一个 puppet 食谱 rspec 示例测试,如下所示

it {
  is_expected.to contain_file('/etc/resolv.conf')
    .with(
      'ensure' => 'file',
      'owner' => 'root',
      'group' => 'root',
      'mode' => '0444',
      'validate_cmd' => '/var/tmp/checkdns_nameservers.sh',
    )
    .with_content(resolvconf_contents)
    .that_notifies('Service[sshd]')
    .that_notifies('Service[nscd]')
}

问题是这只是检查 list 中是否存在 validate_cmd。

我必须模拟 validate_cmd 函数并返回 true/false,并基于此我必须检查新创建的文件的内容。 如何使用 rspec 模块模拟 validate_cmd 命令? 任何帮助将不胜感激!!

最佳答案

简短的回答是,这是不可能的,您需要使用 Beaker、Test Kitchen 等工具。

要获得更长的答案,我会将您引导至 Rspec-puppet tutorial :

There are a lot of people confused by the purpose of these tests as they can’t test the result of the manifest on a live system. That is not the point of rspec-puppet. Rspec-puppet tests are there to test the behaviour of Puppet when it compiles your manifests into a catalogue of Puppet resources.

关键点是 Rspec-puppet 永远不会应用它编译的目录;它只检查编译后的目录并允许您对其中的内容做出断言。但要观察响应 validate_cmd 功能差异的行为,您确实需要在真实系统上应用该目录。

当然,在 Beaker 或 Test Kitchen 中设置系统以使您的脚本返回两个不同的值也不是一件容易的事。此外,您真正要测试的是什么。你不是在测试Puppet本身是否做到了它 promise 的事情吗?如果是这样,您真的不应该尝试测试它。

或者,这是你自己写的脚本吗?如果是这样,请考虑使用 Bash 单元测试框架,例如 shunit2BATS反而。相反,请证明您的 shell 脚本的行为符合您期望的操作系统响应方式。

关于puppet - 如何使用 rspec 在 puppet 中模拟 puppet validate_cmd?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55981035/

相关文章:

xml - Puppet Augeas - 无法更新包含 CDATA 的 xml 节点中的值

Vagrant+Puppet puppet.module_path 不起作用

git - 您如何在 Git for Puppet 中安全地存储 TLS/SSL key 文件?

amazon-web-services - Amazon VPC 中的 Puppet Behavior 不一致

puppet - hiera 合并不起作用

ruby - 为什么在切换 ruby​​ 版本时 rspec puppet 会因未定义的方法错误而失败?

testing - Puppet:rake spec 找不到 class::splunk

rspec - 如何覆盖 rspec-puppet 中的 hiera_data?