simulation_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 '%s': open %s: The system cannot find the file specified.", testXXFullPath, 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 '%s': open %s: no such file or directory", testXXFullPath, 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. } else if test.err != "" {
  111. if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) {
  112. fmt.Printf("TEST '%s': NOK\n", test.name)
  113. t.Fatalf("%d/%d expected '%s' got '%s'", idx, len(tests),
  114. test.err,
  115. fmt.Sprintf("%s", err))
  116. }
  117. }
  118. isOk := assert.Equal(t, test.expectedResult, test.Input.Crowdsec.SimulationConfig)
  119. if !isOk {
  120. t.Fatalf("TEST '%s': NOK\n", test.name)
  121. } else {
  122. fmt.Printf("TEST '%s': OK\n", test.name)
  123. }
  124. }
  125. }
  126. func TestIsSimulated(t *testing.T) {
  127. simCfgOff := &SimulationConfig{
  128. Simulation: new(bool),
  129. Exclusions: []string{"test"},
  130. }
  131. simCfgOn := &SimulationConfig{
  132. Simulation: new(bool),
  133. Exclusions: []string{"test"},
  134. }
  135. *simCfgOn.Simulation = true
  136. tests := []struct {
  137. name string
  138. SimulationConfig *SimulationConfig
  139. Input string
  140. expectedResult bool
  141. err string
  142. }{
  143. {
  144. name: "No simulation except (in exclusion)",
  145. SimulationConfig: simCfgOff,
  146. Input: "test",
  147. expectedResult: true,
  148. },
  149. {
  150. name: "All simulation (not in exclusion)",
  151. SimulationConfig: simCfgOn,
  152. Input: "toto",
  153. expectedResult: true,
  154. },
  155. {
  156. name: "All simulation (in exclusion)",
  157. SimulationConfig: simCfgOn,
  158. Input: "test",
  159. expectedResult: false,
  160. },
  161. }
  162. for _, test := range tests {
  163. IsSimulated := test.SimulationConfig.IsSimulated(test.Input)
  164. isOk := assert.Equal(t, test.expectedResult, IsSimulated)
  165. if !isOk {
  166. fmt.Printf("TEST: '%v' failed", test.name)
  167. t.Fatal()
  168. }
  169. }
  170. }