如何使用 ChefSpec 测试我的 LWRP?

2024-02-25

我创建了自定义 LWRP,但是当我运行ChefSpec https://github.com/sethvargo/chefspec单元测试。它不知道我的 LWRP 操作。

这是我的resource:

actions :install, :uninstall
default_action :install

attribute :version, :kind_of => String
attribute :options, :kind_of => String

这是我的provider:

def whyrun_supported?
  true
end

action :install do
  version = @new_resource.version
  options = @new_resource.options
  e = execute "sudo apt-get install postgresql-#{version} #{options}"
  @new_resource.updated_by_last_action(e.updated_by_last_action?)
end

这是我的 ChefSpec 单元测试:

require_relative '../spec_helper'

describe 'app_cookbook::postgresql' do

  let(:chef_run) do
    ChefSpec::Runner.new do |node|
      node.set[:app][:postgresql][:database_user] = 'test'
      node.set[:app][:postgresql][:database_password] = 'test'
      node.set[:app][:postgresql][:database_name] = 'testdb'
    end.converge(described_recipe)
  end

  it 'installs postgresql 9.1 package' do
    expect(chef_run).to run_execute('sudo apt-get install postgresql-9.1 -y --force-yes')
  end
end

这是控制台输出:

app_cookbook::postgresql

expected "execute[sudo apt-get install postgresql-9.1 -y --force-yes] with" action :run to be in Chef run. Other execute resources:

./spec/recipes/postgresql_spec.rb:23:in `block (2 levels) in <top (required)>'
  installs postgresql 9.1 package (FAILED - 1)

Failures:

  1) app_cookbook::postgresql installs postgresql 9.1 package
     Failure/Error: expect(chef_run).to run_execute('sudo apt-get install postgresql-9.1 -y --force-yes')
       expected "execute[sudo apt-get install postgresql-9.1 -y --force-yes] with" action :run to be in Chef run. Other execute resources:

     # ./spec/recipes/postgresql_spec.rb:23:in `block (2 levels) in <top (required)>'

1 example, 1 failure, 0 passed

我该如何对 ChefSpec 说使用 LWRP 操作运行测试?


您需要告诉 Chefspec 介入您的资源。您可以按如下方式执行此操作:

require_relative '../spec_helper'

describe 'app_cookbook::postgresql' do

  let(:chef_run) do
    ChefSpec::Runner.new(step_into: ['my_lwrp']) do |node|
      node.set[:app][:postgresql][:database_user] = 'test'
      node.set[:app][:postgresql][:database_password] = 'test'
      node.set[:app][:postgresql][:database_name] = 'testdb'
    end.converge(described_recipe)
  end

  it 'installs postgresql 9.1 package' do
    expect(chef_run).to run_execute('sudo apt-get install postgresql-9.1 -y --force-yes')
  end
end

您可以将 my_lwrp 替换为您想要单步执行的资源。

有关更多详细信息,请参阅测试 LWRPS https://github.com/sethvargo/chefspec#testing-lwrpsChefspec 存储库自述文件中的部分。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 ChefSpec 测试我的 LWRP? 的相关文章

