2021-03-24 17:16:17 +00:00
|
|
|
package csconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-06-01 14:31:56 +00:00
|
|
|
|
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 {
|
|
|
|
name string
|
|
|
|
Input *Config
|
|
|
|
expectedResult *DatabaseCfg
|
|
|
|
err string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "basic valid configuration",
|
|
|
|
Input: &Config{
|
|
|
|
DbConfig: &DatabaseCfg{
|
2022-02-17 16:52:04 +00:00
|
|
|
Type: "sqlite",
|
|
|
|
DbPath: "./tests/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{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedResult: &DatabaseCfg{
|
2022-02-17 16:52:04 +00:00
|
|
|
Type: "sqlite",
|
|
|
|
DbPath: "./tests/test.db",
|
2023-06-01 14:31:56 +00:00
|
|
|
MaxOpenConns: ptr.Of(10),
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no configuration path",
|
|
|
|
Input: &Config{},
|
|
|
|
expectedResult: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx, test := range tests {
|
|
|
|
err := test.Input.LoadDBConfig()
|
|
|
|
if err == nil && test.err != "" {
|
|
|
|
fmt.Printf("TEST '%s': NOK\n", test.name)
|
|
|
|
t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests))
|
|
|
|
} else if test.err != "" {
|
|
|
|
if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) {
|
|
|
|
fmt.Printf("TEST '%s': NOK\n", test.name)
|
|
|
|
t.Fatalf("%d/%d expected '%s' got '%s'", idx, len(tests),
|
|
|
|
test.err,
|
|
|
|
fmt.Sprintf("%s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
isOk := assert.Equal(t, test.expectedResult, test.Input.DbConfig)
|
|
|
|
if !isOk {
|
|
|
|
t.Fatalf("TEST '%s': NOK", test.name)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("TEST '%s': OK\n", test.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|