206 lines
5.4 KiB
Go
206 lines
5.4 KiB
Go
package logging
|
|
|
|
import (
|
|
`fmt`
|
|
`os`
|
|
`testing`
|
|
)
|
|
|
|
/*
|
|
TestWinLogger tests functionality for WinLogger.
|
|
You will probably need to run it with an Administrator shell.
|
|
*/
|
|
func TestWinLogger(t *testing.T) {
|
|
|
|
var l *WinLogger
|
|
var ltype string = "WinLogger"
|
|
var prefix string
|
|
var exists bool
|
|
var err error
|
|
|
|
l = &WinLogger{
|
|
EnableDebug: true,
|
|
Prefix: TestLogPrefix,
|
|
RemoveOnClose: true,
|
|
EIDs: DefaultEventID,
|
|
}
|
|
|
|
if exists, err = l.Exists(); err != nil {
|
|
t.Fatalf("error when checking for existence of registered Event Log source '%v': %v", TestLogPrefix, err.Error())
|
|
} else {
|
|
t.Logf("Prefix (source) '%v' exists before setup: %v", TestLogPrefix, exists)
|
|
}
|
|
|
|
if err = l.Setup(); err != nil {
|
|
t.Fatalf("error when running Setup: %v", err.Error())
|
|
}
|
|
|
|
if exists, err = l.Exists(); err != nil {
|
|
t.Fatalf("error when checking for existence of registered Event Log source '%v': %v", TestLogPrefix, err.Error())
|
|
} else {
|
|
t.Logf("Prefix (source) '%v' exists after setup: %v", TestLogPrefix, exists)
|
|
}
|
|
|
|
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, DefaultEventID, logFlags, tempfilePath); err != nil {
|
|
t.Fatalf("error when spawning default Windows 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)
|
|
}
|