- Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathsyslog_windows_test.go
140 lines (120 loc) · 3.88 KB
/
syslog_windows_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2012-2024 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build windows
package logger
import (
"os/exec"
"strings"
"testing"
"golang.org/x/sys/windows/svc/eventlog"
)
// Skips testing if we do not have privledges to run this test.
// This lets us skip the tests for general (non admin/system) users.
funccheckPrivledges(t*testing.T) {
src:="NATS-eventlog-testsource"
defereventlog.Remove(src)
iferr:=eventlog.InstallAsEventCreate(src, eventlog.Info|eventlog.Error|eventlog.Warning); err!=nil {
ifstrings.Contains(err.Error(), "Access is denied") {
// Skip this test because elevated privileges are required.
t.SkipNow()
}
// 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.
funclastLogEntryContains(t*testing.T, textstring) bool {
varoutput []byte
varerrerror
cmd:=exec.Command("wevtutil.exe", "qe", "Application", "/q:*[System[Provider[@Name='NATS-Server']]]",
"/rd:true", "/c:1")
ifoutput, err=cmd.Output(); err!=nil {
t.Fatalf("Unable to execute command: %v", err)
}
returnstrings.Contains(string(output), text)
}
// TestSysLogger tests event logging on windows
funcTestSysLogger(t*testing.T) {
checkPrivledges(t)
logger:=NewSysLogger(false, false)
iflogger.debug {
t.Fatalf("Expected %t, received %t\n", false, logger.debug)
}
iflogger.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")
iflastLogEntryContains(t, "Tracef") {
t.Fatalf("should not contain log entry")
}
logger.Debugf("%s", "Debugf")
iflastLogEntryContains(t, "Debugf") {
t.Fatalf("should not contain log entry")
}
}
// TestSysLoggerWithDebugAndTrace tests event logging
funcTestSysLoggerWithDebugAndTrace(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")
}
}
// TestRemoteSysLoggerWithDebugAndTrace tests remote event logging
funcTestRemoteSysLoggerWithDebugAndTrace(t*testing.T) {
checkPrivledges(t)
logger:=NewRemoteSysLogger("", 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")
}
}
funcTestSysLoggerFatalf(t*testing.T) {
deferfunc() {
ifr:=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")
}