(maint) Remove duplicated fact declarations (#828)
[puppet-modules/puppetlabs-apt.git] / spec / classes / apt_update_spec.rb
1 require 'spec_helper'
2
3 describe 'apt::update', type: :class do
4   context "when apt::update['frequency']='always'" do
5     {
6       'a recent run'                                 => Time.now.to_i,
7       'we are due for a run'                         => 1_406_660_561,
8       'the update-success-stamp file does not exist' => -1,
9     }.each_pair do |desc, factval|
10       context "when $::apt_update_last_success indicates #{desc}" do
11         let(:facts) do
12           {
13             os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
14             lsbdistid: 'Debian',
15             osfamily: 'Debian',
16             apt_update_last_success: factval,
17             lsbdistcodename: 'jessie',
18           }
19         end
20         let(:pre_condition) do
21           "class{'::apt': update => {'frequency' => 'always' },}"
22         end
23
24         it 'triggers an apt-get update run' do
25           # set the apt_update exec's refreshonly attribute to false
26           is_expected.to contain_exec('apt_update').with('refreshonly' => false)
27         end
28       end
29     end
30     context 'when $::apt_update_last_success is nil' do
31       let(:facts) do
32         {
33           os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
34           lsbdistid: 'Debian',
35           osfamily: 'Debian',
36           lsbdistcodename: 'jessie',
37         }
38       end
39       let(:pre_condition) { "class{ '::apt': update => {'frequency' => 'always' },}" }
40
41       it 'triggers an apt-get update run' do
42         # set the apt_update exec\'s refreshonly attribute to false
43         is_expected.to contain_exec('apt_update').with('refreshonly' => false)
44       end
45     end
46   end
47   context "when apt::update['frequency']='reluctantly'" do
48     {
49       'a recent run'                                 => Time.now.to_i,
50       'we are due for a run'                         => 1_406_660_561,
51       'the update-success-stamp file does not exist' => -1,
52     }.each_pair do |desc, factval|
53       context "when $::apt_update_last_success indicates #{desc}" do
54         let(:facts) do
55           {
56             os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
57             lsbdistid: 'Debian',
58             osfamily: 'Debian',
59             apt_update_last_success: factval,
60             lsbdistcodename: 'jessie',
61           }
62         end
63         let(:pre_condition) { "class{ '::apt': update => {'frequency' => 'reluctantly' },}" }
64
65         it 'does not trigger an apt-get update run' do
66           # don't change the apt_update exec's refreshonly attribute. (it should be true)
67           is_expected.to contain_exec('apt_update').with('refreshonly' => true)
68         end
69       end
70     end
71     context 'when $::apt_update_last_success is nil' do
72       let(:facts) do
73         {
74           os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
75           lsbdistid: 'Debian',
76           osfamily: 'Debian',
77           lsbdistcodename: 'jessie',
78         }
79       end
80       let(:pre_condition) { "class{ '::apt': update => {'frequency' => 'reluctantly' },}" }
81
82       it 'does not trigger an apt-get update run' do
83         # don't change the apt_update exec's refreshonly attribute. (it should be true)
84         is_expected.to contain_exec('apt_update').with('refreshonly' => true)
85       end
86     end
87   end
88   ['daily', 'weekly'].each do |update_frequency|
89     context "when apt::update['frequency'] has the value of #{update_frequency}" do
90       { 'we are due for a run' => 1_406_660_561, 'the update-success-stamp file does not exist' => -1 }.each_pair do |desc, factval|
91         context "when $::apt_update_last_success indicates #{desc}" do
92           let(:facts) do
93             {
94               os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
95               lsbdistid: 'Debian',
96               osfamily: 'Debian',
97               apt_update_last_success: factval,
98               lsbdistcodename: 'jessie',
99             }
100           end
101           let(:pre_condition) { "class{ '::apt': update => {'frequency' => '#{update_frequency}',} }" }
102
103           it 'triggers an apt-get update run' do
104             # set the apt_update exec\'s refreshonly attribute to false
105             is_expected.to contain_exec('apt_update').with('refreshonly' => false)
106           end
107         end
108       end
109       context 'when the $::apt_update_last_success fact has a recent value' do
110         let(:facts) do
111           {
112             os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
113             lsbdistid: 'Debian',
114             osfamily: 'Debian',
115             lsbdistcodename: 'jessie',
116             apt_update_last_success: Time.now.to_i,
117           }
118         end
119         let(:pre_condition) { "class{ '::apt': update => {'frequency' => '#{update_frequency}',} }" }
120
121         it 'does not trigger an apt-get update run' do
122           # don't change the apt_update exec\'s refreshonly attribute. (it should be true)
123           is_expected.to contain_exec('apt_update').with('refreshonly' => true)
124         end
125       end
126       context 'when $::apt_update_last_success is nil' do
127         let(:facts) do
128           {
129             os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
130             lsbdistid: 'Debian',
131             osfamily: 'Debian',
132             lsbdistcodename: 'jessie',
133             apt_update_last_success: nil,
134           }
135         end
136         let(:pre_condition) { "class{ '::apt': update => {'frequency' => '#{update_frequency}',} }" }
137
138         it 'triggers an apt-get update run' do
139           # set the apt_update exec\'s refreshonly attribute to false
140           is_expected.to contain_exec('apt_update').with('refreshonly' => false)
141         end
142       end
143     end
144   end
145 end