mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-11 20:36:12 +02:00
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:
parent
6b5da29e3d
commit
9ae8bd79c5
27 changed files with 276 additions and 349 deletions
|
@ -229,6 +229,24 @@ Disable given information push to the central API.`,
|
|||
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 {
|
||||
for _, arg := range args {
|
||||
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)
|
||||
}
|
||||
|
||||
|
|
|
@ -58,7 +58,6 @@ type CTICfg struct {
|
|||
}
|
||||
|
||||
func (a *CTICfg) Load() error {
|
||||
|
||||
if a.Key == nil {
|
||||
*a.Enabled = false
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package csconfig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
@ -10,6 +9,7 @@ import (
|
|||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gopkg.in/yaml.v2"
|
||||
|
||||
"github.com/crowdsecurity/go-cs-lib/cstest"
|
||||
|
@ -26,7 +26,7 @@ func TestLoadLocalApiClientCfg(t *testing.T) {
|
|||
{
|
||||
name: "basic valid configuration",
|
||||
input: &LocalApiClientCfg{
|
||||
CredentialsFilePath: "./tests/lapi-secrets.yaml",
|
||||
CredentialsFilePath: "./testdata/lapi-secrets.yaml",
|
||||
},
|
||||
expected: &ApiCredentialsCfg{
|
||||
URL: "http://localhost:8080/",
|
||||
|
@ -37,7 +37,7 @@ func TestLoadLocalApiClientCfg(t *testing.T) {
|
|||
{
|
||||
name: "invalid configuration",
|
||||
input: &LocalApiClientCfg{
|
||||
CredentialsFilePath: "./tests/bad_lapi-secrets.yaml",
|
||||
CredentialsFilePath: "./testdata/bad_lapi-secrets.yaml",
|
||||
},
|
||||
expected: &ApiCredentialsCfg{},
|
||||
expectedErr: "field unknown_key not found in type csconfig.ApiCredentialsCfg",
|
||||
|
@ -45,15 +45,15 @@ func TestLoadLocalApiClientCfg(t *testing.T) {
|
|||
{
|
||||
name: "invalid configuration filepath",
|
||||
input: &LocalApiClientCfg{
|
||||
CredentialsFilePath: "./tests/nonexist_lapi-secrets.yaml",
|
||||
CredentialsFilePath: "./testdata/nonexist_lapi-secrets.yaml",
|
||||
},
|
||||
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",
|
||||
input: &LocalApiClientCfg{
|
||||
CredentialsFilePath: "./tests/lapi-secrets.yaml",
|
||||
CredentialsFilePath: "./testdata/lapi-secrets.yaml",
|
||||
InsecureSkipVerify: ptr.Of(false),
|
||||
},
|
||||
expected: &ApiCredentialsCfg{
|
||||
|
@ -88,7 +88,7 @@ func TestLoadOnlineApiClientCfg(t *testing.T) {
|
|||
{
|
||||
name: "basic valid configuration",
|
||||
input: &OnlineApiClientCfg{
|
||||
CredentialsFilePath: "./tests/online-api-secrets.yaml",
|
||||
CredentialsFilePath: "./testdata/online-api-secrets.yaml",
|
||||
},
|
||||
expected: &ApiCredentialsCfg{
|
||||
URL: "http://crowdsec.api",
|
||||
|
@ -99,7 +99,7 @@ func TestLoadOnlineApiClientCfg(t *testing.T) {
|
|||
{
|
||||
name: "invalid configuration",
|
||||
input: &OnlineApiClientCfg{
|
||||
CredentialsFilePath: "./tests/bad_lapi-secrets.yaml",
|
||||
CredentialsFilePath: "./testdata/bad_lapi-secrets.yaml",
|
||||
},
|
||||
expected: &ApiCredentialsCfg{},
|
||||
expectedErr: "failed unmarshaling api server credentials",
|
||||
|
@ -107,14 +107,14 @@ func TestLoadOnlineApiClientCfg(t *testing.T) {
|
|||
{
|
||||
name: "missing field configuration",
|
||||
input: &OnlineApiClientCfg{
|
||||
CredentialsFilePath: "./tests/bad_online-api-secrets.yaml",
|
||||
CredentialsFilePath: "./testdata/bad_online-api-secrets.yaml",
|
||||
},
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "invalid configuration filepath",
|
||||
input: &OnlineApiClientCfg{
|
||||
CredentialsFilePath: "./tests/nonexist_online-api-secrets.yaml",
|
||||
CredentialsFilePath: "./testdata/nonexist_online-api-secrets.yaml",
|
||||
},
|
||||
expected: &ApiCredentialsCfg{},
|
||||
expectedErr: "failed to read api server credentials",
|
||||
|
@ -137,27 +137,23 @@ func TestLoadOnlineApiClientCfg(t *testing.T) {
|
|||
|
||||
func TestLoadAPIServer(t *testing.T) {
|
||||
tmpLAPI := &LocalApiServerCfg{
|
||||
ProfilesPath: "./tests/profiles.yaml",
|
||||
}
|
||||
if err := tmpLAPI.LoadProfiles(); err != nil {
|
||||
t.Fatalf("loading tmp profiles: %+v", err)
|
||||
ProfilesPath: "./testdata/profiles.yaml",
|
||||
}
|
||||
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
|
||||
config := &Config{}
|
||||
fcontent, err := os.ReadFile("./tests/config.yaml")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fcontent, err := os.ReadFile("./testdata/config.yaml")
|
||||
require.NoError(t, err)
|
||||
|
||||
configData := os.ExpandEnv(string(fcontent))
|
||||
err = yaml.UnmarshalStrict([]byte(configData), &config)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input *Config
|
||||
|
@ -172,18 +168,18 @@ func TestLoadAPIServer(t *testing.T) {
|
|||
Server: &LocalApiServerCfg{
|
||||
ListenURI: "http://crowdsec.api",
|
||||
OnlineClient: &OnlineApiClientCfg{
|
||||
CredentialsFilePath: "./tests/online-api-secrets.yaml",
|
||||
CredentialsFilePath: "./testdata/online-api-secrets.yaml",
|
||||
},
|
||||
ProfilesPath: "./tests/profiles.yaml",
|
||||
ProfilesPath: "./testdata/profiles.yaml",
|
||||
PapiLogLevel: &logLevel,
|
||||
},
|
||||
},
|
||||
DbConfig: &DatabaseCfg{
|
||||
Type: "sqlite",
|
||||
DbPath: "./tests/test.db",
|
||||
DbPath: "./testdata/test.db",
|
||||
},
|
||||
Common: &CommonCfg{
|
||||
LogDir: "./tests/",
|
||||
LogDir: "./testdata/",
|
||||
LogMedia: "stdout",
|
||||
},
|
||||
DisableAPI: false,
|
||||
|
@ -193,9 +189,10 @@ func TestLoadAPIServer(t *testing.T) {
|
|||
ListenURI: "http://crowdsec.api",
|
||||
TLS: nil,
|
||||
DbConfig: &DatabaseCfg{
|
||||
DbPath: "./tests/test.db",
|
||||
Type: "sqlite",
|
||||
MaxOpenConns: ptr.Of(DEFAULT_MAX_OPEN_CONNS),
|
||||
DbPath: "./testdata/test.db",
|
||||
Type: "sqlite",
|
||||
MaxOpenConns: ptr.Of(DEFAULT_MAX_OPEN_CONNS),
|
||||
DecisionBulkSize: defaultDecisionBulkSize,
|
||||
},
|
||||
ConsoleConfigPath: DefaultConfigPath("console.yaml"),
|
||||
ConsoleConfig: &ConsoleConfig{
|
||||
|
@ -208,7 +205,7 @@ func TestLoadAPIServer(t *testing.T) {
|
|||
LogDir: LogDirFullPath,
|
||||
LogMedia: "stdout",
|
||||
OnlineClient: &OnlineApiClientCfg{
|
||||
CredentialsFilePath: "./tests/online-api-secrets.yaml",
|
||||
CredentialsFilePath: "./testdata/online-api-secrets.yaml",
|
||||
Credentials: &ApiCredentialsCfg{
|
||||
URL: "http://crowdsec.api",
|
||||
Login: "test",
|
||||
|
@ -216,7 +213,7 @@ func TestLoadAPIServer(t *testing.T) {
|
|||
},
|
||||
},
|
||||
Profiles: tmpLAPI.Profiles,
|
||||
ProfilesPath: "./tests/profiles.yaml",
|
||||
ProfilesPath: "./testdata/profiles.yaml",
|
||||
UseForwardedForHeaders: false,
|
||||
PapiLogLevel: &logLevel,
|
||||
},
|
||||
|
@ -229,7 +226,7 @@ func TestLoadAPIServer(t *testing.T) {
|
|||
Server: &LocalApiServerCfg{},
|
||||
},
|
||||
Common: &CommonCfg{
|
||||
LogDir: "./tests/",
|
||||
LogDir: "./testdata/",
|
||||
LogMedia: "stdout",
|
||||
},
|
||||
DisableAPI: false,
|
||||
|
@ -242,30 +239,23 @@ func TestLoadAPIServer(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
for idx, test := range tests {
|
||||
err := test.input.LoadAPIServer()
|
||||
if err == nil && test.expectedErr != "" {
|
||||
fmt.Printf("TEST '%s': NOK\n", test.name)
|
||||
t.Fatalf("Test number %d/%d expected error, didn't get it", idx+1, len(tests))
|
||||
} else if test.expectedErr != "" {
|
||||
fmt.Printf("ERR: %+v\n", err)
|
||||
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))
|
||||
for _, tc := range tests {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.input.LoadAPIServer()
|
||||
cstest.RequireErrorContains(t, err, tc.expectedErr)
|
||||
if tc.expectedErr != "" {
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("MustParseCIDR: parsing %q: %v", s, err))
|
||||
}
|
||||
require.NoError(t, err)
|
||||
return ipNet
|
||||
}
|
||||
|
||||
|
@ -306,7 +296,7 @@ func TestParseCapiWhitelists(t *testing.T) {
|
|||
input: `{"cidrs": ["1.2.3.0/24"]}`,
|
||||
expected: &CapiWhitelist{
|
||||
Ips: []net.IP{},
|
||||
Cidrs: []*net.IPNet{mustParseCIDRNet("1.2.3.0/24")},
|
||||
Cidrs: []*net.IPNet{mustParseCIDRNet(t, "1.2.3.0/24")},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
|
@ -1,44 +1,41 @@
|
|||
package csconfig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/crowdsecurity/go-cs-lib/cstest"
|
||||
)
|
||||
|
||||
func TestLoadCommon(t *testing.T) {
|
||||
pidDirPath := "./tests"
|
||||
LogDirFullPath, err := filepath.Abs("./tests/log/")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pidDirPath := "./testdata"
|
||||
LogDirFullPath, err := filepath.Abs("./testdata/log/")
|
||||
require.NoError(t, err)
|
||||
|
||||
WorkingDirFullPath, err := filepath.Abs("./tests")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
WorkingDirFullPath, err := filepath.Abs("./testdata")
|
||||
require.NoError(t, err)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
Input *Config
|
||||
expectedResult *CommonCfg
|
||||
err string
|
||||
name string
|
||||
input *Config
|
||||
expected *CommonCfg
|
||||
expectedErr string
|
||||
}{
|
||||
{
|
||||
name: "basic valid configuration",
|
||||
Input: &Config{
|
||||
input: &Config{
|
||||
Common: &CommonCfg{
|
||||
Daemonize: true,
|
||||
PidDir: "./tests",
|
||||
PidDir: "./testdata",
|
||||
LogMedia: "file",
|
||||
LogDir: "./tests/log/",
|
||||
WorkingDir: "./tests/",
|
||||
LogDir: "./testdata/log/",
|
||||
WorkingDir: "./testdata/",
|
||||
},
|
||||
},
|
||||
expectedResult: &CommonCfg{
|
||||
expected: &CommonCfg{
|
||||
Daemonize: true,
|
||||
PidDir: pidDirPath,
|
||||
LogMedia: "file",
|
||||
|
@ -48,15 +45,15 @@ func TestLoadCommon(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "empty working dir",
|
||||
Input: &Config{
|
||||
input: &Config{
|
||||
Common: &CommonCfg{
|
||||
Daemonize: true,
|
||||
PidDir: "./tests",
|
||||
PidDir: "./testdata",
|
||||
LogMedia: "file",
|
||||
LogDir: "./tests/log/",
|
||||
LogDir: "./testdata/log/",
|
||||
},
|
||||
},
|
||||
expectedResult: &CommonCfg{
|
||||
expected: &CommonCfg{
|
||||
Daemonize: true,
|
||||
PidDir: pidDirPath,
|
||||
LogMedia: "file",
|
||||
|
@ -64,31 +61,23 @@ func TestLoadCommon(t *testing.T) {
|
|||
},
|
||||
},
|
||||
{
|
||||
name: "no common",
|
||||
Input: &Config{},
|
||||
expectedResult: nil,
|
||||
name: "no common",
|
||||
input: &Config{},
|
||||
expected: nil,
|
||||
expectedErr: "no common block provided in configuration file",
|
||||
},
|
||||
}
|
||||
|
||||
for idx, test := range tests {
|
||||
err := test.Input.LoadCommon()
|
||||
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))
|
||||
for _, tc := range tests {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.input.LoadCommon()
|
||||
cstest.RequireErrorContains(t, err, tc.expectedErr)
|
||||
if tc.expectedErr != "" {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
isOk := assert.Equal(t, test.expectedResult, test.Input.Common)
|
||||
if !isOk {
|
||||
t.Fatalf("TEST '%s': NOK", test.name)
|
||||
} else {
|
||||
fmt.Printf("TEST '%s': OK\n", test.name)
|
||||
}
|
||||
assert.Equal(t, tc.expected, tc.input.Common)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// Package csconfig contains the configuration structures for crowdsec and cscli.
|
||||
|
||||
package csconfig
|
||||
|
||||
import (
|
||||
|
@ -37,15 +39,6 @@ type Config struct {
|
|||
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) {
|
||||
patcher := yamlpatch.NewPatcher(configFile, ".local")
|
||||
patcher.SetQuiet(quiet)
|
||||
|
|
|
@ -5,42 +5,43 @@ import (
|
|||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gopkg.in/yaml.v2"
|
||||
|
||||
"github.com/crowdsecurity/go-cs-lib/cstest"
|
||||
)
|
||||
|
||||
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)
|
||||
|
||||
_, _, err = NewConfig("./tests/xxx.yaml", false, false, false)
|
||||
assert.EqualError(t, err, "while reading yaml file: open ./tests/xxx.yaml: "+cstest.FileNotFoundMessage)
|
||||
_, _, err = NewConfig("./testdata/xxx.yaml", false, false, false)
|
||||
assert.EqualError(t, err, "while reading yaml file: open ./testdata/xxx.yaml: "+cstest.FileNotFoundMessage)
|
||||
|
||||
_, _, err = NewConfig("./tests/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")
|
||||
_, _, err = NewConfig("./testdata/simulation.yaml", false, false, false)
|
||||
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) {
|
||||
tests := []struct {
|
||||
name string
|
||||
expectedResult *Config
|
||||
name string
|
||||
expected *Config
|
||||
}{
|
||||
{
|
||||
name: "new configuration: basic",
|
||||
expectedResult: &Config{},
|
||||
name: "new configuration: basic",
|
||||
expected: &Config{},
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := &Config{}
|
||||
assert.Equal(t, tc.expectedResult, result)
|
||||
assert.Equal(t, tc.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultConfig(t *testing.T) {
|
||||
x := NewDefaultConfig()
|
||||
err := x.Dump()
|
||||
require.NoError(t, err)
|
||||
_, err := yaml.Marshal(x)
|
||||
require.NoError(t, err, "failed marshaling config: %s", err)
|
||||
}
|
||||
|
|
|
@ -82,23 +82,3 @@ func (c *LocalApiServerCfg) LoadConsoleConfig() error {
|
|||
|
||||
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
|
||||
}
|
||||
|
|
|
@ -1,24 +1,23 @@
|
|||
package csconfig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/crowdsecurity/go-cs-lib/cstest"
|
||||
"github.com/crowdsecurity/go-cs-lib/ptr"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestLoadCrowdsec(t *testing.T) {
|
||||
acquisFullPath, err := filepath.Abs("./tests/acquis.yaml")
|
||||
acquisFullPath, err := filepath.Abs("./testdata/acquis.yaml")
|
||||
require.NoError(t, err)
|
||||
|
||||
acquisInDirFullPath, err := filepath.Abs("./tests/acquis/acquis.yaml")
|
||||
acquisInDirFullPath, err := filepath.Abs("./testdata/acquis/acquis.yaml")
|
||||
require.NoError(t, err)
|
||||
|
||||
acquisDirFullPath, err := filepath.Abs("./tests/acquis")
|
||||
acquisDirFullPath, err := filepath.Abs("./testdata/acquis")
|
||||
require.NoError(t, err)
|
||||
|
||||
hubFullPath, err := filepath.Abs("./hub")
|
||||
|
@ -27,42 +26,42 @@ func TestLoadCrowdsec(t *testing.T) {
|
|||
dataFullPath, err := filepath.Abs("./data")
|
||||
require.NoError(t, err)
|
||||
|
||||
configDirFullPath, err := filepath.Abs("./tests")
|
||||
configDirFullPath, err := filepath.Abs("./testdata")
|
||||
require.NoError(t, err)
|
||||
|
||||
hubIndexFileFullPath, err := filepath.Abs("./hub/.index.json")
|
||||
require.NoError(t, err)
|
||||
|
||||
contextFileFullPath, err := filepath.Abs("./tests/context.yaml")
|
||||
contextFileFullPath, err := filepath.Abs("./testdata/context.yaml")
|
||||
require.NoError(t, err)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input *Config
|
||||
expectedResult *CrowdsecServiceCfg
|
||||
expectedErr string
|
||||
name string
|
||||
input *Config
|
||||
expected *CrowdsecServiceCfg
|
||||
expectedErr string
|
||||
}{
|
||||
{
|
||||
name: "basic valid configuration",
|
||||
input: &Config{
|
||||
ConfigPaths: &ConfigurationPaths{
|
||||
ConfigDir: "./tests",
|
||||
ConfigDir: "./testdata",
|
||||
DataDir: "./data",
|
||||
HubDir: "./hub",
|
||||
},
|
||||
API: &APICfg{
|
||||
Client: &LocalApiClientCfg{
|
||||
CredentialsFilePath: "./tests/lapi-secrets.yaml",
|
||||
CredentialsFilePath: "./testdata/lapi-secrets.yaml",
|
||||
},
|
||||
},
|
||||
Crowdsec: &CrowdsecServiceCfg{
|
||||
AcquisitionFilePath: "./tests/acquis.yaml",
|
||||
SimulationFilePath: "./tests/simulation.yaml",
|
||||
ConsoleContextPath: "./tests/context.yaml",
|
||||
AcquisitionFilePath: "./testdata/acquis.yaml",
|
||||
SimulationFilePath: "./testdata/simulation.yaml",
|
||||
ConsoleContextPath: "./testdata/context.yaml",
|
||||
ConsoleContextValueLength: 2500,
|
||||
},
|
||||
},
|
||||
expectedResult: &CrowdsecServiceCfg{
|
||||
expected: &CrowdsecServiceCfg{
|
||||
Enable: ptr.Of(true),
|
||||
AcquisitionDirPath: "",
|
||||
ConsoleContextPath: contextFileFullPath,
|
||||
|
@ -76,7 +75,7 @@ func TestLoadCrowdsec(t *testing.T) {
|
|||
OutputRoutinesCount: 1,
|
||||
ConsoleContextValueLength: 2500,
|
||||
AcquisitionFiles: []string{acquisFullPath},
|
||||
SimulationFilePath: "./tests/simulation.yaml",
|
||||
SimulationFilePath: "./testdata/simulation.yaml",
|
||||
ContextToSend: map[string][]string{
|
||||
"source_ip": {"evt.Parsed.source_ip"},
|
||||
},
|
||||
|
@ -89,23 +88,23 @@ func TestLoadCrowdsec(t *testing.T) {
|
|||
name: "basic valid configuration with acquisition dir",
|
||||
input: &Config{
|
||||
ConfigPaths: &ConfigurationPaths{
|
||||
ConfigDir: "./tests",
|
||||
ConfigDir: "./testdata",
|
||||
DataDir: "./data",
|
||||
HubDir: "./hub",
|
||||
},
|
||||
API: &APICfg{
|
||||
Client: &LocalApiClientCfg{
|
||||
CredentialsFilePath: "./tests/lapi-secrets.yaml",
|
||||
CredentialsFilePath: "./testdata/lapi-secrets.yaml",
|
||||
},
|
||||
},
|
||||
Crowdsec: &CrowdsecServiceCfg{
|
||||
AcquisitionFilePath: "./tests/acquis.yaml",
|
||||
AcquisitionDirPath: "./tests/acquis/",
|
||||
SimulationFilePath: "./tests/simulation.yaml",
|
||||
ConsoleContextPath: "./tests/context.yaml",
|
||||
AcquisitionFilePath: "./testdata/acquis.yaml",
|
||||
AcquisitionDirPath: "./testdata/acquis/",
|
||||
SimulationFilePath: "./testdata/simulation.yaml",
|
||||
ConsoleContextPath: "./testdata/context.yaml",
|
||||
},
|
||||
},
|
||||
expectedResult: &CrowdsecServiceCfg{
|
||||
expected: &CrowdsecServiceCfg{
|
||||
Enable: ptr.Of(true),
|
||||
AcquisitionDirPath: acquisDirFullPath,
|
||||
AcquisitionFilePath: acquisFullPath,
|
||||
|
@ -122,7 +121,7 @@ func TestLoadCrowdsec(t *testing.T) {
|
|||
ContextToSend: map[string][]string{
|
||||
"source_ip": {"evt.Parsed.source_ip"},
|
||||
},
|
||||
SimulationFilePath: "./tests/simulation.yaml",
|
||||
SimulationFilePath: "./testdata/simulation.yaml",
|
||||
SimulationConfig: &SimulationConfig{
|
||||
Simulation: ptr.Of(false),
|
||||
},
|
||||
|
@ -132,13 +131,13 @@ func TestLoadCrowdsec(t *testing.T) {
|
|||
name: "no acquisition file and dir",
|
||||
input: &Config{
|
||||
ConfigPaths: &ConfigurationPaths{
|
||||
ConfigDir: "./tests",
|
||||
ConfigDir: "./testdata",
|
||||
DataDir: "./data",
|
||||
HubDir: "./hub",
|
||||
},
|
||||
API: &APICfg{
|
||||
Client: &LocalApiClientCfg{
|
||||
CredentialsFilePath: "./tests/lapi-secrets.yaml",
|
||||
CredentialsFilePath: "./testdata/lapi-secrets.yaml",
|
||||
},
|
||||
},
|
||||
Crowdsec: &CrowdsecServiceCfg{
|
||||
|
@ -146,7 +145,7 @@ func TestLoadCrowdsec(t *testing.T) {
|
|||
ConsoleContextValueLength: 10,
|
||||
},
|
||||
},
|
||||
expectedResult: &CrowdsecServiceCfg{
|
||||
expected: &CrowdsecServiceCfg{
|
||||
Enable: ptr.Of(true),
|
||||
AcquisitionDirPath: "",
|
||||
AcquisitionFilePath: "",
|
||||
|
@ -173,18 +172,18 @@ func TestLoadCrowdsec(t *testing.T) {
|
|||
name: "non existing acquisition file",
|
||||
input: &Config{
|
||||
ConfigPaths: &ConfigurationPaths{
|
||||
ConfigDir: "./tests",
|
||||
ConfigDir: "./testdata",
|
||||
DataDir: "./data",
|
||||
HubDir: "./hub",
|
||||
},
|
||||
API: &APICfg{
|
||||
Client: &LocalApiClientCfg{
|
||||
CredentialsFilePath: "./tests/lapi-secrets.yaml",
|
||||
CredentialsFilePath: "./testdata/lapi-secrets.yaml",
|
||||
},
|
||||
},
|
||||
Crowdsec: &CrowdsecServiceCfg{
|
||||
ConsoleContextPath: "",
|
||||
AcquisitionFilePath: "./tests/acquis_not_exist.yaml",
|
||||
AcquisitionFilePath: "./testdata/acquis_not_exist.yaml",
|
||||
},
|
||||
},
|
||||
expectedErr: cstest.FileNotFoundMessage,
|
||||
|
@ -193,26 +192,25 @@ func TestLoadCrowdsec(t *testing.T) {
|
|||
name: "agent disabled",
|
||||
input: &Config{
|
||||
ConfigPaths: &ConfigurationPaths{
|
||||
ConfigDir: "./tests",
|
||||
ConfigDir: "./testdata",
|
||||
DataDir: "./data",
|
||||
HubDir: "./hub",
|
||||
},
|
||||
},
|
||||
expectedResult: nil,
|
||||
expected: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
fmt.Printf("TEST '%s'\n", tc.name)
|
||||
err := tc.input.LoadCrowdsec()
|
||||
cstest.RequireErrorContains(t, err, tc.expectedErr)
|
||||
if tc.expectedErr != "" {
|
||||
return
|
||||
}
|
||||
|
||||
require.Equal(t, tc.expectedResult, tc.input.Crowdsec)
|
||||
require.Equal(t, tc.expected, tc.input.Crowdsec)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,52 +1,45 @@
|
|||
package csconfig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/crowdsecurity/go-cs-lib/cstest"
|
||||
)
|
||||
|
||||
func TestLoadCSCLI(t *testing.T) {
|
||||
hubFullPath, err := filepath.Abs("./hub")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
dataFullPath, err := filepath.Abs("./data")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
configDirFullPath, err := filepath.Abs("./tests")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
configDirFullPath, err := filepath.Abs("./testdata")
|
||||
require.NoError(t, err)
|
||||
|
||||
hubIndexFileFullPath, err := filepath.Abs("./hub/.index.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
Input *Config
|
||||
expectedResult *CscliCfg
|
||||
err string
|
||||
name string
|
||||
input *Config
|
||||
expected *CscliCfg
|
||||
expectedErr string
|
||||
}{
|
||||
{
|
||||
name: "basic valid configuration",
|
||||
Input: &Config{
|
||||
input: &Config{
|
||||
ConfigPaths: &ConfigurationPaths{
|
||||
ConfigDir: "./tests",
|
||||
ConfigDir: "./testdata",
|
||||
DataDir: "./data",
|
||||
HubDir: "./hub",
|
||||
HubIndexFile: "./hub/.index.json",
|
||||
},
|
||||
},
|
||||
expectedResult: &CscliCfg{
|
||||
expected: &CscliCfg{
|
||||
ConfigDir: configDirFullPath,
|
||||
DataDir: dataFullPath,
|
||||
HubDir: hubFullPath,
|
||||
|
@ -54,31 +47,23 @@ func TestLoadCSCLI(t *testing.T) {
|
|||
},
|
||||
},
|
||||
{
|
||||
name: "no configuration path",
|
||||
Input: &Config{},
|
||||
expectedResult: &CscliCfg{},
|
||||
name: "no configuration path",
|
||||
input: &Config{},
|
||||
expected: &CscliCfg{},
|
||||
expectedErr: "no configuration paths provided",
|
||||
},
|
||||
}
|
||||
|
||||
for idx, test := range tests {
|
||||
err := test.Input.LoadCSCLI()
|
||||
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))
|
||||
for _, tc := range tests {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.input.LoadCSCLI()
|
||||
cstest.RequireErrorContains(t, err, tc.expectedErr)
|
||||
if tc.expectedErr != "" {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
isOk := assert.Equal(t, test.expectedResult, test.Input.Cscli)
|
||||
if !isOk {
|
||||
t.Fatalf("TEST '%s': NOK", test.name)
|
||||
} else {
|
||||
fmt.Printf("TEST '%s': OK\n", test.name)
|
||||
}
|
||||
assert.Equal(t, tc.expected, tc.input.Cscli)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,13 +10,12 @@ import (
|
|||
"github.com/crowdsecurity/go-cs-lib/ptr"
|
||||
)
|
||||
|
||||
var DEFAULT_MAX_OPEN_CONNS = 100
|
||||
|
||||
const (
|
||||
DEFAULT_MAX_OPEN_CONNS = 100
|
||||
defaultDecisionBulkSize = 1000
|
||||
// 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
|
||||
maxDecisionBulkSize = 2000
|
||||
maxDecisionBulkSize = 2000
|
||||
)
|
||||
|
||||
type DatabaseCfg struct {
|
||||
|
|
|
@ -1,28 +1,27 @@
|
|||
package csconfig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/crowdsecurity/go-cs-lib/cstest"
|
||||
"github.com/crowdsecurity/go-cs-lib/ptr"
|
||||
)
|
||||
|
||||
func TestLoadDBConfig(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
Input *Config
|
||||
expectedResult *DatabaseCfg
|
||||
err string
|
||||
name string
|
||||
input *Config
|
||||
expected *DatabaseCfg
|
||||
expectedErr string
|
||||
}{
|
||||
{
|
||||
name: "basic valid configuration",
|
||||
Input: &Config{
|
||||
input: &Config{
|
||||
DbConfig: &DatabaseCfg{
|
||||
Type: "sqlite",
|
||||
DbPath: "./tests/test.db",
|
||||
DbPath: "./testdata/test.db",
|
||||
MaxOpenConns: ptr.Of(10),
|
||||
},
|
||||
Cscli: &CscliCfg{},
|
||||
|
@ -30,38 +29,31 @@ func TestLoadDBConfig(t *testing.T) {
|
|||
Server: &LocalApiServerCfg{},
|
||||
},
|
||||
},
|
||||
expectedResult: &DatabaseCfg{
|
||||
Type: "sqlite",
|
||||
DbPath: "./tests/test.db",
|
||||
MaxOpenConns: ptr.Of(10),
|
||||
expected: &DatabaseCfg{
|
||||
Type: "sqlite",
|
||||
DbPath: "./testdata/test.db",
|
||||
MaxOpenConns: ptr.Of(10),
|
||||
DecisionBulkSize: defaultDecisionBulkSize,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "no configuration path",
|
||||
Input: &Config{},
|
||||
expectedResult: nil,
|
||||
name: "no configuration path",
|
||||
input: &Config{},
|
||||
expected: nil,
|
||||
expectedErr: "no database configuration provided",
|
||||
},
|
||||
}
|
||||
|
||||
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))
|
||||
for _, tc := range tests {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.input.LoadDBConfig()
|
||||
cstest.RequireErrorContains(t, err, tc.expectedErr)
|
||||
if tc.expectedErr != "" {
|
||||
return
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
assert.Equal(t, tc.expected, tc.input.DbConfig)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,7 +10,6 @@ import (
|
|||
"github.com/crowdsecurity/crowdsec/pkg/fflag"
|
||||
)
|
||||
|
||||
|
||||
// LoadFeatureFlagsEnv parses the environment variables to enable feature flags.
|
||||
func LoadFeatureFlagsEnv(logger *log.Logger) error {
|
||||
if err := fflag.Crowdsec.SetFromEnv(logger); err != nil {
|
||||
|
@ -19,7 +18,6 @@ func LoadFeatureFlagsEnv(logger *log.Logger) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
|
||||
// LoadFeatureFlags parses feature.yaml to enable feature flags.
|
||||
// The file is in the same directory as config.yaml, which is provided
|
||||
// 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
|
||||
}
|
||||
|
||||
|
||||
// ListFeatureFlags returns a list of the enabled feature flags.
|
||||
func ListFeatureFlags() string {
|
||||
enabledFeatures := fflag.Crowdsec.GetEnabledFeatures()
|
||||
|
|
|
@ -1,52 +1,45 @@
|
|||
package csconfig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/crowdsecurity/go-cs-lib/cstest"
|
||||
)
|
||||
|
||||
func TestLoadHub(t *testing.T) {
|
||||
hubFullPath, err := filepath.Abs("./hub")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
dataFullPath, err := filepath.Abs("./data")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
configDirFullPath, err := filepath.Abs("./tests")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
configDirFullPath, err := filepath.Abs("./testdata")
|
||||
require.NoError(t, err)
|
||||
|
||||
hubIndexFileFullPath, err := filepath.Abs("./hub/.index.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
Input *Config
|
||||
expectedResult *Hub
|
||||
err string
|
||||
name string
|
||||
input *Config
|
||||
expected *Hub
|
||||
expectedErr string
|
||||
}{
|
||||
{
|
||||
name: "basic valid configuration",
|
||||
Input: &Config{
|
||||
input: &Config{
|
||||
ConfigPaths: &ConfigurationPaths{
|
||||
ConfigDir: "./tests",
|
||||
ConfigDir: "./testdata",
|
||||
DataDir: "./data",
|
||||
HubDir: "./hub",
|
||||
HubIndexFile: "./hub/.index.json",
|
||||
},
|
||||
},
|
||||
expectedResult: &Hub{
|
||||
expected: &Hub{
|
||||
ConfigDir: configDirFullPath,
|
||||
DataDir: dataFullPath,
|
||||
HubDir: hubFullPath,
|
||||
|
@ -55,40 +48,32 @@ func TestLoadHub(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "no data dir",
|
||||
Input: &Config{
|
||||
input: &Config{
|
||||
ConfigPaths: &ConfigurationPaths{
|
||||
ConfigDir: "./tests",
|
||||
ConfigDir: "./testdata",
|
||||
HubDir: "./hub",
|
||||
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",
|
||||
Input: &Config{},
|
||||
expectedResult: nil,
|
||||
name: "no configuration path",
|
||||
input: &Config{},
|
||||
expectedErr: "no configuration paths provided",
|
||||
},
|
||||
}
|
||||
|
||||
for idx, test := range tests {
|
||||
err := test.Input.LoadHub()
|
||||
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))
|
||||
for _, tc := range tests {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.input.LoadHub()
|
||||
cstest.RequireErrorContains(t, err, tc.expectedErr)
|
||||
if tc.expectedErr != "" {
|
||||
return
|
||||
}
|
||||
}
|
||||
isOk := assert.Equal(t, test.expectedResult, test.Input.Hub)
|
||||
if !isOk {
|
||||
t.Fatalf("TEST '%s': NOK", test.name)
|
||||
} else {
|
||||
fmt.Printf("TEST '%s': OK\n", test.name)
|
||||
}
|
||||
|
||||
assert.Equal(t, tc.expected, tc.input.Hub)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,10 +6,11 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
|
||||
"github.com/crowdsecurity/go-cs-lib/yamlpatch"
|
||||
|
||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// var OnErrorDefault = OnErrorIgnore
|
||||
|
@ -43,7 +44,6 @@ func (c *LocalApiServerCfg) LoadProfiles() error {
|
|||
}
|
||||
reader := bytes.NewReader(fcontent)
|
||||
|
||||
//process the yaml
|
||||
dec := yaml.NewDecoder(reader)
|
||||
dec.SetStrict(true)
|
||||
for {
|
||||
|
|
|
@ -3,21 +3,21 @@ package csconfig
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/crowdsecurity/go-cs-lib/cstest"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/crowdsecurity/go-cs-lib/cstest"
|
||||
)
|
||||
|
||||
func TestLoadPrometheus(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
Input *Config
|
||||
input *Config
|
||||
expectedURL string
|
||||
expectedErr string
|
||||
}{
|
||||
{
|
||||
name: "basic valid configuration",
|
||||
Input: &Config{
|
||||
input: &Config{
|
||||
Prometheus: &PrometheusCfg{
|
||||
Enabled: true,
|
||||
Level: "full",
|
||||
|
@ -33,10 +33,10 @@ func TestLoadPrometheus(t *testing.T) {
|
|||
for _, tc := range tests {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.Input.LoadPrometheus()
|
||||
err := tc.input.LoadPrometheus()
|
||||
cstest.RequireErrorContains(t, err, tc.expectedErr)
|
||||
|
||||
require.Equal(t, tc.expectedURL, tc.Input.Cscli.PrometheusUrl)
|
||||
require.Equal(t, tc.expectedURL, tc.input.Cscli.PrometheusUrl)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,8 +4,9 @@ import (
|
|||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/crowdsecurity/go-cs-lib/yamlpatch"
|
||||
"gopkg.in/yaml.v2"
|
||||
|
||||
"github.com/crowdsecurity/go-cs-lib/yamlpatch"
|
||||
)
|
||||
|
||||
type SimulationConfig struct {
|
||||
|
|
|
@ -12,46 +12,46 @@ import (
|
|||
)
|
||||
|
||||
func TestSimulationLoading(t *testing.T) {
|
||||
testXXFullPath, err := filepath.Abs("./tests/xxx.yaml")
|
||||
testXXFullPath, err := filepath.Abs("./testdata/xxx.yaml")
|
||||
require.NoError(t, err)
|
||||
|
||||
badYamlFullPath, err := filepath.Abs("./tests/config.yaml")
|
||||
badYamlFullPath, err := filepath.Abs("./testdata/config.yaml")
|
||||
require.NoError(t, err)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
Input *Config
|
||||
expectedResult *SimulationConfig
|
||||
expectedErr string
|
||||
name string
|
||||
input *Config
|
||||
expected *SimulationConfig
|
||||
expectedErr string
|
||||
}{
|
||||
{
|
||||
name: "basic valid simulation",
|
||||
Input: &Config{
|
||||
input: &Config{
|
||||
ConfigPaths: &ConfigurationPaths{
|
||||
SimulationFilePath: "./tests/simulation.yaml",
|
||||
SimulationFilePath: "./testdata/simulation.yaml",
|
||||
DataDir: "./data",
|
||||
},
|
||||
Crowdsec: &CrowdsecServiceCfg{},
|
||||
Cscli: &CscliCfg{},
|
||||
},
|
||||
expectedResult: &SimulationConfig{Simulation: new(bool)},
|
||||
expected: &SimulationConfig{Simulation: new(bool)},
|
||||
},
|
||||
{
|
||||
name: "basic nil config",
|
||||
Input: &Config{
|
||||
input: &Config{
|
||||
ConfigPaths: &ConfigurationPaths{
|
||||
SimulationFilePath: "",
|
||||
DataDir: "./data",
|
||||
},
|
||||
Crowdsec: &CrowdsecServiceCfg{},
|
||||
},
|
||||
expectedErr: "simulation.yaml: "+cstest.FileNotFoundMessage,
|
||||
expectedErr: "simulation.yaml: " + cstest.FileNotFoundMessage,
|
||||
},
|
||||
{
|
||||
name: "basic bad file name",
|
||||
Input: &Config{
|
||||
input: &Config{
|
||||
ConfigPaths: &ConfigurationPaths{
|
||||
SimulationFilePath: "./tests/xxx.yaml",
|
||||
SimulationFilePath: "./testdata/xxx.yaml",
|
||||
DataDir: "./data",
|
||||
},
|
||||
Crowdsec: &CrowdsecServiceCfg{},
|
||||
|
@ -60,9 +60,9 @@ func TestSimulationLoading(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "basic bad file content",
|
||||
Input: &Config{
|
||||
input: &Config{
|
||||
ConfigPaths: &ConfigurationPaths{
|
||||
SimulationFilePath: "./tests/config.yaml",
|
||||
SimulationFilePath: "./testdata/config.yaml",
|
||||
DataDir: "./data",
|
||||
},
|
||||
Crowdsec: &CrowdsecServiceCfg{},
|
||||
|
@ -71,9 +71,9 @@ func TestSimulationLoading(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "basic bad file content",
|
||||
Input: &Config{
|
||||
input: &Config{
|
||||
ConfigPaths: &ConfigurationPaths{
|
||||
SimulationFilePath: "./tests/config.yaml",
|
||||
SimulationFilePath: "./testdata/config.yaml",
|
||||
DataDir: "./data",
|
||||
},
|
||||
Crowdsec: &CrowdsecServiceCfg{},
|
||||
|
@ -85,10 +85,10 @@ func TestSimulationLoading(t *testing.T) {
|
|||
for _, tc := range tests {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.Input.LoadSimulation()
|
||||
err := tc.input.LoadSimulation()
|
||||
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
|
||||
SimulationConfig *SimulationConfig
|
||||
Input string
|
||||
expectedResult bool
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "No simulation except (in exclusion)",
|
||||
SimulationConfig: simCfgOff,
|
||||
Input: "test",
|
||||
expectedResult: true,
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "All simulation (not in exclusion)",
|
||||
SimulationConfig: simCfgOn,
|
||||
Input: "toto",
|
||||
expectedResult: true,
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "All simulation (in exclusion)",
|
||||
SimulationConfig: simCfgOn,
|
||||
Input: "test",
|
||||
expectedResult: false,
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
IsSimulated := tc.SimulationConfig.IsSimulated(tc.Input)
|
||||
require.Equal(t, tc.expectedResult, IsSimulated)
|
||||
isSimulated := tc.SimulationConfig.IsSimulated(tc.Input)
|
||||
require.Equal(t, tc.expected, isSimulated)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ prometheus:
|
|||
enabled: true
|
||||
level: full
|
||||
crowdsec_service:
|
||||
acquisition_path: ./tests/acquis.yaml
|
||||
acquisition_path: ./testdata/acquis.yaml
|
||||
parser_routines: 1
|
||||
cscli:
|
||||
output: human
|
||||
|
@ -21,17 +21,17 @@ db_config:
|
|||
type: sqlite
|
||||
api:
|
||||
client:
|
||||
credentials_path: ./tests/lapi-secrets.yaml
|
||||
credentials_path: ./testdata/lapi-secrets.yaml
|
||||
server:
|
||||
profiles_path: ./tests/profiles.yaml
|
||||
profiles_path: ./testdata/profiles.yaml
|
||||
listen_uri: 127.0.0.1:8080
|
||||
tls: null
|
||||
online_client:
|
||||
credentials_path: ./tests/online-api-secrets.yaml
|
||||
credentials_path: ./testdata/online-api-secrets.yaml
|
||||
config_paths:
|
||||
config_dir: ./tests
|
||||
config_dir: ./testdata
|
||||
data_dir: .
|
||||
simulation_path: ./tests/simulation.yaml
|
||||
index_path: ./tests/hub/.index.json
|
||||
hub_dir: ./tests/hub
|
||||
simulation_path: ./testdata/simulation.yaml
|
||||
index_path: ./testdata/hub/.index.json
|
||||
hub_dir: ./testdata/hub
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue