Chef_spec无法测试curl命令

时间:2018-06-21 09:34:19

标签: chef chefspec

我正在通过厨师食谱中的execute资源安装docker-compose,然后execute命令通知file资源触摸下载的文件,将所有者更改为root并将权限更改为{{1 }}

针对这些资源运行单元测试时,我收到一条失败消息,但没有任何有关失败原因的有用信息

我的包装指南中的相关资源如下

0755

对于单元测试,我先定义上下文,然后按以下步骤对这些资源执行测试

execute 'install_docker_compose' do
    command '/usr/bin/curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose'
    user 'root'
    not_if { ::File.exist?('/usr/local/bin/docker-compose') }
    notifies :touch, 'file[docker_compose_command_file]', :immediately
end

file 'docker_compose_command_file' do
    path '/usr/local/bin/docker-compose'
    mode '0755'
    owner 'root'
    group 'root'
    notifies :create_if_missing, 'template[add_docker_compose_yml]', :immediately
end

然后是测试

describe 'pipeline-jumpstart-chef::default' do
    context "when the cookbook is installed it" do
        let(:chef_run) do
            runner = ChefSpec::ServerRunner.new(platform: 'ubuntu', version: '16.04')
            runner.converge(described_recipe)        
        end
        let(:execute_run) { chef_run.execute('install_docker_compose') }
        let(:docker_compose_command_file) { chef_run.file('/usr/local/bin/docker-compose') }
        let(:compose_template) { chef_run.template('add_docker_compose_yml') }

我说的失败消息无助于找出问题所在

    it 'installs docker compose' do
        # Mock 
        allow(File).to receive(:exists?).with(anything).and_call_original
        allow(File).to receive(:exists?).with('/usr/local/bin/docker-compose').and_return true
        # this fails
        expect(chef_run).to run_execute('install_docker_compose')
        # this passes
        expect(execute_run).to notify('file[docker_compose_command_file]').immediately
    end

    it 'makes docker-compose download exectuable command' do
        expect(chef_run).to touch_file('docker_compose_command_file').with(
            user:   'root',
            group:  'root',
            mode: '0755'
        )
    end

link to source code of entire recipe and spec on github

1 个答案:

答案 0 :(得分:0)

第二个是因为您要求检查touch_file('docker_compose_command_file'),但与食谱中的名称(file '/usr/local/bin/docker-compose')不匹配。虽然第一个不太明显,但我希望它能起作用。您可能需要对File.exist?进行存根处理才能使其正常工作,我猜您正在运行的计算机上确实存在该文件。