Skip to content

Commit 16a9f10

Browse files
authored
chore: Add Close Calls to BoltDB Tests to fix Windows Test Errors (#5289)
Signed-off-by: X-Guardian <[email protected]>
1 parent 9dfc3ea commit 16a9f10

7 files changed

+80
-1
lines changed

server/core/db/boltdb.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,3 +504,7 @@ func (b *BoltDB) projectResultToProject(p command.ProjectResult) models.ProjectS
504504
Status: p.PlanStatus(),
505505
}
506506
}
507+
508+
func (b *BoltDB) Close() error {
509+
return b.db.Close()
510+
}

server/core/db/boltdb_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ func TestPullStatus_UpdateGet(t *testing.T) {
489489
Status: models.ErroredPlanStatus,
490490
},
491491
}, status.Projects)
492+
b.Close()
492493
}
493494

494495
// Test we can create a status, delete it, and then we shouldn't be able to getCommandLock
@@ -533,6 +534,7 @@ func TestPullStatus_UpdateDeleteGet(t *testing.T) {
533534
maybeStatus, err := b.GetPullStatus(pull)
534535
Ok(t, err)
535536
Assert(t, maybeStatus == nil, "exp nil")
537+
b.Close()
536538
}
537539

538540
// Test we can create a status, update a specific project's status within that
@@ -597,6 +599,7 @@ func TestPullStatus_UpdateProject(t *testing.T) {
597599
Status: models.AppliedPlanStatus,
598600
},
599601
}, status.Projects) // nolint: staticcheck
602+
b.Close()
600603
}
601604

602605
// Test that if we update an existing pull status and our new status is for a
@@ -659,6 +662,7 @@ func TestPullStatus_UpdateNewCommit(t *testing.T) {
659662
Status: models.AppliedPlanStatus,
660663
},
661664
}, maybeStatus.Projects)
665+
b.Close()
662666
}
663667

664668
// Test that if we update an existing pull status via Apply and our new status is for a
@@ -771,6 +775,7 @@ func TestPullStatus_UpdateMerge_Apply(t *testing.T) {
771775
},
772776
}, updateStatus.Projects)
773777
}
778+
b.Close()
774779
}
775780

776781
// Test that if we update one existing policy status via approve_policies and our new status is for a
@@ -885,6 +890,7 @@ func TestPullStatus_UpdateMerge_ApprovePolicies(t *testing.T) {
885890
},
886891
}, updateStatus.Projects)
887892
}
893+
b.Close()
888894
}
889895

890896
// newTestDB returns a TestDB using a temporary path.
@@ -925,6 +931,6 @@ func newTestDB2(t *testing.T) *db.BoltDB {
925931
}
926932

927933
func cleanupDB(db *bolt.DB) {
928-
os.Remove(db.Path()) // nolint: errcheck
929934
db.Close() // nolint: errcheck
935+
os.Remove(db.Path()) // nolint: errcheck
930936
}

