github.com/Jeffail/benthos/v3@v3.65.0/lib/output/broker_out_common_test.go (about) 1 package output_test 2 3 import ( 4 "encoding/json" 5 "errors" 6 "testing" 7 8 "github.com/Jeffail/benthos/v3/lib/log" 9 "github.com/Jeffail/benthos/v3/lib/metrics" 10 "github.com/Jeffail/benthos/v3/lib/output" 11 "github.com/Jeffail/benthos/v3/lib/types" 12 13 _ "github.com/Jeffail/benthos/v3/public/components/all" 14 ) 15 16 func TestOutBrokerConfigDefaults(t *testing.T) { 17 testConf := []byte(`{ 18 "type": "broker", 19 "broker": { 20 "outputs": [ 21 { 22 "type": "http_client", 23 "http_client": { 24 "url": "address:1", 25 "timeout": "1ms" 26 } 27 }, 28 { 29 "type": "http_client", 30 "http_client": { 31 "url": "address:2", 32 "retry_period": "2ms" 33 } 34 } 35 ] 36 } 37 }`) 38 39 conf := output.NewConfig() 40 if err := json.Unmarshal(testConf, &conf); err != nil { 41 t.Error(err) 42 return 43 } 44 45 outputConfs := conf.Broker.Outputs 46 47 if exp, actual := 2, len(outputConfs); exp != actual { 48 t.Errorf("unexpected number of output configs: %v != %v", exp, actual) 49 return 50 } 51 52 if exp, actual := "http_client", outputConfs[0].Type; exp != actual { 53 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 54 } 55 if exp, actual := "http_client", outputConfs[1].Type; exp != actual { 56 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 57 } 58 59 if exp, actual := "address:1", outputConfs[0].HTTPClient.URL; exp != actual { 60 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 61 } 62 if exp, actual := "address:2", outputConfs[1].HTTPClient.URL; exp != actual { 63 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 64 } 65 66 if exp, actual := "1ms", outputConfs[0].HTTPClient.Timeout; exp != actual { 67 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 68 } 69 if exp, actual := "5s", outputConfs[1].HTTPClient.Timeout; exp != actual { 70 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 71 } 72 73 if exp, actual := "1s", outputConfs[0].HTTPClient.Retry; exp != actual { 74 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 75 } 76 if exp, actual := "2ms", outputConfs[1].HTTPClient.Retry; exp != actual { 77 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 78 } 79 } 80 81 func TestOutBrokerConfigDitto(t *testing.T) { 82 testConf := []byte(`{ 83 "type": "broker", 84 "broker": { 85 "outputs": [ 86 { 87 "type": "http_client", 88 "http_client": { 89 "url": "address:1", 90 "timeout": "1ms" 91 } 92 }, 93 { 94 "type": "ditto", 95 "http_client": { 96 "timeout": "2ms" 97 } 98 }, 99 { 100 "type": "ditto", 101 "http_client": { 102 "timeout": "3ms" 103 } 104 } 105 ] 106 } 107 }`) 108 109 conf := output.NewConfig() 110 if err := json.Unmarshal(testConf, &conf); err != nil { 111 t.Error(err) 112 return 113 } 114 115 outputConfs := conf.Broker.Outputs 116 117 if exp, actual := 3, len(outputConfs); exp != actual { 118 t.Errorf("unexpected number of output configs: %v != %v", exp, actual) 119 return 120 } 121 122 if exp, actual := "http_client", outputConfs[0].Type; exp != actual { 123 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 124 } 125 if exp, actual := "http_client", outputConfs[1].Type; exp != actual { 126 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 127 } 128 if exp, actual := "http_client", outputConfs[2].Type; exp != actual { 129 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 130 } 131 132 if exp, actual := "address:1", outputConfs[0].HTTPClient.URL; exp != actual { 133 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 134 } 135 if exp, actual := "address:1", outputConfs[1].HTTPClient.URL; exp != actual { 136 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 137 } 138 if exp, actual := "address:1", outputConfs[2].HTTPClient.URL; exp != actual { 139 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 140 } 141 142 if exp, actual := "1ms", outputConfs[0].HTTPClient.Timeout; exp != actual { 143 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 144 } 145 if exp, actual := "2ms", outputConfs[1].HTTPClient.Timeout; exp != actual { 146 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 147 } 148 if exp, actual := "3ms", outputConfs[2].HTTPClient.Timeout; exp != actual { 149 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 150 } 151 } 152 153 type exampleConfig struct { 154 Foo int `json:"foo" yaml:"foo"` 155 Bar string `json:"bar" yaml:"bar"` 156 Baz string `json:"baz" yaml:"baz"` 157 } 158 159 func newExampleConfig() *exampleConfig { 160 return &exampleConfig{ 161 Foo: 1000, 162 Bar: "bar", 163 Baz: "default", 164 } 165 } 166 167 func TestBrokerConfigPluginDitto(t *testing.T) { 168 t.Parallel() 169 170 output.RegisterPlugin( 171 "example", 172 func() interface{} { 173 return newExampleConfig() 174 }, 175 func(iconf interface{}, mgr types.Manager, logger log.Modular, stats metrics.Type) (types.Output, error) { 176 return nil, errors.New("err not implemented") 177 }, 178 ) 179 180 testConf := []byte(`{ 181 "type": "broker", 182 "broker": { 183 "outputs": [ 184 { 185 "type": "example", 186 "plugin": { 187 "foo": 23 188 } 189 }, 190 { 191 "type": "ditto", 192 "plugin": { 193 "bar": "baz" 194 } 195 }, 196 { 197 "type": "ditto", 198 "plugin": { 199 "foo": 29 200 } 201 } 202 ] 203 } 204 }`) 205 206 conf := output.NewConfig() 207 if err := json.Unmarshal(testConf, &conf); err != nil { 208 t.Error(err) 209 return 210 } 211 212 outputConfs := conf.Broker.Outputs 213 214 if exp, actual := 3, len(outputConfs); exp != actual { 215 t.Errorf("unexpected number of output configs: %v != %v", exp, actual) 216 return 217 } 218 219 if exp, actual := "example", outputConfs[0].Type; exp != actual { 220 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 221 } 222 if exp, actual := "example", outputConfs[1].Type; exp != actual { 223 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 224 } 225 if exp, actual := "example", outputConfs[2].Type; exp != actual { 226 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 227 } 228 229 plugOneConf, ok := outputConfs[0].Plugin.(*exampleConfig) 230 if !ok { 231 t.Fatalf("Wrong config type: %T", outputConfs[0].Plugin) 232 } 233 if exp, actual := 23, plugOneConf.Foo; exp != actual { 234 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 235 } 236 if exp, actual := "bar", plugOneConf.Bar; exp != actual { 237 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 238 } 239 if exp, actual := "default", plugOneConf.Baz; exp != actual { 240 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 241 } 242 243 plugTwoConf, ok := outputConfs[1].Plugin.(*exampleConfig) 244 if !ok { 245 t.Fatalf("Wrong config type: %T", outputConfs[1].Plugin) 246 } 247 if exp, actual := 23, plugTwoConf.Foo; exp != actual { 248 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 249 } 250 if exp, actual := "baz", plugTwoConf.Bar; exp != actual { 251 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 252 } 253 if exp, actual := "default", plugTwoConf.Baz; exp != actual { 254 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 255 } 256 257 plugThreeConf, ok := outputConfs[2].Plugin.(*exampleConfig) 258 if !ok { 259 t.Fatalf("Wrong config type: %T", outputConfs[2].Plugin) 260 } 261 if exp, actual := 29, plugThreeConf.Foo; exp != actual { 262 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 263 } 264 if exp, actual := "baz", plugThreeConf.Bar; exp != actual { 265 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 266 } 267 if exp, actual := "default", plugThreeConf.Baz; exp != actual { 268 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 269 } 270 } 271 272 func TestOutBrokerConfigDittoMulti(t *testing.T) { 273 testConf := []byte(`{ 274 "type": "broker", 275 "broker": { 276 "outputs": [ 277 { 278 "type": "http_client", 279 "http_client": { 280 "url": "address:1", 281 "timeout": "1ms" 282 } 283 }, 284 { 285 "type": "ditto_2", 286 "http_client": { 287 "timeout": "2ms" 288 } 289 } 290 ] 291 } 292 }`) 293 294 conf := output.NewConfig() 295 if err := json.Unmarshal(testConf, &conf); err != nil { 296 t.Error(err) 297 return 298 } 299 300 outputConfs := conf.Broker.Outputs 301 302 if exp, actual := 3, len(outputConfs); exp != actual { 303 t.Errorf("unexpected number of output configs: %v != %v", exp, actual) 304 return 305 } 306 307 if exp, actual := "http_client", outputConfs[0].Type; exp != actual { 308 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 309 } 310 if exp, actual := "http_client", outputConfs[1].Type; exp != actual { 311 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 312 } 313 if exp, actual := "http_client", outputConfs[2].Type; exp != actual { 314 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 315 } 316 317 if exp, actual := "address:1", outputConfs[0].HTTPClient.URL; exp != actual { 318 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 319 } 320 if exp, actual := "address:1", outputConfs[1].HTTPClient.URL; exp != actual { 321 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 322 } 323 if exp, actual := "address:1", outputConfs[2].HTTPClient.URL; exp != actual { 324 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 325 } 326 327 if exp, actual := "1ms", outputConfs[0].HTTPClient.Timeout; exp != actual { 328 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 329 } 330 if exp, actual := "2ms", outputConfs[1].HTTPClient.Timeout; exp != actual { 331 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 332 } 333 if exp, actual := "2ms", outputConfs[2].HTTPClient.Timeout; exp != actual { 334 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 335 } 336 } 337 338 func TestOutBrokerConfigDittoZeroed(t *testing.T) { 339 testConf := []byte(`{ 340 "type": "broker", 341 "broker": { 342 "outputs": [ 343 { 344 "type": "http_client", 345 "http_client": { 346 "url": "address:1", 347 "timeout": "1ms" 348 } 349 }, 350 { 351 "type": "ditto_0", 352 "http_client": { 353 "timeout": "2ms" 354 } 355 } 356 ] 357 } 358 }`) 359 360 conf := output.NewConfig() 361 if err := json.Unmarshal(testConf, &conf); err != nil { 362 t.Error(err) 363 return 364 } 365 366 outputConfs := conf.Broker.Outputs 367 368 if exp, actual := 1, len(outputConfs); exp != actual { 369 t.Errorf("unexpected number of output configs: %v != %v", exp, actual) 370 return 371 } 372 373 if exp, actual := "http_client", outputConfs[0].Type; exp != actual { 374 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 375 } 376 377 if exp, actual := "address:1", outputConfs[0].HTTPClient.URL; exp != actual { 378 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 379 } 380 381 if exp, actual := "1ms", outputConfs[0].HTTPClient.Timeout; exp != actual { 382 t.Errorf("Unexpected value from config: %v != %v", exp, actual) 383 } 384 }