@@ -284,6 +284,78 @@ func TestFetchZonesInventoryOtherError(t *testing.T) {
284
284
require .Empty (t , zones )
285
285
}
286
286
287
+ // This test verifies that an error is returned indicating the database error
288
+ // while deleting local zones before inserting new ones.
289
+ func TestFetchZonesInventoryDeleteLocalZonesError (t * testing.T ) {
290
+ db , _ , teardown := dbtest .SetupDatabaseTestCase (t )
291
+ defer teardown ()
292
+
293
+ controller := gomock .NewController (t )
294
+ mock := NewMockConnectedAgents (controller )
295
+
296
+ randomZones := testutil .GenerateRandomZones (1 )
297
+
298
+ // Add a machine and app.
299
+ machine := & dbmodel.Machine {
300
+ ID : 0 ,
301
+ Address : "localhost" ,
302
+ AgentPort : int64 (8080 ),
303
+ }
304
+ err := dbmodel .AddMachine (db , machine )
305
+ require .NoError (t , err )
306
+
307
+ app := & dbmodel.App {
308
+ ID : 0 ,
309
+ MachineID : machine .ID ,
310
+ Type : dbmodel .AppTypeBind9 ,
311
+ Daemons : []* dbmodel.Daemon {
312
+ dbmodel .NewBind9Daemon (true ),
313
+ },
314
+ }
315
+ _ , err = dbmodel .AddApp (db , app )
316
+ require .NoError (t , err )
317
+
318
+ // Return "uninitialized" error on first iteration.
319
+ mock .EXPECT ().ReceiveZones (gomock .Any (), gomock .Cond (func (a any ) bool {
320
+ return a .(* dbmodel.App ).ID == app .ID
321
+ }), nil ).DoAndReturn (func (context.Context , * dbmodel.App , * bind9stats.ZoneFilter ) iter.Seq2 [* bind9stats.ExtendedZone , error ] {
322
+ return func (yield func (* bind9stats.ExtendedZone , error ) bool ) {
323
+ // We are on the fist iteration. Let's close the database connection
324
+ // to cause an error.
325
+ teardown ()
326
+ // Return the zone.
327
+ zone := & bind9stats.ExtendedZone {
328
+ Zone : bind9stats.Zone {
329
+ ZoneName : randomZones [0 ].Name ,
330
+ Class : randomZones [0 ].Class ,
331
+ Serial : randomZones [0 ].Serial ,
332
+ Type : randomZones [0 ].Type ,
333
+ Loaded : time .Now ().UTC (),
334
+ },
335
+ ViewName : "foo" ,
336
+ TotalZoneCount : int64 (len (randomZones )),
337
+ }
338
+ _ = yield (zone , nil )
339
+ }
340
+ })
341
+ require .NoError (t , err )
342
+
343
+ manager := NewManager (& appstest.ManagerAccessorsWrapper {
344
+ DB : db ,
345
+ Agents : mock ,
346
+ })
347
+ require .NotNil (t , manager )
348
+
349
+ notifyChannel , err := manager .FetchZones (10 , 100 , true )
350
+ require .NoError (t , err )
351
+ notification := <- notifyChannel
352
+
353
+ // Make sure other error was reported.
354
+ require .Len (t , notification .results , 1 )
355
+ require .NotNil (t , notification .results [app .Daemons [0 ].ID ].Error )
356
+ require .Contains (t , * notification .results [app .Daemons [0 ].ID ].Error , "database is closed" )
357
+ }
358
+
287
359
// This test verifies that the manager can fetch zones from many servers
288
360
// simultaneously.
289
361
func TestFetchZones (t * testing.T ) {
0 commit comments