2 * Copyright (c) 2019 AT&T Intellectual Property.
3 * Copyright (c) 2018-2019 Nokia.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 * platform project (RICP).
28 "github.com/stretchr/testify/assert"
31 // getTestLogger returns a logger instance where
32 // the output is directed to a byte buffer instead
34 func getTestLogger(t *testing.T) (*MdcLogger, *bytes.Buffer) {
35 logbuffer := new(bytes.Buffer)
36 logger, err := initLogger("foo", logbuffer)
38 return logger, logbuffer
41 func TestLogInitDoesNotReturnAnError(t *testing.T) {
42 _, err := InitLogger("foo")
43 assert.Nil(t, err, "create failed")
46 func TestDebugFunctionLogsCorrectString(t *testing.T) {
47 logger, logbuffer := getTestLogger(t)
48 logger.Debug("test debug")
49 logstr := logbuffer.String()
50 assert.Contains(t, logstr, "crit\":\"DEBUG\",\"id\":\"foo\",\"mdc\":{},\"msg\":\"test debug\"}\n")
53 func TestInfoFunctionLogsCorrectString(t *testing.T) {
54 logger, logbuffer := getTestLogger(t)
55 logger.Info("test info")
56 logstr := logbuffer.String()
57 assert.Contains(t, logstr, "crit\":\"INFO\",\"id\":\"foo\",\"mdc\":{},\"msg\":\"test info\"}\n")
60 func TestWarningLogsCorrectString(t *testing.T) {
61 logger, logbuffer := getTestLogger(t)
62 logger.Warning("test warn")
63 logstr := logbuffer.String()
64 assert.Contains(t, logstr, "crit\":\"WARNING\",\"id\":\"foo\",\"mdc\":{},\"msg\":\"test warn\"}\n")
67 func TestErrorFunctionLogsCorrectString(t *testing.T) {
68 logger, logbuffer := getTestLogger(t)
69 logger.Error("test err")
70 logstr := logbuffer.String()
71 assert.Contains(t, logstr, "crit\":\"ERROR\",\"id\":\"foo\",\"mdc\":{},\"msg\":\"test err\"}\n")
74 func TestLogFunctionLogsCorrectString(t *testing.T) {
75 logger, logbuffer := getTestLogger(t)
76 logger.Log(ERR, "test err")
77 logstr := logbuffer.String()
78 assert.Contains(t, logstr, "crit\":\"ERROR\",\"id\":\"foo\",\"mdc\":{},\"msg\":\"test err\"}\n")
81 func TestFormatWithMdcReturnsJsonFormatedString(t *testing.T) {
82 logger, _ := InitLogger("foo")
83 logger.MdcAdd("foo", "bar")
84 logstr, err := logger.formatLog(INFO, "test2")
85 assert.Nil(t, err, "formatLog fails")
86 v := make(map[string]interface{})
87 err = json.Unmarshal(logstr, &v)
88 assert.Equal(t, "INFO", v["crit"])
89 assert.Equal(t, "test2", v["msg"])
90 assert.Equal(t, "foo", v["id"])
91 expectedmdc := map[string]interface{}{"foo": "bar"}
92 assert.Equal(t, expectedmdc, v["mdc"])
95 func TestMdcAddIsOk(t *testing.T) {
96 logger, _ := InitLogger("foo")
97 logger.MdcAdd("foo", "bar")
98 val, ok := logger.MdcGet("foo")
100 assert.Equal(t, "bar", val)
103 func TestMdcRemoveWorks(t *testing.T) {
104 logger, _ := InitLogger("foo")
105 logger.MdcAdd("foo", "bar")
106 val, ok := logger.MdcGet("foo")
108 assert.Equal(t, "bar", val)
109 logger.MdcRemove("foo")
110 val, ok = logger.MdcGet("foo")
115 func TestRemoveNonExistentMdcDoesNotCrash(t *testing.T) {
116 logger, _ := InitLogger("foo")
117 logger.MdcRemove("foo")
120 func TestMdcCleanRemovesAllMdcs(t *testing.T) {
121 logger, _ := InitLogger("foo")
122 logger.MdcAdd("foo1", "bar")
123 logger.MdcAdd("foo2", "bar")
124 logger.MdcAdd("foo3", "bar")
126 _, ok := logger.MdcGet("foo1")
128 _, ok = logger.MdcGet("foo2")
130 _, ok = logger.MdcGet("foo3")
134 func TestLevelStringsGetterWorks(t *testing.T) {
135 assert.Equal(t, "ERROR", levelString(ERR))
136 assert.Equal(t, "WARNING", levelString(WARN))
137 assert.Equal(t, "INFO", levelString(INFO))
138 assert.Equal(t, "DEBUG", levelString(DEBUG))
141 func TestDefaultLoggingLevelIsDebug(t *testing.T) {
142 logger, _ := InitLogger("foo")
143 assert.Equal(t, DEBUG, logger.LevelGet())
146 func TestLevelGetReturnsWhatWasSet(t *testing.T) {
147 logger, _ := InitLogger("foo")
149 assert.Equal(t, ERR, logger.LevelGet())
152 func TestDebugLogIsNotWrittenIfCurrentLevelIsInfo(t *testing.T) {
153 logger, logbuffer := getTestLogger(t)
154 logger.LevelSet(INFO)
156 assert.Empty(t, logbuffer.String())