diff options
author | Thom May <thom@chef.io> | 2016-01-19 11:30:38 +0000 |
---|---|---|
committer | Thom May <thom@chef.io> | 2016-01-20 15:29:06 +0000 |
commit | 354cfa12ce137ef9128fdc6d2fa789e4ad5a60c7 (patch) | |
tree | 1f01ff3c078a5669022d23964521f6e8c7f75036 | |
parent | 8d9bbbcda3b39fd1e0c8d20693c186213fdc8ffe (diff) | |
download | chef-354cfa12ce137ef9128fdc6d2fa789e4ad5a60c7.tar.gz |
Add periodic action as the default
-rw-r--r-- | lib/chef/provider/apt_update.rb | 24 | ||||
-rw-r--r-- | lib/chef/resource/apt_update.rb | 6 | ||||
-rw-r--r-- | spec/unit/provider/apt_update_spec.rb | 20 |
3 files changed, 47 insertions, 3 deletions
diff --git a/lib/chef/provider/apt_update.rb b/lib/chef/provider/apt_update.rb index f321f55018..498c83a3a6 100644 --- a/lib/chef/provider/apt_update.rb +++ b/lib/chef/provider/apt_update.rb @@ -30,11 +30,33 @@ class Chef def load_current_resource end + def action_periodic + if !apt_up_to_date? + converge_by "update new lists of packages" do + shell_out!("apt-get -q update") + end + end + end + def action_update - converge_by "retrieve new lists of packages" do + converge_by "force update new lists of packages" do shell_out!("apt-get -q update") end end + + private + # Determines whether we need to run `apt-get update` + # + # @return [Boolean] + def apt_up_to_date? + if ::File.exist?("/var/lib/apt/periodic/update-success-stamp") && + ::File.mtime("/var/lib/apt/periodic/update-success-stamp") > Time.now - new_resource.frequency + true + else + false + end + end + end end end diff --git a/lib/chef/resource/apt_update.rb b/lib/chef/resource/apt_update.rb index 0492ce8e96..660bcd819f 100644 --- a/lib/chef/resource/apt_update.rb +++ b/lib/chef/resource/apt_update.rb @@ -25,8 +25,10 @@ class Chef resource_name :apt_update provides :apt_update, os: "linux" - default_action :update - allowed_actions :update + property :frequency, Integer, default: 86_400 + + default_action :periodic + allowed_actions :update, :periodic end end end diff --git a/spec/unit/provider/apt_update_spec.rb b/spec/unit/provider/apt_update_spec.rb index 6f89d1b94a..e234cca960 100644 --- a/spec/unit/provider/apt_update_spec.rb +++ b/spec/unit/provider/apt_update_spec.rb @@ -40,4 +40,24 @@ describe Chef::Provider::AptUpdate do expect(new_resource).to be_updated_by_last_action end end + + describe "#action_periodic" do + before do + allow(File).to receive(:exist?).with("/var/lib/apt/periodic/update-success-stamp").and_return(true) + end + + it "should run if the time stamp is old" do + expect(File).to receive(:mtime).with("/var/lib/apt/periodic/update-success-stamp").and_return(Time.now - 86_500) + expect(provider).to receive(:shell_out!).with("apt-get -q update") + provider.run_action(:periodic) + expect(new_resource).to be_updated_by_last_action + end + + it "should not run if the time stamp is new" do + expect(File).to receive(:mtime).with("/var/lib/apt/periodic/update-success-stamp").and_return(Time.now) + expect(provider).to_not receive(:shell_out!).with("apt-get -q update") + provider.run_action(:periodic) + expect(new_resource).to_not be_updated_by_last_action + end + end end |