@@ -180,6 +180,96 @@ func TestHandleFilesNoFiles(t *testing.T) {
180180 require .NoError (t , err )
181181}
182182
183+ func TestHasRecentDelta (t * testing.T ) {
184+ tf , dir := getPlugin (t )
185+ defer os .RemoveAll (dir )
186+
187+ // No files (so no deltas)
188+ hasDelta , err := tf .hasRecentDeltas (60 )
189+ require .NoError (t , err )
190+ require .False (t , hasDelta )
191+
192+ // Non tf.json[.new] file (should be ignored)
193+ err = afero .WriteFile (tf .fs , filepath .Join (tf .Dir , "foo.txt" ), []byte ("some-text" ), 0644 )
194+ require .NoError (t , err )
195+ hasDelta , err = tf .hasRecentDeltas (60 )
196+ require .NoError (t , err )
197+ require .False (t , hasDelta )
198+
199+ // Write out a tf.json file
200+ info := fileInfo {
201+ ResInfo : []resInfo {
202+ {
203+ ResType : VMIBMCloud ,
204+ ResName : TResourceName ("instance-12345" ),
205+ },
206+ },
207+ NewFile : false ,
208+ Plugin : tf ,
209+ }
210+ writeFile (info , t )
211+
212+ // File delta in the last 5 seconds
213+ hasDelta , err = tf .hasRecentDeltas (5 )
214+ require .NoError (t , err )
215+ require .True (t , hasDelta )
216+
217+ // Wait 2 seconds, now the file will not a delta in a 1 second window
218+ time .Sleep (2 * time .Second )
219+ hasDelta , err = tf .hasRecentDeltas (1 )
220+ require .NoError (t , err )
221+ require .False (t , hasDelta )
222+
223+ // But not if we ask for deltas in a longer window
224+ hasDelta , err = tf .hasRecentDeltas (60 )
225+ require .NoError (t , err )
226+ require .True (t , hasDelta )
227+
228+ // Add another, should be a delta
229+ info .ResInfo [0 ].ResName = TResourceName ("instance-12346" )
230+ writeFile (info , t )
231+ hasDelta , err = tf .hasRecentDeltas (1 )
232+ require .NoError (t , err )
233+ require .True (t , hasDelta )
234+ }
235+
236+ func TestHasRecentDeltaInFuture (t * testing.T ) {
237+ tf , dir := getPlugin (t )
238+ defer os .RemoveAll (dir )
239+
240+ // Write out a tf.json file
241+ info := fileInfo {
242+ ResInfo : []resInfo {
243+ {
244+ ResType : VMIBMCloud ,
245+ ResName : TResourceName ("instance-12345" ),
246+ },
247+ },
248+ NewFile : false ,
249+ Plugin : tf ,
250+ }
251+ writeFile (info , t )
252+
253+ // Update the timestamp to 29 seconds in the future
254+ path := filepath .Join (tf .Dir , "instance-12345.tf.json" )
255+ newTime := time .Now ().Add (time .Duration (29 ) * time .Second )
256+ err := tf .fs .Chtimes (path , newTime , newTime )
257+ require .NoError (t , err )
258+
259+ // Since it's less than 30 seconds it will be a delta
260+ hasDelta , err := tf .hasRecentDeltas (1 )
261+ require .NoError (t , err )
262+ require .True (t , hasDelta )
263+
264+ // More than 30 seconds will be ignored
265+ newTime = time .Now ().Add (time .Duration (35 ) * time .Second )
266+ err = tf .fs .Chtimes (path , newTime , newTime )
267+ require .NoError (t , err )
268+ hasDelta , err = tf .hasRecentDeltas (1 )
269+ require .NoError (t , err )
270+ require .False (t , hasDelta )
271+ }
272+
183273func TestHandleFilesNoPruneNoNewFiles (t * testing.T ) {
184274 tf , dir := getPlugin (t )
185275 defer os .RemoveAll (dir )
0 commit comments