Skip to content

Commit

Permalink
chore: Add Close Calls to BoltDB Tests to fix Windows Test Errors (#5289
Browse files Browse the repository at this point in the history
)

Signed-off-by: X-Guardian <[email protected]>
  • Loading branch information
X-Guardian authored Feb 4, 2025
1 parent 9dfc3ea commit 16a9f10
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 1 deletion.
4 changes: 4 additions & 0 deletions server/core/db/boltdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,3 +504,7 @@ func (b *BoltDB) projectResultToProject(p command.ProjectResult) models.ProjectS
Status: p.PlanStatus(),
}
}

func (b *BoltDB) Close() error {
return b.db.Close()
}
8 changes: 7 additions & 1 deletion server/core/db/boltdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ func TestPullStatus_UpdateGet(t *testing.T) {
Status: models.ErroredPlanStatus,
},
}, status.Projects)
b.Close()
}

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

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

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

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

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

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

func cleanupDB(db *bolt.DB) {
os.Remove(db.Path()) // nolint: errcheck
db.Close() // nolint: errcheck
os.Remove(db.Path()) // nolint: errcheck
}
3 changes: 3 additions & 0 deletions server/events/apply_command_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ func TestApplyCommandRunner_IsSilenced(t *testing.T) {
// create an empty DB
tmp := t.TempDir()
db, err := db.New(tmp)
t.Cleanup(func() {
db.Close()
})
Ok(t, err)

vcsClient := setup(t, func(tc *TestConfig) {
Expand Down
42 changes: 42 additions & 0 deletions server/events/command_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ func setup(t *testing.T, options ...func(testConfig *TestConfig)) *vcsmocks.Mock
// create an empty DB
tmp := t.TempDir()
defaultBoltDB, err := db.New(tmp)
t.Cleanup(func() {
defaultBoltDB.Close()
})
Ok(t, err)

testConfig := &TestConfig{
Expand Down Expand Up @@ -773,6 +776,9 @@ func TestRunAutoplanCommand_DeletePlans(t *testing.T) {
setup(t)
tmp := t.TempDir()
boltDB, err := db.New(tmp)
t.Cleanup(func() {
boltDB.Close()
})
Ok(t, err)
dbUpdater.Backend = boltDB
applyCommandRunner.Backend = boltDB
Expand Down Expand Up @@ -800,6 +806,9 @@ func TestRunAutoplanCommand_FailedPreWorkflowHook_FailOnPreWorkflowHookError_Fal
setup(t)
tmp := t.TempDir()
boltDB, err := db.New(tmp)
t.Cleanup(func() {
boltDB.Close()
})
Ok(t, err)
dbUpdater.Backend = boltDB
applyCommandRunner.Backend = boltDB
Expand Down Expand Up @@ -828,6 +837,9 @@ func TestRunAutoplanCommand_FailedPreWorkflowHook_FailOnPreWorkflowHookError_Tru
setup(t)
tmp := t.TempDir()
boltDB, err := db.New(tmp)
t.Cleanup(func() {
boltDB.Close()
})
Ok(t, err)
dbUpdater.Backend = boltDB
applyCommandRunner.Backend = boltDB
Expand All @@ -852,6 +864,9 @@ func TestRunCommentCommand_FailedPreWorkflowHook_FailOnPreWorkflowHookError_Fals
setup(t)
tmp := t.TempDir()
boltDB, err := db.New(tmp)
t.Cleanup(func() {
boltDB.Close()
})
Ok(t, err)
dbUpdater.Backend = boltDB
applyCommandRunner.Backend = boltDB
Expand All @@ -874,6 +889,9 @@ func TestRunCommentCommand_FailedPreWorkflowHook_FailOnPreWorkflowHookError_True
setup(t)
tmp := t.TempDir()
boltDB, err := db.New(tmp)
t.Cleanup(func() {
boltDB.Close()
})
Ok(t, err)
dbUpdater.Backend = boltDB
applyCommandRunner.Backend = boltDB
Expand All @@ -892,6 +910,9 @@ func TestRunGenericPlanCommand_DeletePlans(t *testing.T) {
setup(t)
tmp := t.TempDir()
boltDB, err := db.New(tmp)
t.Cleanup(func() {
boltDB.Close()
})
Ok(t, err)
dbUpdater.Backend = boltDB
applyCommandRunner.Backend = boltDB
Expand All @@ -914,6 +935,9 @@ func TestRunSpecificPlanCommandDoesnt_DeletePlans(t *testing.T) {
setup(t)
tmp := t.TempDir()
boltDB, err := db.New(tmp)
t.Cleanup(func() {
boltDB.Close()
})
Ok(t, err)
dbUpdater.Backend = boltDB
applyCommandRunner.Backend = boltDB
Expand All @@ -934,6 +958,9 @@ func TestRunAutoplanCommandWithError_DeletePlans(t *testing.T) {

tmp := t.TempDir()
boltDB, err := db.New(tmp)
t.Cleanup(func() {
boltDB.Close()
})
Ok(t, err)
dbUpdater.Backend = boltDB
applyCommandRunner.Backend = boltDB
Expand Down Expand Up @@ -986,6 +1013,9 @@ func TestRunGenericPlanCommand_DiscardApprovals(t *testing.T) {

tmp := t.TempDir()
boltDB, err := db.New(tmp)
t.Cleanup(func() {
boltDB.Close()
})
Ok(t, err)
dbUpdater.Backend = boltDB
applyCommandRunner.Backend = boltDB
Expand All @@ -1011,6 +1041,9 @@ func TestFailedApprovalCreatesFailedStatusUpdate(t *testing.T) {
setup(t)
tmp := t.TempDir()
boltDB, err := db.New(tmp)
t.Cleanup(func() {
boltDB.Close()
})
Ok(t, err)
dbUpdater.Backend = boltDB
applyCommandRunner.Backend = boltDB
Expand Down Expand Up @@ -1057,6 +1090,9 @@ func TestApprovedPoliciesUpdateFailedPolicyStatus(t *testing.T) {
setup(t)
tmp := t.TempDir()
boltDB, err := db.New(tmp)
t.Cleanup(func() {
boltDB.Close()
})
Ok(t, err)
dbUpdater.Backend = boltDB
applyCommandRunner.Backend = boltDB
Expand Down Expand Up @@ -1113,6 +1149,9 @@ func TestApplyMergeablityWhenPolicyCheckFails(t *testing.T) {
setup(t)
tmp := t.TempDir()
boltDB, err := db.New(tmp)
t.Cleanup(func() {
boltDB.Close()
})
Ok(t, err)
dbUpdater.Backend = boltDB
applyCommandRunner.Backend = boltDB
Expand Down Expand Up @@ -1191,6 +1230,9 @@ func TestRunApply_DiscardedProjects(t *testing.T) {
defer func() { autoMerger.GlobalAutomerge = false }()
tmp := t.TempDir()
boltDB, err := db.New(tmp)
t.Cleanup(func() {
boltDB.Close()
})
Ok(t, err)
dbUpdater.Backend = boltDB
applyCommandRunner.Backend = boltDB
Expand Down
3 changes: 3 additions & 0 deletions server/events/delete_lock_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func TestDeleteLock_Success(t *testing.T) {
}, nil)
tmp := t.TempDir()
db, err := db.New(tmp)
t.Cleanup(func() {
db.Close()
})
Ok(t, err)
dlc := events.DefaultDeleteLockCommand{
Locker: l,
Expand Down
9 changes: 9 additions & 0 deletions server/events/plan_command_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func TestPlanCommandRunner_IsSilenced(t *testing.T) {
// create an empty DB
tmp := t.TempDir()
db, err := db.New(tmp)
t.Cleanup(func() {
db.Close()
})
Ok(t, err)

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

tmp := t.TempDir()
db, err := db.New(tmp)
t.Cleanup(func() {
db.Close()
})
Ok(t, err)

vcsClient := setup(t, func(tc *TestConfig) {
Expand Down Expand Up @@ -700,6 +706,9 @@ func TestPlanCommandRunner_AtlantisApplyStatus(t *testing.T) {
// create an empty DB
tmp := t.TempDir()
db, err := db.New(tmp)
t.Cleanup(func() {
db.Close()
})
Ok(t, err)

vcsClient := setup(t, func(tc *TestConfig) {
Expand Down
12 changes: 12 additions & 0 deletions server/events/pull_closed_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func TestCleanUpPullWorkspaceErr(t *testing.T) {
w := mocks.NewMockWorkingDir()
tmp := t.TempDir()
db, err := db.New(tmp)
t.Cleanup(func() {
db.Close()
})
Ok(t, err)
pce := events.PullClosedExecutor{
WorkingDir: w,
Expand All @@ -63,6 +66,9 @@ func TestCleanUpPullUnlockErr(t *testing.T) {
l := lockmocks.NewMockLocker()
tmp := t.TempDir()
db, err := db.New(tmp)
t.Cleanup(func() {
db.Close()
})
Ok(t, err)
pce := events.PullClosedExecutor{
Locker: l,
Expand All @@ -85,6 +91,9 @@ func TestCleanUpPullNoLocks(t *testing.T) {
cp := vcsmocks.NewMockClient()
tmp := t.TempDir()
db, err := db.New(tmp)
t.Cleanup(func() {
db.Close()
})
Ok(t, err)
pce := events.PullClosedExecutor{
Locker: l,
Expand Down Expand Up @@ -182,6 +191,9 @@ func TestCleanUpPullComments(t *testing.T) {
l := lockmocks.NewMockLocker()
tmp := t.TempDir()
db, err := db.New(tmp)
t.Cleanup(func() {
db.Close()
})
Ok(t, err)
pce := events.PullClosedExecutor{
Locker: l,
Expand Down

0 comments on commit 16a9f10

Please sign in to comment.