github.com/atc0005/elbow@v0.8.8/internal/logging/logging_test.go (about) 1 // Copyright 2020 Adam Chalkley 2 // 3 // https://github.com/atc0005/elbow 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // https://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package logging 18 19 import ( 20 // "io" 21 "fmt" 22 "testing" 23 24 "github.com/sirupsen/logrus" 25 ) 26 27 func TestLogBufferFlushNilLoggerShouldFail(t *testing.T) { 28 29 var nilLogger *logrus.Logger 30 31 var logBuffer LogBuffer 32 33 if err := logBuffer.Flush(nilLogger); err == nil { 34 t.Error("passed nil *logrus.Logger without error") 35 } else { 36 t.Log("received error as expected:", err) 37 } 38 } 39 40 func TestLogBufferFlushShouldSucceed(t *testing.T) { 41 42 var testLogBuffer LogBuffer 43 44 logger := logrus.New() 45 // Configure logger to throw everything away 46 // logger.SetOutput(io.Discard) 47 logger.SetLevel(logrus.TraceLevel) 48 49 type test struct { 50 entryLevel logrus.Level 51 // potentially used for dealing with PanicLevel and FatalLevel? 52 // result error 53 } 54 55 tests := []test{ 56 // TODO: Need to add coverage for messages at these log levels: 57 // 58 // {entryLevel: logrus.PanicLevel, result: nil}, 59 // {entryLevel: logrus.FatalLevel, result: nil}, 60 // 61 // Problem: Flushing either of these types results in that immediate 62 // action; FatalLevel forces an exit, PanicLevel forces a panic. 63 64 {entryLevel: logrus.ErrorLevel}, 65 {entryLevel: logrus.WarnLevel}, 66 {entryLevel: logrus.InfoLevel}, 67 {entryLevel: logrus.DebugLevel}, 68 {entryLevel: logrus.TraceLevel}, 69 } 70 71 // Create test log buffer entries 72 for _, v := range tests { 73 74 testLogBuffer.Add(LogRecord{ 75 Level: v.entryLevel, 76 Message: fmt.Sprintf("This is a message at level %v.", v.entryLevel), 77 }) 78 } 79 80 // Verify that the number of entries matches up with the same number of 81 // active test entries 82 if len(testLogBuffer) != len(tests) { 83 t.Errorf("Expected %d log buffer entries, Got %d", 84 len(testLogBuffer), len(tests)) 85 } else { 86 t.Log("Number of log buffer entries matches test entries") 87 } 88 89 if err := testLogBuffer.Flush(logger); err != nil { 90 t.Error("Failed to flush log entries:", err) 91 } else { 92 t.Log("Flushed log buffer entry as expected") 93 } 94 } 95 96 func TestGetLineNumber(t *testing.T) { 97 got := GetLineNumber() 98 if got < 1 { 99 t.Errorf("Line number is incorrect, got: %d, want: greater than 0.", got) 100 } 101 102 } 103 104 func TestSetLoggerLevelShouldFail(t *testing.T) { 105 106 logger := logrus.New() 107 108 give := fakeValue 109 got := SetLoggerLevel(logger, give) 110 if got == nil { 111 t.Error("Expected error for", give, "Got", got) 112 } else { 113 t.Logf("Got error as expected for %v: %v", give, got) 114 } 115 116 } 117 118 // Pass in a valid logLevel string, call logger.GetLevel() 119 // and compare against the expected value 120 func TestSetLoggerLevelShouldSucceed(t *testing.T) { 121 122 type test struct { 123 logLevel string 124 loggerLevel logrus.Level 125 } 126 127 // TODO: Evaluate replacing bare strings with constants (see constants.go) 128 tests := []test{ 129 {logLevel: LogLevelEmergency, loggerLevel: logrus.PanicLevel}, 130 {logLevel: LogLevelPanic, loggerLevel: logrus.PanicLevel}, 131 {logLevel: LogLevelAlert, loggerLevel: logrus.FatalLevel}, 132 {logLevel: LogLevelCritical, loggerLevel: logrus.FatalLevel}, 133 {logLevel: LogLevelFatal, loggerLevel: logrus.FatalLevel}, 134 {logLevel: LogLevelError, loggerLevel: logrus.ErrorLevel}, 135 {logLevel: LogLevelWarn, loggerLevel: logrus.WarnLevel}, 136 {logLevel: LogLevelNotice, loggerLevel: logrus.WarnLevel}, 137 {logLevel: LogLevelInfo, loggerLevel: logrus.InfoLevel}, 138 {logLevel: LogLevelDebug, loggerLevel: logrus.DebugLevel}, 139 {logLevel: LogLevelTrace, loggerLevel: logrus.TraceLevel}, 140 } 141 142 logger := logrus.New() 143 144 for _, v := range tests { 145 give := v.logLevel 146 if err := SetLoggerLevel(logger, give); err != nil { 147 t.Error("Error when calling SetLoggerLevel(): ", err) 148 } else { 149 t.Log("No error when calling SetLoggerLevel()") 150 } 151 want := v.loggerLevel 152 got := logger.GetLevel() 153 154 if got != v.loggerLevel { 155 t.Error("Expected", want, "Got", got) 156 t.FailNow() 157 } else { 158 t.Log("Got", got, "as expected for requested level of", give) 159 } 160 } 161 162 } 163 164 func TestSetLoggerFormatterShouldFail(t *testing.T) { 165 166 logger := logrus.New() 167 168 give := fakeValue 169 got := SetLoggerFormatter(logger, give) 170 if got == nil { 171 t.Error("Expected error for", give, "Got", got) 172 } else { 173 t.Logf("Got error as expected for %v: %v", give, got) 174 } 175 } 176 177 func TestSetLoggerFormatterShouldSucceed(t *testing.T) { 178 179 type test struct { 180 format string 181 result error 182 } 183 184 logger := logrus.New() 185 186 tests := []test{ 187 {format: LogFormatText, result: nil}, 188 {format: LogFormatJSON, result: nil}, 189 } 190 191 for _, give := range tests { 192 got := SetLoggerFormatter(logger, give.format) 193 if got != give.result { 194 t.Error("Expected", give.result, "Got", got) 195 } 196 } 197 198 } 199 200 func TestSetLoggerConsoleOutputShouldFail(t *testing.T) { 201 202 logger := logrus.New() 203 204 give := fakeValue 205 got := SetLoggerConsoleOutput(logger, give) 206 if got == nil { 207 t.Error("Expected error for", give, "Got", got) 208 } else { 209 t.Logf("Got error as expected for %v: %v", give, got) 210 } 211 } 212 213 func TestSetLoggerConsoleOutputShouldSucceed(t *testing.T) { 214 215 type test struct { 216 consoleOutput string 217 result error 218 } 219 220 logger := logrus.New() 221 222 // TODO: Evaluate replacing bare strings with constants (see constants.go) 223 tests := []test{ 224 {consoleOutput: ConsoleOutputStdout, result: nil}, 225 {consoleOutput: ConsoleOutputStderr, result: nil}, 226 } 227 228 for _, give := range tests { 229 got := SetLoggerConsoleOutput(logger, give.consoleOutput) 230 if got != give.result { 231 t.Error("Expected", give.result, "Got", got) 232 } 233 } 234 235 } 236 237 func TestEnableSyslogLogging(t *testing.T) { 238 // TODO: Need to implement this 239 240 t.Log("TODO: Need to implement this test.") 241 }