manifests: use modern os facts
[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: '8',
20                 full: '8.0',
21               },
22               distro: {
23                 codename: 'jessie',
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           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: '8',
48               full: '8.0',
49             },
50             distro: {
51               codename: 'jessie',
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   end
65   context "when apt::update['frequency']='reluctantly'" do
66     {
67       'a recent run'                                 => Time.now.to_i,
68       'we are due for a run'                         => 1_406_660_561,
69       'the update-success-stamp file does not exist' => -1,
70     }.each_pair do |desc, factval|
71       context "when $::apt_update_last_success indicates #{desc}" do
72         let(:facts) do
73           {
74             os: {
75               family: 'Debian',
76               name: 'Debian',
77               release: {
78                 major: '8',
79                 full: '8.0',
80               },
81               distro: {
82                 codename: 'jessie',
83                 id: 'Debian',
84               },
85             },
86             apt_update_last_success: factval,
87           }
88         end
89         let(:pre_condition) { "class{ '::apt': update => {'frequency' => 'reluctantly' },}" }
90
91         it 'does not trigger an apt-get update run' do
92           # don't change the apt_update exec's refreshonly attribute. (it should be true)
93           is_expected.to contain_exec('apt_update').with('refreshonly' => true)
94         end
95       end
96     end
97     context 'when $::apt_update_last_success is nil' do
98       let(:facts) do
99         {
100           os: {
101             family: 'Debian',
102             name: 'Debian',
103             release: {
104               major: '8',
105               full: '8.0',
106             },
107             distro: {
108               codename: 'jessie',
109               id: 'Debian',
110             },
111           },
112         }
113       end
114       let(:pre_condition) { "class{ '::apt': update => {'frequency' => 'reluctantly' },}" }
115
116       it 'does not trigger an apt-get update run' do
117         # don't change the apt_update exec's refreshonly attribute. (it should be true)
118         is_expected.to contain_exec('apt_update').with('refreshonly' => true)
119       end
120     end
121   end
122   ['daily', 'weekly'].each do |update_frequency|
123     context "when apt::update['frequency'] has the value of #{update_frequency}" do
124       { 'we are due for a run' => 1_406_660_561, 'the update-success-stamp file does not exist' => -1 }.each_pair do |desc, factval|
125         context "when $::apt_update_last_success indicates #{desc}" do
126           let(:facts) do
127             {
128               os: {
129                 family: 'Debian',
130                 name: 'Debian',
131                 release: {
132                   major: '8',
133                   full: '8.0',
134                 },
135                 distro: {
136                   codename: 'jessie',
137                   id: 'Debian',
138                 },
139               },
140               apt_update_last_success: factval,
141             }
142           end
143           let(:pre_condition) { "class{ '::apt': update => {'frequency' => '#{update_frequency}',} }" }
144
145           it 'triggers an apt-get update run' do
146             # set the apt_update exec\'s refreshonly attribute to false
147             is_expected.to contain_exec('apt_update').with('refreshonly' => false)
148           end
149         end
150       end
151       context 'when the $::apt_update_last_success fact has a recent value' do
152         let(:facts) do
153           {
154             os: {
155               family: 'Debian',
156               name: 'Debian',
157               release: {
158                 major: '8',
159                 full: '8.0',
160               },
161               distro: {
162                 codename: 'jessie',
163                 id: 'Debian',
164               },
165             },
166             apt_update_last_success: Time.now.to_i,
167           }
168         end
169         let(:pre_condition) { "class{ '::apt': update => {'frequency' => '#{update_frequency}',} }" }
170
171         it 'does not trigger an apt-get update run' do
172           # don't change the apt_update exec\'s refreshonly attribute. (it should be true)
173           is_expected.to contain_exec('apt_update').with('refreshonly' => true)
174         end
175       end
176       context 'when $::apt_update_last_success is nil' do
177         let(:facts) do
178           {
179             os: {
180               family: 'Debian',
181               name: 'Debian',
182               release: {
183                 major: '8',
184                 full: '8.0',
185               },
186               distro: {
187                 codename: 'jessie',
188                 id: 'Debian',
189               },
190             },
191             apt_update_last_success: nil,
192           }
193         end
194         let(:pre_condition) { "class{ '::apt': update => {'frequency' => '#{update_frequency}',} }" }
195
196         it 'triggers an apt-get update run' do
197           # set the apt_update exec\'s refreshonly attribute to false
198           is_expected.to contain_exec('apt_update').with('refreshonly' => false)
199         end
200       end
201     end
202   end
203 end