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