Skip to content

Commit

Permalink
CI: enable testifylint (#2696)
Browse files Browse the repository at this point in the history
 - reverse actual and expected values
 - use assert.False, assert.True
 - use assert.Len, assert.Emtpy
 - use require.Error, require.NoError
 - use assert.InDelta
  • Loading branch information
mmetc authored Jan 5, 2024
1 parent da746f7 commit 5622ac8
Show file tree
Hide file tree
Showing 16 changed files with 283 additions and 215 deletions.
40 changes: 0 additions & 40 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -242,42 +242,6 @@ issues:

# Will fix, trivial - just beware of merge conflicts

- linters:
- testifylint
text: "expected-actual: need to reverse actual and expected values"

- linters:
- testifylint
text: "bool-compare: use assert.False"

- linters:
- testifylint
text: "len: use assert.Len"

- linters:
- testifylint
text: "bool-compare: use assert.True"

- linters:
- testifylint
text: "bool-compare: use require.True"

- linters:
- testifylint
text: "require-error: for error assertions use require"

- linters:
- testifylint
text: "error-nil: use assert.NoError"

- linters:
- testifylint
text: "error-nil: use assert.Error"

- linters:
- testifylint
text: "empty: use assert.Empty"

- linters:
- perfsprint
text: "fmt.Sprintf can be replaced .*"
Expand All @@ -286,10 +250,6 @@ issues:
# Will fix, easy but some neurons required
#

- linters:
- testifylint
text: "float-compare: use assert.InEpsilon .*or InDelta.*"

- linters:
- errorlint
text: "non-wrapping format verb for fmt.Errorf. Use `%w` to format errors"
Expand Down
40 changes: 27 additions & 13 deletions pkg/acquisition/acquisition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,19 @@ func (f *MockSource) Configure(cfg []byte, logger *log.Entry) error {
if err := f.UnmarshalConfig(cfg); err != nil {
return err
}

if f.Mode == "" {
f.Mode = configuration.CAT_MODE
}

if f.Mode != configuration.CAT_MODE && f.Mode != configuration.TAIL_MODE {
return fmt.Errorf("mode %s is not supported", f.Mode)
}

if f.Toto == "" {
return fmt.Errorf("expect non-empty toto")
}

return nil
}
func (f *MockSource) GetMode() string { return f.Mode }
Expand Down Expand Up @@ -77,13 +81,15 @@ func appendMockSource() {
if GetDataSourceIface("mock") == nil {
AcquisitionSources["mock"] = func() DataSource { return &MockSource{} }
}

if GetDataSourceIface("mock_cant_run") == nil {
AcquisitionSources["mock_cant_run"] = func() DataSource { return &MockSourceCantRun{} }
}
}

func TestDataSourceConfigure(t *testing.T) {
appendMockSource()

tests := []struct {
TestName string
String string
Expand Down Expand Up @@ -185,29 +191,30 @@ wowo: ajsajasjas
switch tc.TestName {
case "basic_valid_config":
mock := (*ds).Dump().(*MockSource)
assert.Equal(t, mock.Toto, "test_value1")
assert.Equal(t, mock.Mode, "cat")
assert.Equal(t, mock.logger.Logger.Level, log.InfoLevel)
assert.Equal(t, mock.Labels, map[string]string{"test": "foobar"})
assert.Equal(t, "test_value1", mock.Toto)
assert.Equal(t, "cat", mock.Mode)
assert.Equal(t, log.InfoLevel, mock.logger.Logger.Level)
assert.Equal(t, map[string]string{"test": "foobar"}, mock.Labels)
case "basic_debug_config":
mock := (*ds).Dump().(*MockSource)
assert.Equal(t, mock.Toto, "test_value1")
assert.Equal(t, mock.Mode, "cat")
assert.Equal(t, mock.logger.Logger.Level, log.DebugLevel)
assert.Equal(t, mock.Labels, map[string]string{"test": "foobar"})
assert.Equal(t, "test_value1", mock.Toto)
assert.Equal(t, "cat", mock.Mode)
assert.Equal(t, log.DebugLevel, mock.logger.Logger.Level)
assert.Equal(t, map[string]string{"test": "foobar"}, mock.Labels)
case "basic_tailmode_config":
mock := (*ds).Dump().(*MockSource)
assert.Equal(t, mock.Toto, "test_value1")
assert.Equal(t, mock.Mode, "tail")
assert.Equal(t, mock.logger.Logger.Level, log.DebugLevel)
assert.Equal(t, mock.Labels, map[string]string{"test": "foobar"})
assert.Equal(t, "test_value1", mock.Toto)
assert.Equal(t, "tail", mock.Mode)
assert.Equal(t, log.DebugLevel, mock.logger.Logger.Level)
assert.Equal(t, map[string]string{"test": "foobar"}, mock.Labels)
}
})
}
}

