Created
August 20, 2022 12:01
-
-
Save overdrive3000/1d02d97972b8d31cd88f909d13fb61ff to your computer and use it in GitHub Desktop.
Log package test file
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
package log_test | |
import ( | |
"bytes" | |
"encoding/json" | |
"nomad-datadog-exporter/internal/log" | |
"os" | |
"testing" | |
"github.com/google/go-cmp/cmp" | |
) | |
type format struct { | |
AwsRegion string `json:"aws_region,omitempty"` | |
FunctionName string `json:"function_name,omitempty"` | |
Level string `json:"level,omitempty"` | |
Msg string `json:"msg,omitempty"` | |
} | |
func TestNewLoggerDefaults(t *testing.T) { | |
t.Parallel() | |
os.Setenv("AWS_REGION", "us-east-1") | |
os.Setenv("AWS_LAMBDA_FUNCTION_NAME", "nomad-datadog-exporter") | |
want := format{ | |
AwsRegion: "us-east-1", | |
FunctionName: "nomad-datadog-exporter", | |
Level: "info", | |
Msg: "Test!!!", | |
} | |
fakeStdErr := &bytes.Buffer{} | |
logger, err := log.NewLogger( | |
log.WithOutput(fakeStdErr), | |
) | |
if err != nil { | |
t.Fatal(err) | |
} | |
logger.Info("Test!!!") | |
var got format | |
if err := json.Unmarshal(fakeStdErr.Bytes(), &got); err != nil { | |
t.Fatal(err) | |
} | |
if !cmp.Equal(want, got) { | |
cmp.Diff(want, got) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment