cscli_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. },
  34. expected: &CscliCfg{
  35. ConfigDir: configDirFullPath,
  36. DataDir: dataFullPath,
  37. HubDir: hubFullPath,
  38. HubIndexFile: hubIndexFileFullPath,
  39. },
  40. },
  41. {
  42. name: "no configuration path",
  43. input: &Config{},
  44. expected: &CscliCfg{},
  45. expectedErr: "no configuration paths provided",
  46. },
  47. }
  48. for _, tc := range tests {
  49. tc := tc
  50. t.Run(tc.name, func(t *testing.T) {
  51. err := tc.input.LoadCSCLI()
  52. cstest.RequireErrorContains(t, err, tc.expectedErr)
  53. if tc.expectedErr != "" {
  54. return
  55. }
  56. assert.Equal(t, tc.expected, tc.input.Cscli)
  57. })
  58. }
  59. }