-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Peter Lisy
committed
Jun 28, 2022
1 parent
f42c0eb
commit dd52580
Showing
5 changed files
with
317 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2022 Outreach Corporation. All Rights Reserved. | ||
|
||
package main_test | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"gotest.tools/v3/icmd" | ||
) | ||
|
||
type line struct { | ||
Level string `json:"level"` | ||
Message string `json:"message"` | ||
Timestamp string `json:"@timestamp"` | ||
} | ||
|
||
func TestLogfmtNoJSONText(t *testing.T) { | ||
txt := "Line 1.\nLine 2.\n" | ||
|
||
runLogfmt(t, txt, txt) | ||
} | ||
|
||
func TestLogfmtStructured(t *testing.T) { | ||
var buff bytes.Buffer | ||
enc := json.NewEncoder(&buff) | ||
|
||
enc.Encode(line{ | ||
Level: "info", | ||
Message: "Hello, World!", | ||
Timestamp: (time.Time{}.Add(1 * time.Second)).Format(time.RFC3339Nano), | ||
}) | ||
|
||
runLogfmt(t, | ||
buff.String(), | ||
`time="0001-01-01T00:00:01Z" level=info msg="Hello, World!"`) | ||
} | ||
|
||
func TestLogfmtStructuredFormat(t *testing.T) { | ||
var buff bytes.Buffer | ||
enc := json.NewEncoder(&buff) | ||
|
||
enc.Encode(line{Level: "info", Message: "msg1"}) | ||
enc.Encode(line{Level: "info", Message: "msg2"}) | ||
|
||
runLogfmt(t, | ||
buff.String(), | ||
"msg1\nmsg2\n", | ||
"--format", "{{ .message }}", | ||
) | ||
} | ||
|
||
func TestLogfmtStructuredFilter(t *testing.T) { | ||
var buff bytes.Buffer | ||
enc := json.NewEncoder(&buff) | ||
|
||
enc.Encode(line{Level: "info", Message: "info"}) | ||
enc.Encode(line{Level: "warn", Message: "warn"}) | ||
enc.Encode(line{Level: "error", Message: "error"}) | ||
|
||
runLogfmt(t, | ||
buff.String(), | ||
"info\nerror\n", | ||
"--format", "{{ .message }}", | ||
"--filter", `select(.level != "warn")`, | ||
) | ||
} | ||
|
||
func runLogfmt(t *testing.T, input, output string, args ...string) { | ||
logs := strings.NewReader(input) | ||
|
||
args = append([]string{"run", `logfmt.go`}, args...) | ||
|
||
result := icmd.RunCmd(icmd.Command("go", args...), icmd.WithStdin(logs)) | ||
result.Assert(t, icmd.Expected{ | ||
ExitCode: 0, | ||
Err: output, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.