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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
31 func TestLoggerOnWarningLevel(t *testing.T) {
32 const resultLogName = "testdata/test_result_warn.log"
33 const expectedLogName = "testdata/test_expected_warn.log"
35 SetConfigFileName("testdata/test_logger_config_warn.json")
36 logger := GetLoggerWithName("loggername")
38 logOnAllLevels(logger)
40 resultLogBytes := readFile(resultLogName)
41 expectedLogBytes := readFile(expectedLogName)
43 assertLogEquals(t, resultLogBytes, expectedLogBytes, resultLogName)
46 func TestLoggerOnDebugLevel(t *testing.T) {
47 const resultLogName = "testdata/test_result_debug.log"
48 const expectedLogName = "testdata/test_expected_debug.log"
50 SetConfigFileName("testdata/test_logger_config_debug.json")
51 logger := GetLoggerWithName("loggername")
53 logOnAllLevels(logger)
55 resultLogBytes := readFile(resultLogName)
56 expectedLogBytes := readFile(expectedLogName)
58 assertLogEquals(t, resultLogBytes, expectedLogBytes, resultLogName)
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")
68 func assertLogEquals(t *testing.T, resultLogBytes []byte, expectedLogBytes []byte, resultLogName string) {
69 if areEqual(resultLogBytes, expectedLogBytes) {
70 removeTemporaryFile(resultLogName)
72 t.Fatal("Logs are different than expected. Please check: " + resultLogName)
76 func areEqual(slice1 []byte, slice2 []byte) bool {
77 return bytes.Compare(slice1, slice2) == 0
80 func removeTemporaryFile(fileName string) {
81 if _, err := os.Stat(fileName); err == nil {
82 e := os.Remove(fileName)