Add new license claim
[com/golog.git] / mdclog_test.go
1 /*
2  *  Copyright (c) 2019 AT&T Intellectual Property.
3  *  Copyright (c) 2018-2019 Nokia.
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  *     http://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  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
18  * platform project (RICP).
19  */
20
21 package golog
22
23 import (
24         "bytes"
25         "encoding/json"
26         "testing"
27
28         "github.com/stretchr/testify/assert"
29 )
30
31 // getTestLogger returns a logger instance where
32 // the output is directed to a byte buffer instead
33 // of stdout
34 func getTestLogger(t *testing.T) (*MdcLogger, *bytes.Buffer) {
35         logbuffer := new(bytes.Buffer)
36         logger, err := initLogger("foo", logbuffer)
37         assert.Nil(t, err)
38         return logger, logbuffer
39 }
40
41 func TestLogInitDoesNotReturnAnError(t *testing.T) {
42         _, err := InitLogger("foo")
43         assert.Nil(t, err, "create failed")
44 }
45
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")
51 }
52
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")
58 }
59
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")
65 }
66
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")
72 }
73
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")
79 }
80
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"])
93 }
94
95 func TestMdcAddIsOk(t *testing.T) {
96         logger, _ := InitLogger("foo")
97         logger.MdcAdd("foo", "bar")
98         val, ok := logger.MdcGet("foo")
99         assert.True(t, ok)
100         assert.Equal(t, "bar", val)
101 }
102
103 func TestMdcRemoveWorks(t *testing.T) {
104         logger, _ := InitLogger("foo")
105         logger.MdcAdd("foo", "bar")
106         val, ok := logger.MdcGet("foo")
107         assert.True(t, ok)
108         assert.Equal(t, "bar", val)
109         logger.MdcRemove("foo")
110         val, ok = logger.MdcGet("foo")
111         assert.False(t, ok)
112         assert.Empty(t, val)
113 }
114
115 func TestRemoveNonExistentMdcDoesNotCrash(t *testing.T) {
116         logger, _ := InitLogger("foo")
117         logger.MdcRemove("foo")
118 }
119
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")
125         logger.MdcClean()
126         _, ok := logger.MdcGet("foo1")
127         assert.False(t, ok)
128         _, ok = logger.MdcGet("foo2")
129         assert.False(t, ok)
130         _, ok = logger.MdcGet("foo3")
131         assert.False(t, ok)
132 }
133
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))
139 }
140
141 func TestDefaultLoggingLevelIsDebug(t *testing.T) {
142         logger, _ := InitLogger("foo")
143         assert.Equal(t, DEBUG, logger.LevelGet())
144 }
145
146 func TestLevelGetReturnsWhatWasSet(t *testing.T) {
147         logger, _ := InitLogger("foo")
148         logger.LevelSet(ERR)
149         assert.Equal(t, ERR, logger.LevelGet())
150 }
151
152 func TestDebugLogIsNotWrittenIfCurrentLevelIsInfo(t *testing.T) {
153         logger, logbuffer := getTestLogger(t)
154         logger.LevelSet(INFO)
155         logger.Debug("fooo")
156         assert.Empty(t, logbuffer.String())
157 }