diff --git a/.github/workflows/go-tests-windows.yml b/.github/workflows/go-tests-windows.yml index 3276dbb1bfd..44abbbe24a3 100644 --- a/.github/workflows/go-tests-windows.yml +++ b/.github/workflows/go-tests-windows.yml @@ -61,6 +61,6 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v6 with: - version: v1.62 + version: v1.63 args: --issues-exit-code=1 --timeout 10m only-new-issues: false diff --git a/.github/workflows/go-tests.yml b/.github/workflows/go-tests.yml index d882f88580e..649c47ebd26 100644 --- a/.github/workflows/go-tests.yml +++ b/.github/workflows/go-tests.yml @@ -189,6 +189,6 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v6 with: - version: v1.62 + version: v1.63 args: --issues-exit-code=1 --timeout 10m only-new-issues: false diff --git a/.golangci.yml b/.golangci.yml index 5995f14c512..12d35ed8737 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -460,3 +460,14 @@ issues: - revive path: "pkg/types/utils.go" text: "argument-limit: .*" + + # need some cleanup first: to create db in memory and share the client, not the config + - linters: + - usetesting + path: "pkg/apiserver/(.+)_test.go" + text: "os.MkdirTemp.* could be replaced by t.TempDir.*" + + - linters: + - usetesting + path: "pkg/apiserver/(.+)_test.go" + text: "os.CreateTemp.* could be replaced by os.CreateTemp.*" diff --git a/pkg/csplugin/listfiles_test.go b/pkg/csplugin/listfiles_test.go index c476d7a4e4a..32269f3f5f1 100644 --- a/pkg/csplugin/listfiles_test.go +++ b/pkg/csplugin/listfiles_test.go @@ -12,19 +12,22 @@ import ( ) func TestListFilesAtPath(t *testing.T) { - dir, err := os.MkdirTemp("", "test-listfiles") - require.NoError(t, err) - t.Cleanup(func() { - os.RemoveAll(dir) - }) - _, err = os.Create(filepath.Join(dir, "notification-gitter")) + dir := t.TempDir() + + f, err := os.Create(filepath.Join(dir, "notification-gitter")) require.NoError(t, err) - _, err = os.Create(filepath.Join(dir, "slack")) + require.NoError(t, f.Close()) + + f, err = os.Create(filepath.Join(dir, "slack")) require.NoError(t, err) + require.NoError(t, f.Close()) + err = os.Mkdir(filepath.Join(dir, "somedir"), 0o755) require.NoError(t, err) - _, err = os.Create(filepath.Join(dir, "somedir", "inner")) + + f, err = os.Create(filepath.Join(dir, "somedir", "inner")) require.NoError(t, err) + require.NoError(t, f.Close()) tests := []struct { name string diff --git a/pkg/database/database.go b/pkg/database/database.go index bb41dd3b645..80479710751 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -68,7 +68,7 @@ func NewClient(ctx context.Context, config *csconfig.DatabaseCfg) (*Client, erro return nil, err // unsupported database caught here } - if config.Type == "sqlite" { + if config.Type == "sqlite" && config.DbPath != ":memory:" { /*if it's the first startup, we want to touch and chmod file*/ if _, err = os.Stat(config.DbPath); os.IsNotExist(err) { f, err := os.OpenFile(config.DbPath, os.O_CREATE|os.O_RDWR, 0o600) diff --git a/pkg/exprhelpers/exprlib_test.go b/pkg/exprhelpers/exprlib_test.go index f2eb208ebfa..932db4b7da4 100644 --- a/pkg/exprhelpers/exprlib_test.go +++ b/pkg/exprhelpers/exprlib_test.go @@ -3,7 +3,6 @@ package exprhelpers import ( "context" "errors" - "os" "testing" "time" @@ -26,15 +25,12 @@ const TestFolder = "tests" func getDBClient(t *testing.T) *database.Client { t.Helper() - dbPath, err := os.CreateTemp("", "*sqlite") - require.NoError(t, err) - ctx := context.Background() testDBClient, err := database.NewClient(ctx, &csconfig.DatabaseCfg{ Type: "sqlite", DbName: "crowdsec", - DbPath: dbPath.Name(), + DbPath: ":memory:", }) require.NoError(t, err) diff --git a/pkg/fflag/features_test.go b/pkg/fflag/features_test.go index 144e7049362..bf8ddeca8fd 100644 --- a/pkg/fflag/features_test.go +++ b/pkg/fflag/features_test.go @@ -351,11 +351,9 @@ func TestSetFromYaml(t *testing.T) { } func TestSetFromYamlFile(t *testing.T) { - tmpfile, err := os.CreateTemp("", "test") + tmpfile, err := os.CreateTemp(t.TempDir(), "test") require.NoError(t, err) - defer os.Remove(tmpfile.Name()) - // write the config file _, err = tmpfile.WriteString("- experimental1") require.NoError(t, err) diff --git a/pkg/setup/detect_test.go b/pkg/setup/detect_test.go index 475f3af0928..72356bc1924 100644 --- a/pkg/setup/detect_test.go +++ b/pkg/setup/detect_test.go @@ -60,9 +60,14 @@ func TestSetupHelperProcess(t *testing.T) { func tempYAML(t *testing.T, content string) os.File { t.Helper() require := require.New(t) - file, err := os.CreateTemp("", "") + file, err := os.CreateTemp(t.TempDir(), "") require.NoError(err) + t.Cleanup(func() { + require.NoError(file.Close()) + require.NoError(os.Remove(file.Name())) + }) + _, err = file.WriteString(dedent.Dedent(content)) require.NoError(err) @@ -249,7 +254,6 @@ func TestListSupported(t *testing.T) { t.Parallel() f := tempYAML(t, tc.yml) - defer os.Remove(f.Name()) supported, err := setup.ListSupported(&f) cstest.RequireErrorContains(t, err, tc.expectedErr) @@ -375,7 +379,6 @@ func TestDetectSimpleRule(t *testing.T) { - false ugly: `) - defer os.Remove(f.Name()) detected, err := setup.Detect(&f, setup.DetectOptions{}) require.NoError(err) @@ -421,7 +424,6 @@ detect: for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { f := tempYAML(t, tc.config) - defer os.Remove(f.Name()) detected, err := setup.Detect(&f, setup.DetectOptions{}) cstest.RequireErrorContains(t, err, tc.expectedErr) @@ -514,7 +516,6 @@ detect: for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { f := tempYAML(t, tc.config) - defer os.Remove(f.Name()) detected, err := setup.Detect(&f, setup.DetectOptions{}) cstest.RequireErrorContains(t, err, tc.expectedErr) @@ -542,7 +543,6 @@ func TestDetectForcedUnit(t *testing.T) { journalctl_filter: - _SYSTEMD_UNIT=crowdsec-setup-forced.service `) - defer os.Remove(f.Name()) detected, err := setup.Detect(&f, setup.DetectOptions{ForcedUnits: []string{"crowdsec-setup-forced.service"}}) require.NoError(err) @@ -580,7 +580,6 @@ func TestDetectForcedProcess(t *testing.T) { when: - ProcessRunning("foobar") `) - defer os.Remove(f.Name()) detected, err := setup.Detect(&f, setup.DetectOptions{ForcedProcesses: []string{"foobar"}}) require.NoError(err) @@ -610,7 +609,6 @@ func TestDetectSkipService(t *testing.T) { when: - ProcessRunning("foobar") `) - defer os.Remove(f.Name()) detected, err := setup.Detect(&f, setup.DetectOptions{ForcedProcesses: []string{"foobar"}, SkipServices: []string{"wizard"}}) require.NoError(err) @@ -825,7 +823,6 @@ func TestDetectForcedOS(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { f := tempYAML(t, tc.config) - defer os.Remove(f.Name()) detected, err := setup.Detect(&f, setup.DetectOptions{ForcedOS: tc.forced}) cstest.RequireErrorContains(t, err, tc.expectedErr) @@ -1009,7 +1006,6 @@ func TestDetectDatasourceValidation(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { f := tempYAML(t, tc.config) - defer os.Remove(f.Name()) detected, err := setup.Detect(&f, setup.DetectOptions{}) cstest.RequireErrorContains(t, err, tc.expectedErr) require.Equal(tc.expected, detected)