github.com/nats-io/nats-server/v2@v2.11.0-preview.2/logger/syslog_windows_test.go (about) 1 // Copyright 2012-2018 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 // +build windows 16 17 package logger 18 19 import ( 20 "os/exec" 21 "strings" 22 "testing" 23 24 "golang.org/x/sys/windows/svc/eventlog" 25 ) 26 27 // Skips testing if we do not have privledges to run this test. 28 // This lets us skip the tests for general (non admin/system) users. 29 func checkPrivledges(t *testing.T) { 30 src := "NATS-eventlog-testsource" 31 defer eventlog.Remove(src) 32 if err := eventlog.InstallAsEventCreate(src, eventlog.Info|eventlog.Error|eventlog.Warning); err != nil { 33 if strings.Contains(err.Error(), "Access is denied") { 34 // Skip this test because elevated privileges are required. 35 t.SkipNow() 36 } 37 // let the tests report other types of errors 38 } 39 } 40 41 // lastLogEntryContains reads the last entry (/c:1 /rd:true) written 42 // to the event log by the NATS-Server source, returning true if the 43 // passed text was found, false otherwise. 44 func lastLogEntryContains(t *testing.T, text string) bool { 45 var output []byte 46 var err error 47 48 cmd := exec.Command("wevtutil.exe", "qe", "Application", "/q:*[System[Provider[@Name='NATS-Server']]]", 49 "/rd:true", "/c:1") 50 if output, err = cmd.Output(); err != nil { 51 t.Fatalf("Unable to execute command: %v", err) 52 } 53 return strings.Contains(string(output), text) 54 } 55 56 // TestSysLogger tests event logging on windows 57 func TestSysLogger(t *testing.T) { 58 checkPrivledges(t) 59 logger := NewSysLogger(false, false) 60 if logger.debug { 61 t.Fatalf("Expected %t, received %t\n", false, logger.debug) 62 } 63 64 if logger.trace { 65 t.Fatalf("Expected %t, received %t\n", false, logger.trace) 66 } 67 logger.Noticef("%s", "Noticef") 68 if !lastLogEntryContains(t, "[NOTICE]: Noticef") { 69 t.Fatalf("missing log entry") 70 } 71 72 logger.Errorf("%s", "Errorf") 73 if !lastLogEntryContains(t, "[ERROR]: Errorf") { 74 t.Fatalf("missing log entry") 75 } 76 77 logger.Tracef("%s", "Tracef") 78 if lastLogEntryContains(t, "Tracef") { 79 t.Fatalf("should not contain log entry") 80 } 81 82 logger.Debugf("%s", "Debugf") 83 if lastLogEntryContains(t, "Debugf") { 84 t.Fatalf("should not contain log entry") 85 } 86 } 87 88 // TestSysLoggerWithDebugAndTrace tests event logging 89 func TestSysLoggerWithDebugAndTrace(t *testing.T) { 90 checkPrivledges(t) 91 logger := NewSysLogger(true, true) 92 if !logger.debug { 93 t.Fatalf("Expected %t, received %t\n", true, logger.debug) 94 } 95 96 if !logger.trace { 97 t.Fatalf("Expected %t, received %t\n", true, logger.trace) 98 } 99 100 logger.Tracef("%s", "Tracef") 101 if !lastLogEntryContains(t, "[TRACE]: Tracef") { 102 t.Fatalf("missing log entry") 103 } 104 105 logger.Debugf("%s", "Debugf") 106 if !lastLogEntryContains(t, "[DEBUG]: Debugf") { 107 t.Fatalf("missing log entry") 108 } 109 } 110 111 // TestRemoteSysLoggerWithDebugAndTrace tests remote event logging 112 func TestRemoteSysLoggerWithDebugAndTrace(t *testing.T) { 113 checkPrivledges(t) 114 logger := NewRemoteSysLogger("", true, true) 115 if !logger.debug { 116 t.Fatalf("Expected %t, received %t\n", true, logger.debug) 117 } 118 119 if !logger.trace { 120 t.Fatalf("Expected %t, received %t\n", true, logger.trace) 121 } 122 logger.Tracef("NATS %s", "[TRACE]: Remote Noticef") 123 if !lastLogEntryContains(t, "Remote Noticef") { 124 t.Fatalf("missing log entry") 125 } 126 } 127 128 func TestSysLoggerFatalf(t *testing.T) { 129 defer func() { 130 if r := recover(); r != nil { 131 if !lastLogEntryContains(t, "[FATAL]: Fatalf") { 132 t.Fatalf("missing log entry") 133 } 134 } 135 }() 136 137 checkPrivledges(t) 138 logger := NewSysLogger(true, true) 139 logger.Fatalf("%s", "Fatalf") 140 t.Fatalf("did not panic when expected to") 141 }