github.com/jayanthvn/pure-gobpf@v0.0.0-20230623131354-8d1d959d9e0b/pkg/logger/logger_test.go (about) 1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"). 4 // You may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 //limitations under the License. 14 15 package logger 16 17 import ( 18 "os" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 "go.uber.org/zap/zapcore" 23 "gopkg.in/natefinch/lumberjack.v2" 24 ) 25 26 func TestEnvLogFilePath(t *testing.T) { 27 path := "/var/log/test.log" 28 _ = os.Setenv(envLogFilePath, path) 29 defer os.Unsetenv(envLogFilePath) 30 31 assert.Equal(t, path, GetLogLocation()) 32 } 33 34 func TestLoggerGetSameInstance(t *testing.T) { 35 log1 := Get() 36 log2 := Get() 37 38 assert.True(t, log1 == log2) 39 } 40 41 func TestLoggerNewAndGetSameInstance(t *testing.T) { 42 logConfig := LoadLogConfig() 43 log1 := New(logConfig) 44 log2 := Get() 45 46 assert.True(t, log1 == log2) 47 } 48 49 func TestGetLogFileLocationReturnsDefaultPath(t *testing.T) { 50 defaultPath := "/var/log/aws-routed-eni/ebpf-sdk.log" 51 assert.Equal(t, defaultPath, GetLogLocation()) 52 } 53 54 func TestLogLevelReturnsOverriddenLevel(t *testing.T) { 55 _ = os.Setenv(envLogLevel, "INFO") 56 defer os.Unsetenv(envLogLevel) 57 58 expectedLogLevel := zapcore.InfoLevel 59 inputLogLevel := GetLogLevel() 60 assert.Equal(t, expectedLogLevel, getZapLevel(inputLogLevel)) 61 } 62 63 func TestLogLevelReturnsDefaultLevelWhenEnvNotSet(t *testing.T) { 64 expectedLogLevel := zapcore.DebugLevel 65 inputLogLevel := GetLogLevel() 66 assert.Equal(t, expectedLogLevel, getZapLevel(inputLogLevel)) 67 } 68 69 func TestLogLevelReturnsDefaultLevelWhenEnvSetToInvalidValue(t *testing.T) { 70 _ = os.Setenv(envLogLevel, "EVERYTHING") 71 defer os.Unsetenv(envLogLevel) 72 73 var expectedLogLevel zapcore.Level 74 inputLogLevel := GetLogLevel() 75 expectedLogLevel = zapcore.DebugLevel 76 assert.Equal(t, expectedLogLevel, getZapLevel(inputLogLevel)) 77 } 78 79 func TestGetSDKLogFilePathEmpty(t *testing.T) { 80 expectedWriter := zapcore.Lock(os.Stderr) 81 inputSDKLogFile := "" 82 assert.Equal(t, expectedWriter, getSDKLogFilePath(inputSDKLogFile)) 83 } 84 85 func TestGetSDKLogFilePathStdout(t *testing.T) { 86 expectedWriter := zapcore.Lock(os.Stdout) 87 inputSDKLogFile := "stdout" 88 assert.Equal(t, expectedWriter, getSDKLogFilePath(inputSDKLogFile)) 89 } 90 91 func TestGetSDKLogFilePath(t *testing.T) { 92 inputSDKLogFile := "/var/log/aws-routed-eni/ebpf-sdk.log" 93 expectedLumberJackLogger := &lumberjack.Logger{ 94 Filename: "/var/log/aws-routed-eni/ebpf-sdk.log", 95 MaxSize: 100, 96 MaxBackups: 5, 97 MaxAge: 30, 98 Compress: true, 99 } 100 assert.Equal(t, zapcore.AddSync(expectedLumberJackLogger), getSDKLogFilePath(inputSDKLogFile)) 101 }