2020-07-28 13:38:48 +00:00
|
|
|
package csconfig
|
|
|
|
|
|
|
|
import (
|
2020-11-30 09:37:17 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2020-07-28 13:38:48 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
func TestDefaultConfig(t *testing.T) {
|
|
|
|
x := NewDefaultConfig()
|
|
|
|
x.Dump()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalLoad(t *testing.T) {
|
|
|
|
|
|
|
|
x := NewConfig()
|
2021-02-09 16:59:35 +00:00
|
|
|
err := x.LoadConfigurationFile("./tests/config.yaml", false, false)
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error %s", err)
|
2020-07-28 13:38:48 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
x = NewConfig()
|
2021-02-09 16:59:35 +00:00
|
|
|
err = x.LoadConfigurationFile("./tests/xxx.yaml", false, false)
|
2020-11-30 09:37:17 +00:00
|
|
|
if fmt.Sprintf("%s", err) != "failed to read config file: open ./tests/xxx.yaml: no such file or directory" {
|
|
|
|
t.Fatalf("unexpected error %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
x = NewConfig()
|
2021-02-09 16:59:35 +00:00
|
|
|
err = x.LoadConfigurationFile("./tests/simulation.yaml", false, false)
|
2020-11-30 09:37:17 +00:00
|
|
|
if !strings.HasPrefix(fmt.Sprintf("%s", err), "failed unmarshaling config: yaml: unmarshal error") {
|
|
|
|
t.Fatalf("unexpected error %s", err)
|
2020-07-28 13:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
func TestCleanupPaths(t *testing.T) {
|
2020-07-28 13:38:48 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2020-11-30 09:37:17 +00:00
|
|
|
Input *GlobalConfig
|
|
|
|
expectedResult *GlobalConfig
|
2020-07-28 13:38:48 +00:00
|
|
|
err string
|
|
|
|
}{
|
|
|
|
{
|
2020-11-30 09:37:17 +00:00
|
|
|
name: "daemon cleanup",
|
|
|
|
Input: &GlobalConfig{
|
|
|
|
Common: &CommonCfg{
|
|
|
|
PidDir: "////tmp//",
|
|
|
|
LogDir: "/////tmp///",
|
|
|
|
WorkingDir: "/////tmp///",
|
2020-07-28 13:38:48 +00:00
|
|
|
},
|
|
|
|
},
|
2020-11-30 09:37:17 +00:00
|
|
|
expectedResult: &GlobalConfig{
|
|
|
|
Common: &CommonCfg{
|
|
|
|
PidDir: "/tmp",
|
|
|
|
LogDir: "/tmp",
|
|
|
|
WorkingDir: "/tmp",
|
2020-07-28 13:38:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-11-30 09:37:17 +00:00
|
|
|
//
|
2020-07-28 13:38:48 +00:00
|
|
|
{
|
2020-11-30 09:37:17 +00:00
|
|
|
name: "crowdsec cleanup",
|
|
|
|
Input: &GlobalConfig{
|
|
|
|
Crowdsec: &CrowdsecServiceCfg{
|
|
|
|
AcquisitionFilePath: "////tmp//x.yaml",
|
2020-07-28 13:38:48 +00:00
|
|
|
},
|
|
|
|
},
|
2020-11-30 09:37:17 +00:00
|
|
|
expectedResult: &GlobalConfig{
|
|
|
|
Crowdsec: &CrowdsecServiceCfg{
|
|
|
|
AcquisitionFilePath: "/tmp/x.yaml",
|
|
|
|
},
|
2020-07-28 13:38:48 +00:00
|
|
|
},
|
|
|
|
},
|
2020-11-30 09:37:17 +00:00
|
|
|
//
|
2020-07-28 13:38:48 +00:00
|
|
|
{
|
2020-11-30 09:37:17 +00:00
|
|
|
name: "config paths cleanup",
|
|
|
|
Input: &GlobalConfig{
|
|
|
|
ConfigPaths: &ConfigurationPaths{
|
|
|
|
HubDir: "////tmp//",
|
|
|
|
HubIndexFile: "////tmp//x.yaml",
|
|
|
|
ConfigDir: "////tmp//",
|
|
|
|
DataDir: "////tmp//",
|
|
|
|
SimulationFilePath: "//tmp///toto.yaml",
|
2020-07-28 13:38:48 +00:00
|
|
|
},
|
|
|
|
},
|
2020-11-30 09:37:17 +00:00
|
|
|
expectedResult: &GlobalConfig{
|
|
|
|
ConfigPaths: &ConfigurationPaths{
|
|
|
|
HubDir: "/tmp",
|
|
|
|
HubIndexFile: "/tmp/x.yaml",
|
|
|
|
ConfigDir: "/tmp",
|
|
|
|
DataDir: "/tmp",
|
|
|
|
SimulationFilePath: "/tmp/toto.yaml",
|
|
|
|
},
|
2020-07-28 13:38:48 +00:00
|
|
|
},
|
|
|
|
},
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
for idx, test := range tests {
|
|
|
|
err := test.Input.CleanupPaths()
|
|
|
|
if test.err != "" {
|
|
|
|
if strings.HasPrefix(fmt.Sprintf("%s", err), test.err) {
|
|
|
|
t.Fatalf("%d/%d expected err %s got %s", idx, len(tests), test.err, fmt.Sprintf("%s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
isOk := assert.Equal(t, test.expectedResult, test.Input)
|
|
|
|
if !isOk {
|
|
|
|
t.Fatalf("%d/%d failed test", idx, len(tests))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimulationLoading(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
Input *GlobalConfig
|
|
|
|
expectedResult *SimulationConfig
|
|
|
|
err string
|
|
|
|
}{
|
2020-07-28 13:38:48 +00:00
|
|
|
{
|
2020-11-30 09:37:17 +00:00
|
|
|
name: "basic valid simulation",
|
|
|
|
Input: &GlobalConfig{
|
|
|
|
ConfigPaths: &ConfigurationPaths{
|
|
|
|
SimulationFilePath: "./tests/simulation.yaml",
|
2020-07-28 13:38:48 +00:00
|
|
|
},
|
2020-11-30 09:37:17 +00:00
|
|
|
Crowdsec: &CrowdsecServiceCfg{},
|
2020-07-28 13:38:48 +00:00
|
|
|
},
|
2020-11-30 09:37:17 +00:00
|
|
|
expectedResult: &SimulationConfig{Simulation: new(bool)},
|
2020-07-28 13:38:48 +00:00
|
|
|
},
|
|
|
|
{
|
2020-11-30 09:37:17 +00:00
|
|
|
name: "basic bad file name",
|
|
|
|
Input: &GlobalConfig{
|
|
|
|
ConfigPaths: &ConfigurationPaths{
|
|
|
|
SimulationFilePath: "./tests/xxx.yaml",
|
2020-07-28 13:38:48 +00:00
|
|
|
},
|
2020-11-30 09:37:17 +00:00
|
|
|
Crowdsec: &CrowdsecServiceCfg{},
|
2020-07-28 13:38:48 +00:00
|
|
|
},
|
2020-11-30 09:37:17 +00:00
|
|
|
err: "while reading './tests/xxx.yaml': open ./tests/xxx.yaml: no such file or directory",
|
2020-07-28 13:38:48 +00:00
|
|
|
},
|
|
|
|
{
|
2020-11-30 09:37:17 +00:00
|
|
|
name: "basic nil config",
|
|
|
|
Input: &GlobalConfig{
|
|
|
|
ConfigPaths: &ConfigurationPaths{
|
|
|
|
SimulationFilePath: "",
|
2020-07-28 13:38:48 +00:00
|
|
|
},
|
2020-11-30 09:37:17 +00:00
|
|
|
Crowdsec: &CrowdsecServiceCfg{},
|
2020-07-28 13:38:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-11-30 09:37:17 +00:00
|
|
|
name: "basic bad file content",
|
|
|
|
Input: &GlobalConfig{
|
|
|
|
ConfigPaths: &ConfigurationPaths{
|
|
|
|
SimulationFilePath: "./tests/config.yaml",
|
|
|
|
},
|
|
|
|
Crowdsec: &CrowdsecServiceCfg{},
|
2020-07-28 13:38:48 +00:00
|
|
|
},
|
2020-11-30 09:37:17 +00:00
|
|
|
err: "while unmarshaling simulation file './tests/config.yaml' : yaml: unmarshal errors",
|
2020-07-28 13:38:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
for idx, test := range tests {
|
|
|
|
err := test.Input.LoadSimulation()
|
|
|
|
if err == nil && test.err != "" {
|
|
|
|
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) {
|
|
|
|
t.Fatalf("%d/%d expected '%s' got '%s'", idx, len(tests),
|
|
|
|
test.err,
|
|
|
|
fmt.Sprintf("%s", err))
|
2020-07-28 13:38:48 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
isOk := assert.Equal(t, test.expectedResult, test.Input.Crowdsec.SimulationConfig)
|
|
|
|
if !isOk {
|
|
|
|
t.Fatalf("test '%s' failed", test.name)
|
2020-07-28 13:38:48 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewCrowdSecConfig(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
expectedResult *GlobalConfig
|
|
|
|
err string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "new configuration: basic",
|
|
|
|
expectedResult: &GlobalConfig{},
|
|
|
|
err: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
result := NewConfig()
|
2020-07-28 13:38:48 +00:00
|
|
|
isOk := assert.Equal(t, test.expectedResult, result)
|
|
|
|
if !isOk {
|
|
|
|
t.Fatalf("test '%s' failed", test.name)
|
|
|
|
}
|
|
|
|
log.Infof("test '%s' : OK", test.name)
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2020-07-28 13:38:48 +00:00
|
|
|
}
|