istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/log/options_test.go (about) 1 // Copyright 2017 Istio Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package log 16 17 import ( 18 "reflect" 19 "strconv" 20 "strings" 21 "testing" 22 23 "github.com/spf13/cobra" 24 ) 25 26 func TestOpts(t *testing.T) { 27 resetGlobals() 28 29 cases := []struct { 30 cmdLine string 31 result Options 32 }{ 33 {"--log_as_json", Options{ 34 OutputPaths: []string{defaultOutputPath}, 35 ErrorOutputPaths: []string{defaultErrorOutputPath}, 36 defaultOutputLevels: "default:info,grpc:none", 37 stackTraceLevels: DefaultScopeName + ":" + levelToString[defaultStackTraceLevel], 38 JSONEncoding: true, 39 RotationMaxAge: defaultRotationMaxAge, 40 RotationMaxSize: defaultRotationMaxSize, 41 RotationMaxBackups: defaultRotationMaxBackups, 42 }}, 43 44 {"--log_target stdout --log_target stderr", Options{ 45 OutputPaths: []string{"stdout", "stderr"}, 46 ErrorOutputPaths: []string{defaultErrorOutputPath}, 47 defaultOutputLevels: "default:info,grpc:none", 48 stackTraceLevels: DefaultScopeName + ":" + levelToString[defaultStackTraceLevel], 49 RotationMaxAge: defaultRotationMaxAge, 50 RotationMaxSize: defaultRotationMaxSize, 51 RotationMaxBackups: defaultRotationMaxBackups, 52 }}, 53 54 {"--log_caller default", Options{ 55 OutputPaths: []string{defaultOutputPath}, 56 ErrorOutputPaths: []string{defaultErrorOutputPath}, 57 defaultOutputLevels: "default:info,grpc:none", 58 stackTraceLevels: DefaultScopeName + ":" + levelToString[defaultStackTraceLevel], 59 logCallers: DefaultScopeName, 60 RotationMaxAge: defaultRotationMaxAge, 61 RotationMaxSize: defaultRotationMaxSize, 62 RotationMaxBackups: defaultRotationMaxBackups, 63 }}, 64 65 {"--log_stacktrace_level debug", Options{ 66 OutputPaths: []string{defaultOutputPath}, 67 ErrorOutputPaths: []string{defaultErrorOutputPath}, 68 defaultOutputLevels: "default:info,grpc:none", 69 stackTraceLevels: levelToString[DebugLevel], 70 RotationMaxAge: defaultRotationMaxAge, 71 RotationMaxSize: defaultRotationMaxSize, 72 RotationMaxBackups: defaultRotationMaxBackups, 73 }}, 74 75 {"--log_stacktrace_level default:debug", Options{ 76 OutputPaths: []string{defaultOutputPath}, 77 ErrorOutputPaths: []string{defaultErrorOutputPath}, 78 defaultOutputLevels: "default:info,grpc:none", 79 stackTraceLevels: DefaultScopeName + ":" + levelToString[DebugLevel], 80 RotationMaxAge: defaultRotationMaxAge, 81 RotationMaxSize: defaultRotationMaxSize, 82 RotationMaxBackups: defaultRotationMaxBackups, 83 }}, 84 85 {"--log_stacktrace_level info", Options{ 86 OutputPaths: []string{defaultOutputPath}, 87 ErrorOutputPaths: []string{defaultErrorOutputPath}, 88 defaultOutputLevels: "default:info,grpc:none", 89 stackTraceLevels: levelToString[InfoLevel], 90 RotationMaxAge: defaultRotationMaxAge, 91 RotationMaxSize: defaultRotationMaxSize, 92 RotationMaxBackups: defaultRotationMaxBackups, 93 }}, 94 95 {"--log_stacktrace_level default:info", Options{ 96 OutputPaths: []string{defaultOutputPath}, 97 ErrorOutputPaths: []string{defaultErrorOutputPath}, 98 defaultOutputLevels: "default:info,grpc:none", 99 stackTraceLevels: DefaultScopeName + ":" + levelToString[InfoLevel], 100 RotationMaxAge: defaultRotationMaxAge, 101 RotationMaxSize: defaultRotationMaxSize, 102 RotationMaxBackups: defaultRotationMaxBackups, 103 }}, 104 105 {"--log_stacktrace_level warn", Options{ 106 OutputPaths: []string{defaultOutputPath}, 107 ErrorOutputPaths: []string{defaultErrorOutputPath}, 108 defaultOutputLevels: "default:info,grpc:none", 109 stackTraceLevels: levelToString[WarnLevel], 110 RotationMaxAge: defaultRotationMaxAge, 111 RotationMaxSize: defaultRotationMaxSize, 112 RotationMaxBackups: defaultRotationMaxBackups, 113 }}, 114 115 {"--log_stacktrace_level default:warn", Options{ 116 OutputPaths: []string{defaultOutputPath}, 117 ErrorOutputPaths: []string{defaultErrorOutputPath}, 118 defaultOutputLevels: "default:info,grpc:none", 119 stackTraceLevels: DefaultScopeName + ":" + levelToString[WarnLevel], 120 RotationMaxAge: defaultRotationMaxAge, 121 RotationMaxSize: defaultRotationMaxSize, 122 RotationMaxBackups: defaultRotationMaxBackups, 123 }}, 124 125 {"--log_stacktrace_level error", Options{ 126 OutputPaths: []string{defaultOutputPath}, 127 ErrorOutputPaths: []string{defaultErrorOutputPath}, 128 defaultOutputLevels: "default:info,grpc:none", 129 stackTraceLevels: levelToString[ErrorLevel], 130 RotationMaxAge: defaultRotationMaxAge, 131 RotationMaxSize: defaultRotationMaxSize, 132 RotationMaxBackups: defaultRotationMaxBackups, 133 }}, 134 135 {"--log_stacktrace_level default:error", Options{ 136 OutputPaths: []string{defaultOutputPath}, 137 ErrorOutputPaths: []string{defaultErrorOutputPath}, 138 defaultOutputLevels: "default:info,grpc:none", 139 stackTraceLevels: DefaultScopeName + ":" + levelToString[ErrorLevel], 140 RotationMaxAge: defaultRotationMaxAge, 141 RotationMaxSize: defaultRotationMaxSize, 142 RotationMaxBackups: defaultRotationMaxBackups, 143 }}, 144 145 {"--log_stacktrace_level none", Options{ 146 OutputPaths: []string{defaultOutputPath}, 147 ErrorOutputPaths: []string{defaultErrorOutputPath}, 148 defaultOutputLevels: "default:info,grpc:none", 149 stackTraceLevels: levelToString[NoneLevel], 150 RotationMaxAge: defaultRotationMaxAge, 151 RotationMaxSize: defaultRotationMaxSize, 152 RotationMaxBackups: defaultRotationMaxBackups, 153 }}, 154 155 {"--log_stacktrace_level default:none", Options{ 156 OutputPaths: []string{defaultOutputPath}, 157 ErrorOutputPaths: []string{defaultErrorOutputPath}, 158 defaultOutputLevels: "default:info,grpc:none", 159 stackTraceLevels: "default:none", 160 RotationMaxAge: defaultRotationMaxAge, 161 RotationMaxSize: defaultRotationMaxSize, 162 RotationMaxBackups: defaultRotationMaxBackups, 163 }}, 164 165 {"--log_output_level debug", Options{ 166 OutputPaths: []string{defaultOutputPath}, 167 ErrorOutputPaths: []string{defaultErrorOutputPath}, 168 defaultOutputLevels: "default:info,grpc:none", 169 outputLevels: levelToString[DebugLevel], 170 stackTraceLevels: DefaultScopeName + ":" + levelToString[defaultStackTraceLevel], 171 RotationMaxAge: defaultRotationMaxAge, 172 RotationMaxSize: defaultRotationMaxSize, 173 RotationMaxBackups: defaultRotationMaxBackups, 174 }}, 175 176 {"--log_output_level info", Options{ 177 OutputPaths: []string{defaultOutputPath}, 178 ErrorOutputPaths: []string{defaultErrorOutputPath}, 179 defaultOutputLevels: "default:info,grpc:none", 180 outputLevels: levelToString[InfoLevel], 181 stackTraceLevels: DefaultScopeName + ":" + levelToString[defaultStackTraceLevel], 182 RotationMaxAge: defaultRotationMaxAge, 183 RotationMaxSize: defaultRotationMaxSize, 184 RotationMaxBackups: defaultRotationMaxBackups, 185 }}, 186 187 {"--log_output_level warn", Options{ 188 OutputPaths: []string{defaultOutputPath}, 189 ErrorOutputPaths: []string{defaultErrorOutputPath}, 190 defaultOutputLevels: "default:info,grpc:none", 191 outputLevels: levelToString[WarnLevel], 192 stackTraceLevels: DefaultScopeName + ":" + levelToString[defaultStackTraceLevel], 193 RotationMaxAge: defaultRotationMaxAge, 194 RotationMaxSize: defaultRotationMaxSize, 195 RotationMaxBackups: defaultRotationMaxBackups, 196 }}, 197 198 {"--log_output_level error", Options{ 199 OutputPaths: []string{defaultOutputPath}, 200 ErrorOutputPaths: []string{defaultErrorOutputPath}, 201 defaultOutputLevels: "default:info,grpc:none", 202 outputLevels: levelToString[ErrorLevel], 203 stackTraceLevels: DefaultScopeName + ":" + levelToString[defaultStackTraceLevel], 204 RotationMaxAge: defaultRotationMaxAge, 205 RotationMaxSize: defaultRotationMaxSize, 206 RotationMaxBackups: defaultRotationMaxBackups, 207 }}, 208 209 {"--log_output_level none", Options{ 210 OutputPaths: []string{defaultOutputPath}, 211 ErrorOutputPaths: []string{defaultErrorOutputPath}, 212 defaultOutputLevels: "default:info,grpc:none", 213 outputLevels: levelToString[NoneLevel], 214 stackTraceLevels: DefaultScopeName + ":" + levelToString[defaultStackTraceLevel], 215 RotationMaxAge: defaultRotationMaxAge, 216 RotationMaxSize: defaultRotationMaxSize, 217 RotationMaxBackups: defaultRotationMaxBackups, 218 }}, 219 220 {"--log_rotate foobar", Options{ 221 OutputPaths: []string{defaultOutputPath}, 222 ErrorOutputPaths: []string{defaultErrorOutputPath}, 223 defaultOutputLevels: "default:info,grpc:none", 224 stackTraceLevels: DefaultScopeName + ":" + levelToString[defaultStackTraceLevel], 225 RotateOutputPath: "foobar", 226 RotationMaxAge: defaultRotationMaxAge, 227 RotationMaxSize: defaultRotationMaxSize, 228 RotationMaxBackups: defaultRotationMaxBackups, 229 }}, 230 231 {"--log_rotate_max_age 1234", Options{ 232 OutputPaths: []string{defaultOutputPath}, 233 ErrorOutputPaths: []string{defaultErrorOutputPath}, 234 defaultOutputLevels: "default:info,grpc:none", 235 stackTraceLevels: DefaultScopeName + ":" + levelToString[defaultStackTraceLevel], 236 RotationMaxAge: 1234, 237 RotationMaxSize: defaultRotationMaxSize, 238 RotationMaxBackups: defaultRotationMaxBackups, 239 }}, 240 241 {"--log_rotate_max_size 1234", Options{ 242 OutputPaths: []string{defaultOutputPath}, 243 ErrorOutputPaths: []string{defaultErrorOutputPath}, 244 defaultOutputLevels: "default:info,grpc:none", 245 stackTraceLevels: DefaultScopeName + ":" + levelToString[defaultStackTraceLevel], 246 RotationMaxAge: defaultRotationMaxAge, 247 RotationMaxSize: 1234, 248 RotationMaxBackups: defaultRotationMaxBackups, 249 }}, 250 251 {"--log_rotate_max_backups 1234", Options{ 252 OutputPaths: []string{defaultOutputPath}, 253 ErrorOutputPaths: []string{defaultErrorOutputPath}, 254 defaultOutputLevels: "default:info,grpc:none", 255 stackTraceLevels: DefaultScopeName + ":" + levelToString[defaultStackTraceLevel], 256 RotationMaxAge: defaultRotationMaxAge, 257 RotationMaxSize: defaultRotationMaxSize, 258 RotationMaxBackups: 1234, 259 }}, 260 } 261 262 for j := 0; j < 2; j++ { 263 for i, c := range cases { 264 t.Run(strconv.Itoa(j*100+i), func(t *testing.T) { 265 o := DefaultOptions() 266 cmd := &cobra.Command{} 267 o.AttachCobraFlags(cmd) 268 cmd.SetArgs(strings.Split(c.cmdLine, " ")) 269 270 if err := cmd.Execute(); err != nil { 271 t.Errorf("Got %v, expecting success", err) 272 } 273 274 if !reflect.DeepEqual(c.result, *o) { 275 t.Errorf("Got %v, expected %v", *o, c.result) 276 } 277 }) 278 } 279 280 _ = RegisterScope("foo", "bar") 281 } 282 } 283 284 func TestSetDefaultLevel(t *testing.T) { 285 resetGlobals() 286 287 _ = RegisterScope("TestSetLevel", "") 288 289 cases := []struct { 290 flagLevels string 291 scope string 292 defaultLevel Level 293 expectedLevel Level 294 }{ 295 {"debug", "default", DebugLevel, DebugLevel}, 296 {"default:debug", "default", DebugLevel, DebugLevel}, 297 {"info", "default", DebugLevel, InfoLevel}, 298 {"default:info", "default", DebugLevel, InfoLevel}, 299 {"warn", "default", DebugLevel, WarnLevel}, 300 {"default:warn", "default", DebugLevel, WarnLevel}, 301 {"error", "default", DebugLevel, ErrorLevel}, 302 {"default:error", "default", DebugLevel, ErrorLevel}, 303 {"none", "default", DebugLevel, NoneLevel}, 304 {"default:none", "default", DebugLevel, NoneLevel}, 305 306 {"debug", "default", ErrorLevel, DebugLevel}, 307 {"default:debug", "default", ErrorLevel, DebugLevel}, 308 {"info", "default", ErrorLevel, InfoLevel}, 309 {"default:info", "default", ErrorLevel, InfoLevel}, 310 {"warn", "default", ErrorLevel, WarnLevel}, 311 {"default:warn", "default", ErrorLevel, WarnLevel}, 312 {"error", "default", ErrorLevel, ErrorLevel}, 313 {"default:error", "default", ErrorLevel, ErrorLevel}, 314 {"none", "default", ErrorLevel, NoneLevel}, 315 {"default:none", "default", ErrorLevel, NoneLevel}, 316 } 317 318 for i, c := range cases { 319 t.Run(strconv.Itoa(i), func(t *testing.T) { 320 o := DefaultOptions() 321 o.outputLevels = c.flagLevels 322 o.stackTraceLevels = c.flagLevels 323 324 o.SetDefaultOutputLevel(c.scope, c.defaultLevel) 325 if err := Configure(o); err != nil { 326 t.Fatal(err) 327 } 328 if got := FindScope(c.scope).GetOutputLevel(); got != c.expectedLevel { 329 t.Fatalf("got %v want %v", got, c.expectedLevel) 330 } 331 }) 332 } 333 }