Skip to content

Commit

Permalink
lint: explicit error checks (#3388)
Browse files Browse the repository at this point in the history
* errcheck: tests
* fflag errcheck
* http_test errcheck (avoid duplicate metric registration)
  • Loading branch information
mmetc authored Jan 2, 2025
1 parent 5c0c4f9 commit 4e6e6de
Show file tree
Hide file tree
Showing 19 changed files with 185 additions and 99 deletions.
8 changes: 6 additions & 2 deletions cmd/crowdsec-cli/clihub/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,15 @@ func (cli *cliHub) upgrade(ctx context.Context, yes bool, dryRun bool, force boo

for _, itemType := range cwhub.ItemTypes {
for _, item := range hub.GetInstalledByType(itemType, true) {
plan.AddCommand(hubops.NewDownloadCommand(item, contentProvider, force))
if err := plan.AddCommand(hubops.NewDownloadCommand(item, contentProvider, force)); err != nil {
return err
}
}
}

plan.AddCommand(hubops.NewDataRefreshCommand(force))
if err := plan.AddCommand(hubops.NewDataRefreshCommand(force)); err != nil {
return err
}

verbose := (cfg.Cscli.Output == "raw")

Expand Down
18 changes: 12 additions & 6 deletions pkg/acquisition/modules/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,19 @@ force_inotify: true`, testPattern),
logLevel: log.DebugLevel,
name: "GlobInotifyChmod",
afterConfigure: func() {
f, _ := os.Create("test_files/a.log")
f.Close()
f, err := os.Create("test_files/a.log")
require.NoError(t, err)
err = f.Close()
require.NoError(t, err)
time.Sleep(1 * time.Second)
os.Chmod("test_files/a.log", 0o000)
err = os.Chmod("test_files/a.log", 0o000)
require.NoError(t, err)
},
teardown: func() {
os.Chmod("test_files/a.log", 0o644)
os.Remove("test_files/a.log")
err := os.Chmod("test_files/a.log", 0o644)
require.NoError(t, err)
err = os.Remove("test_files/a.log")
require.NoError(t, err)
},
},
{
Expand All @@ -353,7 +358,8 @@ force_inotify: true`, testPattern),
logLevel: log.DebugLevel,
name: "InotifyMkDir",
afterConfigure: func() {
os.Mkdir("test_files/pouet/", 0o700)
err := os.Mkdir("test_files/pouet/", 0o700)
require.NoError(t, err)
},
teardown: func() {
os.Remove("test_files/pouet/")
Expand Down
89 changes: 52 additions & 37 deletions pkg/acquisition/modules/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func TestGetName(t *testing.T) {
assert.Equal(t, "http", h.GetName())
}

func SetupAndRunHTTPSource(t *testing.T, h *HTTPSource, config []byte, metricLevel int) (chan types.Event, *tomb.Tomb) {
func SetupAndRunHTTPSource(t *testing.T, h *HTTPSource, config []byte, metricLevel int) (chan types.Event, *prometheus.Registry, *tomb.Tomb) {
ctx := context.Background()
subLogger := log.WithFields(log.Fields{
"type": "http",
Expand All @@ -230,16 +230,18 @@ func SetupAndRunHTTPSource(t *testing.T, h *HTTPSource, config []byte, metricLev
err = h.StreamingAcquisition(ctx, out, &tomb)
require.NoError(t, err)

testRegistry := prometheus.NewPedanticRegistry()
for _, metric := range h.GetMetrics() {
prometheus.Register(metric)
err = testRegistry.Register(metric)
require.NoError(t, err)
}

return out, &tomb
return out, testRegistry, &tomb
}

func TestStreamingAcquisitionWrongHTTPMethod(t *testing.T) {
h := &HTTPSource{}
_, tomb := SetupAndRunHTTPSource(t, h, []byte(`
_, _, tomb:= SetupAndRunHTTPSource(t, h, []byte(`
source: http
listen_addr: 127.0.0.1:8080
path: /test
Expand All @@ -256,12 +258,13 @@ basic_auth:

h.Server.Close()
tomb.Kill(nil)
tomb.Wait()
err = tomb.Wait()
require.NoError(t, err)
}

func TestStreamingAcquisitionUnknownPath(t *testing.T) {
h := &HTTPSource{}
_, tomb := SetupAndRunHTTPSource(t, h, []byte(`
_, _, tomb := SetupAndRunHTTPSource(t, h, []byte(`
source: http
listen_addr: 127.0.0.1:8080
path: /test
Expand All @@ -278,12 +281,13 @@ basic_auth:

h.Server.Close()
tomb.Kill(nil)
tomb.Wait()
err = tomb.Wait()
require.NoError(t, err)
}

func TestStreamingAcquisitionBasicAuth(t *testing.T) {
h := &HTTPSource{}
_, tomb := SetupAndRunHTTPSource(t, h, []byte(`
_, _, tomb := SetupAndRunHTTPSource(t, h, []byte(`
source: http
listen_addr: 127.0.0.1:8080
path: /test
Expand All @@ -310,12 +314,13 @@ basic_auth:

h.Server.Close()
tomb.Kill(nil)
tomb.Wait()
err = tomb.Wait()
require.NoError(t, err)
}

func TestStreamingAcquisitionBadHeaders(t *testing.T) {
h := &HTTPSource{}
_, tomb := SetupAndRunHTTPSource(t, h, []byte(`
_, _, tomb := SetupAndRunHTTPSource(t, h, []byte(`
source: http
listen_addr: 127.0.0.1:8080
path: /test
Expand All @@ -337,12 +342,13 @@ headers:

h.Server.Close()
tomb.Kill(nil)
tomb.Wait()
err = tomb.Wait()
require.NoError(t, err)
}

func TestStreamingAcquisitionMaxBodySize(t *testing.T) {
h := &HTTPSource{}
_, tomb := SetupAndRunHTTPSource(t, h, []byte(`
_, _, tomb := SetupAndRunHTTPSource(t, h, []byte(`
source: http
listen_addr: 127.0.0.1:8080
path: /test
Expand All @@ -365,12 +371,13 @@ max_body_size: 5`), 0)

h.Server.Close()
tomb.Kill(nil)
tomb.Wait()
err = tomb.Wait()
require.NoError(t, err)
}

func TestStreamingAcquisitionSuccess(t *testing.T) {
h := &HTTPSource{}
out, tomb := SetupAndRunHTTPSource(t, h, []byte(`
out, reg, tomb := SetupAndRunHTTPSource(t, h, []byte(`
source: http
listen_addr: 127.0.0.1:8080
path: /test
Expand All @@ -396,16 +403,17 @@ headers:
err = <-errChan
require.NoError(t, err)

assertMetrics(t, h.GetMetrics(), 1)
assertMetrics(t, reg, h.GetMetrics(), 1)

h.Server.Close()
tomb.Kill(nil)
tomb.Wait()
err = tomb.Wait()
require.NoError(t, err)
}

func TestStreamingAcquisitionCustomStatusCodeAndCustomHeaders(t *testing.T) {
h := &HTTPSource{}
out, tomb := SetupAndRunHTTPSource(t, h, []byte(`
out, reg, tomb := SetupAndRunHTTPSource(t, h, []byte(`
source: http
listen_addr: 127.0.0.1:8080
path: /test
Expand Down Expand Up @@ -435,11 +443,12 @@ custom_headers:
err = <-errChan
require.NoError(t, err)

assertMetrics(t, h.GetMetrics(), 1)
assertMetrics(t, reg, h.GetMetrics(), 1)

h.Server.Close()
tomb.Kill(nil)
tomb.Wait()
err = tomb.Wait()
require.NoError(t, err)
}

type slowReader struct {
Expand Down Expand Up @@ -495,7 +504,7 @@ func assertEvents(out chan types.Event, expected []string, errChan chan error) {

func TestStreamingAcquisitionTimeout(t *testing.T) {
h := &HTTPSource{}
_, tomb := SetupAndRunHTTPSource(t, h, []byte(`
_, _, tomb := SetupAndRunHTTPSource(t, h, []byte(`
source: http
listen_addr: 127.0.0.1:8080
path: /test
Expand Down Expand Up @@ -525,12 +534,13 @@ timeout: 1s`), 0)

h.Server.Close()
tomb.Kill(nil)
tomb.Wait()
err = tomb.Wait()
require.NoError(t, err)
}

func TestStreamingAcquisitionTLSHTTPRequest(t *testing.T) {
h := &HTTPSource{}
_, tomb := SetupAndRunHTTPSource(t, h, []byte(`
_, _, tomb := SetupAndRunHTTPSource(t, h, []byte(`
source: http
listen_addr: 127.0.0.1:8080
auth_type: mtls
Expand All @@ -549,12 +559,13 @@ tls:

h.Server.Close()
tomb.Kill(nil)
tomb.Wait()
err = tomb.Wait()
require.NoError(t, err)
}

func TestStreamingAcquisitionTLSWithHeadersAuthSuccess(t *testing.T) {
h := &HTTPSource{}
out, tomb := SetupAndRunHTTPSource(t, h, []byte(`
out, reg, tomb := SetupAndRunHTTPSource(t, h, []byte(`
source: http
listen_addr: 127.0.0.1:8080
path: /test
Expand Down Expand Up @@ -600,16 +611,17 @@ tls:
err = <-errChan
require.NoError(t, err)

assertMetrics(t, h.GetMetrics(), 0)
assertMetrics(t, reg, h.GetMetrics(), 0)

h.Server.Close()
tomb.Kill(nil)
tomb.Wait()
err = tomb.Wait()
require.NoError(t, err)
}

func TestStreamingAcquisitionMTLS(t *testing.T) {
h := &HTTPSource{}
out, tomb := SetupAndRunHTTPSource(t, h, []byte(`
out, reg, tomb := SetupAndRunHTTPSource(t, h, []byte(`
source: http
listen_addr: 127.0.0.1:8080
path: /test
Expand Down Expand Up @@ -657,16 +669,17 @@ tls:
err = <-errChan
require.NoError(t, err)

assertMetrics(t, h.GetMetrics(), 0)
assertMetrics(t, reg, h.GetMetrics(), 0)

h.Server.Close()
tomb.Kill(nil)
tomb.Wait()
err = tomb.Wait()
require.NoError(t, err)
}

func TestStreamingAcquisitionGzipData(t *testing.T) {
h := &HTTPSource{}
out, tomb := SetupAndRunHTTPSource(t, h, []byte(`
out, reg, tomb := SetupAndRunHTTPSource(t, h, []byte(`
source: http
listen_addr: 127.0.0.1:8080
path: /test
Expand Down Expand Up @@ -709,16 +722,17 @@ headers:
err = <-errChan
require.NoError(t, err)

assertMetrics(t, h.GetMetrics(), 2)
assertMetrics(t, reg, h.GetMetrics(), 2)

h.Server.Close()
tomb.Kill(nil)
tomb.Wait()
err = tomb.Wait()
require.NoError(t, err)
}

func TestStreamingAcquisitionNDJson(t *testing.T) {
h := &HTTPSource{}
out, tomb := SetupAndRunHTTPSource(t, h, []byte(`
out, reg, tomb := SetupAndRunHTTPSource(t, h, []byte(`
source: http
listen_addr: 127.0.0.1:8080
path: /test
Expand Down Expand Up @@ -747,15 +761,16 @@ headers:
err = <-errChan
require.NoError(t, err)

assertMetrics(t, h.GetMetrics(), 2)
assertMetrics(t, reg, h.GetMetrics(), 2)

h.Server.Close()
tomb.Kill(nil)
tomb.Wait()
err = tomb.Wait()
require.NoError(t, err)
}

func assertMetrics(t *testing.T, metrics []prometheus.Collector, expected int) {
promMetrics, err := prometheus.DefaultGatherer.Gather()
func assertMetrics(t *testing.T, reg *prometheus.Registry, metrics []prometheus.Collector, expected int) {
promMetrics, err := reg.Gather()
require.NoError(t, err)

isExist := false
Expand Down
4 changes: 3 additions & 1 deletion pkg/acquisition/modules/journalctl/journalctl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/tomb.v2"

"github.com/crowdsecurity/go-cs-lib/cstest"
Expand Down Expand Up @@ -268,7 +269,8 @@ journalctl_filter:
}

tomb.Kill(nil)
tomb.Wait()
err = tomb.Wait()
require.NoError(t, err)

output, _ := exec.Command("pgrep", "-x", "journalctl").CombinedOutput()
if len(output) != 0 {
Expand Down
6 changes: 4 additions & 2 deletions pkg/acquisition/modules/kafka/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ topic: crowdsecplaintext`), subLogger, configuration.METRICS_NONE)
}
require.Equal(t, ts.expectedLines, actualLines)
tomb.Kill(nil)
tomb.Wait()
err = tomb.Wait()
require.NoError(t, err)
})
}
}
Expand Down Expand Up @@ -271,7 +272,8 @@ tls:
}
require.Equal(t, ts.expectedLines, actualLines)
tomb.Kill(nil)
tomb.Wait()
err = tomb.Wait()
require.NoError(t, err)
})
}
}
Loading

0 comments on commit 4e6e6de

Please sign in to comment.