func TestLoadAcquisitionFromFile(t *testing.T) {
appendMockSource()

tests := []struct {
TestName string
Config csconfig.CrowdsecServiceCfg
Expand Down Expand Up @@ -284,7 +291,6 @@ func TestLoadAcquisitionFromFile(t *testing.T) {

assert.Len(t, dss, tc.ExpectedLen)
})

}
}

Expand All @@ -304,9 +310,11 @@ func (f *MockCat) Configure(cfg []byte, logger *log.Entry) error {
if f.Mode == "" {
f.Mode = configuration.CAT_MODE
}

if f.Mode != configuration.CAT_MODE {
return fmt.Errorf("mode %s is not supported", f.Mode)
}

return nil
}

Expand All @@ -319,6 +327,7 @@ func (f *MockCat) OneShotAcquisition(out chan types.Event, tomb *tomb.Tomb) erro
evt.Line.Src = "test"
out <- evt
}

return nil
}
func (f *MockCat) StreamingAcquisition(chan types.Event, *tomb.Tomb) error {
Expand All @@ -345,9 +354,11 @@ func (f *MockTail) Configure(cfg []byte, logger *log.Entry) error {
if f.Mode == "" {
f.Mode = configuration.TAIL_MODE
}

if f.Mode != configuration.TAIL_MODE {
return fmt.Errorf("mode %s is not supported", f.Mode)
}

return nil
}

Expand All @@ -364,6 +375,7 @@ func (f *MockTail) StreamingAcquisition(out chan types.Event, t *tomb.Tomb) erro
out <- evt
}
<-t.Dying()

return nil
}
func (f *MockTail) CanRun() error { return nil }
Expand Down Expand Up @@ -446,6 +458,7 @@ func (f *MockTailError) StreamingAcquisition(out chan types.Event, t *tomb.Tomb)
out <- evt
}
t.Kill(fmt.Errorf("got error (tomb)"))

return fmt.Errorf("got error")
}

Expand Down Expand Up @@ -499,6 +512,7 @@ func (f *MockSourceByDSN) ConfigureByDSN(dsn string, labels map[string]string, l
if dsn != "test_expect" {
return fmt.Errorf("unexpected value")
}

return nil
}
func (f *MockSourceByDSN) GetUuid() string { return "" }
Expand Down
34 changes: 27 additions & 7 deletions pkg/acquisition/modules/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ container_name:
subLogger := log.WithFields(log.Fields{
"type": "docker",
})

