github.com/crowdsecurity/crowdsec@v1.6.1/pkg/csconfig/simulation_test.go (about)

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