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