Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gather logs - update "FilterLogFromScanner" function and add some tests #735

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions pkg/gatherers/common/gather_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,24 +164,28 @@ func FilterLogFromScanner(scanner *bufio.Scanner, messagesToSearch []string, reg
cb func(lines []string) []string) (string, error) {
var result []string

var messagesRegexp *regexp.Regexp
if regexSearch {
messagesRegexp = regexp.MustCompile(strings.Join(messagesToSearch, "|"))
}

for scanner.Scan() {
line := scanner.Text()
if len(messagesToSearch) == 0 {
result = append(result, line)
continue
}

for _, messageToSearch := range messagesToSearch {
if regexSearch {
matches, err := regexp.MatchString(messageToSearch, line)
if err != nil {
return "", err
}
if matches {
if regexSearch && messagesRegexp != nil {
matches := messagesRegexp.MatchString(line)
if matches {
result = append(result, line)
}
} else {
for _, messageToSearch := range messagesToSearch {
if strings.Contains(strings.ToLower(line), strings.ToLower(messageToSearch)) {
result = append(result, line)
}
} else if strings.Contains(strings.ToLower(line), strings.ToLower(messageToSearch)) {
result = append(result, line)
}
}
}
Expand Down
88 changes: 88 additions & 0 deletions pkg/gatherers/common/gather_logs_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package common

import (
"bufio"
"context"
"fmt"
"runtime"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -13,6 +16,29 @@ import (
"github.com/openshift/insights-operator/pkg/utils/marshal"
)

// nolint: lll, misspell
var testText = `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ullamcorper eget nulla facilisi etiam dignissim diam quis enim.
Rhoncus mattis rhoncus urna neque viverra.
Tempus urna et pharetra pharetra massa. Enim tortor at auctor urna nunc.
Id volutpat lacus laoreet non curabitur.
Feugiat pretium nibh ipsum consequat nisl vel.
Morbi tristique senectus et netus.
Tellus mauris a diam maecenas sed enim ut sem viverra.
Nunc scelerisque viverra mauris in aliquam.
Facilisis volutpat est velit egestas. Et netus et malesuada fames ac turpis egestas.
Sapien eget mi proin sed libero enim sed. Urna id volutpat lacus laoreet non.
Scelerisque eu ultrices vitae auctor.
Volutpat maecenas volutpat blandit aliquam etiam.
Sit amet nisl purus in mollis nunc sed id.
Tortor at auctor urna nunc id.
Purus in mollis nunc sed.
Enim ut tellus elementum sagittis vitae et leo.Quis viverra nibh cras pulvinar mattis nunc sed blandit libero.
Morbi tempus iaculis urna id volutpat lacus laoreet.
Pellentesque elit ullamcorper dignissim cras tincidunt lobortis.
Vitae proin sagittis nisl rhoncus.
Tortor condimentum lacinia quis vel eros donec ac odio tempor.`

func testGatherLogs(t *testing.T, regexSearch bool, stringToSearch string, shouldExist bool) {
const testPodName = "test"
const testLogFileName = "errors"
Expand Down Expand Up @@ -101,3 +127,65 @@ func Test_GatherLogs(t *testing.T) {
testGatherLogs(t, true, "[0-9]99", false)
})
}

func Test_FilterLogFromScanner(t *testing.T) {
tests := []struct {
name string
messagesToSearch []string
isRegexSearch bool
expectedOutput string
}{
{
name: "simple non-regex search",
messagesToSearch: []string{"Pellentesque"},
isRegexSearch: false,
expectedOutput: "Pellentesque elit ullamcorper dignissim cras tincidunt lobortis.",
},
{
name: "non-regex search with empty messages",
messagesToSearch: []string{},
isRegexSearch: false,
expectedOutput: testText,
},
{
name: "advanced non-regex search",
messagesToSearch: []string{"Pellentesque", "scelerisque", "this is not there"},
isRegexSearch: false,
// nolint lll
expectedOutput: "Nunc scelerisque viverra mauris in aliquam.\nScelerisque eu ultrices vitae auctor.\nPellentesque elit ullamcorper dignissim cras tincidunt lobortis.",
},
{
name: "Regex search with empty messages",
messagesToSearch: []string{},
isRegexSearch: true,
expectedOutput: testText,
},
{
name: "advanced regex search",
messagesToSearch: []string{"Pellentesque", "scelerisque", "this is not there"},
isRegexSearch: true,
expectedOutput: "Nunc scelerisque viverra mauris in aliquam.\nPellentesque elit ullamcorper dignissim cras tincidunt lobortis.",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reader := strings.NewReader(testText)
result, err := FilterLogFromScanner(bufio.NewScanner(reader), tt.messagesToSearch, tt.isRegexSearch, nil)
assert.NoError(t, err)
assert.Equal(t, tt.expectedOutput, result)
})
}
}

func Benchmark_FilterLogFromScanner(b *testing.B) {
var m runtime.MemStats
messagesToSearch := []string{"Pellentesque", "scelerisque", "this is not there"}
reader := strings.NewReader(testText)
for i := 0; i <= b.N; i++ {
// nolint errcheck
FilterLogFromScanner(bufio.NewScanner(reader), messagesToSearch, true, nil)
runtime.ReadMemStats(&m)
b.Logf("Size of allocated heap objects: %d MB, Size of heap in use: %d MB", m.Alloc/1024/1024, m.HeapInuse/1024/1024)
}
}