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