2021-03-24 17:16:17 +00:00
|
|
|
package csconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-10-09 09:10:51 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/crowdsecurity/go-cs-lib/cstest"
|
2021-03-24 17:16:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoadCommon(t *testing.T) {
|
2023-10-09 09:10:51 +00:00
|
|
|
pidDirPath := "./testdata"
|
|
|
|
LogDirFullPath, err := filepath.Abs("./testdata/log/")
|
|
|
|
require.NoError(t, err)
|
2021-03-24 17:16:17 +00:00
|
|
|
|
2023-10-09 09:10:51 +00:00
|
|
|
WorkingDirFullPath, err := filepath.Abs("./testdata")
|
|
|
|
require.NoError(t, err)
|
2021-03-24 17:16:17 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
2023-10-09 09:10:51 +00:00
|
|
|
name string
|
|
|
|
input *Config
|
|
|
|
expected *CommonCfg
|
|
|
|
expectedErr string
|
2021-03-24 17:16:17 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "basic valid configuration",
|
2023-10-09 09:10:51 +00:00
|
|
|
input: &Config{
|
2021-03-24 17:16:17 +00:00
|
|
|
Common: &CommonCfg{
|
|
|
|
Daemonize: true,
|
2023-10-09 09:10:51 +00:00
|
|
|
PidDir: "./testdata",
|
2021-03-24 17:16:17 +00:00
|
|
|
LogMedia: "file",
|
2023-10-09 09:10:51 +00:00
|
|
|
LogDir: "./testdata/log/",
|
|
|
|
WorkingDir: "./testdata/",
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
},
|
2023-10-09 09:10:51 +00:00
|
|
|
expected: &CommonCfg{
|
2021-03-24 17:16:17 +00:00
|
|
|
Daemonize: true,
|
2022-03-16 08:23:49 +00:00
|
|
|
PidDir: pidDirPath,
|
2021-03-24 17:16:17 +00:00
|
|
|
LogMedia: "file",
|
|
|
|
LogDir: LogDirFullPath,
|
|
|
|
WorkingDir: WorkingDirFullPath,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty working dir",
|
2023-10-09 09:10:51 +00:00
|
|
|
input: &Config{
|
2021-03-24 17:16:17 +00:00
|
|
|
Common: &CommonCfg{
|
|
|
|
Daemonize: true,
|
2023-10-09 09:10:51 +00:00
|
|
|
PidDir: "./testdata",
|
2021-03-24 17:16:17 +00:00
|
|
|
LogMedia: "file",
|
2023-10-09 09:10:51 +00:00
|
|
|
LogDir: "./testdata/log/",
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
},
|
2023-10-09 09:10:51 +00:00
|
|
|
expected: &CommonCfg{
|
2021-03-24 17:16:17 +00:00
|
|
|
Daemonize: true,
|
2022-03-16 08:23:49 +00:00
|
|
|
PidDir: pidDirPath,
|
2021-03-24 17:16:17 +00:00
|
|
|
LogMedia: "file",
|
|
|
|
LogDir: LogDirFullPath,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-10-09 09:10:51 +00:00
|
|
|
name: "no common",
|
|
|
|
input: &Config{},
|
|
|
|
expected: nil,
|
|
|
|
expectedErr: "no common block provided in configuration file",
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-10-09 09:10:51 +00:00
|
|
|
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
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 09:10:51 +00:00
|
|
|
assert.Equal(t, tc.expected, tc.input.Common)
|
|
|
|
})
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
}
|