simulation_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package csconfig
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "runtime"
  6. "strings"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestSimulationLoading(t *testing.T) {
  11. testXXFullPath, err := filepath.Abs("./tests/xxx.yaml")
  12. if err != nil {
  13. panic(err)
  14. }
  15. badYamlFullPath, err := filepath.Abs("./tests/config.yaml")
  16. if err != nil {
  17. panic(err)
  18. }
  19. tests := []struct {
  20. name string
  21. Input *Config
  22. expectedResult *SimulationConfig
  23. err string
  24. }{
  25. {
  26. name: "basic valid simulation",
  27. Input: &Config{
  28. ConfigPaths: &ConfigurationPaths{
  29. SimulationFilePath: "./tests/simulation.yaml",
  30. DataDir: "./data",
  31. },
  32. Crowdsec: &CrowdsecServiceCfg{},
  33. Cscli: &CscliCfg{},
  34. },
  35. expectedResult: &SimulationConfig{Simulation: new(bool)},
  36. },
  37. {
  38. name: "basic nil config",
  39. Input: &Config{
  40. ConfigPaths: &ConfigurationPaths{
  41. SimulationFilePath: "",
  42. DataDir: "./data",
  43. },
  44. Crowdsec: &CrowdsecServiceCfg{},
  45. },
  46. },
  47. {
  48. name: "basic bad file content",
  49. Input: &Config{
  50. ConfigPaths: &ConfigurationPaths{
  51. SimulationFilePath: "./tests/config.yaml",
  52. DataDir: "./data",
  53. },
  54. Crowdsec: &CrowdsecServiceCfg{},
  55. },
  56. err: fmt.Sprintf("while unmarshaling simulation file '%s' : yaml: unmarshal errors", badYamlFullPath),
  57. },
  58. {
  59. name: "basic bad file content",
  60. Input: &Config{
  61. ConfigPaths: &ConfigurationPaths{
  62. SimulationFilePath: "./tests/config.yaml",
  63. DataDir: "./data",
  64. },
  65. Crowdsec: &CrowdsecServiceCfg{},
  66. },
  67. err: fmt.Sprintf("while unmarshaling simulation file '%s' : yaml: unmarshal errors", badYamlFullPath),
  68. },
  69. }
  70. if runtime.GOOS == "windows" {
  71. tests = append(tests, struct {
  72. name string
  73. Input *Config
  74. expectedResult *SimulationConfig
  75. err string
  76. }{
  77. name: "basic bad file name",
  78. Input: &Config{
  79. ConfigPaths: &ConfigurationPaths{
  80. SimulationFilePath: "./tests/xxx.yaml",
  81. DataDir: "./data",
  82. },
  83. Crowdsec: &CrowdsecServiceCfg{},
  84. },
  85. err: fmt.Sprintf("while reading yaml file: open %s: The system cannot find the file specified.", testXXFullPath),
  86. })
  87. } else {
  88. tests = append(tests, struct {
  89. name string
  90. Input *Config
  91. expectedResult *SimulationConfig
  92. err string
  93. }{
  94. name: "basic bad file name",
  95. Input: &Config{
  96. ConfigPaths: &ConfigurationPaths{
  97. SimulationFilePath: "./tests/xxx.yaml",
  98. DataDir: "./data",
  99. },
  100. Crowdsec: &CrowdsecServiceCfg{},
  101. },
  102. err: fmt.Sprintf("while reading yaml file: open %s: no such file or directory", testXXFullPath),
  103. })
  104. }
  105. for idx, test := range tests {
  106. err := test.Input.LoadSimulation()
  107. if err == nil && test.err != "" {
  108. fmt.Printf("TEST '%s': NOK\n", test.name)
  109. t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests))
  110. }
  111. if test.err != "" {
  112. if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) {
  113. fmt.Printf("TEST '%s': NOK\n", test.name)
  114. t.Fatalf("%d/%d expected '%s' got '%s'", idx, len(tests),
  115. test.err,
  116. fmt.Sprintf("%s", err))
  117. }
  118. }
  119. isOk := assert.Equal(t, test.expectedResult, test.Input.Crowdsec.SimulationConfig)
  120. if !isOk {
  121. t.Fatalf("TEST '%s': NOK\n", test.name)
  122. } else {
  123. fmt.Printf("TEST '%s': OK\n", test.name)
  124. }
  125. }
  126. }
  127. func TestIsSimulated(t *testing.T) {
  128. simCfgOff := &SimulationConfig{
  129. Simulation: new(bool),
  130. Exclusions: []string{"test"},
  131. }
  132. simCfgOn := &SimulationConfig{
  133. Simulation: new(bool),
  134. Exclusions: []string{"test"},
  135. }
  136. *simCfgOn.Simulation = true
  137. tests := []struct {
  138. name string
  139. SimulationConfig *SimulationConfig
  140. Input string
  141. expectedResult bool
  142. err string
  143. }{
  144. {
  145. name: "No simulation except (in exclusion)",
  146. SimulationConfig: simCfgOff,
  147. Input: "test",
  148. expectedResult: true,
  149. },
  150. {
  151. name: "All simulation (not in exclusion)",
  152. SimulationConfig: simCfgOn,
  153. Input: "toto",
  154. expectedResult: true,
  155. },
  156. {
  157. name: "All simulation (in exclusion)",
  158. SimulationConfig: simCfgOn,
  159. Input: "test",
  160. expectedResult: false,
  161. },
  162. }
  163. for _, test := range tests {
  164. IsSimulated := test.SimulationConfig.IsSimulated(test.Input)
  165. isOk := assert.Equal(t, test.expectedResult, IsSimulated)
  166. if !isOk {
  167. fmt.Printf("TEST: '%v' failed", test.name)
  168. t.Fatal()
  169. }
  170. }
  171. }