Anadi Misra
Anadi Misra

Reputation: 2103

chef_spec failure for testing curl command

I am installing docker-compose through the execute resource in the chef cookbook, the execute command then notifies a file resource to touch the downloaded file, change owner to root and permission to 0755

While running the unit tests for these resources I get a failure message without any helpful details on why is it failing

the relevant resources from my wrapper cookbook are as follows

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

For the unit tests I define contexts and then execute tests for these resources as follows

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') }

and then the tests

    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

the failure message as I said doesn't help figuring out what's gone wrong

1) pipeline-jumpstart-chef::default when the cookbook is installed it installs docker compose
    Failure/Error: expect(chef_run).to run_execute('install_docker_compose')

    expected "execute[install_docker_compose]" with action :run to be in Chef run. Other execute resources:

        execute[install_docker_compose]
        execute[start_docker_containers]

    # ./spec/unit/recipes/docker_spec.rb:50:in `block (3 levels) in <top (required)

  2) pipeline-jumpstart-chef::default when the cookbook is installed it makes docker-compose download exectuable command
 Failure/Error:
   expect(chef_run).to touch_file('docker_compose_command_file').with(
       user:   'root',
       group:  'root',
       mode: '0755'
   )

   expected "file[docker_compose_command_file]" with action :touch to be in Chef run. Other file resources:

     file[docker_compose_command_file]

 # ./spec/unit/recipes/docker_spec.rb:55:in `block (3 levels) in <top (required)>'

link to source code of entire recipe and spec on github

Upvotes: 0

Views: 267

Answers (1)

coderanger
coderanger

Reputation: 54249

The second one is because you asked to check for touch_file('docker_compose_command_file'), but that doesn't match the name in the recipe (file '/usr/local/bin/docker-compose'). The first is less obvious though, I would expect that to work. You probably need to stub File.exist? to make it work, I'm guessing that file does exist on the machine you are running on.

Upvotes: 0

Related Questions