Update CODEOWNERS
[puppet-modules/puppetlabs-apt.git] / spec / classes / apt_update_spec.rb
1 # frozen_string_literal: true
2
3 require 'spec_helper'
4
5 describe 'apt::update', type: :class do
6   context "when apt::update['frequency']='always'" do
7     {
8       'a recent run' => Time.now.to_i,
9       'we are due for a run' => 1_406_660_561,
10       'the update-success-stamp file does not exist' => -1
11     }.each_pair do |desc, factval|
12       context "when $apt_update_last_success indicates #{desc}" do
13         let(:facts) do
14           {
15             os: {
16               family: 'Debian',
17               name: 'Debian',
18               release: {
19                 major: '9',
20                 full: '9.0'
21               },
22               distro: {
23                 codename: 'stretch',
24                 id: 'Debian'
25               }
26             },
27             apt_update_last_success: factval
28           }
29         end
30         let(:pre_condition) do
31           "class{'::apt': update => {'frequency' => 'always' },}"
32         end
33
34         it 'triggers an apt-get update run' do
35           # set the apt_update exec's refreshonly attribute to false
36           expect(subject).to contain_exec('apt_update').with('refreshonly' => false)
37         end
38       end
39     end
40     context 'when $apt_update_last_success is nil' do
41       let(:facts) do
42         {
43           os: {
44             family: 'Debian',
45             name: 'Debian',
46             release: {
47               major: '9',
48               full: '9.0'
49             },
50             distro: {
51               codename: 'stretch',
52               id: 'Debian'
53             }
54           }
55         }
56       end
57       let(:pre_condition) { "class{ '::apt': update => {'frequency' => 'always' },}" }
58
59       it 'triggers an apt-get update run' do
60         # set the apt_update exec\'s refreshonly attribute to false
61         expect(subject).to contain_exec('apt_update').with('refreshonly' => false)
62       end
63     end
64
65     context 'when Exec[apt_update] refreshonly is overridden to true and has recent run' do
66       let(:facts) do
67         {
68           os: {
69             family: 'Debian',
70             name: 'Debian',
71             release: {
72               major: '9',
73               full: '9.0'
74             },
75             distro: {
76               codename: 'stretch',
77               id: 'Debian'
78             }
79           },
80           apt_update_last_success: Time.now.to_i
81         }
82       end
83       let(:pre_condition) do
84         "
85         class{'::apt': update => {'frequency' => 'always' },}
86         Exec <| title=='apt_update' |> { refreshonly => true }
87         "
88       end
89
90       it 'skips an apt-get update run' do
91         # set the apt_update exec's refreshonly attribute to false
92         expect(subject).to contain_exec('apt_update').with('refreshonly' => true)
93       end
94     end
95   end
96
97   context "when apt::update['frequency']='reluctantly'" do
98     {
99       'a recent run' => Time.now.to_i,
100       'we are due for a run' => 1_406_660_561,
101       'the update-success-stamp file does not exist' => -1
102     }.each_pair do |desc, factval|
103       context "when $apt_update_last_success indicates #{desc}" do
104         let(:facts) do
105           {
106             os: {
107               family: 'Debian',
108               name: 'Debian',
109               release: {
110                 major: '9',
111                 full: '9.0'
112               },
113               distro: {
114                 codename: 'stretch',
115                 id: 'Debian'
116               }
117             },
118             apt_update_last_success: factval
119           }
120         end
121         let(:pre_condition) { "class{ '::apt': update => {'frequency' => 'reluctantly' },}" }
122
123         it 'does not trigger an apt-get update run' do
124           # don't change the apt_update exec's refreshonly attribute. (it should be true)
125           expect(subject).to contain_exec('apt_update').with('refreshonly' => true)
126         end
127       end
128     end
129     context 'when $apt_update_last_success is nil' do
130       let(:facts) do
131         {
132           os: {
133             family: 'Debian',
134             name: 'Debian',
135             release: {
136               major: '9',
137               full: '9.0'
138             },
139             distro: {
140               codename: 'stretch',
141               id: 'Debian'
142             }
143           }
144         }
145       end
146       let(:pre_condition) { "class{ '::apt': update => {'frequency' => 'reluctantly' },}" }
147
148       it 'does not trigger an apt-get update run' do
149         # don't change the apt_update exec's refreshonly attribute. (it should be true)
150         expect(subject).to contain_exec('apt_update').with('refreshonly' => true)
151       end
152     end
153   end
154
155   ['daily', 'weekly'].each do |update_frequency|
156     context "when apt::update['frequency'] has the value of #{update_frequency}" do
157       pair = { 'we are due for a run' => 1_406_660_561, 'the update-success-stamp file does not exist' => -1 }
158       pair.each_pair do |desc, factval|
159         context "when $apt_update_last_success indicates #{desc}" do
160           let(:facts) do
161             {
162               os: {
163                 family: 'Debian',
164                 name: 'Debian',
165                 release: {
166                   major: '9',
167                   full: '9.0'
168                 },
169                 distro: {
170                   codename: 'stretch',
171                   id: 'Debian'
172                 }
173               },
174               apt_update_last_success: factval
175             }
176           end
177           let(:pre_condition) { "class{ '::apt': update => {'frequency' => '#{update_frequency}',} }" }
178
179           it 'triggers an apt-get update run' do
180             # set the apt_update exec\'s refreshonly attribute to false
181             expect(subject).to contain_exec('apt_update').with('refreshonly' => false)
182           end
183         end
184       end
185       context 'when the $apt_update_last_success fact has a recent value' do
186         let(:facts) do
187           {
188             os: {
189               family: 'Debian',
190               name: 'Debian',
191               release: {
192                 major: '9',
193                 full: '9.0'
194               },
195               distro: {
196                 codename: 'stretch',
197                 id: 'Debian'
198               }
199             },
200             apt_update_last_success: Time.now.to_i
201           }
202         end
203         let(:pre_condition) { "class{ '::apt': update => {'frequency' => '#{update_frequency}',} }" }
204
205         it 'does not trigger an apt-get update run' do
206           # don't change the apt_update exec\'s refreshonly attribute. (it should be true)
207           expect(subject).to contain_exec('apt_update').with('refreshonly' => true)
208         end
209       end
210
211       context 'when $apt_update_last_success is nil' do
212         let(:facts) do
213           {
214             os: {
215               family: 'Debian',
216               name: 'Debian',
217               release: {
218                 major: '9',
219                 full: '9.0'
220               },
221               distro: {
222                 codename: 'stretch',
223                 id: 'Debian'
224               }
225             },
226             apt_update_last_success: nil
227           }
228         end
229         let(:pre_condition) { "class{ '::apt': update => {'frequency' => '#{update_frequency}',} }" }
230
231         it 'triggers an apt-get update run' do
232           # set the apt_update exec\'s refreshonly attribute to false
233           expect(subject).to contain_exec('apt_update').with('refreshonly' => false)
234         end
235       end
236     end
237   end
238 end