Merge pull request #375 from raphink/dev/facts_perfs
[puppet-modules/puppetlabs-apt.git] / spec / classes / apt_update_spec.rb
1 #!/usr/bin/env rspec
2 require 'spec_helper'
3
4 describe 'apt::update', :type => :class do
5   context 'when apt::always_apt_update is true' do
6     #This should completely disable all of this logic. These tests are to guarantee that we don't somehow magically change the behavior.
7     let(:facts) { { :lsbdistid => 'Debian', :osfamily => 'Debian' } }
8     let (:pre_condition) { "class{'::apt': always_apt_update => true}" }
9     it 'should trigger an apt-get update run' do
10       #set the apt_update exec's refreshonly attribute to false
11       should contain_exec('apt_update').with({'refreshonly' => false })
12     end
13     ['always','daily','weekly','reluctantly'].each do |update_frequency|
14       context "when apt::apt_update_frequency has the value of #{update_frequency}" do
15         { 'a recent run' => Time.now.to_i, 'we are due for a run' => 1406660561,'the update-success-stamp file does not exist' => -1 }.each_pair do |desc, factval|
16           context "and $::apt_update_last_success indicates #{desc}" do
17             let(:facts) { { :lsbdistid => 'Debian', :osfamily => 'Debian', :apt_update_last_success => factval } }
18             let (:pre_condition) { "class{'::apt': always_apt_update => true, apt_update_frequency => '#{update_frequency}' }" }
19             it 'should trigger an apt-get update run' do
20               # set the apt_update exec's refreshonly attribute to false
21               should contain_exec('apt_update').with({'refreshonly' => false})
22             end
23           end
24           context 'when $::apt_update_last_success is nil' do
25             let(:facts) { { :lsbdistid => 'Debian', :osfamily => 'Debian' } }
26             let (:pre_condition) { "class{'::apt': always_apt_update => true, apt_update_frequency => '#{update_frequency}' }" }
27             it 'should trigger an apt-get update run' do
28               #set the apt_update exec\'s refreshonly attribute to false
29               should contain_exec('apt_update').with({'refreshonly' => false})
30             end
31           end
32         end
33       end
34     end
35   end
36
37   context 'when apt::always_apt_update is false' do
38     context "and apt::apt_update_frequency has the value of always" do
39       { 'a recent run' => Time.now.to_i, 'we are due for a run' => 1406660561,'the update-success-stamp file does not exist' => -1 }.each_pair do |desc, factval|
40         context "and $::apt_update_last_success indicates #{desc}" do
41           let(:facts) { { :lsbdistid => 'Debian', :osfamily => 'Debian', :apt_update_last_success => factval } }
42           let (:pre_condition) { "class{'::apt': always_apt_update => false, apt_update_frequency => 'always' }" }
43           it 'should trigger an apt-get update run' do
44             #set the apt_update exec's refreshonly attribute to false
45             should contain_exec('apt_update').with({'refreshonly' => false})
46           end
47         end
48       end
49       context 'when $::apt_update_last_success is nil' do
50         let(:facts) { { :lsbdistid => 'Debian', :osfamily => 'Debian' } }
51         let (:pre_condition) { "class{ '::apt': always_apt_update => false, apt_update_frequency => 'always' }" }
52         it 'should trigger an apt-get update run' do
53           #set the apt_update exec\'s refreshonly attribute to false
54           should contain_exec('apt_update').with({'refreshonly' => false})
55         end
56       end
57     end
58     context "and apt::apt_update_frequency has the value of reluctantly" do
59       {'a recent run' => Time.now.to_i, 'we are due for a run' => 1406660561,'the update-success-stamp file does not exist' => -1 }.each_pair do |desc, factval|
60         context "and $::apt_update_last_success indicates #{desc}" do
61           let(:facts) { { :lsbdistid => 'Debian', :osfamily => 'Debian', :apt_update_last_success => factval} }
62           let (:pre_condition) { "class{ '::apt': always_apt_update => false, apt_update_frequency => 'reluctantly' }" }
63           it 'should not trigger an apt-get update run' do
64             #don't change the apt_update exec's refreshonly attribute. (it should be true)
65             should contain_exec('apt_update').with({'refreshonly' => true})
66           end
67         end
68       end
69       context 'when $::apt_update_last_success is nil' do
70         let(:facts) { { :lsbdistid => 'Debian', :osfamily => 'Debian' } }
71         let (:pre_condition) { "class{ '::apt': always_apt_update => false, apt_update_frequency => 'reluctantly' }" }
72         it 'should not trigger an apt-get update run' do
73           #don't change the apt_update exec's refreshonly attribute. (it should be true)
74           should contain_exec('apt_update').with({'refreshonly' => true})
75         end
76       end
77     end
78     ['daily','weekly'].each do |update_frequency|
79       context "and apt::apt_update_frequency has the value of #{update_frequency}" do
80         { 'we are due for a run' => 1406660561,'the update-success-stamp file does not exist' => -1 }.each_pair do |desc, factval|
81           context "and $::apt_update_last_success indicates #{desc}" do
82             let(:facts) { { :lsbdistid => 'Debian', :osfamily => 'Debian', :apt_update_last_success => factval } }
83             let (:pre_condition) { "class{ '::apt': always_apt_update => false, apt_update_frequency => '#{update_frequency}' }" }
84             it 'should trigger an apt-get update run' do
85               #set the apt_update exec\'s refreshonly attribute to false
86               should contain_exec('apt_update').with({'refreshonly' => false})
87             end
88           end
89         end
90         context 'when the $::apt_update_last_success fact has a recent value' do
91           let(:facts) { { :lsbdistid => 'Debian', :osfamily => 'Debian', :apt_update_last_success => Time.now.to_i } }
92           let (:pre_condition) { "class{ '::apt': always_apt_update => false, apt_update_frequency => '#{update_frequency}' }" }
93           it 'should not trigger an apt-get update run' do
94             #don't change the apt_update exec\'s refreshonly attribute. (it should be true)
95             should contain_exec('apt_update').with({'refreshonly' => true})
96           end
97         end
98         context 'when $::apt_update_last_success is nil' do
99           let(:facts) { { :lsbdistid => 'Debian', :osfamily => 'Debian' } }
100           let (:pre_condition) { "class{ '::apt': always_apt_update => false, apt_update_frequency => '#{update_frequency}' }" }
101           it 'should trigger an apt-get update run' do
102             #set the apt_update exec\'s refreshonly attribute to false
103             should contain_exec('apt_update').with({'refreshonly' => false})
104           end
105         end
106       end
107     end
108   end
109 end