github.com/observiq/carbon@v0.9.11-0.20200820160507-1b872e368a5e/operator/builtin/transformer/router_test.go (about) 1 package transformer 2 3 import ( 4 "context" 5 "os" 6 "testing" 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 TestRouterOperator(t *testing.T) { 17 os.Setenv("TEST_ROUTER_PLUGIN_ENV", "foo") 18 defer os.Unsetenv("TEST_ROUTER_PLUGIN_ENV") 19 20 basicConfig := func() *RouterOperatorConfig { 21 return &RouterOperatorConfig{ 22 BasicConfig: helper.BasicConfig{ 23 OperatorID: "test_operator_id", 24 OperatorType: "router", 25 }, 26 } 27 } 28 29 cases := []struct { 30 name string 31 input *entry.Entry 32 routes []*RouterOperatorRouteConfig 33 expectedCounts map[string]int 34 expectedLabels map[string]string 35 }{ 36 { 37 "DefaultRoute", 38 entry.New(), 39 []*RouterOperatorRouteConfig{ 40 { 41 helper.NewLabelerConfig(), 42 "true", 43 []string{"output1"}, 44 }, 45 }, 46 map[string]int{"output1": 1}, 47 nil, 48 }, 49 { 50 "NoMatch", 51 entry.New(), 52 []*RouterOperatorRouteConfig{ 53 { 54 helper.NewLabelerConfig(), 55 `false`, 56 []string{"output1"}, 57 }, 58 }, 59 map[string]int{}, 60 nil, 61 }, 62 { 63 "SimpleMatch", 64 &entry.Entry{ 65 Record: map[string]interface{}{ 66 "message": "test_message", 67 }, 68 }, 69 []*RouterOperatorRouteConfig{ 70 { 71 helper.NewLabelerConfig(), 72 `$.message == "non_match"`, 73 []string{"output1"}, 74 }, 75 { 76 helper.NewLabelerConfig(), 77 `$.message == "test_message"`, 78 []string{"output2"}, 79 }, 80 }, 81 map[string]int{"output2": 1}, 82 nil, 83 }, 84 { 85 "MatchWithLabel", 86 &entry.Entry{ 87 Record: map[string]interface{}{ 88 "message": "test_message", 89 }, 90 }, 91 []*RouterOperatorRouteConfig{ 92 { 93 helper.NewLabelerConfig(), 94 `$.message == "non_match"`, 95 []string{"output1"}, 96 }, 97 { 98 helper.LabelerConfig{ 99 Labels: map[string]helper.ExprStringConfig{ 100 "label-key": "label-value", 101 }, 102 }, 103 `$.message == "test_message"`, 104 []string{"output2"}, 105 }, 106 }, 107 map[string]int{"output2": 1}, 108 map[string]string{ 109 "label-key": "label-value", 110 }, 111 }, 112 { 113 "MatchEnv", 114 &entry.Entry{ 115 Record: map[string]interface{}{ 116 "message": "test_message", 117 }, 118 }, 119 []*RouterOperatorRouteConfig{ 120 { 121 helper.NewLabelerConfig(), 122 `env("TEST_ROUTER_PLUGIN_ENV") == "foo"`, 123 []string{"output1"}, 124 }, 125 { 126 helper.NewLabelerConfig(), 127 `true`, 128 []string{"output2"}, 129 }, 130 }, 131 map[string]int{"output1": 1}, 132 nil, 133 }, 134 } 135 136 for _, tc := range cases { 137 t.Run(tc.name, func(t *testing.T) { 138 cfg := basicConfig() 139 cfg.Routes = tc.routes 140 141 buildContext := testutil.NewBuildContext(t) 142 newOperator, err := cfg.Build(buildContext) 143 require.NoError(t, err) 144 145 results := map[string]int{} 146 var labels map[string]string 147 148 mock1 := testutil.NewMockOperator("output1") 149 mock1.On("Process", mock.Anything, mock.Anything).Return(nil).Run(func(args mock.Arguments) { 150 results["output1"] = results["output1"] + 1 151 if entry, ok := args[1].(*entry.Entry); ok { 152 labels = entry.Labels 153 } 154 }) 155 mock2 := testutil.NewMockOperator("output2") 156 mock2.On("Process", mock.Anything, mock.Anything).Return(nil).Run(func(args mock.Arguments) { 157 results["output2"] = results["output2"] + 1 158 if entry, ok := args[1].(*entry.Entry); ok { 159 labels = entry.Labels 160 } 161 }) 162 163 routerOperator := newOperator.(*RouterOperator) 164 err = routerOperator.SetOutputs([]operator.Operator{mock1, mock2}) 165 require.NoError(t, err) 166 167 err = routerOperator.Process(context.Background(), tc.input) 168 require.NoError(t, err) 169 170 require.Equal(t, tc.expectedCounts, results) 171 require.Equal(t, tc.expectedLabels, labels) 172 }) 173 } 174 }