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

[check-log] Add option to suppress pattern display #234

Merged
merged 1 commit into from
May 10, 2018
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
8 changes: 7 additions & 1 deletion check-log/lib/check-log.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
type logOpts struct {
LogFile string `short:"f" long:"file" value-name:"FILE" description:"Path to log file"`
Pattern []string `short:"p" long:"pattern" required:"true" value-name:"PAT" description:"Pattern to search for. If specified multiple, they will be treated together with the AND operator"`
SuppressPattern bool `long:"suppress-pattern" description:"Suppress pattern display"`
Exclude string `short:"E" long:"exclude" value-name:"PAT" description:"Pattern to exclude from matching"`
WarnOver int64 `short:"w" long:"warning-over" description:"Trigger a warning if matched lines is over a number"`
CritOver int64 `short:"c" long:"critical-over" description:"Trigger a critical if matched lines is over a number"`
Expand Down Expand Up @@ -184,7 +185,12 @@ func run(args []string) *checkers.Checker {
for _, ptn := range opts.Pattern {
patterns = append(patterns, fmt.Sprintf("/%s/", ptn))
}
msg := fmt.Sprintf("%d warnings, %d criticals for pattern %s.", warnNum, critNum, strings.Join(patterns, " and "))
var msg string
if opts.SuppressPattern {
msg = fmt.Sprintf("%d warnings, %d criticals.", warnNum, critNum)
} else {
msg = fmt.Sprintf("%d warnings, %d criticals for pattern %s.", warnNum, critNum, strings.Join(patterns, " and "))
}
if errorOverall != "" {
msg += "\n" + errorOverall
}
Expand Down
71 changes: 71 additions & 0 deletions check-log/lib/check-log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,3 +687,74 @@ func TestRunMultiplePattern(t *testing.T) {
}
testInvalidPattern()
}

func TestRunWithSuppressOption(t *testing.T) {
dir, err := ioutil.TempDir("", "check-log-test")
if err != nil {
t.Errorf("something went wrong")
}
defer os.RemoveAll(dir)

logf := filepath.Join(dir, "dummy")
fh, _ := os.Create(logf)
defer fh.Close()

ptn1 := `FATAL`
ptn2 := `TESTAPPLICATION`
params := []string{"-s", dir, "-f", logf, "-p", ptn1, "-p", ptn2, "--suppress-pattern"}
opts, _ := parseArgs(params)
opts.prepare()

stateFile := getStateFile(opts.StateDir, logf, opts.origArgs)

bytes, _ := getBytesToSkip(stateFile)
assert.Equal(t, int64(0), bytes, "something went wrong")

l1 := "FATAL\nTESTAPPLICATION\n"
test2line := func() {
fh.WriteString(l1)
ckr := run(params)
assert.Equal(t, checkers.OK, ckr.Status, "ckr.Status should be OK")
msg := "0 warnings, 0 criticals."
assert.Equal(t, ckr.Message, msg, "something went wrong")

bytes, _ = getBytesToSkip(stateFile)
assert.Equal(t, int64(len(l1)), bytes, "something went wrong")
}
test2line()

l2 := "FATAL TESTAPPLICATION\nTESTAPPLICATION FATAL\n"
testAndCondition := func() {
fh.WriteString(l2)
ckr := run(params)
assert.Equal(t, checkers.CRITICAL, ckr.Status, "ckr.Status should be CRITICAL")
msg := "2 warnings, 2 criticals."
assert.Equal(t, ckr.Message, msg, "something went wrong")

bytes, _ = getBytesToSkip(stateFile)
assert.Equal(t, int64(len(l1)+len(l2)), bytes, "something went wrong")
}
testAndCondition()

l3 := "OK\n"
testWithLevel := func() {
fh.WriteString(l3)
params := []string{"-s", dir, "-f", logf, "-p", ptn1, "-p", ptn2, "--warning-level", "12", "--suppress-pattern"}
ckr := run(params)
assert.Equal(t, checkers.UNKNOWN, ckr.Status, "ckr.Status should be UNKNOWN")
msg := "When multiple patterns specified, --warning-level --critical-level can not be used"
assert.Equal(t, ckr.Message, msg, "something went wrong")
}
testWithLevel()

testInvalidPattern := func() {
fh.WriteString(l3)
ptn3 := "+"
params := []string{"-s", dir, "-f", logf, "-p", ptn1, "-p", ptn3, "--suppress-pattern"}
ckr := run(params)
assert.Equal(t, checkers.UNKNOWN, ckr.Status, "ckr.Status should be UNKNOWN")
msg := "pattern is invalid"
assert.Equal(t, ckr.Message, msg, "something went wrong")
}
testInvalidPattern()
}