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

     1  package output
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/observiq/carbon/entry"
    11  	"github.com/observiq/carbon/operator/helper"
    12  	"github.com/observiq/carbon/testutil"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestStdoutOperator(t *testing.T) {
    17  	cfg := StdoutConfig{
    18  		OutputConfig: helper.OutputConfig{
    19  			BasicConfig: helper.BasicConfig{
    20  				OperatorID:   "test_operator_id",
    21  				OperatorType: "stdout",
    22  			},
    23  		},
    24  	}
    25  
    26  	operator, err := cfg.Build(testutil.NewBuildContext(t))
    27  	require.NoError(t, err)
    28  
    29  	var buf bytes.Buffer
    30  	operator.(*StdoutOperator).encoder = json.NewEncoder(&buf)
    31  
    32  	ts := time.Unix(1591042864, 0)
    33  	e := &entry.Entry{
    34  		Timestamp: ts,
    35  		Record:    "test record",
    36  	}
    37  	err = operator.Process(context.Background(), e)
    38  	require.NoError(t, err)
    39  
    40  	marshalledTimestamp, err := json.Marshal(ts)
    41  	require.NoError(t, err)
    42  
    43  	expected := `{"timestamp":` + string(marshalledTimestamp) + `,"severity":0,"record":"test record"}` + "\n"
    44  	require.Equal(t, expected, buf.String())
    45  }