2021-03-24 17:16:17 +00:00
|
|
|
package csconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-06-01 14:31:56 +00:00
|
|
|
|
2023-10-09 09:10:51 +00:00
|
|
|
"github.com/crowdsecurity/go-cs-lib/cstest"
|
2023-07-28 14:35:08 +00:00
|
|
|
"github.com/crowdsecurity/go-cs-lib/ptr"
|
2021-03-24 17:16:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoadDBConfig(t *testing.T) {
|
|
|
|
tests := []struct {
|
2023-10-09 09:10:51 +00:00
|
|
|
name string
|
|
|
|
input *Config
|
|
|
|
expected *DatabaseCfg
|
|
|
|
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
|
|
|
DbConfig: &DatabaseCfg{
|
2022-02-17 16:52:04 +00:00
|
|
|
Type: "sqlite",
|
2023-10-09 09:10:51 +00:00
|
|
|
DbPath: "./testdata/test.db",
|
2023-06-01 14:31:56 +00:00
|
|
|
MaxOpenConns: ptr.Of(10),
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
Cscli: &CscliCfg{},
|
|
|
|
API: &APICfg{
|
|
|
|
Server: &LocalApiServerCfg{},
|
|
|
|
},
|
|
|
|
},
|
2023-10-09 09:10:51 +00:00
|
|
|
expected: &DatabaseCfg{
|
2024-03-07 13:04:50 +00:00
|
|
|
Type: "sqlite",
|
|
|
|
DbPath: "./testdata/test.db",
|
|
|
|
MaxOpenConns: ptr.Of(10),
|
|
|
|
UseWal: ptr.Of(true),
|
2023-08-25 15:05:17 +00:00
|
|
|
DecisionBulkSize: defaultDecisionBulkSize,
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-10-09 09:10:51 +00:00
|
|
|
name: "no configuration path",
|
|
|
|
input: &Config{},
|
|
|
|
expected: nil,
|
|
|
|
expectedErr: "no database configuration provided",
|
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) {
|
2024-01-30 10:07:53 +00:00
|
|
|
err := tc.input.LoadDBConfig(false)
|
2023-10-09 09:10:51 +00:00
|
|
|
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.DbConfig)
|
|
|
|
})
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
}
|