server/events/apply_command_runner_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ func TestApplyCommandRunner_IsSilenced(t *testing.T) {
140140
// create an empty DB
141141
tmp := t.TempDir()
142142
db, err := db.New(tmp)
143+
t.Cleanup(func() {
144+
db.Close()
145+
})
143146
Ok(t, err)
144147

145148
vcsClient := setup(t, func(tc *TestConfig) {

server/events/command_runner_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ func setup(t *testing.T, options ...func(testConfig *TestConfig)) *vcsmocks.Mock
8686
// create an empty DB
8787
tmp := t.TempDir()
8888
defaultBoltDB, err := db.New(tmp)
89+
t.Cleanup(func() {
90+
defaultBoltDB.Close()
91+
})
8992
Ok(t, err)
9093

9194
testConfig := &TestConfig{
@@ -773,6 +776,9 @@ func TestRunAutoplanCommand_DeletePlans(t *testing.T) {
773776
setup(t)
774777
tmp := t.TempDir()
775778
boltDB, err := db.New(tmp)
779+
t.Cleanup(func() {
780+
boltDB.Close()
781+
})
776782
Ok(t, err)
777783
dbUpdater.Backend = boltDB
778784
applyCommandRunner.Backend = boltDB
@@ -800,6 +806,9 @@ func TestRunAutoplanCommand_FailedPreWorkflowHook_FailOnPreWorkflowHookError_Fal
800806
setup(t)
801807
tmp := t.TempDir()
802808
boltDB, err := db.New(tmp)
809+
t.Cleanup(func() {
810+
boltDB.Close()
811+
})
803812
Ok(t, err)
804813
dbUpdater.Backend = boltDB
805814
applyCommandRunner.Backend = boltDB
@@ -828,6 +837,9 @@ func TestRunAutoplanCommand_FailedPreWorkflowHook_FailOnPreWorkflowHookError_Tru
828837
setup(t)
829838
tmp := t.TempDir()
830839
boltDB, err := db.New(tmp)
840+
t.Cleanup(func() {
841+
boltDB.Close()
842+
})
831843
Ok(t, err)
832844
dbUpdater.Backend = boltDB
833845
applyCommandRunner.Backend = boltDB
@@ -852,6 +864,9 @@ func TestRunCommentCommand_FailedPreWorkflowHook_FailOnPreWorkflowHookError_Fals
852864
setup(t)
853865
tmp := t.TempDir()
854866
boltDB, err := db.New(tmp)
867+
t.Cleanup(func() {
868+
boltDB.Close()
869+
})
855870
Ok(t, err)
856871
dbUpdater.Backend = boltDB
857872
applyCommandRunner.Backend = boltDB
@@ -874,6 +889,9 @@ func TestRunCommentCommand_FailedPreWorkflowHook_FailOnPreWorkflowHookError_True
874889
setup(t)
875890
tmp := t.TempDir()
876891
boltDB, err := db.New(tmp)
892+
t.Cleanup(func() {
893+
boltDB.Close()
894+
})
877895
Ok(t, err)
878896
dbUpdater.Backend = boltDB
879897
applyCommandRunner.Backend = boltDB
@@ -892,6 +910,9 @@ func TestRunGenericPlanCommand_DeletePlans(t *testing.T) {
892910
setup(t)
893911
tmp := t.TempDir()
894912
boltDB, err := db.New(tmp)
913+
t.Cleanup(func() {
914+
boltDB.Close()
915+
})
895916
Ok(t, err)
896917
dbUpdater.Backend = boltDB
897918
applyCommandRunner.Backend = boltDB
@@ -914,6 +935,9 @@ func TestRunSpecificPlanCommandDoesnt_DeletePlans(t *testing.T) {
914935
setup(t)
915936
tmp := t.TempDir()
916937
boltDB, err := db.New(tmp)
938+
t.Cleanup(func() {
939+
boltDB.Close()
940+
})
917941
Ok(t, err)
918942
dbUpdater.Backend = boltDB
919943
applyCommandRunner.Backend = boltDB
@@ -934,6 +958,9 @@ func TestRunAutoplanCommandWithError_DeletePlans(t *testing.T) {
934958

935959
tmp := t.TempDir()
936960
boltDB, err := db.New(tmp)
961+
t.Cleanup(func() {
962+
boltDB.Close()
963+
})
937964
Ok(t, err)
938965
dbUpdater.Backend = boltDB
939966
applyCommandRunner.Backend = boltDB
@@ -986,6 +1013,9 @@ func TestRunGenericPlanCommand_DiscardApprovals(t *testing.T) {
9861013

9871014
tmp := t.TempDir()
9881015
boltDB, err := db.New(tmp)
1016+
t.Cleanup(func() {
1017+
boltDB.Close()
1018+
})
9891019
Ok(t, err)
9901020
dbUpdater.Backend = boltDB
9911021
applyCommandRunner.Backend = boltDB
@@ -1011,6 +1041,9 @@ func TestFailedApprovalCreatesFailedStatusUpdate(t *testing.T) {
10111041
setup(t)
10121042
tmp := t.TempDir()
10131043
boltDB, err := db.New(tmp)
1044+
t.Cleanup(func() {
1045+
boltDB.Close()
1046+
})
10141047
Ok(t, err)
10151048
dbUpdater.Backend = boltDB
10161049
applyCommandRunner.Backend = boltDB
@@ -1057,6 +1090,9 @@ func TestApprovedPoliciesUpdateFailedPolicyStatus(t *testing.T) {
10571090
setup(t)
10581091
tmp := t.TempDir()
10591092
boltDB, err := db.New(tmp)
1093+
t.Cleanup(func() {
1094+
boltDB.Close()
1095+
})
10601096
Ok(t, err)
10611097
dbUpdater.Backend = boltDB
10621098
applyCommandRunner.Backend = boltDB
@@ -1113,6 +1149,9 @@ func TestApplyMergeablityWhenPolicyCheckFails(t *testing.T) {
11131149
setup(t)
11141150
tmp := t.TempDir()
11151151
boltDB, err := db.New(tmp)
1152+
t.Cleanup(func() {
1153+
boltDB.Close()
1154+
})
11161155
Ok(t, err)
11171156
dbUpdater.Backend = boltDB
11181157
applyCommandRunner.Backend = boltDB
@@ -1191,6 +1230,9 @@ func TestRunApply_DiscardedProjects(t *testing.T) {
11911230
defer func() { autoMerger.GlobalAutomerge = false }()
11921231
tmp := t.TempDir()
11931232
boltDB, err := db.New(tmp)
1233+
t.Cleanup(func() {
1234+
boltDB.Close()
1235+
})
11941236
Ok(t, err)
11951237
dbUpdater.Backend = boltDB
11961238
applyCommandRunner.Backend = boltDB

server/events/delete_lock_command_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ func TestDeleteLock_Success(t *testing.T) {
6060
}, nil)
6161
tmp := t.TempDir()
6262
db, err := db.New(tmp)
63+
t.Cleanup(func() {
64+
db.Close()
65+
})
6366
Ok(t, err)
6467
dlc := events.DefaultDeleteLockCommand{
6568
Locker: l,

server/events/plan_command_runner_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ func TestPlanCommandRunner_IsSilenced(t *testing.T) {
7878
// create an empty DB
7979
tmp := t.TempDir()
8080
db, err := db.New(tmp)
81+
t.Cleanup(func() {
82+
db.Close()
83+
})
8184
Ok(t, err)
8285

8386
vcsClient := setup(t, func(tc *TestConfig) {
@@ -463,6 +466,9 @@ func TestPlanCommandRunner_ExecutionOrder(t *testing.T) {
463466

464467
tmp := t.TempDir()
465468
db, err := db.New(tmp)
469+
t.Cleanup(func() {
470+
db.Close()
471+
})
466472
Ok(t, err)
467473

468474
vcsClient := setup(t, func(tc *TestConfig) {
@@ -700,6 +706,9 @@ func TestPlanCommandRunner_AtlantisApplyStatus(t *testing.T) {
700706
// create an empty DB
701707
tmp := t.TempDir()
702708
db, err := db.New(tmp)
709+
t.Cleanup(func() {
710+
db.Close()
711+
})
703712
Ok(t, err)
704713

705714
vcsClient := setup(t, func(tc *TestConfig) {

server/events/pull_closed_executor_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ func TestCleanUpPullWorkspaceErr(t *testing.T) {
4343
w := mocks.NewMockWorkingDir()
4444
tmp := t.TempDir()
4545
db, err := db.New(tmp)
46+
t.Cleanup(func() {
47+
db.Close()
48+
})
4649
Ok(t, err)
4750
pce := events.PullClosedExecutor{
4851
WorkingDir: w,
@@ -63,6 +66,9 @@ func TestCleanUpPullUnlockErr(t *testing.T) {
6366
l := lockmocks.NewMockLocker()
6467
tmp := t.TempDir()
6568
db, err := db.New(tmp)
69+
t.Cleanup(func() {
70+
db.Close()
71+
})
6672
Ok(t, err)
6773
pce := events.PullClosedExecutor{
6874
Locker: l,
@@ -85,6 +91,9 @@ func TestCleanUpPullNoLocks(t *testing.T) {
8591
cp := vcsmocks.NewMockClient()
8692
tmp := t.TempDir()
8793
db, err := db.New(tmp)
94+
t.Cleanup(func() {
95+
db.Close()
96+
})
8897
Ok(t, err)
8998
pce := events.PullClosedExecutor{
9099
Locker: l,
@@ -182,6 +191,9 @@ func TestCleanUpPullComments(t *testing.T) {
182191
l := lockmocks.NewMockLocker()
183192
tmp := t.TempDir()
184193
db, err := db.New(tmp)
194+
t.Cleanup(func() {
195+
db.Close()
196+
})
185197
Ok(t, err)
186198
pce := events.PullClosedExecutor{
187199
Locker: l,

0 commit comments

Comments
 (0)