bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/pkg/csconfig/simulation_test.go (about)

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