首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ChefSpec测试目录存在

ChefSpec测试目录存在
EN

Stack Overflow用户
提问于 2019-01-22 05:41:51
回答 1查看 457关注 0票数 0

我正在尝试编写一个ChefSpec测试,以检查配方是否仅在目录不存在时才创建该目录。我通过了“创建目录”的第一次测试,但第二次测试失败了。食谱在下面。有没有人能帮我把第二部分弄对?因为如果该目录存在,则第一次测试将失败。我必须删除该目录才能使第一个测试通过,然后第二个测试无论如何都会失败。

代码语言:javascript
复制
require 'spec_helper'

describe 'my_cookbook::default' do
  context 'Windows 2012' do
    let(:chef_run) do
      runner = ChefSpec::ServerRunner.new(platform: 'Windows', version: '2012')
      runner.converge(described_recipe)
    end

    it 'converges successfully' do
      expect { chef_run }.to_not raise_error
    end

    it 'creates directory' do
      expect(chef_run).to create_directory('D:\test1\logs')
    end

    it 'checks directory' do
      expect(chef_run).to_not create_directory( ::Dir.exists?("D:\\test1\\logs") )
    end
  end
end

这就是它的配方,它本身就像预期的那样工作,但我似乎无法围绕它写一个测试。

代码语言:javascript
复制
directory "D:\\test1\\logs" do
  recursive true
  action :create
  not_if { ::Dir.exists?("D:\\test1\\logs") }
end
EN

回答 1

Stack Overflow用户

发布于 2019-01-24 14:58:28

not_ifonly_if是厨师guards

然后使用

属性来告诉chef-client它是否应该继续执行资源

为了使用chefspec测试您的directory resource,您将必须将保护存根,以便在chefspec编译您的资源时,您希望not_if保护的计算结果为真或假。

为了让ChefSpec知道如何评估资源,我们需要告诉它,如果命令在实际计算机上运行,该命令将如何返回此测试:

代码语言:javascript
复制
describe 'something' do
  recipe do
    execute '/opt/myapp/install.sh' do
      # Check if myapp is installed and runnable.
      not_if 'myapp --version'
    end
  end

  before do
    # Tell ChefSpec the command would have succeeded.
    stub_command('myapp --version').and_return(true)
    # Tell ChefSpec the command would have failed.
    stub_command('myapp --version').and_return(false)
    # You can also use a regexp to stub multiple commands at once.
    stub_command(/^myapp/).and_return(false)
  end
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54298134

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档