go_goutils/logging/funcs_linux_test.go

271 lines
6.8 KiB
Go
Raw Normal View History

2022-01-16 06:55:29 -05:00
package logging
import (
`fmt`
`os`
`testing`
)
/*
TestSysDLogger tests functionality for SystemDLogger.
*/
func TestSysDLogger(t *testing.T) {
var l *SystemDLogger
var ltype string = "SystemDLogger"
var prefix string
var err error
l = &SystemDLogger{
EnableDebug: true,
Prefix: TestLogPrefix,
}
if err = l.Setup(); err != nil {
t.Fatalf("error when running Setup: %v", err.Error())
}
t.Logf("Logger %v passed Setup. Logger: %#v", ltype, l)
if err = l.Alert(testAlert, ltype); err != nil {
t.Fatalf("error for Alert: %v", err.Error())
}
if err = l.Crit(testCrit, ltype); err != nil {
t.Fatalf("error for Crit: %v", err.Error())
}
if err = l.Debug(testDebug, ltype); err != nil {
t.Fatalf("error for Debug: %v", err.Error())
}
if err = l.Emerg(testEmerg, ltype); err != nil {
t.Fatalf("error for Emerg: %v", err.Error())
}
if err = l.Err(testErr, ltype); err != nil {
t.Fatalf("error for Err: %v", err.Error())
}
if err = l.Info(testInfo, ltype); err != nil {
t.Fatalf("error for Alert: %v", err.Error())
}
if err = l.Notice(testNotice, ltype); err != nil {
t.Fatalf("error for Notice: %v", err.Error())
}
if err = l.Warning(testWarning, ltype); err != nil {
t.Fatalf("error for Warning: %v", err.Error())
}
if prefix, err = l.GetPrefix(); err != nil {
t.Fatalf("error when fetching prefix: %v", err.Error())
}
if prefix != TestLogPrefix {
t.Fatalf("true prefix ('%v') does not match TestLogPrefix ('%v')", prefix, TestLogPrefix)
}
if err = l.SetPrefix(TestLogAltPrefix); err != nil {
t.Fatalf("error when setting prefix to %v: %v", TestLogAltPrefix, err.Error())
} else {
_ = l.SetPrefix(TestLogPrefix)
}
if err = l.DoDebug(false); err != nil {
t.Fatalf("error when changing debug to false: %v", err.Error())
} else if l.EnableDebug {
t.Fatalf("did not properly set Debug filter state")
} else {
_ = l.DoDebug(true)
}
if err = l.Shutdown(); err != nil {
t.Fatalf("Error when running Shutdown: %v", err.Error())
}
t.Logf("Logger %v passed all logging targets.", ltype)
}
/*
TestSyslogLogger tests functionality for SyslogLogger.
*/
func TestSyslogLogger(t *testing.T) {
var l *SyslogLogger
var ltype string = "SyslogLogger"
var prefix string
var err error
l = &SyslogLogger{
EnableDebug: true,
Prefix: TestLogPrefix,
}
if err = l.Setup(); err != nil {
t.Fatalf("error when running Setup: %v", err.Error())
}
t.Logf("Logger %v passed Setup. Logger: %#v", ltype, l)
if err = l.Alert(testAlert, ltype); err != nil {
t.Fatalf("error for Alert: %v", err.Error())
}
if err = l.Crit(testCrit, ltype); err != nil {
t.Fatalf("error for Crit: %v", err.Error())
}
if err = l.Debug(testDebug, ltype); err != nil {
t.Fatalf("error for Debug: %v", err.Error())
}
if err = l.Emerg(testEmerg, ltype); err != nil {
t.Fatalf("error for Emerg: %v", err.Error())
}
if err = l.Err(testErr, ltype); err != nil {
t.Fatalf("error for Err: %v", err.Error())
}
if err = l.Info(testInfo, ltype); err != nil {
t.Fatalf("error for Alert: %v", err.Error())
}
if err = l.Notice(testNotice, ltype); err != nil {
t.Fatalf("error for Notice: %v", err.Error())
}
if err = l.Warning(testWarning, ltype); err != nil {
t.Fatalf("error for Warning: %v", err.Error())
}
if prefix, err = l.GetPrefix(); err != nil {
t.Fatalf("error when fetching prefix: %v", err.Error())
}
if prefix != TestLogPrefix {
t.Fatalf("true prefix ('%v') does not match TestLogPrefix ('%v')", prefix, TestLogPrefix)
}
if err = l.SetPrefix(TestLogAltPrefix); err != nil {
t.Fatalf("error when setting prefix to %v: %v", TestLogAltPrefix, err.Error())
} else {
_ = l.SetPrefix(TestLogPrefix)
}
if err = l.DoDebug(false); err != nil {
t.Fatalf("error when changing debug to false: %v", err.Error())
} else if l.EnableDebug {
t.Fatalf("did not properly set Debug filter state")
} else {
_ = l.DoDebug(true)
}
if err = l.Shutdown(); err != nil {
t.Fatalf("Error when running Shutdown: %v", err.Error())
}
t.Logf("Logger %v passed all logging targets.", ltype)
}
// TestDefaultLogger tests GetLogger.
func TestDefaultLogger(t *testing.T) {
var l Logger
var tempfile *os.File
var tempfilePath string
var keepLog bool
var ltype string
var prefix string
var testPrefix string
var err error
if tempfile, err = os.CreateTemp("", ".LOGGINGTEST_*"); err != nil {
t.Fatalf("error when creating temporary log file '%v': %v", tempfile.Name(), err.Error())
}
tempfilePath = tempfile.Name()
// We can close the handler immediately; we don't need it since the FileLogger opens its own.
if err = tempfile.Close(); err != nil {
t.Fatalf("error when closing handler for temporary log file '%v': %v", tempfile.Name(), err.Error())
}
if l, err = GetLogger(true, TestLogPrefix, logFlags, tempfilePath); err != nil {
t.Fatalf("error when spawning default Linux logger via GetLogger: %v", err.Error())
}
ltype = fmt.Sprintf("%T", l)
t.Logf("Logger %v passed Setup. Logger: %#v", ltype, l)
if err = l.Alert(testAlert, ltype); err != nil {
t.Fatalf("error for Alert: %v", err.Error())
}
if err = l.Crit(testCrit, ltype); err != nil {
t.Fatalf("error for Crit: %v", err.Error())
}
if err = l.Debug(testDebug, ltype); err != nil {
t.Fatalf("error for Debug: %v", err.Error())
}
if err = l.Emerg(testEmerg, ltype); err != nil {
t.Fatalf("error for Emerg: %v", err.Error())
}
if err = l.Err(testErr, ltype); err != nil {
t.Fatalf("error for Err: %v", err.Error())
}
if err = l.Info(testInfo, ltype); err != nil {
t.Fatalf("error for Alert: %v", err.Error())
}
if err = l.Notice(testNotice, ltype); err != nil {
t.Fatalf("error for Notice: %v", err.Error())
}
if err = l.Warning(testWarning, ltype); err != nil {
t.Fatalf("error for Warning: %v", err.Error())
}
if prefix, err = l.GetPrefix(); err != nil {
t.Fatalf("error when fetching prefix: %v", err.Error())
}
if ltype == "StdLogger" || ltype == "FileLogger" { // StdLogger (and thus FileLogger) adds a space at the end.
testPrefix = TestLogPrefix + " "
} else {
testPrefix = TestLogPrefix
}
if prefix != testPrefix {
t.Fatalf("true prefix ('%v') does not match TestLogPrefix ('%v')", prefix, TestLogPrefix)
}
if err = l.SetPrefix(TestLogAltPrefix); err != nil {
t.Fatalf("error when setting prefix to %v: %v", TestLogAltPrefix, err.Error())
} else {
_ = l.SetPrefix(TestLogPrefix)
}
if err = l.DoDebug(false); err != nil {
t.Fatalf("error when changing debug to false: %v", err.Error())
} else {
_ = l.DoDebug(true)
}
if err = l.Shutdown(); err != nil {
t.Fatalf("Error when running Shutdown: %v", err.Error())
}
_, keepLog = os.LookupEnv(EnvVarKeepLog)
if !keepLog {
if err = os.Remove(tempfilePath); err != nil {
t.Fatalf("error when removing temporary log file '%v': %v", tempfilePath, err.Error())
}
}
t.Logf("Logger %v passed all logging targets.", ltype)
}