simulation_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package csconfig
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. "github.com/crowdsecurity/crowdsec/pkg/cstest"
  9. )
  10. func TestSimulationLoading(t *testing.T) {
  11. testXXFullPath, err := filepath.Abs("./tests/xxx.yaml")
  12. require.NoError(t, err)
  13. badYamlFullPath, err := filepath.Abs("./tests/config.yaml")
  14. require.NoError(t, err)
  15. tests := []struct {
  16. name string
  17. Input *Config
  18. expectedResult *SimulationConfig
  19. expectedErr string
  20. }{
  21. {
  22. name: "basic valid simulation",
  23. Input: &Config{
  24. ConfigPaths: &ConfigurationPaths{
  25. SimulationFilePath: "./tests/simulation.yaml",
  26. DataDir: "./data",
  27. },
  28. Crowdsec: &CrowdsecServiceCfg{},
  29. Cscli: &CscliCfg{},
  30. },
  31. expectedResult: &SimulationConfig{Simulation: new(bool)},
  32. },
  33. {
  34. name: "basic nil config",
  35. Input: &Config{
  36. ConfigPaths: &ConfigurationPaths{
  37. SimulationFilePath: "",
  38. DataDir: "./data",
  39. },
  40. Crowdsec: &CrowdsecServiceCfg{},
  41. },
  42. expectedErr: "simulation.yaml: "+cstest.FileNotFoundMessage,
  43. },
  44. {
  45. name: "basic bad file name",
  46. Input: &Config{
  47. ConfigPaths: &ConfigurationPaths{
  48. SimulationFilePath: "./tests/xxx.yaml",
  49. DataDir: "./data",
  50. },
  51. Crowdsec: &CrowdsecServiceCfg{},
  52. },
  53. expectedErr: fmt.Sprintf("while reading yaml file: open %s: %s", testXXFullPath, cstest.FileNotFoundMessage),
  54. },
  55. {
  56. name: "basic bad file content",
  57. Input: &Config{
  58. ConfigPaths: &ConfigurationPaths{
  59. SimulationFilePath: "./tests/config.yaml",
  60. DataDir: "./data",
  61. },
  62. Crowdsec: &CrowdsecServiceCfg{},
  63. },
  64. expectedErr: fmt.Sprintf("while unmarshaling simulation file '%s' : yaml: unmarshal errors", badYamlFullPath),
  65. },
  66. {
  67. name: "basic bad file content",
  68. Input: &Config{
  69. ConfigPaths: &ConfigurationPaths{
  70. SimulationFilePath: "./tests/config.yaml",
  71. DataDir: "./data",
  72. },
  73. Crowdsec: &CrowdsecServiceCfg{},
  74. },
  75. expectedErr: fmt.Sprintf("while unmarshaling simulation file '%s' : yaml: unmarshal errors", badYamlFullPath),
  76. },
  77. }
  78. for _, tc := range tests {
  79. tc := tc
  80. t.Run(tc.name, func(t *testing.T) {
  81. err := tc.Input.LoadSimulation()
  82. cstest.RequireErrorContains(t, err, tc.expectedErr)
  83. assert.Equal(t, tc.expectedResult, tc.Input.Crowdsec.SimulationConfig)
  84. })
  85. }
  86. }
  87. func TestIsSimulated(t *testing.T) {
  88. simCfgOff := &SimulationConfig{
  89. Simulation: new(bool),
  90. Exclusions: []string{"test"},
  91. }
  92. simCfgOn := &SimulationConfig{
  93. Simulation: new(bool),
  94. Exclusions: []string{"test"},
  95. }
  96. *simCfgOn.Simulation = true
  97. tests := []struct {
  98. name string
  99. SimulationConfig *SimulationConfig
  100. Input string
  101. expectedResult bool
  102. }{
  103. {
  104. name: "No simulation except (in exclusion)",
  105. SimulationConfig: simCfgOff,
  106. Input: "test",
  107. expectedResult: true,
  108. },
  109. {
  110. name: "All simulation (not in exclusion)",
  111. SimulationConfig: simCfgOn,
  112. Input: "toto",
  113. expectedResult: true,
  114. },
  115. {
  116. name: "All simulation (in exclusion)",
  117. SimulationConfig: simCfgOn,
  118. Input: "test",
  119. expectedResult: false,
  120. },
  121. }
  122. for _, tc := range tests {
  123. tc := tc
  124. t.Run(tc.name, func(t *testing.T) {
  125. IsSimulated := tc.SimulationConfig.IsSimulated(tc.Input)
  126. require.Equal(t, tc.expectedResult, IsSimulated)
  127. })
  128. }
  129. }