Skip to content

Commit e487da9

Browse files
authored
tests: remove temporary sqlite/plugin files from /tmp/ (#4332)
1 parent 0dbdf33 commit e487da9

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

pkg/apiserver/apic_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/netip"
1010
"net/url"
1111
"os"
12+
"path/filepath"
1213
"reflect"
1314
"sync"
1415
"testing"
@@ -38,15 +39,21 @@ import (
3839
func getDBClient(t *testing.T, ctx context.Context) *database.Client {
3940
t.Helper()
4041

41-
dbPath, err := os.CreateTemp("", "*sqlite")
42-
require.NoError(t, err)
42+
dbPath := filepath.Join(t.TempDir(), "test.sqlite")
43+
4344
dbClient, err := database.NewClient(ctx, &csconfig.DatabaseCfg{
4445
Type: "sqlite",
4546
DbName: "crowdsec",
46-
DbPath: dbPath.Name(),
47+
DbPath: dbPath,
4748
}, nil)
4849
require.NoError(t, err)
4950

51+
t.Cleanup(func() {
52+
if err := dbClient.Ent.Close(); err != nil {
53+
t.Logf("cleanup: closing db: %v", err)
54+
}
55+
})
56+
5057
return dbClient
5158
}
5259

pkg/apiserver/apiserver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ func (s *APIServer) Close() {
470470
s.papi.Shutdown() // papi also uses the dbClient
471471
}
472472

473-
s.dbClient.Ent.Close()
473+
s.dbClient.Close()
474474

475475
if s.flushScheduler != nil {
476476
_ = s.flushScheduler.Shutdown()

pkg/csplugin/broker_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (s *PluginSuite) SetupSuite() {
4242

4343
t := s.T()
4444

45-
s.buildDir, err = os.MkdirTemp("", "cs_plugin_test_build")
45+
s.buildDir = t.TempDir()
4646
require.NoError(t, err)
4747

4848
s.builtBinary = filepath.Join(s.buildDir, "notification-dummy")

pkg/database/database.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,7 @@ func NewClient(ctx context.Context, config *csconfig.DatabaseCfg, logger *log.En
108108
decisionBulkSize: config.DecisionBulkSize,
109109
}, nil
110110
}
111+
112+
func (c *Client) Close() error {
113+
return c.Ent.Close()
114+
}

0 commit comments

Comments
 (0)