github.com/observiq/bindplane-agent@v1.51.0/internal/logging/config_test.go (about)

     1  // Copyright  observIQ, Inc.
     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 logging
    16  
    17  import (
    18  	"os"
    19  	"path/filepath"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  	"go.uber.org/zap/zapcore"
    24  	"gopkg.in/natefinch/lumberjack.v2"
    25  )
    26  
    27  func TestNewLoggerConfig(t *testing.T) {
    28  	t.Setenv("MYVAR", "/some/path")
    29  
    30  	cases := []struct {
    31  		name        string
    32  		configPath  string
    33  		expect      *LoggerConfig
    34  		expectedErr string
    35  	}{
    36  		{
    37  			name:       "file config",
    38  			configPath: filepath.Join("testdata", "info.yaml"),
    39  			expect: &LoggerConfig{
    40  				Output: fileOutput,
    41  				Level:  zapcore.InfoLevel,
    42  				File: &lumberjack.Logger{
    43  					Filename:   "log/collector.log",
    44  					MaxBackups: 5,
    45  					MaxSize:    1,
    46  					MaxAge:     7,
    47  				},
    48  			},
    49  		},
    50  		{
    51  			name:       "stdout config",
    52  			configPath: filepath.Join("testdata", "stdout.yaml"),
    53  			expect: &LoggerConfig{
    54  				Output: stdOutput,
    55  				Level:  zapcore.DebugLevel,
    56  			},
    57  		},
    58  		{
    59  			name:       "config with environment variables in filename",
    60  			configPath: filepath.Join("testdata", "expand-env.yaml"),
    61  			expect: &LoggerConfig{
    62  				Output: fileOutput,
    63  				Level:  zapcore.InfoLevel,
    64  				File: &lumberjack.Logger{
    65  					Filename:   "/some/path/collector.log",
    66  					MaxBackups: 5,
    67  					MaxSize:    1,
    68  					MaxAge:     7,
    69  				},
    70  			},
    71  		},
    72  		{
    73  			name:        "config does not exist",
    74  			configPath:  filepath.Join("testdata", "does-not-exist.yaml"),
    75  			expectedErr: "failed to read config",
    76  		},
    77  		{
    78  			name:        "config exists but is not valid yaml",
    79  			configPath:  filepath.Join("testdata", "not-yaml.txt"),
    80  			expectedErr: "failed to unmarshal config",
    81  		},
    82  	}
    83  
    84  	for _, tc := range cases {
    85  		t.Run(tc.name, func(t *testing.T) {
    86  			conf, err := NewLoggerConfig(tc.configPath)
    87  			if tc.expectedErr != "" {
    88  				require.Error(t, err)
    89  				require.ErrorContains(t, err, tc.expectedErr)
    90  				return
    91  			}
    92  
    93  			require.NoError(t, err)
    94  			require.Equal(t, tc.expect, conf)
    95  
    96  			opts, err := conf.Options()
    97  			require.NoError(t, err)
    98  			require.NotNil(t, opts)
    99  			require.Len(t, opts, 1)
   100  
   101  		})
   102  	}
   103  }
   104  
   105  func TestNewLoggerConfigDefaultPath(t *testing.T) {
   106  	t.Run("config does not exist in default location", func(t *testing.T) {
   107  		tempDir := t.TempDir()
   108  		chDir(t, tempDir)
   109  
   110  		require.NoFileExists(t, DefaultConfigPath)
   111  
   112  		conf, err := NewLoggerConfig(DefaultConfigPath)
   113  		require.NoError(t, err)
   114  		require.Equal(t, defaultConfig(), conf)
   115  
   116  		require.FileExists(t, DefaultConfigPath)
   117  
   118  		// Calling again with the existing config should give the same result
   119  		conf, err = NewLoggerConfig(DefaultConfigPath)
   120  		require.NoError(t, err)
   121  		require.Equal(t, defaultConfig(), conf)
   122  	})
   123  
   124  	t.Run("config exists in the default location", func(t *testing.T) {
   125  		tempDir := t.TempDir()
   126  
   127  		testYaml, err := filepath.Abs(filepath.Join("testdata", "info.yaml"))
   128  		require.NoError(t, err)
   129  
   130  		testYamlBytes, err := os.ReadFile(testYaml)
   131  		require.NoError(t, err)
   132  
   133  		chDir(t, tempDir)
   134  
   135  		err = os.WriteFile(DefaultConfigPath, testYamlBytes, 0600)
   136  		require.NoError(t, err)
   137  
   138  		conf, err := NewLoggerConfig(DefaultConfigPath)
   139  		require.NoError(t, err)
   140  		require.Equal(t, &LoggerConfig{
   141  			Output: fileOutput,
   142  			Level:  zapcore.InfoLevel,
   143  			File: &lumberjack.Logger{
   144  				Filename:   "log/collector.log",
   145  				MaxBackups: 5,
   146  				MaxSize:    1,
   147  				MaxAge:     7,
   148  			},
   149  		}, conf)
   150  	})
   151  
   152  }
   153  
   154  func chDir(t *testing.T, dir string) {
   155  	t.Helper()
   156  
   157  	oldWd, err := os.Getwd()
   158  	require.NoError(t, err)
   159  
   160  	err = os.Chdir(dir)
   161  	require.NoError(t, err)
   162  
   163  	t.Cleanup(func() {
   164  		err = os.Chdir(oldWd)
   165  		require.NoError(t, err)
   166  	})
   167  }