github.com/nats-io/nats-server/v2@v2.11.0-preview.2/logger/syslog_test.go (about) 1 // Copyright 2012-2019 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 "fmt" 21 "log" 22 "net" 23 "os" 24 "path/filepath" 25 "strings" 26 "testing" 27 "time" 28 ) 29 30 var serverFQN string 31 32 func TestSysLogger(t *testing.T) { 33 logger := NewSysLogger(false, false) 34 35 if logger.debug { 36 t.Fatalf("Expected %t, received %t\n", false, logger.debug) 37 } 38 39 if logger.trace { 40 t.Fatalf("Expected %t, received %t\n", false, logger.trace) 41 } 42 } 43 44 func TestSysLoggerWithDebugAndTrace(t *testing.T) { 45 logger := NewSysLogger(true, true) 46 47 if !logger.debug { 48 t.Fatalf("Expected %t, received %t\n", true, logger.debug) 49 } 50 51 if !logger.trace { 52 t.Fatalf("Expected %t, received %t\n", true, logger.trace) 53 } 54 } 55 56 func testTag(t *testing.T, exePath, expected string) { 57 os.Args[0] = exePath 58 if result := GetSysLoggerTag(); result != expected { 59 t.Fatalf("Expected %s, received %s", expected, result) 60 } 61 } 62 63 func restoreArg(orig string) { 64 os.Args[0] = orig 65 } 66 67 func TestSysLoggerTagGen(t *testing.T) { 68 origArg := os.Args[0] 69 defer restoreArg(origArg) 70 71 testTag(t, "nats-server", "nats-server") 72 testTag(t, filepath.Join(".", "nats-server"), "nats-server") 73 testTag(t, filepath.Join("home", "bin", "nats-server"), "nats-server") 74 testTag(t, filepath.Join("..", "..", "nats-server"), "nats-server") 75 testTag(t, "nats-server.service1", "nats-server.service1") 76 testTag(t, "nats-server_service1", "nats-server_service1") 77 testTag(t, "nats-server-service1", "nats-server-service1") 78 testTag(t, "nats-server service1", "nats-server service1") 79 } 80 81 func TestSysLoggerTag(t *testing.T) { 82 origArg := os.Args[0] 83 defer restoreArg(origArg) 84 85 os.Args[0] = "ServerLoggerTag" 86 87 done := make(chan string) 88 startServer(done) 89 logger := NewRemoteSysLogger(serverFQN, true, true) 90 logger.Noticef("foo") 91 92 line := <-done 93 data := strings.Split(line, "[") 94 if len(data) != 2 { 95 t.Fatalf("Unexpected syslog line %s\n", line) 96 } 97 98 if !strings.Contains(data[0], os.Args[0]) { 99 t.Fatalf("Expected '%s', received '%s'\n", os.Args[0], data[0]) 100 } 101 } 102 103 func TestRemoteSysLogger(t *testing.T) { 104 done := make(chan string) 105 startServer(done) 106 logger := NewRemoteSysLogger(serverFQN, true, true) 107 108 if !logger.debug { 109 t.Fatalf("Expected %t, received %t\n", true, logger.debug) 110 } 111 112 if !logger.trace { 113 t.Fatalf("Expected %t, received %t\n", true, logger.trace) 114 } 115 logger.Noticef("foo") 116 <-done 117 } 118 119 func TestRemoteSysLoggerNotice(t *testing.T) { 120 done := make(chan string) 121 startServer(done) 122 logger := NewRemoteSysLogger(serverFQN, true, true) 123 124 logger.Noticef("foo %s", "bar") 125 expectSyslogOutput(t, <-done, "foo bar\n") 126 } 127 128 func TestRemoteSysLoggerDebug(t *testing.T) { 129 done := make(chan string) 130 startServer(done) 131 logger := NewRemoteSysLogger(serverFQN, true, true) 132 133 logger.Debugf("foo %s", "qux") 134 expectSyslogOutput(t, <-done, "foo qux\n") 135 } 136 137 func TestRemoteSysLoggerDebugDisabled(t *testing.T) { 138 done := make(chan string) 139 startServer(done) 140 logger := NewRemoteSysLogger(serverFQN, false, false) 141 142 logger.Debugf("foo %s", "qux") 143 rcvd := <-done 144 if rcvd != "" { 145 t.Fatalf("Unexpected syslog response %s\n", rcvd) 146 } 147 } 148 149 func TestRemoteSysLoggerTrace(t *testing.T) { 150 done := make(chan string) 151 startServer(done) 152 logger := NewRemoteSysLogger(serverFQN, true, true) 153 154 logger.Tracef("foo %s", "qux") 155 expectSyslogOutput(t, <-done, "foo qux\n") 156 } 157 158 func TestRemoteSysLoggerTraceDisabled(t *testing.T) { 159 done := make(chan string) 160 startServer(done) 161 logger := NewRemoteSysLogger(serverFQN, true, false) 162 163 logger.Tracef("foo %s", "qux") 164 rcvd := <-done 165 if rcvd != "" { 166 t.Fatalf("Unexpected syslog response %s\n", rcvd) 167 } 168 } 169 170 func TestGetNetworkAndAddrUDP(t *testing.T) { 171 n, a := getNetworkAndAddr("udp://foo.com:1000") 172 173 if n != "udp" { 174 t.Fatalf("Unexpected network %s\n", n) 175 } 176 177 if a != "foo.com:1000" { 178 t.Fatalf("Unexpected addr %s\n", a) 179 } 180 } 181 182 func TestGetNetworkAndAddrTCP(t *testing.T) { 183 n, a := getNetworkAndAddr("tcp://foo.com:1000") 184 185 if n != "tcp" { 186 t.Fatalf("Unexpected network %s\n", n) 187 } 188 189 if a != "foo.com:1000" { 190 t.Fatalf("Unexpected addr %s\n", a) 191 } 192 } 193 194 func TestGetNetworkAndAddrUnix(t *testing.T) { 195 n, a := getNetworkAndAddr("unix:///foo.sock") 196 197 if n != "unix" { 198 t.Fatalf("Unexpected network %s\n", n) 199 } 200 201 if a != "/foo.sock" { 202 t.Fatalf("Unexpected addr %s\n", a) 203 } 204 } 205 func expectSyslogOutput(t *testing.T, line string, expected string) { 206 data := strings.Split(line, "]: ") 207 if len(data) != 2 { 208 t.Fatalf("Unexpected syslog line %s\n", line) 209 } 210 211 if data[1] != expected { 212 t.Fatalf("Expected '%s', received '%s'\n", expected, data[1]) 213 } 214 } 215 216 func runSyslog(c net.PacketConn, done chan<- string) { 217 var buf [4096]byte 218 var rcvd string 219 for { 220 n, _, err := c.ReadFrom(buf[:]) 221 if err != nil || n == 0 { 222 break 223 } 224 rcvd += string(buf[:n]) 225 } 226 done <- rcvd 227 } 228 229 func startServer(done chan<- string) { 230 c, e := net.ListenPacket("udp", "127.0.0.1:0") 231 if e != nil { 232 log.Fatalf("net.ListenPacket failed udp :0 %v", e) 233 } 234 235 serverFQN = fmt.Sprintf("udp://%s", c.LocalAddr().String()) 236 c.SetReadDeadline(time.Now().Add(100 * time.Millisecond)) 237 go runSyslog(c, done) 238 }