* cmd/crowdsec: removed log.Fatal()s, added tests and print error for unrecognized argument * updated golangci-lint to v1.46 * lint/deadcode: fix existing issues * tests: cscli config backup/restore * tests: cscli completion powershell/fish * err check: pflags MarkHidden() * empty .dockerignore (and explain the reason) * tests, errors.Wrap * test for CS_LAPI_SECRET and minor refactoring * minor style changes * log cleanup
95 lines
2 KiB
Go
95 lines
2 KiB
Go
package csconfig
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestLoadHub(t *testing.T) {
|
|
hubFullPath, err := filepath.Abs("./hub")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
dataFullPath, err := filepath.Abs("./data")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
configDirFullPath, err := filepath.Abs("./tests")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
hubIndexFileFullPath, err := filepath.Abs("./hub/.index.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
Input *Config
|
|
expectedResult *Hub
|
|
err string
|
|
}{
|
|
{
|
|
name: "basic valid configuration",
|
|
Input: &Config{
|
|
ConfigPaths: &ConfigurationPaths{
|
|
ConfigDir: "./tests",
|
|
DataDir: "./data",
|
|
HubDir: "./hub",
|
|
HubIndexFile: "./hub/.index.json",
|
|
},
|
|
},
|
|
expectedResult: &Hub{
|
|
ConfigDir: configDirFullPath,
|
|
DataDir: dataFullPath,
|
|
HubDir: hubFullPath,
|
|
HubIndexFile: hubIndexFileFullPath,
|
|
},
|
|
},
|
|
{
|
|
name: "no data dir",
|
|
Input: &Config{
|
|
ConfigPaths: &ConfigurationPaths{
|
|
ConfigDir: "./tests",
|
|
HubDir: "./hub",
|
|
HubIndexFile: "./hub/.index.json",
|
|
},
|
|
},
|
|
expectedResult: nil,
|
|
},
|
|
{
|
|
name: "no configuration path",
|
|
Input: &Config{},
|
|
expectedResult: nil,
|
|
},
|
|
}
|
|
|
|
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))
|
|
}
|
|
}
|
|
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)
|
|
}
|
|
}
|
|
}
|