gelf/gelflogger_test.go

159 lines
4.0 KiB
Go
Raw Normal View History

package gelf
import (
"bytes"
"log"
"os"
"os/exec"
"testing"
. "github.com/smartystreets/goconvey/convey"
golf "gopkg.in/aphistic/golf.v0"
)
func TestSetDefaultConfig(t *testing.T) {
Convey("SetDefaultConfig with empty host keeps GELF client disabled", t, func() {
originalClient := c
defer func() { c = originalClient }()
c = nil
SetDefaultConfig("", 12201, map[string]interface{}{"service": "test"})
So(c, ShouldBeNil)
})
Convey("SetDefaultConfig with host initializes GELF client", t, func() {
originalClient := c
defer func() {
if c != nil {
c.Close()
}
c = originalClient
}()
c = nil
SetDefaultConfig("127.0.0.1", 12201, map[string]interface{}{"service": "test"})
So(c, ShouldNotBeNil)
})
}
func TestLoggingWithoutGelfClient(t *testing.T) {
Convey("Logging functions write to standard logger when GELF client is nil", t, func() {
originalClient := c
defer func() { c = originalClient }()
c = nil
buffer, restore := captureStandardLogger()
defer restore()
Debug("debug message")
Debugf("debugf %d", 42)
Info("info message")
Infof("infof %s", "ok")
Alert("alert message")
Alertf("alertf %d", 7)
output := buffer.String()
So(output, ShouldContainSubstring, "debug message")
So(output, ShouldContainSubstring, "debugf 42")
So(output, ShouldContainSubstring, "info message")
So(output, ShouldContainSubstring, "infof ok")
So(output, ShouldContainSubstring, "Alert: alert message")
So(output, ShouldContainSubstring, "Alert: alertf 7")
})
}
func TestLoggingWithGelfClient(t *testing.T) {
Convey("Logging functions execute without panic when GELF client is configured", t, func() {
originalClient := c
defer func() {
if c != nil {
c.Close()
}
c = originalClient
}()
client, err := golf.NewClient()
So(err, ShouldBeNil)
c = client
So(c.Dial("udp://127.0.0.1:12201"), ShouldBeNil)
logger, err := c.NewLogger()
So(err, ShouldBeNil)
golf.DefaultLogger(logger)
buffer, restore := captureStandardLogger()
defer restore()
So(func() { Debug("debug with gelf") }, ShouldNotPanic)
So(func() { Debugf("debugf %d", 1) }, ShouldNotPanic)
So(func() { Info("info with gelf") }, ShouldNotPanic)
So(func() { Infof("infof %d", 2) }, ShouldNotPanic)
So(func() { Alert("alert with gelf") }, ShouldNotPanic)
So(func() { Alertf("alertf %d", 3) }, ShouldNotPanic)
output := buffer.String()
So(output, ShouldContainSubstring, "debug with gelf")
So(output, ShouldContainSubstring, "Alert: alert with gelf")
})
}
func TestFatalExits(t *testing.T) {
Convey("Fatal exits with non-zero status and logs message", t, func() {
cmd := exec.Command(os.Args[0], "-test.run=TestFatalHelperProcess")
cmd.Env = append(os.Environ(), "GO_WANT_FATAL_HELPER=1", "FATAL_MODE=plain", "FATAL_WITH_GELF=1")
output, err := cmd.CombinedOutput()
So(err, ShouldNotBeNil)
So(string(output), ShouldContainSubstring, "Fatal: fatal message")
})
}
func TestFatalfExits(t *testing.T) {
Convey("Fatalf exits with non-zero status and logs formatted message", t, func() {
cmd := exec.Command(os.Args[0], "-test.run=TestFatalHelperProcess")
cmd.Env = append(os.Environ(), "GO_WANT_FATAL_HELPER=1", "FATAL_MODE=format", "FATAL_WITH_GELF=1")
output, err := cmd.CombinedOutput()
So(err, ShouldNotBeNil)
So(string(output), ShouldContainSubstring, "Fatal: formatted 9")
})
}
func TestFatalHelperProcess(t *testing.T) {
if os.Getenv("GO_WANT_FATAL_HELPER") != "1" {
return
}
log.SetFlags(0)
mode := os.Getenv("FATAL_MODE")
if os.Getenv("FATAL_WITH_GELF") == "1" {
SetDefaultConfig("127.0.0.1", 12201, map[string]interface{}{"service": "test"})
}
if mode == "format" {
Fatalf("formatted %d", 9)
}
Fatal("fatal message")
}
func captureStandardLogger() (*bytes.Buffer, func()) {
var buffer bytes.Buffer
originalWriter := log.Writer()
originalFlags := log.Flags()
originalPrefix := log.Prefix()
log.SetOutput(&buffer)
log.SetFlags(0)
log.SetPrefix("")
return &buffer, func() {
log.SetOutput(originalWriter)
log.SetFlags(originalFlags)
log.SetPrefix(originalPrefix)
}
}