cscli_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package csconfig
  2. import (
  3. "path/filepath"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. "github.com/crowdsecurity/go-cs-lib/cstest"
  8. )
  9. func TestLoadCSCLI(t *testing.T) {
  10. hubFullPath, err := filepath.Abs("./hub")
  11. require.NoError(t, err)
  12. dataFullPath, err := filepath.Abs("./data")
  13. require.NoError(t, err)
  14. configDirFullPath, err := filepath.Abs("./testdata")
  15. require.NoError(t, err)
  16. hubIndexFileFullPath, err := filepath.Abs("./hub/.index.json")
  17. require.NoError(t, err)
  18. tests := []struct {
  19. name string
  20. input *Config
  21. expected *CscliCfg
  22. expectedErr string
  23. }{
  24. {
  25. name: "basic valid configuration",
  26. input: &Config{
  27. ConfigPaths: &ConfigurationPaths{
  28. ConfigDir: "./testdata",
  29. DataDir: "./data",
  30. HubDir: "./hub",
  31. HubIndexFile: "./hub/.index.json",
  32. },
  33. Prometheus: &PrometheusCfg{
  34. Enabled: true,
  35. Level: "full",
  36. ListenAddr: "127.0.0.1",
  37. ListenPort: 6060,
  38. },
  39. },
  40. expected: &CscliCfg{
  41. ConfigDir: configDirFullPath,
  42. DataDir: dataFullPath,
  43. HubDir: hubFullPath,
  44. HubIndexFile: hubIndexFileFullPath,
  45. PrometheusUrl: "http://127.0.0.1:6060/metrics",
  46. },
  47. },
  48. {
  49. name: "no configuration path",
  50. input: &Config{},
  51. expected: &CscliCfg{},
  52. expectedErr: "no configuration paths provided",
  53. },
  54. }
  55. for _, tc := range tests {
  56. tc := tc
  57. t.Run(tc.name, func(t *testing.T) {
  58. err := tc.input.LoadCSCLI()
  59. cstest.RequireErrorContains(t, err, tc.expectedErr)
  60. if tc.expectedErr != "" {
  61. return
  62. }
  63. assert.Equal(t, tc.expected, tc.input.Cscli)
  64. })
  65. }
  66. }