forked from nats-io/nats-server
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Colin Sullivan
committed
Dec 29, 2016
1 parent
bc5f864
commit 1df5af0
Showing
4 changed files
with
186 additions
and
11 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,126 @@ | ||
// Copyright 2016 Apcera Inc. All rights reserved. | ||
// +build windows | ||
|
||
package logger | ||
|
||
import ( | ||
"golang.org/x/sys/windows/svc/eventlog" | ||
"os/exec" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
// Skips testing if we do not have privledges to run this test. | ||
// This lets us skip the tests for general (non admin/system) users. | ||
func checkPrivledges(t *testing.T) { | ||
src := "NATS-eventlog-testsource" | ||
defer eventlog.Remove(src) | ||
if err := eventlog.InstallAsEventCreate(src, eventlog.Info|eventlog.Error|eventlog.Warning); err != nil { | ||
if strings.Contains(err.Error(), "Access is denied") { | ||
t.Skip("skipping: elevated privledges are required.") | ||
} | ||
// let the tests report other types of errors | ||
} | ||
} | ||
|
||
// lastLogEntryContains reads the last entry (/c:1 /rd:true) written | ||
// to the event log by the NATS-Server source, returning true if the | ||
// passed text was found, false otherwise. | ||
func lastLogEntryContains(t *testing.T, text string) bool { | ||
var output []byte | ||
var err error | ||
|
||
cmd := exec.Command("wevtutil.exe", "qe", "Application", "/q:*[System[Provider[@Name='NATS-Server']]]", | ||
"/rd:true", "/c:1") | ||
if output, err = cmd.Output(); err != nil { | ||
t.Fatalf("Unable to execute command: %v", err) | ||
} | ||
return strings.Contains(string(output), text) | ||
} | ||
|
||
// TestSysLogger tests event logging on windows | ||
func TestSysLogger(t *testing.T) { | ||
checkPrivledges(t) | ||
logger := NewSysLogger(false, false) | ||
if logger.debug { | ||
t.Fatalf("Expected %t, received %t\n", false, logger.debug) | ||
} | ||
|
||
if logger.trace { | ||
t.Fatalf("Expected %t, received %t\n", false, logger.trace) | ||
} | ||
logger.Noticef("%s", "Noticef") | ||
if !lastLogEntryContains(t, "[NOTICE]: Noticef") { | ||
t.Fatalf("missing log entry") | ||
} | ||
|
||
logger.Errorf("%s", "Errorf") | ||
if !lastLogEntryContains(t, "[ERROR]: Errorf") { | ||
t.Fatalf("missing log entry") | ||
} | ||
|
||
logger.Tracef("%s", "Tracef") | ||
if lastLogEntryContains(t, "Tracef") { | ||
t.Fatalf("should not contain log entry") | ||
} | ||
|
||
logger.Debugf("%s", "Debugf") | ||
if lastLogEntryContains(t, "Debugf") { | ||
t.Fatalf("should not contain log entry") | ||
} | ||
} | ||
|
||
// TestSysLoggerWithDebugAndTrace tests event logging | ||
func TestSysLoggerWithDebugAndTrace(t *testing.T) { | ||
checkPrivledges(t) | ||
logger := NewSysLogger(true, true) | ||
if !logger.debug { | ||
t.Fatalf("Expected %t, received %t\n", true, logger.debug) | ||
} | ||
|
||
if !logger.trace { | ||
t.Fatalf("Expected %t, received %t\n", true, logger.trace) | ||
} | ||
|
||
logger.Tracef("%s", "Tracef") | ||
if !lastLogEntryContains(t, "[TRACE]: Tracef") { | ||
t.Fatalf("missing log entry") | ||
} | ||
|
||
logger.Debugf("%s", "Debugf") | ||
if !lastLogEntryContains(t, "[DEBUG]: Debugf") { | ||
t.Fatalf("missing log entry") | ||
} | ||
} | ||
|
||
// TestSysLoggerWithDebugAndTrace tests remote event logging | ||
func TestRemoteSysLoggerWithDebugAndTrace(t *testing.T) { | ||
checkPrivledges(t) | ||
logger := NewRemoteSysLogger("127.0.0.1", true, true) | ||
if !logger.debug { | ||
t.Fatalf("Expected %t, received %t\n", true, logger.debug) | ||
} | ||
|
||
if !logger.trace { | ||
t.Fatalf("Expected %t, received %t\n", true, logger.trace) | ||
} | ||
logger.Tracef("NATS %s", "[TRACE]: Remote Noticef") | ||
if !lastLogEntryContains(t, "Remote Noticef") { | ||
t.Fatalf("missing log entry") | ||
} | ||
} | ||
|
||
func TestSysLoggerFatalf(t *testing.T) { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
if !lastLogEntryContains(t, "[FATAL]: Fatalf") { | ||
t.Fatalf("missing log entry") | ||
} | ||
} | ||
}() | ||
|
||
checkPrivledges(t) | ||
logger := NewSysLogger(true, true) | ||
logger.Fatalf("%s", "Fatalf") | ||
t.Fatalf("did not panic when expected to") | ||
} |
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