Skip to content

Commit

Permalink
fix golang ci yaml and improve makefile (#61)
Browse files Browse the repository at this point in the history
* fix golang ci yaml and improve makefile
  • Loading branch information
wph95 authored Mar 16, 2021
1 parent aa8d825 commit 81f80e6
Show file tree
Hide file tree
Showing 24 changed files with 93 additions and 135 deletions.
21 changes: 18 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
---
run:
issues-exit-code: 1
tests: true
skip-dirs:
skip-dirs-use-default: false
skip-files:
linters:
disable:
- errcheck
enable:
- gofmt
- bodyclose
- goconst
- gocritic
- goimports
- maligned
- misspell
- nakedret
- prealloc
Expand All @@ -14,5 +21,13 @@ linters:
- whitespace
- ineffassign
linters-settings:
gci:
local-prefixes: github.com/open-telemetry/opentelemetry-log-collection
goconst:
min-occurrences: 5
min-occurrences: 5
issues:
exclude-rules:
- path: operator/builtin/input/windows/xml\.go
linters:
- unused
- deadcode
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ listmod:

.PHONY: lint
lint:
golangci-lint run ./...
golangci-lint run --allow-parallel-runners ./...

.PHONY: lint-fix
lint-fix:
golangci-lint run --fix --allow-parallel-runners ./...

.PHONY: vet
vet:
Expand Down
17 changes: 6 additions & 11 deletions operator/builtin/input/file/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,7 @@ func TestConfig(t *testing.T) {
false,
func() *InputConfig {
cfg := defaultCfg()
var newMulti *MultilineConfig
newMulti = new(MultilineConfig)
newMulti := new(MultilineConfig)
newMulti.LineStartPattern = "Start"
cfg.Multiline = newMulti
return cfg
Expand All @@ -351,8 +350,7 @@ func TestConfig(t *testing.T) {
false,
func() *InputConfig {
cfg := defaultCfg()
var newMulti *MultilineConfig
newMulti = new(MultilineConfig)
newMulti := new(MultilineConfig)
newMulti.LineStartPattern = "%"
cfg.Multiline = newMulti
return cfg
Expand All @@ -363,8 +361,7 @@ func TestConfig(t *testing.T) {
false,
func() *InputConfig {
cfg := defaultCfg()
var newMulti *MultilineConfig
newMulti = new(MultilineConfig)
newMulti := new(MultilineConfig)
newMulti.LineEndPattern = "Start"
cfg.Multiline = newMulti
return cfg
Expand All @@ -375,8 +372,7 @@ func TestConfig(t *testing.T) {
false,
func() *InputConfig {
cfg := defaultCfg()
var newMulti *MultilineConfig
newMulti = new(MultilineConfig)
newMulti := new(MultilineConfig)
newMulti.LineEndPattern = "%"
cfg.Multiline = newMulti
return cfg
Expand Down Expand Up @@ -468,7 +464,7 @@ func TestConfig(t *testing.T) {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
cfgFromYaml, yamlErr := configFromFileViaYaml(t, path.Join(".", "testdata", fmt.Sprintf("%s.yaml", tc.name)))
cfgFromYaml, yamlErr := configFromFileViaYaml(path.Join(".", "testdata", fmt.Sprintf("%s.yaml", tc.name)))
cfgFromMapstructure, mapErr := configFromFileViaMapstructure(path.Join(".", "testdata", fmt.Sprintf("%s.yaml", tc.name)))
if tc.expectErr {
require.Error(t, yamlErr)
Expand All @@ -483,7 +479,7 @@ func TestConfig(t *testing.T) {
}
}

func configFromFileViaYaml(t *testing.T, file string) (*InputConfig, error) {
func configFromFileViaYaml(file string) (*InputConfig, error) {
bytes, err := ioutil.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("could not find config file: %s", err)
Expand Down Expand Up @@ -520,7 +516,6 @@ func configFromFileViaMapstructure(file string) (*InputConfig, error) {
return nil, err
}
return cfg, nil

}

func defaultCfg() *InputConfig {
Expand Down
35 changes: 16 additions & 19 deletions operator/builtin/input/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ type InputOperator struct {
encoding encoding.Encoding

wg sync.WaitGroup
readerWg sync.WaitGroup
firstCheck bool
cancel context.CancelFunc
}
Expand Down Expand Up @@ -111,25 +110,26 @@ func (f *InputOperator) startPoller(ctx context.Context) {

// poll checks all the watched paths for new entries
func (f *InputOperator) poll(ctx context.Context) {

var matches []string
if len(f.queuedMatches) > f.MaxConcurrentFiles {
matches, f.queuedMatches = f.queuedMatches[:f.MaxConcurrentFiles], f.queuedMatches[f.MaxConcurrentFiles:]
} else if len(f.queuedMatches) > 0 {
matches, f.queuedMatches = f.queuedMatches, make([]string, 0)
} else {
// Increment the generation on all known readers
// This is done here because the next generation is about to start
for i := 0; i < len(f.knownFiles); i++ {
f.knownFiles[i].generation++
}
if len(f.queuedMatches) > 0 {
matches, f.queuedMatches = f.queuedMatches, make([]string, 0)
} else {
// Increment the generation on all known readers
// This is done here because the next generation is about to start
for i := 0; i < len(f.knownFiles); i++ {
f.knownFiles[i].generation++
}

// Get the list of paths on disk
matches = getMatches(f.Include, f.Exclude)
if f.firstCheck && len(matches) == 0 {
f.Warnw("no files match the configured include patterns", "include", f.Include)
} else if len(matches) > f.MaxConcurrentFiles {
matches, f.queuedMatches = matches[:f.MaxConcurrentFiles], matches[f.MaxConcurrentFiles:]
// Get the list of paths on disk
matches = getMatches(f.Include, f.Exclude)
if f.firstCheck && len(matches) == 0 {
f.Warnw("no files match the configured include patterns", "include", f.Include)
} else if len(matches) > f.MaxConcurrentFiles {
matches, f.queuedMatches = matches[:f.MaxConcurrentFiles], matches[f.MaxConcurrentFiles:]
}
}
}

Expand Down Expand Up @@ -229,7 +229,6 @@ OUTER:
// Empty file, don't read it until we can compare its fingerprint
fps = append(fps[:i], fps[i+1:]...)
filesCopy = append(filesCopy[:i], filesCopy[i+1:]...)

}

for j := 0; j < len(fps); j++ {
Expand Down Expand Up @@ -267,9 +266,7 @@ OUTER:
// before clearing out readers that have existed for 3 generations.
func (f *InputOperator) saveCurrent(readers []*Reader) {
// Add readers from the current, completed poll interval to the list of known files
for _, reader := range readers {
f.knownFiles = append(f.knownFiles, reader)
}
f.knownFiles = append(f.knownFiles, readers...)

// Clear out old readers. They are sorted such that they are oldest first,
// so we can just find the first reader whose generation is less than our
Expand Down
3 changes: 1 addition & 2 deletions operator/builtin/input/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestBuild(t *testing.T) {
}{
{
"Basic",
func(f *InputConfig) { return },
func(f *InputConfig) {},
require.NoError,
func(t *testing.T, f *InputOperator) {
require.Equal(t, f.OutputOperators[0], fakeOutput)
Expand Down Expand Up @@ -802,7 +802,6 @@ func (rt rotationTest) run(tc rotationTest, copyTruncate, sequential bool) func(
}

func TestRotation(t *testing.T) {

cases := []rotationTest{
{
name: "NoRotation",
Expand Down
2 changes: 0 additions & 2 deletions operator/builtin/input/file/fingerprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
)

func TestNewFingerprintDoesNotModifyOffset(t *testing.T) {

fingerprint := "this is the fingerprint"
next := "this comes after the fingerprint and is substantially longer than the fingerprint"
extra := "fin"
Expand Down Expand Up @@ -61,7 +60,6 @@ func TestNewFingerprintDoesNotModifyOffset(t *testing.T) {
}

func TestNewFingerprint(t *testing.T) {

cases := []struct {
name string
fingerprintSize int
Expand Down
4 changes: 2 additions & 2 deletions operator/builtin/input/file/line_splitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,14 @@ func TestLineStartSplitFunc(t *testing.T) {
data := []byte(`LOGSTART`)

t.Run("NotAtEOF", func(t *testing.T) {
advance, token, err := splitFunc(data[:], false)
advance, token, err := splitFunc(data, false)
require.NoError(t, err)
require.Equal(t, 0, advance)
require.Nil(t, token)
})

t.Run("AtEOF", func(t *testing.T) {
advance, token, err := splitFunc(data[:], true)
advance, token, err := splitFunc(data, true)
require.NoError(t, err)
require.Equal(t, 0, advance)
require.Nil(t, token)
Expand Down
7 changes: 3 additions & 4 deletions operator/builtin/input/syslog/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,14 @@ func (c *SyslogInputConfig) UnmarshalYAML(unmarshal func(interface{}) error) err
}
c.SyslogParserConfig = *parserCfg

base := &BaseSyslogInputConfig{
}
err = unmarshal(base)
base := &BaseSyslogInputConfig{}
err = unmarshal(base)
if err != nil {
return err
}

c.InputConfig = base.InputConfig
c.Tcp= base.Tcp
c.Tcp = base.Tcp
c.Udp = base.Udp
return nil
}
12 changes: 5 additions & 7 deletions operator/builtin/input/syslog/syslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@ func SyslogInputTest(t *testing.T, cfg *SyslogInputConfig, tc syslog.Case) {
require.NoError(t, err)
}

switch tc.InputRecord.(type) {
case string:
_, err = conn.Write([]byte(tc.InputRecord.(string)))
case []byte:
if v, ok := tc.InputRecord.(string); ok {
_, err = conn.Write([]byte(v))
} else {
_, err = conn.Write(tc.InputRecord.([]byte))
}

Expand Down Expand Up @@ -121,7 +120,7 @@ udp:
var cfg SyslogInputConfig
err := yaml.Unmarshal([]byte(base), &cfg)
require.NoError(t, err)
require.Equal(t, "rfc5424", cfg.Protocol)
require.Equal(t, syslog.RFC5424, cfg.Protocol)
require.Equal(t, "localhost:1234", cfg.Udp.ListenAddress)

base = `type: syslog_input
Expand All @@ -133,8 +132,7 @@ tcp:
`
err = yaml.Unmarshal([]byte(base), &cfg)
require.NoError(t, err)
require.Equal(t, "rfc5424", cfg.Protocol)
require.Equal(t, syslog.RFC5424, cfg.Protocol)
require.Equal(t, "localhost:1234", cfg.Tcp.ListenAddress)
require.Equal(t, "/tmp/test.ca", cfg.Tcp.TLS.CAFile)

}
2 changes: 1 addition & 1 deletion operator/builtin/input/tcp/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (t *TCPInput) configureListener() error {
return nil
}

t.tls.Time = func() time.Time { return time.Now() }
t.tls.Time = time.Now
t.tls.Rand = rand.Reader

listener, err := tls.Listen("tcp", t.address, t.tls)
Expand Down
2 changes: 0 additions & 2 deletions operator/builtin/input/tcp/tcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ func tcpInputTest(input []byte, expected []string) func(t *testing.T) {

func tlsTCPInputTest(input []byte, expected []string) func(t *testing.T) {
return func(t *testing.T) {

f, err := os.Create("test.crt")
require.NoError(t, err)
defer f.Close()
Expand Down Expand Up @@ -335,5 +334,4 @@ func createTlsConfig(cert string, key string) *helper.TLSServerConfig {
KeyFile: key,
},
})

}
4 changes: 2 additions & 2 deletions operator/builtin/input/windows/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
)

func parseSecurity(message string) (string, map[string]interface{}) {

subject, details := message, map[string]interface{}{}
var subject string
details := map[string]interface{}{}

mp := newMessageProcessor(message)

Expand Down
10 changes: 0 additions & 10 deletions operator/builtin/input/windows/security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
)

func TestParseSecurity(t *testing.T) {

testCases := []string{
"account_name_changed",
"audit_settings_changed",
Expand All @@ -51,7 +50,6 @@ func TestParseSecurity(t *testing.T) {

for _, tc := range testCases {
t.Run(tc, func(t *testing.T) {

testDir := filepath.Join("testdata", "security", tc)
messageBytes, err := ioutil.ReadFile(filepath.Join(testDir, "message.in"))
require.NoError(t, err, "problem reading input file")
Expand All @@ -77,11 +75,3 @@ func TestParseSecurity(t *testing.T) {
})
}
}

// Use this to initialize test results from a WEL security message
// make sure to validate manually!
func initTestResult(testDir, message string, details map[string]interface{}) {
ioutil.WriteFile(filepath.Join(testDir, "message.out"), []byte(message), 0644)
bytes, _ := json.MarshalIndent(details, "", " ")
ioutil.WriteFile(filepath.Join(testDir, "details.out"), bytes, 0644)
}
1 change: 0 additions & 1 deletion operator/builtin/parser/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ func TestJSONParser(t *testing.T) {
}

func TestJSONParserWithEmbeddedTimeParser(t *testing.T) {

testTime := time.Unix(1136214245, 0)

cases := []struct {
Expand Down
8 changes: 3 additions & 5 deletions operator/builtin/parser/severity/severity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ type severityTestCase struct {
}

func TestSeverityParser(t *testing.T) {

testCases := []severityTestCase{
{
name: "unknown",
Expand Down Expand Up @@ -304,17 +303,16 @@ func TestSeverityParser(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
rootCfg := parseSeverityTestConfig(rootField, tc.mappingSet, tc.mapping)
rootEntry := makeTestEntry(rootField, tc.sample)
t.Run("root", runSeverityParseTest(t, rootCfg, rootEntry, tc.buildErr, tc.parseErr, tc.expected))
t.Run("root", runSeverityParseTest(rootCfg, rootEntry, tc.buildErr, tc.parseErr, tc.expected))

nonRootCfg := parseSeverityTestConfig(someField, tc.mappingSet, tc.mapping)
nonRootEntry := makeTestEntry(someField, tc.sample)
t.Run("non-root", runSeverityParseTest(t, nonRootCfg, nonRootEntry, tc.buildErr, tc.parseErr, tc.expected))
t.Run("non-root", runSeverityParseTest(nonRootCfg, nonRootEntry, tc.buildErr, tc.parseErr, tc.expected))
})
}
}

func runSeverityParseTest(t *testing.T, cfg *SeverityParserConfig, ent *entry.Entry, buildErr bool, parseErr bool, expected entry.Severity) func(*testing.T) {

func runSeverityParseTest(cfg *SeverityParserConfig, ent *entry.Entry, buildErr bool, parseErr bool, expected entry.Severity) func(*testing.T) {
return func(t *testing.T) {
buildContext := testutil.NewBuildContext(t)

Expand Down
Loading

0 comments on commit 81f80e6

Please sign in to comment.