github.com/Jeffail/benthos/v3@v3.65.0/lib/input/broker_test.go (about)

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