github.com/observiq/carbon@v0.9.11-0.20200820160507-1b872e368a5e/operator/helper/operator_test.go (about) 1 package helper 2 3 import ( 4 "testing" 5 6 "github.com/observiq/carbon/operator" 7 "github.com/observiq/carbon/testutil" 8 "github.com/stretchr/testify/require" 9 "go.uber.org/zap" 10 ) 11 12 func TestBasicConfigID(t *testing.T) { 13 config := BasicConfig{ 14 OperatorID: "test-id", 15 OperatorType: "test-type", 16 } 17 require.Equal(t, "test-id", config.ID()) 18 } 19 20 func TestBasicConfigType(t *testing.T) { 21 config := BasicConfig{ 22 OperatorID: "test-id", 23 OperatorType: "test-type", 24 } 25 require.Equal(t, "test-type", config.Type()) 26 } 27 28 func TestBasicConfigBuildWithoutID(t *testing.T) { 29 config := BasicConfig{ 30 OperatorType: "test-type", 31 } 32 context := testutil.NewBuildContext(t) 33 _, err := config.Build(context) 34 require.NoError(t, err) 35 } 36 37 func TestBasicConfigBuildWithoutType(t *testing.T) { 38 config := BasicConfig{ 39 OperatorID: "test-id", 40 } 41 context := operator.BuildContext{} 42 _, err := config.Build(context) 43 require.Error(t, err) 44 require.Contains(t, err.Error(), "missing required `type` field.") 45 } 46 47 func TestBasicConfigBuildMissingLogger(t *testing.T) { 48 config := BasicConfig{ 49 OperatorID: "test-id", 50 OperatorType: "test-type", 51 } 52 context := operator.BuildContext{} 53 _, err := config.Build(context) 54 require.Error(t, err) 55 require.Contains(t, err.Error(), "operator build context is missing a logger.") 56 } 57 58 func TestBasicConfigBuildValid(t *testing.T) { 59 config := BasicConfig{ 60 OperatorID: "test-id", 61 OperatorType: "test-type", 62 } 63 context := testutil.NewBuildContext(t) 64 operator, err := config.Build(context) 65 require.NoError(t, err) 66 require.Equal(t, "test-id", operator.OperatorID) 67 require.Equal(t, "test-type", operator.OperatorType) 68 } 69 70 func TestBasicOperatorID(t *testing.T) { 71 operator := BasicOperator{ 72 OperatorID: "test-id", 73 OperatorType: "test-type", 74 } 75 require.Equal(t, "test-id", operator.ID()) 76 } 77 78 func TestBasicOperatorType(t *testing.T) { 79 operator := BasicOperator{ 80 OperatorID: "test-id", 81 OperatorType: "test-type", 82 } 83 require.Equal(t, "test-type", operator.Type()) 84 } 85 86 func TestBasicOperatorLogger(t *testing.T) { 87 logger := &zap.SugaredLogger{} 88 operator := BasicOperator{ 89 OperatorID: "test-id", 90 OperatorType: "test-type", 91 SugaredLogger: logger, 92 } 93 require.Equal(t, logger, operator.Logger()) 94 } 95 96 func TestBasicOperatorStart(t *testing.T) { 97 operator := BasicOperator{ 98 OperatorID: "test-id", 99 OperatorType: "test-type", 100 } 101 err := operator.Start() 102 require.NoError(t, err) 103 } 104 105 func TestBasicOperatorStop(t *testing.T) { 106 operator := BasicOperator{ 107 OperatorID: "test-id", 108 OperatorType: "test-type", 109 } 110 err := operator.Stop() 111 require.NoError(t, err) 112 }