github.com/observiq/carbon@v0.9.11-0.20200820160507-1b872e368a5e/operator/builtin/input/generate_test.go (about)

     1  package input
     2  
     3  import (
     4  	"testing"
     5  	"text/template"
     6  	"time"
     7  
     8  	"github.com/observiq/carbon/entry"
     9  	"github.com/observiq/carbon/operator"
    10  	"github.com/observiq/carbon/operator/helper"
    11  	"github.com/observiq/carbon/testutil"
    12  	"github.com/stretchr/testify/mock"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestInputGenerate(t *testing.T) {
    17  	count := 5
    18  	basicConfig := func() *GenerateInputConfig {
    19  		cfg := NewGenerateInputConfig("test_operator_id")
    20  		cfg.OutputIDs = []string{"output1"}
    21  		cfg.Entry = entry.Entry{
    22  			Record: "test message",
    23  		}
    24  		cfg.Count = count
    25  		return cfg
    26  	}
    27  
    28  	buildContext := testutil.NewBuildContext(t)
    29  	newOperator, err := basicConfig().Build(buildContext)
    30  	require.NoError(t, err)
    31  
    32  	receivedEntries := make(chan *entry.Entry)
    33  	mockOutput := testutil.NewMockOperator("output1")
    34  	mockOutput.On("Process", mock.Anything, mock.Anything).Return(nil).Run(func(args mock.Arguments) {
    35  		receivedEntries <- args.Get(1).(*entry.Entry)
    36  	})
    37  
    38  	generateInput := newOperator.(*GenerateInput)
    39  	err = generateInput.SetOutputs([]operator.Operator{mockOutput})
    40  	require.NoError(t, err)
    41  
    42  	err = generateInput.Start()
    43  	require.NoError(t, err)
    44  	defer generateInput.Stop()
    45  
    46  	for i := 0; i < count; i++ {
    47  		select {
    48  		case <-receivedEntries:
    49  			continue
    50  		case <-time.After(time.Second):
    51  			require.FailNow(t, "Timed out waiting for generated entries")
    52  		}
    53  	}
    54  }
    55  
    56  func TestRenderFromPluginTemplate(t *testing.T) {
    57  	templateText := `
    58  pipeline:
    59    - id: my_generator
    60      type: generate_input
    61      output: {{ .output }}
    62      entry:
    63        record:
    64          message: testmessage
    65  `
    66  	tmpl, err := template.New("my_generator").Parse(templateText)
    67  	require.NoError(t, err)
    68  
    69  	registry := operator.PluginRegistry{
    70  		"sample": tmpl,
    71  	}
    72  
    73  	params := map[string]interface{}{
    74  		"output": "sampleoutput",
    75  	}
    76  	config, err := registry.Render("sample", params)
    77  	require.NoError(t, err)
    78  
    79  	expectedConfig := operator.PluginConfig{
    80  		Pipeline: []operator.Config{
    81  			{
    82  				Builder: &GenerateInputConfig{
    83  					InputConfig: helper.InputConfig{
    84  						LabelerConfig:    helper.NewLabelerConfig(),
    85  						IdentifierConfig: helper.NewIdentifierConfig(),
    86  						WriteTo:          entry.NewRecordField(),
    87  						WriterConfig: helper.WriterConfig{
    88  							BasicConfig: helper.BasicConfig{
    89  								OperatorID:   "my_generator",
    90  								OperatorType: "generate_input",
    91  							},
    92  							OutputIDs: []string{"sampleoutput"},
    93  						},
    94  					},
    95  					Entry: entry.Entry{
    96  						Record: map[interface{}]interface{}{
    97  							"message": "testmessage",
    98  						},
    99  					},
   100  				},
   101  			},
   102  		},
   103  	}
   104  
   105  	require.Equal(t, expectedConfig, config)
   106  }