1

While building out some recipes we discovered that triggering an apt_update as part of the flow can greatly slow things down if the system is already in a good state but the update runs anyways.

To work around this we made an apt_update call with action :nothing and then set up anything that actually requires the update to notify :before or :immediately (like before installing a package or after adding an apt repository, though the repository case can be controlled via a flag when adding).

We would like to test that the apt_update call is only triggered when necessary, and doesn't run as part of a converge that wouldn't otherwise install packages.

apt_update 'update' do
  action :nothing
end

apt_repository 'git-core' do
  uri          'ppa:git-core/ppa'
  distribution node['lsb']['codename']
  notifies :update, 'apt_update[update]', :immediately
end
dragon788
  • 3,583
  • 1
  • 40
  • 49

2 Answers2

1

It took me forever to find code that was actually testing apt_update and even longer to make sense out of it, and then even longer when I wanted to test that it was always running apt_update at the correct time when I needed it triggered early during the compile phase rather than the converge phase.

it 'triggered apt update' do
  ppa_call = chef_run.apt_repository('git-core')
  expect(ppa_call).to notify('apt_update[update]').to(:update).immediately
end

it 'default apt update' do
  expect(chef_run).to update_apt_update('update')
end

it 'install git' do
  expect(chef_run).to install_apt_package('git')
end

I can't remember where I found the conditionally triggered syntax, but after looking again for the regular apt_update test I finally found it in the chefspec examples. https://github.com/chefspec/chefspec/blob/master/examples/apt_update/spec/update_spec.rb

dragon788
  • 3,583
  • 1
  • 40
  • 49
0

Run kitchen converge to check if apt_repository gets executed every time you run it or skips if repository already exists. I am not sure if that resource is idempotent. If its not idempotent, it will definitely notify update every time it runs. I would suggest to add a guard clause for apt_repository so it skips if repository already exists.

Unit tests won't be able to confirm that update will be only called when a new repository is been added.

slashpai
  • 1,141
  • 7
  • 12