bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/pkg/csconfig/crowdsec_service_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 TestLoadSynsec(t *testing.T) {
    13  	acquisFullPath, err := filepath.Abs("./tests/acquis.yaml")
    14  	if err != nil {
    15  		t.Fatalf(err.Error())
    16  	}
    17  
    18  	acquisInDirFullPath, err := filepath.Abs("./tests/acquis/acquis.yaml")
    19  	if err != nil {
    20  		t.Fatalf(err.Error())
    21  	}
    22  
    23  	acquisDirFullPath, err := filepath.Abs("./tests/acquis")
    24  	if err != nil {
    25  		t.Fatalf(err.Error())
    26  	}
    27  
    28  	hubFullPath, err := filepath.Abs("./hub")
    29  	if err != nil {
    30  		t.Fatalf(err.Error())
    31  	}
    32  
    33  	dataFullPath, err := filepath.Abs("./data")
    34  	if err != nil {
    35  		t.Fatalf(err.Error())
    36  	}
    37  
    38  	configDirFullPath, err := filepath.Abs("./tests")
    39  	if err != nil {
    40  		t.Fatalf(err.Error())
    41  	}
    42  
    43  	hubIndexFileFullPath, err := filepath.Abs("./hub/.index.json")
    44  	if err != nil {
    45  		t.Fatalf(err.Error())
    46  	}
    47  
    48  	tests := []struct {
    49  		name           string
    50  		Input          *Config
    51  		expectedResult *SynsecServiceCfg
    52  		err            string
    53  	}{
    54  		{
    55  			name: "basic valid configuration",
    56  			Input: &Config{
    57  				ConfigPaths: &ConfigurationPaths{
    58  					ConfigDir: "./tests",
    59  					DataDir:   "./data",
    60  					HubDir:    "./hub",
    61  				},
    62  				API: &APICfg{
    63  					Client: &LocalApiClientCfg{
    64  						CredentialsFilePath: "./tests/lapi-secrets.yaml",
    65  					},
    66  				},
    67  				Synsec: &SynsecServiceCfg{
    68  					AcquisitionFilePath: "./tests/acquis.yaml",
    69  				},
    70  			},
    71  			expectedResult: &SynsecServiceCfg{
    72  				AcquisitionDirPath:   "",
    73  				AcquisitionFilePath:  acquisFullPath,
    74  				ConfigDir:            configDirFullPath,
    75  				DataDir:              dataFullPath,
    76  				HubDir:               hubFullPath,
    77  				HubIndexFile:         hubIndexFileFullPath,
    78  				BucketsRoutinesCount: 1,
    79  				ParserRoutinesCount:  1,
    80  				OutputRoutinesCount:  1,
    81  				AcquisitionFiles:     []string{acquisFullPath},
    82  			},
    83  		},
    84  		{
    85  			name: "basic valid configuration with acquisition dir",
    86  			Input: &Config{
    87  				ConfigPaths: &ConfigurationPaths{
    88  					ConfigDir: "./tests",
    89  					DataDir:   "./data",
    90  					HubDir:    "./hub",
    91  				},
    92  				API: &APICfg{
    93  					Client: &LocalApiClientCfg{
    94  						CredentialsFilePath: "./tests/lapi-secrets.yaml",
    95  					},
    96  				},
    97  				Synsec: &SynsecServiceCfg{
    98  					AcquisitionFilePath: "./tests/acquis.yaml",
    99  					AcquisitionDirPath:  "./tests/acquis/",
   100  				},
   101  			},
   102  			expectedResult: &SynsecServiceCfg{
   103  				AcquisitionDirPath:   acquisDirFullPath,
   104  				AcquisitionFilePath:  acquisFullPath,
   105  				ConfigDir:            configDirFullPath,
   106  				HubIndexFile:         hubIndexFileFullPath,
   107  				DataDir:              dataFullPath,
   108  				HubDir:               hubFullPath,
   109  				BucketsRoutinesCount: 1,
   110  				ParserRoutinesCount:  1,
   111  				OutputRoutinesCount:  1,
   112  				AcquisitionFiles:     []string{acquisFullPath, acquisInDirFullPath},
   113  			},
   114  		},
   115  		{
   116  			name: "no acquisition file and dir",
   117  			Input: &Config{
   118  				ConfigPaths: &ConfigurationPaths{
   119  					ConfigDir: "./tests",
   120  					DataDir:   "./data",
   121  					HubDir:    "./hub",
   122  				},
   123  				API: &APICfg{
   124  					Client: &LocalApiClientCfg{
   125  						CredentialsFilePath: "./tests/lapi-secrets.yaml",
   126  					},
   127  				},
   128  				Synsec: &SynsecServiceCfg{},
   129  			},
   130  			expectedResult: &SynsecServiceCfg{
   131  				BucketsRoutinesCount: 0,
   132  				ParserRoutinesCount:  0,
   133  				OutputRoutinesCount:  0,
   134  			},
   135  		},
   136  		{
   137  			name: "non existing acquisition file",
   138  			Input: &Config{
   139  				ConfigPaths: &ConfigurationPaths{
   140  					ConfigDir: "./tests",
   141  					DataDir:   "./data",
   142  					HubDir:    "./hub",
   143  				},
   144  				API: &APICfg{
   145  					Client: &LocalApiClientCfg{
   146  						CredentialsFilePath: "./tests/lapi-secrets.yaml",
   147  					},
   148  				},
   149  				Synsec: &SynsecServiceCfg{
   150  					AcquisitionFilePath: "./tests/acquis_not_exist.yaml",
   151  				},
   152  			},
   153  			expectedResult: &SynsecServiceCfg{
   154  				AcquisitionFilePath:  "./tests/acquis_not_exist.yaml",
   155  				BucketsRoutinesCount: 0,
   156  				ParserRoutinesCount:  0,
   157  				OutputRoutinesCount:  0,
   158  			},
   159  		},
   160  		{
   161  			name: "agent disabled",
   162  			Input: &Config{
   163  				ConfigPaths: &ConfigurationPaths{
   164  					ConfigDir: "./tests",
   165  					DataDir:   "./data",
   166  					HubDir:    "./hub",
   167  				},
   168  			},
   169  			expectedResult: nil,
   170  		},
   171  	}
   172  
   173  	for idx, test := range tests {
   174  		fmt.Printf("TEST '%s'\n", test.name)
   175  		err := test.Input.LoadSynsec()
   176  		if err == nil && test.err != "" {
   177  			t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests))
   178  		} else if test.err != "" {
   179  			if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) {
   180  				t.Fatalf("%d/%d expected '%s' got '%s'", idx, len(tests),
   181  					test.err,
   182  					fmt.Sprintf("%s", err))
   183  			}
   184  		}
   185  
   186  		isOk := assert.Equal(t, test.expectedResult, test.Input.Synsec)
   187  		if !isOk {
   188  			t.Fatalf("test '%s' failed", test.name)
   189  		}
   190  
   191  	}
   192  }