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