1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package csconfig
- import (
- "path/filepath"
- "testing"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "github.com/crowdsecurity/go-cs-lib/cstest"
- )
- func TestLoadCommon(t *testing.T) {
- pidDirPath := "./testdata"
- LogDirFullPath, err := filepath.Abs("./testdata/log/")
- require.NoError(t, err)
- WorkingDirFullPath, err := filepath.Abs("./testdata")
- require.NoError(t, err)
- tests := []struct {
- name string
- input *Config
- expected *CommonCfg
- expectedErr string
- }{
- {
- name: "basic valid configuration",
- input: &Config{
- Common: &CommonCfg{
- Daemonize: true,
- PidDir: "./testdata",
- LogMedia: "file",
- LogDir: "./testdata/log/",
- WorkingDir: "./testdata/",
- },
- },
- expected: &CommonCfg{
- Daemonize: true,
- PidDir: pidDirPath,
- LogMedia: "file",
- LogDir: LogDirFullPath,
- WorkingDir: WorkingDirFullPath,
- },
- },
- {
- name: "empty working dir",
- input: &Config{
- Common: &CommonCfg{
- Daemonize: true,
- PidDir: "./testdata",
- LogMedia: "file",
- LogDir: "./testdata/log/",
- },
- },
- expected: &CommonCfg{
- Daemonize: true,
- PidDir: pidDirPath,
- LogMedia: "file",
- LogDir: LogDirFullPath,
- },
- },
- {
- name: "no common",
- input: &Config{},
- expected: nil,
- expectedErr: "no common block provided in configuration file",
- },
- }
- for _, tc := range tests {
- tc := tc
- t.Run(tc.name, func(t *testing.T) {
- err := tc.input.LoadCommon()
- cstest.RequireErrorContains(t, err, tc.expectedErr)
- if tc.expectedErr != "" {
- return
- }
- assert.Equal(t, tc.expected, tc.input.Common)
- })
- }
- }
|