use helpers for shorter tests, add a couple of error cases (#2016)
This commit is contained in:
parent
3fb3decf49
commit
e37d09e5b4
|
@ -1,31 +1,30 @@
|
||||||
package csconfig
|
package csconfig
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
||||||
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLoadLocalApiClientCfg(t *testing.T) {
|
func TestLoadLocalApiClientCfg(t *testing.T) {
|
||||||
True := true
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
Input *LocalApiClientCfg
|
input *LocalApiClientCfg
|
||||||
expectedResult *ApiCredentialsCfg
|
expected *ApiCredentialsCfg
|
||||||
err string
|
expectedErr string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "basic valid configuration",
|
name: "basic valid configuration",
|
||||||
Input: &LocalApiClientCfg{
|
input: &LocalApiClientCfg{
|
||||||
CredentialsFilePath: "./tests/lapi-secrets.yaml",
|
CredentialsFilePath: "./tests/lapi-secrets.yaml",
|
||||||
},
|
},
|
||||||
expectedResult: &ApiCredentialsCfg{
|
expected: &ApiCredentialsCfg{
|
||||||
URL: "http://localhost:8080/",
|
URL: "http://localhost:8080/",
|
||||||
Login: "test",
|
Login: "test",
|
||||||
Password: "testpassword",
|
Password: "testpassword",
|
||||||
|
@ -33,25 +32,27 @@ func TestLoadLocalApiClientCfg(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalid configuration",
|
name: "invalid configuration",
|
||||||
Input: &LocalApiClientCfg{
|
input: &LocalApiClientCfg{
|
||||||
CredentialsFilePath: "./tests/bad_lapi-secrets.yaml",
|
CredentialsFilePath: "./tests/bad_lapi-secrets.yaml",
|
||||||
},
|
},
|
||||||
expectedResult: &ApiCredentialsCfg{},
|
expected: &ApiCredentialsCfg{},
|
||||||
|
expectedErr: "field unknown_key not found in type csconfig.ApiCredentialsCfg",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalid configuration filepath",
|
name: "invalid configuration filepath",
|
||||||
Input: &LocalApiClientCfg{
|
input: &LocalApiClientCfg{
|
||||||
CredentialsFilePath: "./tests/nonexist_lapi-secrets.yaml",
|
CredentialsFilePath: "./tests/nonexist_lapi-secrets.yaml",
|
||||||
},
|
},
|
||||||
expectedResult: nil,
|
expected: nil,
|
||||||
|
expectedErr: "open ./tests/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: "./tests/lapi-secrets.yaml",
|
||||||
InsecureSkipVerify: &True,
|
InsecureSkipVerify: types.BoolPtr(false),
|
||||||
},
|
},
|
||||||
expectedResult: &ApiCredentialsCfg{
|
expected: &ApiCredentialsCfg{
|
||||||
URL: "http://localhost:8080/",
|
URL: "http://localhost:8080/",
|
||||||
Login: "test",
|
Login: "test",
|
||||||
Password: "testpassword",
|
Password: "testpassword",
|
||||||
|
@ -59,40 +60,33 @@ func TestLoadLocalApiClientCfg(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for idx, test := range tests {
|
for _, tc := range tests {
|
||||||
fmt.Printf("TEST '%s'\n", test.name)
|
tc := tc
|
||||||
err := test.Input.Load()
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
if err == nil && test.err != "" {
|
err := tc.input.Load()
|
||||||
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
|
||||||
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.Credentials)
|
|
||||||
if !isOk {
|
|
||||||
t.Fatalf("test '%s' failed", test.name)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
assert.Equal(t, tc.expected, tc.input.Credentials)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadOnlineApiClientCfg(t *testing.T) {
|
func TestLoadOnlineApiClientCfg(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
Input *OnlineApiClientCfg
|
input *OnlineApiClientCfg
|
||||||
expectedResult *ApiCredentialsCfg
|
expected *ApiCredentialsCfg
|
||||||
err string
|
expectedErr string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "basic valid configuration",
|
name: "basic valid configuration",
|
||||||
Input: &OnlineApiClientCfg{
|
input: &OnlineApiClientCfg{
|
||||||
CredentialsFilePath: "./tests/online-api-secrets.yaml",
|
CredentialsFilePath: "./tests/online-api-secrets.yaml",
|
||||||
},
|
},
|
||||||
expectedResult: &ApiCredentialsCfg{
|
expected: &ApiCredentialsCfg{
|
||||||
URL: "http://crowdsec.api",
|
URL: "http://crowdsec.api",
|
||||||
Login: "test",
|
Login: "test",
|
||||||
Password: "testpassword",
|
Password: "testpassword",
|
||||||
|
@ -100,50 +94,40 @@ func TestLoadOnlineApiClientCfg(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalid configuration",
|
name: "invalid configuration",
|
||||||
Input: &OnlineApiClientCfg{
|
input: &OnlineApiClientCfg{
|
||||||
CredentialsFilePath: "./tests/bad_lapi-secrets.yaml",
|
CredentialsFilePath: "./tests/bad_lapi-secrets.yaml",
|
||||||
},
|
},
|
||||||
expectedResult: &ApiCredentialsCfg{},
|
expected: &ApiCredentialsCfg{},
|
||||||
err: "failed unmarshaling api server credentials",
|
expectedErr: "failed unmarshaling api server credentials",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "missing field configuration",
|
name: "missing field configuration",
|
||||||
Input: &OnlineApiClientCfg{
|
input: &OnlineApiClientCfg{
|
||||||
CredentialsFilePath: "./tests/bad_online-api-secrets.yaml",
|
CredentialsFilePath: "./tests/bad_online-api-secrets.yaml",
|
||||||
},
|
},
|
||||||
expectedResult: nil,
|
expected: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalid configuration filepath",
|
name: "invalid configuration filepath",
|
||||||
Input: &OnlineApiClientCfg{
|
input: &OnlineApiClientCfg{
|
||||||
CredentialsFilePath: "./tests/nonexist_online-api-secrets.yaml",
|
CredentialsFilePath: "./tests/nonexist_online-api-secrets.yaml",
|
||||||
},
|
},
|
||||||
expectedResult: &ApiCredentialsCfg{},
|
expected: &ApiCredentialsCfg{},
|
||||||
err: "failed to read api server credentials",
|
expectedErr: "failed to read api server credentials",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for idx, test := range tests {
|
for _, tc := range tests {
|
||||||
err := test.Input.Load()
|
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.Load()
|
||||||
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.Credentials)
|
|
||||||
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.Credentials)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -171,14 +155,14 @@ func TestLoadAPIServer(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
Input *Config
|
input *Config
|
||||||
expectedResult *LocalApiServerCfg
|
expected *LocalApiServerCfg
|
||||||
err string
|
expectedErr string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "basic valid configuration",
|
name: "basic valid configuration",
|
||||||
Input: &Config{
|
input: &Config{
|
||||||
Self: []byte(configData),
|
Self: []byte(configData),
|
||||||
API: &APICfg{
|
API: &APICfg{
|
||||||
Server: &LocalApiServerCfg{
|
Server: &LocalApiServerCfg{
|
||||||
|
@ -199,7 +183,7 @@ func TestLoadAPIServer(t *testing.T) {
|
||||||
},
|
},
|
||||||
DisableAPI: false,
|
DisableAPI: false,
|
||||||
},
|
},
|
||||||
expectedResult: &LocalApiServerCfg{
|
expected: &LocalApiServerCfg{
|
||||||
Enable: types.BoolPtr(true),
|
Enable: types.BoolPtr(true),
|
||||||
ListenURI: "http://crowdsec.api",
|
ListenURI: "http://crowdsec.api",
|
||||||
TLS: nil,
|
TLS: nil,
|
||||||
|
@ -229,11 +213,10 @@ func TestLoadAPIServer(t *testing.T) {
|
||||||
ProfilesPath: "./tests/profiles.yaml",
|
ProfilesPath: "./tests/profiles.yaml",
|
||||||
UseForwardedForHeaders: false,
|
UseForwardedForHeaders: false,
|
||||||
},
|
},
|
||||||
err: "",
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "basic invalid configuration",
|
name: "basic invalid configuration",
|
||||||
Input: &Config{
|
input: &Config{
|
||||||
Self: []byte(configData),
|
Self: []byte(configData),
|
||||||
API: &APICfg{
|
API: &APICfg{
|
||||||
Server: &LocalApiServerCfg{},
|
Server: &LocalApiServerCfg{},
|
||||||
|
@ -244,35 +227,25 @@ func TestLoadAPIServer(t *testing.T) {
|
||||||
},
|
},
|
||||||
DisableAPI: false,
|
DisableAPI: false,
|
||||||
},
|
},
|
||||||
expectedResult: &LocalApiServerCfg{
|
expected: &LocalApiServerCfg{
|
||||||
Enable: types.BoolPtr(true),
|
Enable: types.BoolPtr(true),
|
||||||
LogDir: LogDirFullPath,
|
LogDir: LogDirFullPath,
|
||||||
LogMedia: "stdout",
|
LogMedia: "stdout",
|
||||||
},
|
},
|
||||||
err: "while loading profiles for LAPI",
|
expectedErr: "while loading profiles for LAPI",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for idx, test := range tests {
|
for _, tc := range tests {
|
||||||
err := test.Input.LoadAPIServer()
|
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.LoadAPIServer()
|
||||||
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.API.Server)
|
|
||||||
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.API.Server)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue