(GH-cat-9) Update module to match current syntax standard
[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::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           is_expected.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         is_expected.to contain_exec('apt_update').with('refreshonly' => false)
62       end
63     end
64     context 'and Exec[apt_update] refreshonly is overridden to true and has recent run' do
65       let(:facts) do
66         {
67           os: {
68             family: 'Debian',
69             name: 'Debian',
70             release: {
71               major: '9',
72               full: '9.0',
73             },
74             distro: {
75               codename: 'stretch',
76               id: 'Debian',
77             },
78           },
79           'apt::apt_update_last_success': Time.now.to_i,
80         }
81       end
82       let(:pre_condition) do
83         "
84         class{'::apt': update => {'frequency' => 'always' },}
85         Exec <| title=='apt_update' |> { refreshonly => true }
86         "
87       end
88
89       it 'skips an apt-get update run' do
90         # set the apt_update exec's refreshonly attribute to false
91         is_expected.to contain_exec('apt_update').with('refreshonly' => true)
92       end
93     end
94   end
95   context "when apt::update['frequency']='reluctantly'" do
96     {
97       'a recent run'                                 => Time.now.to_i,
98       'we are due for a run'                         => 1_406_660_561,
99       'the update-success-stamp file does not exist' => -1,
100     }.each_pair do |desc, factval|
101       context "when $apt_update_last_success indicates #{desc}" do
102         let(:facts) do
103           {
104             os: {
105               family: 'Debian',
106               name: 'Debian',
107               release: {
108                 major: '9',
109                 full: '9.0',
110               },
111               distro: {
112                 codename: 'stretch',
113                 id: 'Debian',
114               },
115             },
116             'apt::apt_update_last_success': factval,
117           }
118         end
119         let(:pre_condition) { "class{ '::apt': update => {'frequency' => 'reluctantly' },}" }
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     end
127     context 'when $apt_update_last_success is nil' do
128       let(:facts) do
129         {
130           os: {
131             family: 'Debian',
132             name: 'Debian',
133             release: {
134               major: '9',
135               full: '9.0',
136             },
137             distro: {
138               codename: 'stretch',
139               id: 'Debian',
140             },
141           },
142         }
143       end
144       let(:pre_condition) { "class{ '::apt': update => {'frequency' => 'reluctantly' },}" }
145
146       it 'does not trigger an apt-get update run' do
147         # don't change the apt_update exec's refreshonly attribute. (it should be true)
148         is_expected.to contain_exec('apt_update').with('refreshonly' => true)
149       end
150     end
151   end
152   ['daily', 'weekly'].each do |update_frequency|
153     context "when apt::update['frequency'] has the value of #{update_frequency}" do
154       { 'we are due for a run' => 1_406_660_561, 'the update-success-stamp file does not exist' => -1 }.each_pair do |desc, factval|
155         context "when $apt_update_last_success indicates #{desc}" do
156           let(:facts) do
157             {
158               os: {
159                 family: 'Debian',
160                 name: 'Debian',
161                 release: {
162                   major: '9',
163                   full: '9.0',
164                 },
165                 distro: {
166                   codename: 'stretch',
167                   id: 'Debian',
168                 },
169               },
170               'apt::apt_update_last_success': factval,
171             }
172           end
173           let(:pre_condition) { "class{ '::apt': update => {'frequency' => '#{update_frequency}',} }" }
174
175           it 'triggers an apt-get update run' do
176             # set the apt_update exec\'s refreshonly attribute to false
177             is_expected.to contain_exec('apt_update').with('refreshonly' => false)
178           end
179         end
180       end
181       context 'when the $apt_update_last_success fact has a recent value' do
182         let(:facts) do
183           {
184             os: {
185               family: 'Debian',
186               name: 'Debian',
187               release: {
188                 major: '9',
189                 full: '9.0',
190               },
191               distro: {
192                 codename: 'stretch',
193                 id: 'Debian',
194               },
195             },
196             'apt::apt_update_last_success': Time.now.to_i,
197           }
198         end
199         let(:pre_condition) { "class{ '::apt': update => {'frequency' => '#{update_frequency}',} }" }
200
201         it 'does not trigger an apt-get update run' do
202           # don't change the apt_update exec\'s refreshonly attribute. (it should be true)
203           is_expected.to contain_exec('apt_update').with('refreshonly' => true)
204         end
205       end
206       context 'when $apt_update_last_success is nil' do
207         let(:facts) do
208           {
209             os: {
210               family: 'Debian',
211               name: 'Debian',
212               release: {
213                 major: '9',
214                 full: '9.0',
215               },
216               distro: {
217                 codename: 'stretch',
218                 id: 'Debian',
219               },
220             },
221             'apt::apt_update_last_success': nil,
222           }
223         end
224         let(:pre_condition) { "class{ '::apt': update => {'frequency' => '#{update_frequency}',} }" }
225
226         it 'triggers an apt-get update run' do
227           # set the apt_update exec\'s refreshonly attribute to false
228           is_expected.to contain_exec('apt_update').with('refreshonly' => false)
229         end
230       end
231     end
232   end
233 end