随机推荐

  • ASP.NET MVC 4 - 用于嵌套集合的 EditorTemplate

    我有以下模型类 为了解决这个问题而简化了类 public class Lesson public Guid Id get set public string Name get set public List
  • 触发复选框的更改事件

    我有 7 个复选框 A B C D E F G 当我单击其中之一时 该值将附加到文本区域 如果我取消选中一项 该值将从文本区域中删除 jquery代码如下 var checkboxes input type checkbox checkbo
  • facet_grid 中每个图基于密度的独立 alpha

    我正在构建一个facet grid using stat hexbin但是我希望每个方面图的 alpha 值都是独立的 我目前正在使用以下代码 ggplot data aes x y fill z stat binhex bins 20 a
  • 在 Bash 中连接变量[重复]

    这个问题在这里已经有答案了 我正在尝试将一个变量添加到变量的中间 因此例如在 PHP 中我会这样做 mystring arg1 12 arg2 endoffile 所以输出可能是20121201endoffile 如何在 Linux bas
  • E492:不是编辑器命令:NERDTree

    当我跑步时vim 打开文件夹时出现以下错误 Error detected while processing VimEnter Auto commands for E492 Not an editor command NERDTree Pre
  • Firefox 插件的“document_start”?

    我来自 Chrome 扩展 所以我习惯于通过设置来定义何时注入文件run at 例如 到document start用于在 DOM 构建之前进行注入 Firefox 插件有等效的吗 是的 相当于content document global
  • 用户提供的 Terminate() 函数必须是线程安全的吗?

    如中所述http en cppreference com w cpp error terminate http en cppreference com w cpp error terminate调用终止的原因有很多 我可以想象其中一些原因几
  • 在 Ruby 数组中收集重复项的最快/单行方法?

    转换数组的最快 单行方法是什么 1 1 1 1 2 2 3 5 5 5 8 13 21 21 21 到这样的对象数组中 1 gt 4 2 gt 2 3 gt 1 5 gt 3 8 gt 1 13 gt 1 21 gt 3 为了实现您想要的格
  • 如何等待WPF绑定延迟完成

    我的 ViewModel 实现了 INotifyPropertyChanged 和 INotifyDataErrorInfo 接口 当属性更改时 将触发验证 从而启用 禁用 保存 按钮 由于验证步骤非常耗时 因此我使用了 延迟 绑定属性 我
  • GAPI 的 OAuth - 首次登录 Javascript 后避免身份验证和授权

    我创建了一个 chrome 扩展 可以读取电子邮件 执行某些操作并使用 javascript 的 google 客户端 API 创建任务 我使用 chrome 身份进行身份验证和授权 扩展按预期工作 然而 它每隔一段时间就会要求签名 我想要
  • Boost::Log 和日志编号

    我最近尝试通过 Boost log 将一些日志记录添加到一个小型应用程序中 但是 使用日志轮换 由于某种原因 我无法让它在日志目录中拾取正确的计数器 例如 如果我的Logs目录包含文件Log 000 log and Log 001 log我
  • 将 javascript 注入 SwiftUI WebKit

    如何将 SwiftUI WebKit 视图的文本大小设置为 State 变量 我有这段代码 允许我在 SwiftUI 中使用 WebViews import SwiftUI import WebKit struct WebView UIVi
  • 如何在 Angular 2 中实现 Chart.js?

    我正在使用最新版本的 Angular 2 V4 0 0 并且我想在我的项目中使用 Chart js 库中的图表 而不会有太多复杂性 如何在我的 Angular 项目中实现 Chart js 并且不会在最终生产中给我带来问题 您可以按照以下说
  • 如何在Opencart中添加子选项?

    我使用的是最新的3 0 2 0版本的opencart 对于选项 我想添加子选项 我也想像选项一样对子选项进行定价 从 2009 年及更早的时间起 有很多关于 opencart 的依赖选项的讨论 但它从未出现在核心版本中 为什么大家都说实现依
  • 有任何 API 或 Web UI 项目来管理 Docker 私有注册表吗?

    我找不到如何管理私人注册表中的图像 我可以推送或拉取图像 因为我知道 id 但如何获取推送图像的列表 举例来说 一个人想要查看其组织的私人注册表下的可用图像 她该怎么办 除非我弄错了 否则我找不到 API 或 Web UI 来发现注册表内容
  • 更改 Mac/Linux 上进程的用户所有者?

    我有一个以 root 身份运行的程序 该应用程序调用另一个程序 processA 来运行 当 processA 运行时 它由 root 拥有 但我希望它的所有者是当前登录的用户 怎么做 嗯 这有点棘手 取决于它是守护进程 服务 还是运行此命
  • 可以从 Google Chrome 设置面板中的地址栏(小书签)运行 Javascript 吗?

    我想制作一个书签来打开谷歌浏览器的设置面板并通过单击清除我的缓存 一段时间以来 我有一个书签 可以打开 Chrome 的设置面板 并已选择 清除缓存 设置 单击书签 通常在新选项卡中打开它 后 我必须打开该选项卡并提交表单 然而 在开发时
  • msiexec MsiSetExternalUI 的外部处理程序

    早上好 我希望为 msiexec 实现一个外部处理程序 但不能阻止 msiexec 执行任何不同的操作 我希望能够利用 MsiSetExternalUI 拦截所有消息 但仍然对最终用户具有相同的响应和对话框 我的主要目标是拦截安装 msi
  • UIImage 方面适合并对齐到顶部

    看起来像aspect fit默认情况下将图像与框架底部对齐 有没有办法override对齐同时保持aspect fit intact 编辑 这个问题早于自动布局 事实上 在提出这个问题的同一周 汽车布局就在 WWDC 2012 上公布了 简
  • 如何使用 ChefSpec 测试我的 LWRP?

    我创建了自定义 LWRP 但是当我运行ChefSpec https github com sethvargo chefspec单元测试 它不知道我的 LWRP 操作 这是我的resource actions install uninstal