Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 17 | package common_test |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 18 | |
| 19 | import ( |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 20 | "github.com/opencord/bbsim/internal/common" |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 21 | "github.com/sirupsen/logrus" |
| 22 | "gotest.tools/assert" |
| 23 | "testing" |
| 24 | ) |
| 25 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 26 | func init() { |
| 27 | common.SetLogLevel(logrus.StandardLogger(), "error", false) |
| 28 | } |
| 29 | |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 30 | func Test_SetLogLevel(t *testing.T) { |
| 31 | log := logrus.New() |
| 32 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 33 | common.SetLogLevel(log, "trace", false) |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 34 | assert.Equal(t, log.Level, logrus.TraceLevel) |
| 35 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 36 | common.SetLogLevel(log, "debug", false) |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 37 | assert.Equal(t, log.Level, logrus.DebugLevel) |
| 38 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 39 | common.SetLogLevel(log, "info", false) |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 40 | assert.Equal(t, log.Level, logrus.InfoLevel) |
| 41 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 42 | common.SetLogLevel(log, "warn", false) |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 43 | assert.Equal(t, log.Level, logrus.WarnLevel) |
| 44 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 45 | common.SetLogLevel(log, "error", false) |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 46 | assert.Equal(t, log.Level, logrus.ErrorLevel) |
| 47 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 48 | common.SetLogLevel(log, "foobar", false) |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 49 | assert.Equal(t, log.Level, logrus.DebugLevel) |
| 50 | } |
| 51 | |
| 52 | func Test_SetLogLevelCaller(t *testing.T) { |
| 53 | log := logrus.New() |
| 54 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 55 | common.SetLogLevel(log, "debug", true) |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 56 | assert.Equal(t, log.ReportCaller, true) |
| 57 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 58 | common.SetLogLevel(log, "debug", false) |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 59 | assert.Equal(t, log.ReportCaller, false) |
| 60 | } |