[OOM-K8S-CERT-EXTERNAL-PROVIDER] Change logger implementation provider
[oom/platform/cert-service.git] / certServiceK8sExternalProvider / src / leveledlogger / logger_test.go
1 /*
2  * ============LICENSE_START=======================================================
3  * oom-certservice-k8s-external-provider
4  * ================================================================================
5  * Copyright (C) 2020 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package leveledlogger
22
23 import (
24         "bytes"
25         "fmt"
26         "log"
27         "os"
28         "testing"
29 )
30
31 func TestLoggerOnWarningLevel(t *testing.T) {
32         const resultLogName = "testdata/test_result_warn.log"
33         const expectedLogName = "testdata/test_expected_warn.log"
34
35         SetConfigFileName("testdata/test_logger_config_warn.json")
36         logger := GetLoggerWithName("loggername")
37
38         logOnAllLevels(logger)
39
40         resultLogBytes := readFile(resultLogName)
41         expectedLogBytes := readFile(expectedLogName)
42
43         assertLogEquals(t, resultLogBytes, expectedLogBytes, resultLogName)
44 }
45
46 func TestLoggerOnDebugLevel(t *testing.T) {
47         const resultLogName = "testdata/test_result_debug.log"
48         const expectedLogName = "testdata/test_expected_debug.log"
49
50         SetConfigFileName("testdata/test_logger_config_debug.json")
51         logger := GetLoggerWithName("loggername")
52
53     logOnAllLevels(logger)
54
55         resultLogBytes := readFile(resultLogName)
56         expectedLogBytes := readFile(expectedLogName)
57
58         assertLogEquals(t, resultLogBytes, expectedLogBytes, resultLogName)
59 }
60
61 func logOnAllLevels(logger Logger) {
62         logger.Debug("this is a debug message")
63         logger.Info("this is an info message")
64         logger.Warning("this is a warning message", "key1", "value1")
65         logger.Error(fmt.Errorf("this is an error message"), "err msg")
66 }
67
68 func assertLogEquals(t *testing.T, resultLogBytes []byte, expectedLogBytes []byte, resultLogName string) {
69         if areEqual(resultLogBytes, expectedLogBytes) {
70                 removeTemporaryFile(resultLogName)
71         } else {
72                 t.Fatal("Logs are different than expected. Please check: " + resultLogName)
73         }
74 }
75
76 func areEqual(slice1 []byte, slice2 []byte) bool {
77         return bytes.Compare(slice1, slice2) == 0
78 }
79
80 func removeTemporaryFile(fileName string) {
81         if _, err := os.Stat(fileName); err == nil {
82                 e := os.Remove(fileName)
83                 if e != nil {
84                         log.Fatal(e)
85                 }
86         }
87 }