for _, test := range tests {
f := DockerSource{}
err := f.Configure([]byte(test.config), subLogger)
Expand All @@ -66,12 +67,15 @@ container_name:

func TestConfigureDSN(t *testing.T) {
log.Infof("Test 'TestConfigureDSN'")

var dockerHost string

if runtime.GOOS == "windows" {
dockerHost = "npipe:////./pipe/docker_engine"
} else {
dockerHost = "unix:///var/run/podman/podman.sock"
}

tests := []struct {
name string
dsn string
Expand Down Expand Up @@ -106,6 +110,7 @@ func TestConfigureDSN(t *testing.T) {
subLogger := log.WithFields(log.Fields{
"type": "docker",
})

for _, test := range tests {
f := DockerSource{}
err := f.ConfigureByDSN(test.dsn, map[string]string{"type": "testtype"}, subLogger, "")
Expand Down Expand Up @@ -156,8 +161,11 @@ container_name_regexp:
}

for _, ts := range tests {
var logger *log.Logger
var subLogger *log.Entry
var (
logger *log.Logger
subLogger *log.Entry
)

if ts.expectedOutput != "" {
logger.SetLevel(ts.logLevel)
subLogger = logger.WithFields(log.Fields{
Expand All @@ -173,10 +181,12 @@ container_name_regexp:
dockerTomb := tomb.Tomb{}
out := make(chan types.Event)
dockerSource := DockerSource{}

err := dockerSource.Configure([]byte(ts.config), subLogger)
if err != nil {
t.Fatalf("Unexpected error : %s", err)
}

dockerSource.Client = new(mockDockerCli)
actualLines := 0
readerTomb := &tomb.Tomb{}
Expand Down Expand Up @@ -204,21 +214,23 @@ container_name_regexp:
if err := readerTomb.Wait(); err != nil {
t.Fatal(err)
}

if ts.expectedLines != 0 {
assert.Equal(t, ts.expectedLines, actualLines)
}

err = streamTomb.Wait()
if err != nil {
t.Fatalf("docker acquisition error: %s", err)
}
}

}

func (cli *mockDockerCli) ContainerList(ctx context.Context, options dockerTypes.ContainerListOptions) ([]dockerTypes.Container, error) {
if readLogs == true {
return []dockerTypes.Container{}, nil
}

containers := make([]dockerTypes.Container, 0)
container := &dockerTypes.Container{
ID: "12456",
Expand All @@ -233,16 +245,20 @@ func (cli *mockDockerCli) ContainerLogs(ctx context.Context, container string, o
if readLogs == true {
return io.NopCloser(strings.NewReader("")), nil
}

readLogs = true
data := []string{"docker\n", "test\n", "1234\n"}
ret := ""

for _, line := range data {
startLineByte := make([]byte, 8)
binary.LittleEndian.PutUint32(startLineByte, 1) //stdout stream
binary.BigEndian.PutUint32(startLineByte[4:], uint32(len(line)))
ret += fmt.Sprintf("%s%s", startLineByte, line)
}

r := io.NopCloser(strings.NewReader(ret)) // r type is io.ReadCloser

return r, nil
}

Expand All @@ -252,6 +268,7 @@ func (cli *mockDockerCli) ContainerInspect(ctx context.Context, c string) (docke
Tty: false,
},
}

return r, nil
}

Expand Down Expand Up @@ -285,8 +302,11 @@ func TestOneShot(t *testing.T) {
}

for _, ts := range tests {
var subLogger *log.Entry
var logger *log.Logger
var (
subLogger *log.Entry
logger *log.Logger
)

if ts.expectedOutput != "" {
logger.SetLevel(ts.logLevel)
subLogger = logger.WithFields(log.Fields{
Expand All @@ -307,6 +327,7 @@ func TestOneShot(t *testing.T) {
if err := dockerClient.ConfigureByDSN(ts.dsn, labels, subLogger, ""); err != nil {
t.Fatalf("unable to configure dsn '%s': %s", ts.dsn, err)
}

dockerClient.Client = new(mockDockerCli)
out := make(chan types.Event, 100)
tomb := tomb.Tomb{}
Expand All @@ -315,8 +336,7 @@ func TestOneShot(t *testing.T) {

// else we do the check before actualLines is incremented ...
if ts.expectedLines != 0 {
assert.Equal(t, ts.expectedLines, len(out))
assert.Len(t, out, ts.expectedLines)
}
}

}
Loading

0 comments on commit 5622ac8

Please sign in to comment.