Refact pkg/csconfig tests (#2526)

* remove unused method
* whitespace, redundant comments
* use test helpers
* move DumpConsoleConfig() from pkg/csconfig to cscli
* package doc header
* var -> const
* rename ./tests -> ./testdata
* shorter tests with more error checks
* lint/formatting
* use helpers; fix tests that didn't actually test
* lint; rename expectedResult -> expected
This commit is contained in:
mmetc 2023-10-09 11:10:51 +02:00 committed by GitHub
parent 6b5da29e3d
commit 9ae8bd79c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 276 additions and 349 deletions

View file

@ -229,6 +229,24 @@ Disable given information push to the central API.`,
return cmdConsole return cmdConsole
} }
func dumpConsoleConfig(c *csconfig.LocalApiServerCfg) error {
out, err := yaml.Marshal(c.ConsoleConfig)
if err != nil {
return fmt.Errorf("while marshaling ConsoleConfig (for %s): %w", c.ConsoleConfigPath, err)
}
if c.ConsoleConfigPath == "" {
c.ConsoleConfigPath = csconfig.DefaultConsoleConfigFilePath
log.Debugf("Empty console_path, defaulting to %s", c.ConsoleConfigPath)
}
if err := os.WriteFile(c.ConsoleConfigPath, out, 0600); err != nil {
return fmt.Errorf("while dumping console config to %s: %w", c.ConsoleConfigPath, err)
}
return nil
}
func SetConsoleOpts(args []string, wanted bool) error { func SetConsoleOpts(args []string, wanted bool) error {
for _, arg := range args { for _, arg := range args {
switch arg { switch arg {
@ -326,7 +344,7 @@ func SetConsoleOpts(args []string, wanted bool) error {
} }
} }
if err := csConfig.API.Server.DumpConsoleConfig(); err != nil { if err := dumpConsoleConfig(csConfig.API.Server); err != nil {
return fmt.Errorf("failed writing console config: %s", err) return fmt.Errorf("failed writing console config: %s", err)
} }

View file

@ -58,7 +58,6 @@ type CTICfg struct {
} }
func (a *CTICfg) Load() error { func (a *CTICfg) Load() error {
if a.Key == nil { if a.Key == nil {
*a.Enabled = false *a.Enabled = false
} }

View file

@ -1,7 +1,6 @@
package csconfig package csconfig
import ( import (
"fmt"
"net" "net"
"os" "os"
"path/filepath" "path/filepath"
@ -10,6 +9,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"github.com/crowdsecurity/go-cs-lib/cstest" "github.com/crowdsecurity/go-cs-lib/cstest"
@ -26,7 +26,7 @@ func TestLoadLocalApiClientCfg(t *testing.T) {
{ {
name: "basic valid configuration", name: "basic valid configuration",
input: &LocalApiClientCfg{ input: &LocalApiClientCfg{
CredentialsFilePath: "./tests/lapi-secrets.yaml", CredentialsFilePath: "./testdata/lapi-secrets.yaml",
}, },
expected: &ApiCredentialsCfg{ expected: &ApiCredentialsCfg{
URL: "http://localhost:8080/", URL: "http://localhost:8080/",
@ -37,7 +37,7 @@ func TestLoadLocalApiClientCfg(t *testing.T) {
{ {
name: "invalid configuration", name: "invalid configuration",
input: &LocalApiClientCfg{ input: &LocalApiClientCfg{
CredentialsFilePath: "./tests/bad_lapi-secrets.yaml", CredentialsFilePath: "./testdata/bad_lapi-secrets.yaml",
}, },
expected: &ApiCredentialsCfg{}, expected: &ApiCredentialsCfg{},
expectedErr: "field unknown_key not found in type csconfig.ApiCredentialsCfg", expectedErr: "field unknown_key not found in type csconfig.ApiCredentialsCfg",
@ -45,15 +45,15 @@ func TestLoadLocalApiClientCfg(t *testing.T) {
{ {
name: "invalid configuration filepath", name: "invalid configuration filepath",
input: &LocalApiClientCfg{ input: &LocalApiClientCfg{
CredentialsFilePath: "./tests/nonexist_lapi-secrets.yaml", CredentialsFilePath: "./testdata/nonexist_lapi-secrets.yaml",
}, },
expected: nil, expected: nil,
expectedErr: "open ./tests/nonexist_lapi-secrets.yaml: " + cstest.FileNotFoundMessage, expectedErr: "open ./testdata/nonexist_lapi-secrets.yaml: " + cstest.FileNotFoundMessage,
}, },
{ {
name: "valid configuration with insecure skip verify", name: "valid configuration with insecure skip verify",
input: &LocalApiClientCfg{ input: &LocalApiClientCfg{
CredentialsFilePath: "./tests/lapi-secrets.yaml", CredentialsFilePath: "./testdata/lapi-secrets.yaml",
InsecureSkipVerify: ptr.Of(false), InsecureSkipVerify: ptr.Of(false),
}, },
expected: &ApiCredentialsCfg{ expected: &ApiCredentialsCfg{
@ -88,7 +88,7 @@ func TestLoadOnlineApiClientCfg(t *testing.T) {
{ {
name: "basic valid configuration", name: "basic valid configuration",
input: &OnlineApiClientCfg{ input: &OnlineApiClientCfg{
CredentialsFilePath: "./tests/online-api-secrets.yaml", CredentialsFilePath: "./testdata/online-api-secrets.yaml",
}, },
expected: &ApiCredentialsCfg{ expected: &ApiCredentialsCfg{
URL: "http://crowdsec.api", URL: "http://crowdsec.api",
@ -99,7 +99,7 @@ func TestLoadOnlineApiClientCfg(t *testing.T) {
{ {
name: "invalid configuration", name: "invalid configuration",
input: &OnlineApiClientCfg{ input: &OnlineApiClientCfg{
CredentialsFilePath: "./tests/bad_lapi-secrets.yaml", CredentialsFilePath: "./testdata/bad_lapi-secrets.yaml",
}, },
expected: &ApiCredentialsCfg{}, expected: &ApiCredentialsCfg{},
expectedErr: "failed unmarshaling api server credentials", expectedErr: "failed unmarshaling api server credentials",
@ -107,14 +107,14 @@ func TestLoadOnlineApiClientCfg(t *testing.T) {
{ {
name: "missing field configuration", name: "missing field configuration",
input: &OnlineApiClientCfg{ input: &OnlineApiClientCfg{
CredentialsFilePath: "./tests/bad_online-api-secrets.yaml", CredentialsFilePath: "./testdata/bad_online-api-secrets.yaml",
}, },
expected: nil, expected: nil,
}, },
{ {
name: "invalid configuration filepath", name: "invalid configuration filepath",
input: &OnlineApiClientCfg{ input: &OnlineApiClientCfg{
CredentialsFilePath: "./tests/nonexist_online-api-secrets.yaml", CredentialsFilePath: "./testdata/nonexist_online-api-secrets.yaml",
}, },
expected: &ApiCredentialsCfg{}, expected: &ApiCredentialsCfg{},
expectedErr: "failed to read api server credentials", expectedErr: "failed to read api server credentials",
@ -137,27 +137,23 @@ func TestLoadOnlineApiClientCfg(t *testing.T) {
func TestLoadAPIServer(t *testing.T) { func TestLoadAPIServer(t *testing.T) {
tmpLAPI := &LocalApiServerCfg{ tmpLAPI := &LocalApiServerCfg{
ProfilesPath: "./tests/profiles.yaml", ProfilesPath: "./testdata/profiles.yaml",
}
if err := tmpLAPI.LoadProfiles(); err != nil {
t.Fatalf("loading tmp profiles: %+v", err)
} }
err := tmpLAPI.LoadProfiles()
require.NoError(t, err)
LogDirFullPath, err := filepath.Abs("./testdata")
require.NoError(t, err)
LogDirFullPath, err := filepath.Abs("./tests")
if err != nil {
t.Fatal(err)
}
logLevel := log.InfoLevel logLevel := log.InfoLevel
config := &Config{} config := &Config{}
fcontent, err := os.ReadFile("./tests/config.yaml") fcontent, err := os.ReadFile("./testdata/config.yaml")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
configData := os.ExpandEnv(string(fcontent)) configData := os.ExpandEnv(string(fcontent))
err = yaml.UnmarshalStrict([]byte(configData), &config) err = yaml.UnmarshalStrict([]byte(configData), &config)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
tests := []struct { tests := []struct {
name string name string
input *Config input *Config
@ -172,18 +168,18 @@ func TestLoadAPIServer(t *testing.T) {
Server: &LocalApiServerCfg{ Server: &LocalApiServerCfg{
ListenURI: "http://crowdsec.api", ListenURI: "http://crowdsec.api",
OnlineClient: &OnlineApiClientCfg{ OnlineClient: &OnlineApiClientCfg{
CredentialsFilePath: "./tests/online-api-secrets.yaml", CredentialsFilePath: "./testdata/online-api-secrets.yaml",
}, },
ProfilesPath: "./tests/profiles.yaml", ProfilesPath: "./testdata/profiles.yaml",
PapiLogLevel: &logLevel, PapiLogLevel: &logLevel,
}, },
}, },
DbConfig: &DatabaseCfg{ DbConfig: &DatabaseCfg{
Type: "sqlite", Type: "sqlite",
DbPath: "./tests/test.db", DbPath: "./testdata/test.db",
}, },
Common: &CommonCfg{ Common: &CommonCfg{
LogDir: "./tests/", LogDir: "./testdata/",
LogMedia: "stdout", LogMedia: "stdout",
}, },
DisableAPI: false, DisableAPI: false,
@ -193,9 +189,10 @@ func TestLoadAPIServer(t *testing.T) {
ListenURI: "http://crowdsec.api", ListenURI: "http://crowdsec.api",
TLS: nil, TLS: nil,
DbConfig: &DatabaseCfg{ DbConfig: &DatabaseCfg{
DbPath: "./tests/test.db", DbPath: "./testdata/test.db",
Type: "sqlite", Type: "sqlite",
MaxOpenConns: ptr.Of(DEFAULT_MAX_OPEN_CONNS), MaxOpenConns: ptr.Of(DEFAULT_MAX_OPEN_CONNS),
DecisionBulkSize: defaultDecisionBulkSize,
}, },
ConsoleConfigPath: DefaultConfigPath("console.yaml"), ConsoleConfigPath: DefaultConfigPath("console.yaml"),
ConsoleConfig: &ConsoleConfig{ ConsoleConfig: &ConsoleConfig{
@ -208,7 +205,7 @@ func TestLoadAPIServer(t *testing.T) {
LogDir: LogDirFullPath, LogDir: LogDirFullPath,
LogMedia: "stdout", LogMedia: "stdout",
OnlineClient: &OnlineApiClientCfg{ OnlineClient: &OnlineApiClientCfg{
CredentialsFilePath: "./tests/online-api-secrets.yaml", CredentialsFilePath: "./testdata/online-api-secrets.yaml",
Credentials: &ApiCredentialsCfg{ Credentials: &ApiCredentialsCfg{
URL: "http://crowdsec.api", URL: "http://crowdsec.api",
Login: "test", Login: "test",
@ -216,7 +213,7 @@ func TestLoadAPIServer(t *testing.T) {
}, },
}, },
Profiles: tmpLAPI.Profiles, Profiles: tmpLAPI.Profiles,
ProfilesPath: "./tests/profiles.yaml", ProfilesPath: "./testdata/profiles.yaml",
UseForwardedForHeaders: false, UseForwardedForHeaders: false,
PapiLogLevel: &logLevel, PapiLogLevel: &logLevel,
}, },
@ -229,7 +226,7 @@ func TestLoadAPIServer(t *testing.T) {
Server: &LocalApiServerCfg{}, Server: &LocalApiServerCfg{},
}, },
Common: &CommonCfg{ Common: &CommonCfg{
LogDir: "./tests/", LogDir: "./testdata/",
LogMedia: "stdout", LogMedia: "stdout",
}, },
DisableAPI: false, DisableAPI: false,
@ -242,30 +239,23 @@ func TestLoadAPIServer(t *testing.T) {
}, },
} }
for idx, test := range tests { for _, tc := range tests {
err := test.input.LoadAPIServer() tc := tc
if err == nil && test.expectedErr != "" { t.Run(tc.name, func(t *testing.T) {
fmt.Printf("TEST '%s': NOK\n", test.name) err := tc.input.LoadAPIServer()
t.Fatalf("Test number %d/%d expected error, didn't get it", idx+1, len(tests)) cstest.RequireErrorContains(t, err, tc.expectedErr)
} else if test.expectedErr != "" { if tc.expectedErr != "" {
fmt.Printf("ERR: %+v\n", err) return
if !strings.HasPrefix(fmt.Sprintf("%s", err), test.expectedErr) {
fmt.Printf("TEST '%s': NOK\n", test.name)
t.Fatalf("%d/%d expected '%s' got '%s'", idx, len(tests),
test.expectedErr,
fmt.Sprintf("%s", err))
} }
assert.Equal(t, test.expected, test.input.API.Server) assert.Equal(t, tc.expected, tc.input.API.Server)
} })
} }
} }
func mustParseCIDRNet(s string) *net.IPNet { func mustParseCIDRNet(t *testing.T, s string) *net.IPNet {
_, ipNet, err := net.ParseCIDR(s) _, ipNet, err := net.ParseCIDR(s)
if err != nil { require.NoError(t, err)
panic(fmt.Sprintf("MustParseCIDR: parsing %q: %v", s, err))
}
return ipNet return ipNet
} }
@ -306,7 +296,7 @@ func TestParseCapiWhitelists(t *testing.T) {
input: `{"cidrs": ["1.2.3.0/24"]}`, input: `{"cidrs": ["1.2.3.0/24"]}`,
expected: &CapiWhitelist{ expected: &CapiWhitelist{
Ips: []net.IP{}, Ips: []net.IP{},
Cidrs: []*net.IPNet{mustParseCIDRNet("1.2.3.0/24")}, Cidrs: []*net.IPNet{mustParseCIDRNet(t, "1.2.3.0/24")},
}, },
}, },
} }

View file

@ -1,44 +1,41 @@
package csconfig package csconfig
import ( import (
"fmt"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/crowdsecurity/go-cs-lib/cstest"
) )
func TestLoadCommon(t *testing.T) { func TestLoadCommon(t *testing.T) {
pidDirPath := "./tests" pidDirPath := "./testdata"
LogDirFullPath, err := filepath.Abs("./tests/log/") LogDirFullPath, err := filepath.Abs("./testdata/log/")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
WorkingDirFullPath, err := filepath.Abs("./tests") WorkingDirFullPath, err := filepath.Abs("./testdata")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
tests := []struct { tests := []struct {
name string name string
Input *Config input *Config
expectedResult *CommonCfg expected *CommonCfg
err string expectedErr string
}{ }{
{ {
name: "basic valid configuration", name: "basic valid configuration",
Input: &Config{ input: &Config{
Common: &CommonCfg{ Common: &CommonCfg{
Daemonize: true, Daemonize: true,
PidDir: "./tests", PidDir: "./testdata",
LogMedia: "file", LogMedia: "file",
LogDir: "./tests/log/", LogDir: "./testdata/log/",
WorkingDir: "./tests/", WorkingDir: "./testdata/",
}, },
}, },
expectedResult: &CommonCfg{ expected: &CommonCfg{
Daemonize: true, Daemonize: true,
PidDir: pidDirPath, PidDir: pidDirPath,
LogMedia: "file", LogMedia: "file",
@ -48,15 +45,15 @@ func TestLoadCommon(t *testing.T) {
}, },
{ {
name: "empty working dir", name: "empty working dir",
Input: &Config{ input: &Config{
Common: &CommonCfg{ Common: &CommonCfg{
Daemonize: true, Daemonize: true,
PidDir: "./tests", PidDir: "./testdata",
LogMedia: "file", LogMedia: "file",
LogDir: "./tests/log/", LogDir: "./testdata/log/",
}, },
}, },
expectedResult: &CommonCfg{ expected: &CommonCfg{
Daemonize: true, Daemonize: true,
PidDir: pidDirPath, PidDir: pidDirPath,
LogMedia: "file", LogMedia: "file",
@ -64,31 +61,23 @@ func TestLoadCommon(t *testing.T) {
}, },
}, },
{ {
name: "no common", name: "no common",
Input: &Config{}, input: &Config{},
expectedResult: nil, expected: nil,
expectedErr: "no common block provided in configuration file",
}, },
} }
for idx, test := range tests { for _, tc := range tests {
err := test.Input.LoadCommon() tc := tc
if err == nil && test.err != "" { t.Run(tc.name, func(t *testing.T) {
fmt.Printf("TEST '%s': NOK\n", test.name) err := tc.input.LoadCommon()
t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests)) cstest.RequireErrorContains(t, err, tc.expectedErr)
} else if test.err != "" { if tc.expectedErr != "" {
if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) { return
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.Common) assert.Equal(t, tc.expected, tc.input.Common)
if !isOk { })
t.Fatalf("TEST '%s': NOK", test.name)
} else {
fmt.Printf("TEST '%s': OK\n", test.name)
}
} }
} }

View file

@ -1,3 +1,5 @@
// Package csconfig contains the configuration structures for crowdsec and cscli.
package csconfig package csconfig
import ( import (
@ -37,15 +39,6 @@ type Config struct {
Hub *Hub `yaml:"-"` Hub *Hub `yaml:"-"`
} }
func (c *Config) Dump() error {
out, err := yaml.Marshal(c)
if err != nil {
return fmt.Errorf("failed marshaling config: %w", err)
}
fmt.Printf("%s", string(out))
return nil
}
func NewConfig(configFile string, disableAgent bool, disableAPI bool, quiet bool) (*Config, string, error) { func NewConfig(configFile string, disableAgent bool, disableAPI bool, quiet bool) (*Config, string, error) {
patcher := yamlpatch.NewPatcher(configFile, ".local") patcher := yamlpatch.NewPatcher(configFile, ".local")
patcher.SetQuiet(quiet) patcher.SetQuiet(quiet)

View file

@ -5,42 +5,43 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
"github.com/crowdsecurity/go-cs-lib/cstest" "github.com/crowdsecurity/go-cs-lib/cstest"
) )
func TestNormalLoad(t *testing.T) { func TestNormalLoad(t *testing.T) {
_, _, err := NewConfig("./tests/config.yaml", false, false, false) _, _, err := NewConfig("./testdata/config.yaml", false, false, false)
require.NoError(t, err) require.NoError(t, err)
_, _, err = NewConfig("./tests/xxx.yaml", false, false, false) _, _, err = NewConfig("./testdata/xxx.yaml", false, false, false)
assert.EqualError(t, err, "while reading yaml file: open ./tests/xxx.yaml: "+cstest.FileNotFoundMessage) assert.EqualError(t, err, "while reading yaml file: open ./testdata/xxx.yaml: "+cstest.FileNotFoundMessage)
_, _, err = NewConfig("./tests/simulation.yaml", false, false, false) _, _, err = NewConfig("./testdata/simulation.yaml", false, false, false)
assert.EqualError(t, err, "./tests/simulation.yaml: yaml: unmarshal errors:\n line 1: field simulation not found in type csconfig.Config") assert.EqualError(t, err, "./testdata/simulation.yaml: yaml: unmarshal errors:\n line 1: field simulation not found in type csconfig.Config")
} }
func TestNewCrowdSecConfig(t *testing.T) { func TestNewCrowdSecConfig(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
expectedResult *Config expected *Config
}{ }{
{ {
name: "new configuration: basic", name: "new configuration: basic",
expectedResult: &Config{}, expected: &Config{},
}, },
} }
for _, tc := range tests { for _, tc := range tests {
tc := tc tc := tc
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
result := &Config{} result := &Config{}
assert.Equal(t, tc.expectedResult, result) assert.Equal(t, tc.expected, result)
}) })
} }
} }
func TestDefaultConfig(t *testing.T) { func TestDefaultConfig(t *testing.T) {
x := NewDefaultConfig() x := NewDefaultConfig()
err := x.Dump() _, err := yaml.Marshal(x)
require.NoError(t, err) require.NoError(t, err, "failed marshaling config: %s", err)
} }

View file

@ -82,23 +82,3 @@ func (c *LocalApiServerCfg) LoadConsoleConfig() error {
return nil return nil
} }
func (c *LocalApiServerCfg) DumpConsoleConfig() error {
var out []byte
var err error
if out, err = yaml.Marshal(c.ConsoleConfig); err != nil {
return fmt.Errorf("while marshaling ConsoleConfig (for %s): %w", c.ConsoleConfigPath, err)
}
if c.ConsoleConfigPath == "" {
c.ConsoleConfigPath = DefaultConsoleConfigFilePath
log.Debugf("Empty console_path, defaulting to %s", c.ConsoleConfigPath)
}
if err := os.WriteFile(c.ConsoleConfigPath, out, 0600); err != nil {
return fmt.Errorf("while dumping console config to %s: %w", c.ConsoleConfigPath, err)
}
return nil
}

View file

@ -1,24 +1,23 @@
package csconfig package csconfig
import ( import (
"fmt"
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/stretchr/testify/require"
"github.com/crowdsecurity/go-cs-lib/cstest" "github.com/crowdsecurity/go-cs-lib/cstest"
"github.com/crowdsecurity/go-cs-lib/ptr" "github.com/crowdsecurity/go-cs-lib/ptr"
"github.com/stretchr/testify/require"
) )
func TestLoadCrowdsec(t *testing.T) { func TestLoadCrowdsec(t *testing.T) {
acquisFullPath, err := filepath.Abs("./tests/acquis.yaml") acquisFullPath, err := filepath.Abs("./testdata/acquis.yaml")
require.NoError(t, err) require.NoError(t, err)
acquisInDirFullPath, err := filepath.Abs("./tests/acquis/acquis.yaml") acquisInDirFullPath, err := filepath.Abs("./testdata/acquis/acquis.yaml")
require.NoError(t, err) require.NoError(t, err)
acquisDirFullPath, err := filepath.Abs("./tests/acquis") acquisDirFullPath, err := filepath.Abs("./testdata/acquis")
require.NoError(t, err) require.NoError(t, err)
hubFullPath, err := filepath.Abs("./hub") hubFullPath, err := filepath.Abs("./hub")
@ -27,42 +26,42 @@ func TestLoadCrowdsec(t *testing.T) {
dataFullPath, err := filepath.Abs("./data") dataFullPath, err := filepath.Abs("./data")
require.NoError(t, err) require.NoError(t, err)
configDirFullPath, err := filepath.Abs("./tests") configDirFullPath, err := filepath.Abs("./testdata")
require.NoError(t, err) require.NoError(t, err)
hubIndexFileFullPath, err := filepath.Abs("./hub/.index.json") hubIndexFileFullPath, err := filepath.Abs("./hub/.index.json")
require.NoError(t, err) require.NoError(t, err)
contextFileFullPath, err := filepath.Abs("./tests/context.yaml") contextFileFullPath, err := filepath.Abs("./testdata/context.yaml")
require.NoError(t, err) require.NoError(t, err)
tests := []struct { tests := []struct {
name string name string
input *Config input *Config
expectedResult *CrowdsecServiceCfg expected *CrowdsecServiceCfg
expectedErr string expectedErr string
}{ }{
{ {
name: "basic valid configuration", name: "basic valid configuration",
input: &Config{ input: &Config{
ConfigPaths: &ConfigurationPaths{ ConfigPaths: &ConfigurationPaths{
ConfigDir: "./tests", ConfigDir: "./testdata",
DataDir: "./data", DataDir: "./data",
HubDir: "./hub", HubDir: "./hub",
}, },
API: &APICfg{ API: &APICfg{
Client: &LocalApiClientCfg{ Client: &LocalApiClientCfg{
CredentialsFilePath: "./tests/lapi-secrets.yaml", CredentialsFilePath: "./testdata/lapi-secrets.yaml",
}, },
}, },
Crowdsec: &CrowdsecServiceCfg{ Crowdsec: &CrowdsecServiceCfg{
AcquisitionFilePath: "./tests/acquis.yaml", AcquisitionFilePath: "./testdata/acquis.yaml",
SimulationFilePath: "./tests/simulation.yaml", SimulationFilePath: "./testdata/simulation.yaml",
ConsoleContextPath: "./tests/context.yaml", ConsoleContextPath: "./testdata/context.yaml",
ConsoleContextValueLength: 2500, ConsoleContextValueLength: 2500,
}, },
}, },
expectedResult: &CrowdsecServiceCfg{ expected: &CrowdsecServiceCfg{
Enable: ptr.Of(true), Enable: ptr.Of(true),
AcquisitionDirPath: "", AcquisitionDirPath: "",
ConsoleContextPath: contextFileFullPath, ConsoleContextPath: contextFileFullPath,
@ -76,7 +75,7 @@ func TestLoadCrowdsec(t *testing.T) {
OutputRoutinesCount: 1, OutputRoutinesCount: 1,
ConsoleContextValueLength: 2500, ConsoleContextValueLength: 2500,
AcquisitionFiles: []string{acquisFullPath}, AcquisitionFiles: []string{acquisFullPath},
SimulationFilePath: "./tests/simulation.yaml", SimulationFilePath: "./testdata/simulation.yaml",
ContextToSend: map[string][]string{ ContextToSend: map[string][]string{
"source_ip": {"evt.Parsed.source_ip"}, "source_ip": {"evt.Parsed.source_ip"},
}, },
@ -89,23 +88,23 @@ func TestLoadCrowdsec(t *testing.T) {
name: "basic valid configuration with acquisition dir", name: "basic valid configuration with acquisition dir",
input: &Config{ input: &Config{
ConfigPaths: &ConfigurationPaths{ ConfigPaths: &ConfigurationPaths{
ConfigDir: "./tests", ConfigDir: "./testdata",
DataDir: "./data", DataDir: "./data",
HubDir: "./hub", HubDir: "./hub",
}, },
API: &APICfg{ API: &APICfg{
Client: &LocalApiClientCfg{ Client: &LocalApiClientCfg{
CredentialsFilePath: "./tests/lapi-secrets.yaml", CredentialsFilePath: "./testdata/lapi-secrets.yaml",
}, },
}, },
Crowdsec: &CrowdsecServiceCfg{ Crowdsec: &CrowdsecServiceCfg{
AcquisitionFilePath: "./tests/acquis.yaml", AcquisitionFilePath: "./testdata/acquis.yaml",
AcquisitionDirPath: "./tests/acquis/", AcquisitionDirPath: "./testdata/acquis/",
SimulationFilePath: "./tests/simulation.yaml", SimulationFilePath: "./testdata/simulation.yaml",
ConsoleContextPath: "./tests/context.yaml", ConsoleContextPath: "./testdata/context.yaml",
}, },
}, },
expectedResult: &CrowdsecServiceCfg{ expected: &CrowdsecServiceCfg{
Enable: ptr.Of(true), Enable: ptr.Of(true),
AcquisitionDirPath: acquisDirFullPath, AcquisitionDirPath: acquisDirFullPath,
AcquisitionFilePath: acquisFullPath, AcquisitionFilePath: acquisFullPath,
@ -122,7 +121,7 @@ func TestLoadCrowdsec(t *testing.T) {
ContextToSend: map[string][]string{ ContextToSend: map[string][]string{
"source_ip": {"evt.Parsed.source_ip"}, "source_ip": {"evt.Parsed.source_ip"},
}, },
SimulationFilePath: "./tests/simulation.yaml", SimulationFilePath: "./testdata/simulation.yaml",
SimulationConfig: &SimulationConfig{ SimulationConfig: &SimulationConfig{
Simulation: ptr.Of(false), Simulation: ptr.Of(false),
}, },
@ -132,13 +131,13 @@ func TestLoadCrowdsec(t *testing.T) {
name: "no acquisition file and dir", name: "no acquisition file and dir",
input: &Config{ input: &Config{
ConfigPaths: &ConfigurationPaths{ ConfigPaths: &ConfigurationPaths{
ConfigDir: "./tests", ConfigDir: "./testdata",
DataDir: "./data", DataDir: "./data",
HubDir: "./hub", HubDir: "./hub",
}, },
API: &APICfg{ API: &APICfg{
Client: &LocalApiClientCfg{ Client: &LocalApiClientCfg{
CredentialsFilePath: "./tests/lapi-secrets.yaml", CredentialsFilePath: "./testdata/lapi-secrets.yaml",
}, },
}, },
Crowdsec: &CrowdsecServiceCfg{ Crowdsec: &CrowdsecServiceCfg{
@ -146,7 +145,7 @@ func TestLoadCrowdsec(t *testing.T) {
ConsoleContextValueLength: 10, ConsoleContextValueLength: 10,
}, },
}, },
expectedResult: &CrowdsecServiceCfg{ expected: &CrowdsecServiceCfg{
Enable: ptr.Of(true), Enable: ptr.Of(true),
AcquisitionDirPath: "", AcquisitionDirPath: "",
AcquisitionFilePath: "", AcquisitionFilePath: "",
@ -173,18 +172,18 @@ func TestLoadCrowdsec(t *testing.T) {
name: "non existing acquisition file", name: "non existing acquisition file",
input: &Config{ input: &Config{
ConfigPaths: &ConfigurationPaths{ ConfigPaths: &ConfigurationPaths{
ConfigDir: "./tests", ConfigDir: "./testdata",
DataDir: "./data", DataDir: "./data",
HubDir: "./hub", HubDir: "./hub",
}, },
API: &APICfg{ API: &APICfg{
Client: &LocalApiClientCfg{ Client: &LocalApiClientCfg{
CredentialsFilePath: "./tests/lapi-secrets.yaml", CredentialsFilePath: "./testdata/lapi-secrets.yaml",
}, },
}, },
Crowdsec: &CrowdsecServiceCfg{ Crowdsec: &CrowdsecServiceCfg{
ConsoleContextPath: "", ConsoleContextPath: "",
AcquisitionFilePath: "./tests/acquis_not_exist.yaml", AcquisitionFilePath: "./testdata/acquis_not_exist.yaml",
}, },
}, },
expectedErr: cstest.FileNotFoundMessage, expectedErr: cstest.FileNotFoundMessage,
@ -193,26 +192,25 @@ func TestLoadCrowdsec(t *testing.T) {
name: "agent disabled", name: "agent disabled",
input: &Config{ input: &Config{
ConfigPaths: &ConfigurationPaths{ ConfigPaths: &ConfigurationPaths{
ConfigDir: "./tests", ConfigDir: "./testdata",
DataDir: "./data", DataDir: "./data",
HubDir: "./hub", HubDir: "./hub",
}, },
}, },
expectedResult: nil, expected: nil,
}, },
} }
for _, tc := range tests { for _, tc := range tests {
tc := tc tc := tc
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
fmt.Printf("TEST '%s'\n", tc.name)
err := tc.input.LoadCrowdsec() err := tc.input.LoadCrowdsec()
cstest.RequireErrorContains(t, err, tc.expectedErr) cstest.RequireErrorContains(t, err, tc.expectedErr)
if tc.expectedErr != "" { if tc.expectedErr != "" {
return return
} }
require.Equal(t, tc.expectedResult, tc.input.Crowdsec) require.Equal(t, tc.expected, tc.input.Crowdsec)
}) })
} }
} }

View file

@ -1,52 +1,45 @@
package csconfig package csconfig
import ( import (
"fmt"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/crowdsecurity/go-cs-lib/cstest"
) )
func TestLoadCSCLI(t *testing.T) { func TestLoadCSCLI(t *testing.T) {
hubFullPath, err := filepath.Abs("./hub") hubFullPath, err := filepath.Abs("./hub")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
dataFullPath, err := filepath.Abs("./data") dataFullPath, err := filepath.Abs("./data")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
configDirFullPath, err := filepath.Abs("./tests") configDirFullPath, err := filepath.Abs("./testdata")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
hubIndexFileFullPath, err := filepath.Abs("./hub/.index.json") hubIndexFileFullPath, err := filepath.Abs("./hub/.index.json")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
tests := []struct { tests := []struct {
name string name string
Input *Config input *Config
expectedResult *CscliCfg expected *CscliCfg
err string expectedErr string
}{ }{
{ {
name: "basic valid configuration", name: "basic valid configuration",
Input: &Config{ input: &Config{
ConfigPaths: &ConfigurationPaths{ ConfigPaths: &ConfigurationPaths{
ConfigDir: "./tests", ConfigDir: "./testdata",
DataDir: "./data", DataDir: "./data",
HubDir: "./hub", HubDir: "./hub",
HubIndexFile: "./hub/.index.json", HubIndexFile: "./hub/.index.json",
}, },
}, },
expectedResult: &CscliCfg{ expected: &CscliCfg{
ConfigDir: configDirFullPath, ConfigDir: configDirFullPath,
DataDir: dataFullPath, DataDir: dataFullPath,
HubDir: hubFullPath, HubDir: hubFullPath,
@ -54,31 +47,23 @@ func TestLoadCSCLI(t *testing.T) {
}, },
}, },
{ {
name: "no configuration path", name: "no configuration path",
Input: &Config{}, input: &Config{},
expectedResult: &CscliCfg{}, expected: &CscliCfg{},
expectedErr: "no configuration paths provided",
}, },
} }
for idx, test := range tests { for _, tc := range tests {
err := test.Input.LoadCSCLI() tc := tc
if err == nil && test.err != "" { t.Run(tc.name, func(t *testing.T) {
fmt.Printf("TEST '%s': NOK\n", test.name) err := tc.input.LoadCSCLI()
t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests)) cstest.RequireErrorContains(t, err, tc.expectedErr)
} else if test.err != "" { if tc.expectedErr != "" {
if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) { return
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.Cscli) assert.Equal(t, tc.expected, tc.input.Cscli)
if !isOk { })
t.Fatalf("TEST '%s': NOK", test.name)
} else {
fmt.Printf("TEST '%s': OK\n", test.name)
}
} }
} }

View file

@ -10,13 +10,12 @@ import (
"github.com/crowdsecurity/go-cs-lib/ptr" "github.com/crowdsecurity/go-cs-lib/ptr"
) )
var DEFAULT_MAX_OPEN_CONNS = 100
const ( const (
DEFAULT_MAX_OPEN_CONNS = 100
defaultDecisionBulkSize = 1000 defaultDecisionBulkSize = 1000
// we need an upper bound due to the sqlite limit of 32k variables in a query // we need an upper bound due to the sqlite limit of 32k variables in a query
// we have 15 variables per decision, so 32768/15 = 2184.5333 // we have 15 variables per decision, so 32768/15 = 2184.5333
maxDecisionBulkSize = 2000 maxDecisionBulkSize = 2000
) )
type DatabaseCfg struct { type DatabaseCfg struct {

View file

@ -1,28 +1,27 @@
package csconfig package csconfig
import ( import (
"fmt"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/crowdsecurity/go-cs-lib/cstest"
"github.com/crowdsecurity/go-cs-lib/ptr" "github.com/crowdsecurity/go-cs-lib/ptr"
) )
func TestLoadDBConfig(t *testing.T) { func TestLoadDBConfig(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
Input *Config input *Config
expectedResult *DatabaseCfg expected *DatabaseCfg
err string expectedErr string
}{ }{
{ {
name: "basic valid configuration", name: "basic valid configuration",
Input: &Config{ input: &Config{
DbConfig: &DatabaseCfg{ DbConfig: &DatabaseCfg{
Type: "sqlite", Type: "sqlite",
DbPath: "./tests/test.db", DbPath: "./testdata/test.db",
MaxOpenConns: ptr.Of(10), MaxOpenConns: ptr.Of(10),
}, },
Cscli: &CscliCfg{}, Cscli: &CscliCfg{},
@ -30,38 +29,31 @@ func TestLoadDBConfig(t *testing.T) {
Server: &LocalApiServerCfg{}, Server: &LocalApiServerCfg{},
}, },
}, },
expectedResult: &DatabaseCfg{ expected: &DatabaseCfg{
Type: "sqlite", Type: "sqlite",
DbPath: "./tests/test.db", DbPath: "./testdata/test.db",
MaxOpenConns: ptr.Of(10), MaxOpenConns: ptr.Of(10),
DecisionBulkSize: defaultDecisionBulkSize, DecisionBulkSize: defaultDecisionBulkSize,
}, },
}, },
{ {
name: "no configuration path", name: "no configuration path",
Input: &Config{}, input: &Config{},
expectedResult: nil, expected: nil,
expectedErr: "no database configuration provided",
}, },
} }
for idx, test := range tests { for _, tc := range tests {
err := test.Input.LoadDBConfig() tc := tc
if err == nil && test.err != "" { t.Run(tc.name, func(t *testing.T) {
fmt.Printf("TEST '%s': NOK\n", test.name) err := tc.input.LoadDBConfig()
t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests)) cstest.RequireErrorContains(t, err, tc.expectedErr)
} else if test.err != "" { if tc.expectedErr != "" {
if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) { return
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) assert.Equal(t, tc.expected, tc.input.DbConfig)
if !isOk { })
t.Fatalf("TEST '%s': NOK", test.name)
} else {
fmt.Printf("TEST '%s': OK\n", test.name)
}
} }
} }

View file

@ -10,7 +10,6 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/fflag" "github.com/crowdsecurity/crowdsec/pkg/fflag"
) )
// LoadFeatureFlagsEnv parses the environment variables to enable feature flags. // LoadFeatureFlagsEnv parses the environment variables to enable feature flags.
func LoadFeatureFlagsEnv(logger *log.Logger) error { func LoadFeatureFlagsEnv(logger *log.Logger) error {
if err := fflag.Crowdsec.SetFromEnv(logger); err != nil { if err := fflag.Crowdsec.SetFromEnv(logger); err != nil {
@ -19,7 +18,6 @@ func LoadFeatureFlagsEnv(logger *log.Logger) error {
return nil return nil
} }
// LoadFeatureFlags parses feature.yaml to enable feature flags. // LoadFeatureFlags parses feature.yaml to enable feature flags.
// The file is in the same directory as config.yaml, which is provided // The file is in the same directory as config.yaml, which is provided
// as the fist parameter. This can be different than ConfigPaths.ConfigDir // as the fist parameter. This can be different than ConfigPaths.ConfigDir
@ -33,7 +31,6 @@ func LoadFeatureFlagsFile(configPath string, logger *log.Logger) error {
return nil return nil
} }
// ListFeatureFlags returns a list of the enabled feature flags. // ListFeatureFlags returns a list of the enabled feature flags.
func ListFeatureFlags() string { func ListFeatureFlags() string {
enabledFeatures := fflag.Crowdsec.GetEnabledFeatures() enabledFeatures := fflag.Crowdsec.GetEnabledFeatures()

View file

@ -1,52 +1,45 @@
package csconfig package csconfig
import ( import (
"fmt"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/crowdsecurity/go-cs-lib/cstest"
) )
func TestLoadHub(t *testing.T) { func TestLoadHub(t *testing.T) {
hubFullPath, err := filepath.Abs("./hub") hubFullPath, err := filepath.Abs("./hub")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
dataFullPath, err := filepath.Abs("./data") dataFullPath, err := filepath.Abs("./data")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
configDirFullPath, err := filepath.Abs("./tests") configDirFullPath, err := filepath.Abs("./testdata")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
hubIndexFileFullPath, err := filepath.Abs("./hub/.index.json") hubIndexFileFullPath, err := filepath.Abs("./hub/.index.json")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
tests := []struct { tests := []struct {
name string name string
Input *Config input *Config
expectedResult *Hub expected *Hub
err string expectedErr string
}{ }{
{ {
name: "basic valid configuration", name: "basic valid configuration",
Input: &Config{ input: &Config{
ConfigPaths: &ConfigurationPaths{ ConfigPaths: &ConfigurationPaths{
ConfigDir: "./tests", ConfigDir: "./testdata",
DataDir: "./data", DataDir: "./data",
HubDir: "./hub", HubDir: "./hub",
HubIndexFile: "./hub/.index.json", HubIndexFile: "./hub/.index.json",
}, },
}, },
expectedResult: &Hub{ expected: &Hub{
ConfigDir: configDirFullPath, ConfigDir: configDirFullPath,
DataDir: dataFullPath, DataDir: dataFullPath,
HubDir: hubFullPath, HubDir: hubFullPath,
@ -55,40 +48,32 @@ func TestLoadHub(t *testing.T) {
}, },
{ {
name: "no data dir", name: "no data dir",
Input: &Config{ input: &Config{
ConfigPaths: &ConfigurationPaths{ ConfigPaths: &ConfigurationPaths{
ConfigDir: "./tests", ConfigDir: "./testdata",
HubDir: "./hub", HubDir: "./hub",
HubIndexFile: "./hub/.index.json", HubIndexFile: "./hub/.index.json",
}, },
}, },
expectedResult: nil, expectedErr: "please provide a data directory with the 'data_dir' directive in the 'config_paths' section",
}, },
{ {
name: "no configuration path", name: "no configuration path",
Input: &Config{}, input: &Config{},
expectedResult: nil, expectedErr: "no configuration paths provided",
}, },
} }
for idx, test := range tests { for _, tc := range tests {
err := test.Input.LoadHub() tc := tc
if err == nil && test.err != "" { t.Run(tc.name, func(t *testing.T) {
fmt.Printf("TEST '%s': NOK\n", test.name) err := tc.input.LoadHub()
t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests)) cstest.RequireErrorContains(t, err, tc.expectedErr)
} else if test.err != "" { if tc.expectedErr != "" {
if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) { return
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.Hub) assert.Equal(t, tc.expected, tc.input.Hub)
if !isOk { })
t.Fatalf("TEST '%s': NOK", test.name)
} else {
fmt.Printf("TEST '%s': OK\n", test.name)
}
} }
} }

View file

@ -6,10 +6,11 @@ import (
"fmt" "fmt"
"io" "io"
"gopkg.in/yaml.v2"
"github.com/crowdsecurity/go-cs-lib/yamlpatch" "github.com/crowdsecurity/go-cs-lib/yamlpatch"
"github.com/crowdsecurity/crowdsec/pkg/models" "github.com/crowdsecurity/crowdsec/pkg/models"
"gopkg.in/yaml.v2"
) )
// var OnErrorDefault = OnErrorIgnore // var OnErrorDefault = OnErrorIgnore
@ -43,7 +44,6 @@ func (c *LocalApiServerCfg) LoadProfiles() error {
} }
reader := bytes.NewReader(fcontent) reader := bytes.NewReader(fcontent)
//process the yaml
dec := yaml.NewDecoder(reader) dec := yaml.NewDecoder(reader)
dec.SetStrict(true) dec.SetStrict(true)
for { for {

View file

@ -3,21 +3,21 @@ package csconfig
import ( import (
"testing" "testing"
"github.com/crowdsecurity/go-cs-lib/cstest"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/crowdsecurity/go-cs-lib/cstest"
) )
func TestLoadPrometheus(t *testing.T) { func TestLoadPrometheus(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
Input *Config input *Config
expectedURL string expectedURL string
expectedErr string expectedErr string
}{ }{
{ {
name: "basic valid configuration", name: "basic valid configuration",
Input: &Config{ input: &Config{
Prometheus: &PrometheusCfg{ Prometheus: &PrometheusCfg{
Enabled: true, Enabled: true,
Level: "full", Level: "full",
@ -33,10 +33,10 @@ func TestLoadPrometheus(t *testing.T) {
for _, tc := range tests { for _, tc := range tests {
tc := tc tc := tc
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
err := tc.Input.LoadPrometheus() err := tc.input.LoadPrometheus()
cstest.RequireErrorContains(t, err, tc.expectedErr) cstest.RequireErrorContains(t, err, tc.expectedErr)
require.Equal(t, tc.expectedURL, tc.Input.Cscli.PrometheusUrl) require.Equal(t, tc.expectedURL, tc.input.Cscli.PrometheusUrl)
}) })
} }
} }

View file

@ -4,8 +4,9 @@ import (
"fmt" "fmt"
"path/filepath" "path/filepath"
"github.com/crowdsecurity/go-cs-lib/yamlpatch"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"github.com/crowdsecurity/go-cs-lib/yamlpatch"
) )
type SimulationConfig struct { type SimulationConfig struct {

View file

@ -12,46 +12,46 @@ import (
) )
func TestSimulationLoading(t *testing.T) { func TestSimulationLoading(t *testing.T) {
testXXFullPath, err := filepath.Abs("./tests/xxx.yaml") testXXFullPath, err := filepath.Abs("./testdata/xxx.yaml")
require.NoError(t, err) require.NoError(t, err)
badYamlFullPath, err := filepath.Abs("./tests/config.yaml") badYamlFullPath, err := filepath.Abs("./testdata/config.yaml")
require.NoError(t, err) require.NoError(t, err)
tests := []struct { tests := []struct {
name string name string
Input *Config input *Config
expectedResult *SimulationConfig expected *SimulationConfig
expectedErr string expectedErr string
}{ }{
{ {
name: "basic valid simulation", name: "basic valid simulation",
Input: &Config{ input: &Config{
ConfigPaths: &ConfigurationPaths{ ConfigPaths: &ConfigurationPaths{
SimulationFilePath: "./tests/simulation.yaml", SimulationFilePath: "./testdata/simulation.yaml",
DataDir: "./data", DataDir: "./data",
}, },
Crowdsec: &CrowdsecServiceCfg{}, Crowdsec: &CrowdsecServiceCfg{},
Cscli: &CscliCfg{}, Cscli: &CscliCfg{},
}, },
expectedResult: &SimulationConfig{Simulation: new(bool)}, expected: &SimulationConfig{Simulation: new(bool)},
}, },
{ {
name: "basic nil config", name: "basic nil config",
Input: &Config{ input: &Config{
ConfigPaths: &ConfigurationPaths{ ConfigPaths: &ConfigurationPaths{
SimulationFilePath: "", SimulationFilePath: "",
DataDir: "./data", DataDir: "./data",
}, },
Crowdsec: &CrowdsecServiceCfg{}, Crowdsec: &CrowdsecServiceCfg{},
}, },
expectedErr: "simulation.yaml: "+cstest.FileNotFoundMessage, expectedErr: "simulation.yaml: " + cstest.FileNotFoundMessage,
}, },
{ {
name: "basic bad file name", name: "basic bad file name",
Input: &Config{ input: &Config{
ConfigPaths: &ConfigurationPaths{ ConfigPaths: &ConfigurationPaths{
SimulationFilePath: "./tests/xxx.yaml", SimulationFilePath: "./testdata/xxx.yaml",
DataDir: "./data", DataDir: "./data",
}, },
Crowdsec: &CrowdsecServiceCfg{}, Crowdsec: &CrowdsecServiceCfg{},
@ -60,9 +60,9 @@ func TestSimulationLoading(t *testing.T) {
}, },
{ {
name: "basic bad file content", name: "basic bad file content",
Input: &Config{ input: &Config{
ConfigPaths: &ConfigurationPaths{ ConfigPaths: &ConfigurationPaths{
SimulationFilePath: "./tests/config.yaml", SimulationFilePath: "./testdata/config.yaml",
DataDir: "./data", DataDir: "./data",
}, },
Crowdsec: &CrowdsecServiceCfg{}, Crowdsec: &CrowdsecServiceCfg{},
@ -71,9 +71,9 @@ func TestSimulationLoading(t *testing.T) {
}, },
{ {
name: "basic bad file content", name: "basic bad file content",
Input: &Config{ input: &Config{
ConfigPaths: &ConfigurationPaths{ ConfigPaths: &ConfigurationPaths{
SimulationFilePath: "./tests/config.yaml", SimulationFilePath: "./testdata/config.yaml",
DataDir: "./data", DataDir: "./data",
}, },
Crowdsec: &CrowdsecServiceCfg{}, Crowdsec: &CrowdsecServiceCfg{},
@ -85,10 +85,10 @@ func TestSimulationLoading(t *testing.T) {
for _, tc := range tests { for _, tc := range tests {
tc := tc tc := tc
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
err := tc.Input.LoadSimulation() err := tc.input.LoadSimulation()
cstest.RequireErrorContains(t, err, tc.expectedErr) cstest.RequireErrorContains(t, err, tc.expectedErr)
assert.Equal(t, tc.expectedResult, tc.Input.Crowdsec.SimulationConfig) assert.Equal(t, tc.expected, tc.input.Crowdsec.SimulationConfig)
}) })
} }
} }
@ -109,32 +109,32 @@ func TestIsSimulated(t *testing.T) {
name string name string
SimulationConfig *SimulationConfig SimulationConfig *SimulationConfig
Input string Input string
expectedResult bool expected bool
}{ }{
{ {
name: "No simulation except (in exclusion)", name: "No simulation except (in exclusion)",
SimulationConfig: simCfgOff, SimulationConfig: simCfgOff,
Input: "test", Input: "test",
expectedResult: true, expected: true,
}, },
{ {
name: "All simulation (not in exclusion)", name: "All simulation (not in exclusion)",
SimulationConfig: simCfgOn, SimulationConfig: simCfgOn,
Input: "toto", Input: "toto",
expectedResult: true, expected: true,
}, },
{ {
name: "All simulation (in exclusion)", name: "All simulation (in exclusion)",
SimulationConfig: simCfgOn, SimulationConfig: simCfgOn,
Input: "test", Input: "test",
expectedResult: false, expected: false,
}, },
} }
for _, tc := range tests { for _, tc := range tests {
tc := tc tc := tc
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
IsSimulated := tc.SimulationConfig.IsSimulated(tc.Input) isSimulated := tc.SimulationConfig.IsSimulated(tc.Input)
require.Equal(t, tc.expectedResult, IsSimulated) require.Equal(t, tc.expected, isSimulated)
}) })
} }
} }

View file

@ -7,7 +7,7 @@ prometheus:
enabled: true enabled: true
level: full level: full
crowdsec_service: crowdsec_service:
acquisition_path: ./tests/acquis.yaml acquisition_path: ./testdata/acquis.yaml
parser_routines: 1 parser_routines: 1
cscli: cscli:
output: human output: human
@ -21,17 +21,17 @@ db_config:
type: sqlite type: sqlite
api: api:
client: client:
credentials_path: ./tests/lapi-secrets.yaml credentials_path: ./testdata/lapi-secrets.yaml
server: server:
profiles_path: ./tests/profiles.yaml profiles_path: ./testdata/profiles.yaml
listen_uri: 127.0.0.1:8080 listen_uri: 127.0.0.1:8080
tls: null tls: null
online_client: online_client:
credentials_path: ./tests/online-api-secrets.yaml credentials_path: ./testdata/online-api-secrets.yaml
config_paths: config_paths:
config_dir: ./tests config_dir: ./testdata
data_dir: . data_dir: .
simulation_path: ./tests/simulation.yaml simulation_path: ./testdata/simulation.yaml
index_path: ./tests/hub/.index.json index_path: ./testdata/hub/.index.json
hub_dir: ./tests/hub hub_dir: ./testdata/hub