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

     1  package acquisition
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"bitbucket.org/Aishee/synsec/pkg/csconfig"
    11  	"bitbucket.org/Aishee/synsec/pkg/types"
    12  	log "github.com/sirupsen/logrus"
    13  	tomb "gopkg.in/tomb.v2"
    14  )
    15  
    16  func TestConfigLoading(t *testing.T) {
    17  	//bad filename
    18  	cfg := csconfig.SynsecServiceCfg{
    19  		AcquisitionFiles: []string{"./tests/xxx.yaml"},
    20  	}
    21  	_, err := LoadAcquisitionFromFile(&cfg)
    22  	assert.Contains(t, fmt.Sprintf("%s", err), "can't open ./tests/xxx.yaml: open ./tests/xxx.yaml: no such file or directory")
    23  	//bad config file
    24  	cfg = csconfig.SynsecServiceCfg{
    25  		AcquisitionFiles: []string{"./tests/test.log"},
    26  	}
    27  	_, err = LoadAcquisitionFromFile(&cfg)
    28  	assert.Contains(t, fmt.Sprintf("%s", err), "failed to yaml decode ./tests/test.log: yaml: unmarshal errors")
    29  	//correct config file
    30  	cfg = csconfig.SynsecServiceCfg{
    31  		AcquisitionFiles: []string{"./tests/acquis_test.yaml"},
    32  	}
    33  	srcs, err := LoadAcquisitionFromFile(&cfg)
    34  	if err != nil {
    35  		t.Fatalf("unexpected error : %s", err)
    36  	}
    37  	assert.Equal(t, len(srcs), 1)
    38  }
    39  
    40  func TestDataSourceConfigure(t *testing.T) {
    41  	tests := []struct {
    42  		cfg DataSourceCfg
    43  		//tombState
    44  		config_error string
    45  		read_error   string
    46  		tomb_error   string
    47  		lines        int
    48  	}{
    49  		{ //missing filename(s)
    50  			cfg: DataSourceCfg{
    51  				Mode: CAT_MODE,
    52  			},
    53  			config_error: "empty filename(s) and journalctl filter, malformed datasource",
    54  		},
    55  		{ //missing filename(s)
    56  			cfg: DataSourceCfg{
    57  				Mode: TAIL_MODE,
    58  			},
    59  			config_error: "empty filename(s) and journalctl filter, malformed datasource",
    60  		},
    61  		{ //bad mode(s)
    62  			cfg: DataSourceCfg{
    63  				Filename: "./tests/test.log",
    64  				Mode:     "ratata",
    65  			},
    66  			config_error: "configuring file datasource: unknown mode ratata for file acquisition",
    67  		},
    68  		{ //ok test
    69  			cfg: DataSourceCfg{
    70  				Mode:     CAT_MODE,
    71  				Filename: "./tests/test.log",
    72  			},
    73  		},
    74  		{ //missing mode, default to CAT_MODE
    75  			cfg: DataSourceCfg{
    76  				Filename: "./tests/test.log",
    77  			},
    78  		},
    79  		{ //ok test for journalctl
    80  			cfg: DataSourceCfg{
    81  				Mode:              CAT_MODE,
    82  				JournalctlFilters: []string{"-test.run=TestSimJournalctlCatOneLine", "--"},
    83  			},
    84  		},
    85  	}
    86  
    87  	for tidx, test := range tests {
    88  
    89  		srcs, err := DataSourceConfigure(test.cfg)
    90  		if test.config_error != "" {
    91  			assert.Contains(t, fmt.Sprintf("%s", err), test.config_error)
    92  			log.Infof("expected config error ok : %s", test.config_error)
    93  			continue
    94  		} else {
    95  			if err != nil {
    96  				t.Fatalf("%d/%d unexpected config error %s", tidx, len(tests), err)
    97  			}
    98  		}
    99  
   100  		//check we got the expected mode
   101  		if tests[tidx].cfg.Mode == "" {
   102  			tests[tidx].cfg.Mode = TAIL_MODE
   103  		}
   104  		assert.Equal(t, srcs.Mode(), tests[tidx].cfg.Mode)
   105  
   106  		out := make(chan types.Event)
   107  		tomb := tomb.Tomb{}
   108  
   109  		go func() {
   110  			err = StartAcquisition([]DataSource{srcs}, out, &tomb)
   111  			if test.read_error != "" {
   112  				assert.Contains(t, fmt.Sprintf("%s", err), test.read_error)
   113  				log.Infof("expected read error ok : %s", test.read_error)
   114  			} else {
   115  				if err != nil {
   116  					log.Fatalf("%d/%d unexpected read error %s", tidx, len(tests), err)
   117  				}
   118  			}
   119  		}()
   120  
   121  		log.Printf("kill iiittt")
   122  		//we're actually not interested in the result :)
   123  		tomb.Kill(nil)
   124  		time.Sleep(1 * time.Second)
   125  
   126  		if test.tomb_error != "" {
   127  			assert.Contains(t, fmt.Sprintf("%s", tomb.Err()), test.tomb_error)
   128  			log.Infof("expected tomb error ok : %s", test.read_error)
   129  			continue
   130  		} else {
   131  			if tomb.Err() != nil {
   132  				t.Fatalf("%d/%d unexpected tomb error %s", tidx, len(tests), tomb.Err())
   133  			}
   134  		}
   135  
   136  	}
   137  
   138  }