simulation_test.go 3.2 KB

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