|
25 | 25 | let(:group_name) { 'operating system' } |
26 | 26 | let(:cache_file_name) { File.join(cache_dir, group_name) } |
27 | 27 | let(:fact_groups) { instance_spy(Facter::FactGroups) } |
| 28 | + let(:os_fact) { { ttls: 60, group: 'operating system' } } |
| 29 | + let(:external_fact) { { ttls: 60, group: 'ext_file.txt' } } |
28 | 30 |
|
29 | 31 | before do |
30 | 32 | allow(LegacyFacter::Util::Config).to receive(:facts_cache_dir).and_return(cache_dir) |
|
72 | 74 | allow(File).to receive(:directory?).with(cache_dir).and_return(true) |
73 | 75 | allow(fact_groups).to receive(:get_fact_group).with('os').and_return(group_name) |
74 | 76 | allow(fact_groups).to receive(:get_fact_group).with('my_custom_fact').and_return(nil) |
| 77 | + allow(fact_groups).to receive(:get_fact_group).with('ext_file.txt').and_return(nil) |
75 | 78 | allow(fact_groups).to receive(:get_group_ttls).with('ext_file.txt').and_return(nil) |
| 79 | + allow(fact_groups).to receive(:get_fact).with('ext_file.txt').and_return(nil) |
76 | 80 | allow(File).to receive(:readable?).with(cache_file_name).and_return(true) |
77 | 81 | allow(File).to receive(:mtime).with(cache_file_name).and_return(Time.now) |
78 | 82 | allow(Facter::Util::FileHelper).to receive(:safe_read).with(cache_file_name).and_return(cached_core_fact) |
79 | 83 | allow(Facter::Options).to receive(:[]).with(:cache).and_return(true) |
80 | 84 | end |
81 | 85 |
|
82 | 86 | it 'returns cached fact' do |
83 | | - allow(fact_groups).to receive(:get_group_ttls).with(group_name).and_return(1000) |
| 87 | + allow(fact_groups).to receive(:get_fact).with('os').and_return(os_fact) |
84 | 88 | allow(File).to receive(:readable?).with(cache_file_name).and_return(true) |
85 | 89 | allow(File).to receive(:readable?).with(File.join(cache_dir, 'ext_file.txt')).and_return(false) |
86 | 90 |
|
|
91 | 95 | end |
92 | 96 |
|
93 | 97 | it 'returns searched fact' do |
94 | | - allow(fact_groups).to receive(:get_group_ttls).with(group_name).and_return(1000) |
| 98 | + allow(fact_groups).to receive(:get_fact).with('os').and_return(os_fact) |
95 | 99 | allow(File).to receive(:readable?).with(cache_file_name).and_return(true) |
96 | 100 | allow(File).to receive(:readable?).with(File.join(cache_dir, 'ext_file.txt')).and_return(false) |
97 | 101 |
|
|
103 | 107 | end |
104 | 108 |
|
105 | 109 | it 'deletes cache file' do |
106 | | - allow(fact_groups).to receive(:get_group_ttls).with(group_name).and_return(nil) |
| 110 | + allow(fact_groups).to receive(:get_fact).with('os').and_return(nil) |
107 | 111 | allow(File).to receive(:readable?).with(cache_file_name).and_return(true) |
108 | 112 | allow(File).to receive(:delete).with(cache_file_name) |
| 113 | + allow(fact_groups).to receive(:get_fact_group).with('os').and_return(group_name) |
109 | 114 | allow(File).to receive(:readable?).with(File.join(cache_dir, 'ext_file.txt')).and_return(false) |
110 | 115 |
|
111 | 116 | cache_manager.resolve_facts(searched_facts) |
112 | 117 | expect(File).to have_received(:delete).with(cache_file_name) |
113 | 118 | end |
114 | 119 |
|
115 | 120 | it 'returns cached external facts' do |
116 | | - allow(fact_groups).to receive(:get_group_ttls).with('ext_file.txt').and_return(1000) |
117 | | - allow(fact_groups).to receive(:get_group_ttls).with(group_name).and_return(nil) |
| 121 | + allow(fact_groups).to receive(:get_fact).with('ext_file.txt').and_return(external_fact) |
| 122 | + allow(fact_groups).to receive(:get_fact).with('os').and_return(nil) |
118 | 123 | allow(File).to receive(:readable?).with(cache_file_name).and_return(false) |
119 | 124 | allow(Facter::Util::FileHelper).to receive(:safe_read).with(File.join(cache_dir, 'ext_file.txt')) |
120 | 125 | .and_return(cached_external_fact) |
|
135 | 140 | before do |
136 | 141 | allow(File).to receive(:directory?).with(cache_dir).and_return(true) |
137 | 142 | allow(File).to receive(:readable?).with(cache_file_name).and_return(false) |
138 | | - allow(fact_groups).to receive(:get_group_ttls).with(group_name).and_return(nil) |
| 143 | + allow(fact_groups).to receive(:get_fact).with('os').and_return(nil) |
139 | 144 | allow(fact_groups).to receive(:get_fact_group).with('os').and_return(group_name) |
140 | 145 | allow(File).to receive(:write).with(cache_file_name, cached_core_fact) |
141 | 146 | allow(Facter::Options).to receive(:[]).with(:cache).and_return(true) |
|
150 | 155 | context 'with cache group' do |
151 | 156 | before do |
152 | 157 | allow(File).to receive(:directory?).with(cache_dir).and_return(true) |
| 158 | + allow(fact_groups).to receive(:get_fact).with('os').and_return(os_fact) |
153 | 159 | allow(fact_groups).to receive(:get_fact_group).with('os').and_return(group_name) |
154 | 160 | allow(fact_groups).to receive(:get_fact_group).with('my_custom_fact').and_return(nil) |
155 | 161 | allow(fact_groups).to receive(:get_fact_group).with('my_external_fact').and_return(nil) |
156 | | - allow(fact_groups).to receive(:get_group_ttls).with(group_name).and_return(1000) |
157 | 162 | allow(File).to receive(:readable?).with(cache_file_name).and_return(false) |
158 | 163 | allow(File).to receive(:write).with(cache_file_name, cached_core_fact) |
159 | 164 | allow(Facter::Options).to receive(:[]).with(:cache).and_return(true) |
|
166 | 171 | end |
167 | 172 | end |
168 | 173 |
|
169 | | - describe '#group_cached?' do |
| 174 | + describe '#fact_cache_enabled?' do |
170 | 175 | context 'with ttls' do |
171 | 176 | before do |
172 | | - allow(fact_groups).to receive(:get_group_ttls).with(group_name).and_return(1000) |
| 177 | + allow(fact_groups).to receive(:get_fact).with('os').and_return(os_fact) |
173 | 178 | allow(File).to receive(:readable?).with(cache_file_name).and_return(false) |
174 | 179 | end |
175 | 180 |
|
176 | 181 | it 'returns true' do |
177 | | - result = cache_manager.group_cached?(group_name) |
| 182 | + result = cache_manager.fact_cache_enabled?('os') |
178 | 183 | expect(result).to be true |
179 | 184 | end |
180 | 185 | end |
181 | 186 |
|
182 | 187 | context 'without ttls' do |
183 | 188 | before do |
184 | | - allow(fact_groups).to receive(:get_group_ttls).with(group_name).and_return(nil) |
| 189 | + allow(fact_groups).to receive(:get_fact).with('os').and_return(nil) |
| 190 | + allow(fact_groups).to receive(:get_fact_group).with('os').and_return(group_name) |
185 | 191 | allow(Facter::Options).to receive(:[]).with(:cache).and_return(true) |
186 | 192 | allow(File).to receive(:delete).with(cache_file_name) |
187 | 193 | end |
188 | 194 |
|
189 | 195 | it 'returns false' do |
190 | 196 | allow(File).to receive(:readable?).with(cache_file_name).and_return(false) |
191 | | - result = cache_manager.group_cached?(group_name) |
| 197 | + result = cache_manager.fact_cache_enabled?('os') |
192 | 198 | expect(result).to be false |
193 | 199 | end |
194 | 200 |
|
195 | 201 | it 'deletes invalid cache file' do |
196 | 202 | allow(File).to receive(:readable?).with(cache_file_name).and_return(true) |
197 | | - cache_manager.group_cached?(group_name) |
| 203 | + cache_manager.fact_cache_enabled?('os') |
198 | 204 | expect(File).to have_received(:delete).with(cache_file_name) |
199 | 205 | end |
200 | 206 | end |
|
0 commit comments