2021-03-24 17:16:17 +00:00
|
|
|
package csconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-10-17 12:17:23 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-07-28 14:35:08 +00:00
|
|
|
"github.com/crowdsecurity/go-cs-lib/cstest"
|
2021-03-24 17:16:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSimulationLoading(t *testing.T) {
|
|
|
|
tests := []struct {
|
2023-10-09 09:10:51 +00:00
|
|
|
name string
|
|
|
|
input *Config
|
|
|
|
expected *SimulationConfig
|
|
|
|
expectedErr string
|
2021-03-24 17:16:17 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "basic valid simulation",
|
2023-10-09 09:10:51 +00:00
|
|
|
input: &Config{
|
2021-03-24 17:16:17 +00:00
|
|
|
ConfigPaths: &ConfigurationPaths{
|
2023-10-09 09:10:51 +00:00
|
|
|
SimulationFilePath: "./testdata/simulation.yaml",
|
2021-03-24 17:16:17 +00:00
|
|
|
DataDir: "./data",
|
|
|
|
},
|
|
|
|
Crowdsec: &CrowdsecServiceCfg{},
|
|
|
|
Cscli: &CscliCfg{},
|
|
|
|
},
|
2023-10-09 09:10:51 +00:00
|
|
|
expected: &SimulationConfig{Simulation: new(bool)},
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
{
|
2022-05-17 10:14:59 +00:00
|
|
|
name: "basic nil config",
|
2023-10-09 09:10:51 +00:00
|
|
|
input: &Config{
|
2021-03-24 17:16:17 +00:00
|
|
|
ConfigPaths: &ConfigurationPaths{
|
2022-05-17 10:14:59 +00:00
|
|
|
SimulationFilePath: "",
|
2021-03-24 17:16:17 +00:00
|
|
|
DataDir: "./data",
|
|
|
|
},
|
|
|
|
Crowdsec: &CrowdsecServiceCfg{},
|
|
|
|
},
|
2023-10-09 09:10:51 +00:00
|
|
|
expectedErr: "simulation.yaml: " + cstest.FileNotFoundMessage,
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
{
|
2022-10-17 12:17:23 +00:00
|
|
|
name: "basic bad file name",
|
2023-10-09 09:10:51 +00:00
|
|
|
input: &Config{
|
2021-03-24 17:16:17 +00:00
|
|
|
ConfigPaths: &ConfigurationPaths{
|
2023-10-09 09:10:51 +00:00
|
|
|
SimulationFilePath: "./testdata/xxx.yaml",
|
2021-03-24 17:16:17 +00:00
|
|
|
DataDir: "./data",
|
|
|
|
},
|
|
|
|
Crowdsec: &CrowdsecServiceCfg{},
|
|
|
|
},
|
2023-11-24 14:57:32 +00:00
|
|
|
expectedErr: fmt.Sprintf("while reading yaml file: open ./testdata/xxx.yaml: %s", cstest.FileNotFoundMessage),
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "basic bad file content",
|
2023-10-09 09:10:51 +00:00
|
|
|
input: &Config{
|
2021-03-24 17:16:17 +00:00
|
|
|
ConfigPaths: &ConfigurationPaths{
|
2023-10-09 09:10:51 +00:00
|
|
|
SimulationFilePath: "./testdata/config.yaml",
|
2021-03-24 17:16:17 +00:00
|
|
|
DataDir: "./data",
|
|
|
|
},
|
|
|
|
Crowdsec: &CrowdsecServiceCfg{},
|
|
|
|
},
|
2023-11-24 14:57:32 +00:00
|
|
|
expectedErr: "while unmarshaling simulation file './testdata/config.yaml' : yaml: unmarshal errors",
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
2022-10-17 12:17:23 +00:00
|
|
|
{
|
|
|
|
name: "basic bad file content",
|
2023-10-09 09:10:51 +00:00
|
|
|
input: &Config{
|
2022-05-17 10:14:59 +00:00
|
|
|
ConfigPaths: &ConfigurationPaths{
|
2023-10-09 09:10:51 +00:00
|
|
|
SimulationFilePath: "./testdata/config.yaml",
|
2022-05-17 10:14:59 +00:00
|
|
|
DataDir: "./data",
|
|
|
|
},
|
|
|
|
Crowdsec: &CrowdsecServiceCfg{},
|
|
|
|
},
|
2023-11-24 14:57:32 +00:00
|
|
|
expectedErr: "while unmarshaling simulation file './testdata/config.yaml' : yaml: unmarshal errors",
|
2022-10-17 12:17:23 +00:00
|
|
|
},
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 12:17:23 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-10-09 09:10:51 +00:00
|
|
|
err := tc.input.LoadSimulation()
|
2022-10-17 12:17:23 +00:00
|
|
|
cstest.RequireErrorContains(t, err, tc.expectedErr)
|
|
|
|
|
2023-10-09 09:10:51 +00:00
|
|
|
assert.Equal(t, tc.expected, tc.input.Crowdsec.SimulationConfig)
|
2022-10-17 12:17:23 +00:00
|
|
|
})
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsSimulated(t *testing.T) {
|
|
|
|
simCfgOff := &SimulationConfig{
|
|
|
|
Simulation: new(bool),
|
|
|
|
Exclusions: []string{"test"},
|
|
|
|
}
|
|
|
|
|
|
|
|
simCfgOn := &SimulationConfig{
|
|
|
|
Simulation: new(bool),
|
|
|
|
Exclusions: []string{"test"},
|
|
|
|
}
|
|
|
|
*simCfgOn.Simulation = true
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
SimulationConfig *SimulationConfig
|
|
|
|
Input string
|
2023-10-09 09:10:51 +00:00
|
|
|
expected bool
|
2021-03-24 17:16:17 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "No simulation except (in exclusion)",
|
|
|
|
SimulationConfig: simCfgOff,
|
|
|
|
Input: "test",
|
2023-10-09 09:10:51 +00:00
|
|
|
expected: true,
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "All simulation (not in exclusion)",
|
|
|
|
SimulationConfig: simCfgOn,
|
|
|
|
Input: "toto",
|
2023-10-09 09:10:51 +00:00
|
|
|
expected: true,
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "All simulation (in exclusion)",
|
|
|
|
SimulationConfig: simCfgOn,
|
|
|
|
Input: "test",
|
2023-10-09 09:10:51 +00:00
|
|
|
expected: false,
|
2021-03-24 17:16:17 +00:00
|
|
|
},
|
|
|
|
}
|
2022-10-17 12:17:23 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-10-09 09:10:51 +00:00
|
|
|
isSimulated := tc.SimulationConfig.IsSimulated(tc.Input)
|
|
|
|
require.Equal(t, tc.expected, isSimulated)
|
2022-10-17 12:17:23 +00:00
|
|
|
})
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
}
|