common_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 TestLoadCommon(t *testing.T) {
  10. pidDirPath := "./testdata"
  11. LogDirFullPath, err := filepath.Abs("./testdata/log/")
  12. require.NoError(t, err)
  13. WorkingDirFullPath, err := filepath.Abs("./testdata")
  14. require.NoError(t, err)
  15. tests := []struct {
  16. name string
  17. input *Config
  18. expected *CommonCfg
  19. expectedErr string
  20. }{
  21. {
  22. name: "basic valid configuration",
  23. input: &Config{
  24. Common: &CommonCfg{
  25. Daemonize: true,
  26. PidDir: "./testdata",
  27. LogMedia: "file",
  28. LogDir: "./testdata/log/",
  29. WorkingDir: "./testdata/",
  30. },
  31. },
  32. expected: &CommonCfg{
  33. Daemonize: true,
  34. PidDir: pidDirPath,
  35. LogMedia: "file",
  36. LogDir: LogDirFullPath,
  37. WorkingDir: WorkingDirFullPath,
  38. },
  39. },
  40. {
  41. name: "empty working dir",
  42. input: &Config{
  43. Common: &CommonCfg{
  44. Daemonize: true,
  45. PidDir: "./testdata",
  46. LogMedia: "file",
  47. LogDir: "./testdata/log/",
  48. },
  49. },
  50. expected: &CommonCfg{
  51. Daemonize: true,
  52. PidDir: pidDirPath,
  53. LogMedia: "file",
  54. LogDir: LogDirFullPath,
  55. },
  56. },
  57. {
  58. name: "no common",
  59. input: &Config{},
  60. expected: nil,
  61. expectedErr: "no common block provided in configuration file",
  62. },
  63. }
  64. for _, tc := range tests {
  65. tc := tc
  66. t.Run(tc.name, func(t *testing.T) {
  67. err := tc.input.LoadCommon()
  68. cstest.RequireErrorContains(t, err, tc.expectedErr)
  69. if tc.expectedErr != "" {
  70. return
  71. }
  72. assert.Equal(t, tc.expected, tc.input.Common)
  73. })
  74. }
  75. }