github.com/Jeffail/benthos/v3@v3.65.0/internal/config/config_test.go (about)

     1  package config_test
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	iconfig "github.com/Jeffail/benthos/v3/internal/config"
    10  	"github.com/Jeffail/benthos/v3/lib/config"
    11  	"github.com/Jeffail/benthos/v3/lib/input"
    12  	"github.com/Jeffail/benthos/v3/lib/log"
    13  	"github.com/Jeffail/benthos/v3/lib/metrics"
    14  	"github.com/Jeffail/benthos/v3/lib/types"
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/require"
    17  
    18  	_ "github.com/Jeffail/benthos/v3/public/components/all"
    19  )
    20  
    21  func TestSetOverridesOnNothing(t *testing.T) {
    22  	conf := config.New()
    23  	rdr := iconfig.NewReader("", nil, iconfig.OptAddOverrides(
    24  		"input.type=kafka",
    25  		"input.kafka.addresses=foobarbaz.com",
    26  		"output.type=amqp_0_9",
    27  	))
    28  
    29  	lints, err := rdr.Read(&conf)
    30  	require.NoError(t, err)
    31  	assert.Empty(t, lints)
    32  
    33  	assert.Equal(t, "kafka", conf.Input.Type)
    34  	assert.Equal(t, "foobarbaz.com", conf.Input.Kafka.Addresses[0])
    35  	assert.Equal(t, "amqp_0_9", conf.Output.Type)
    36  }
    37  
    38  func TestSetOverrideErrors(t *testing.T) {
    39  	tests := []struct {
    40  		name  string
    41  		input string
    42  		err   string
    43  	}{
    44  		{
    45  			name:  "no value",
    46  			input: "input.type=",
    47  			err:   "invalid set expression 'input.type='",
    48  		},
    49  		{
    50  			name:  "no equals",
    51  			input: "input.type",
    52  			err:   "invalid set expression 'input.type'",
    53  		},
    54  		{
    55  			name:  "completely empty",
    56  			input: "",
    57  			err:   "invalid set expression ''",
    58  		},
    59  		{
    60  			name:  "cant set that",
    61  			input: "input=meow",
    62  			err:   "yaml: unmarshal errors",
    63  		},
    64  	}
    65  
    66  	for _, test := range tests {
    67  		conf := config.New()
    68  		rdr := iconfig.NewReader("", nil, iconfig.OptAddOverrides(test.input))
    69  
    70  		_, err := rdr.Read(&conf)
    71  		assert.Contains(t, err.Error(), test.err)
    72  	}
    73  }
    74  
    75  func TestSetOverridesOfFile(t *testing.T) {
    76  	dir := t.TempDir()
    77  
    78  	fullPath := filepath.Join(dir, "main.yaml")
    79  	require.NoError(t, os.WriteFile(fullPath, []byte(`
    80  input:
    81    kafka:
    82      addresses: [ foobar.com, barbaz.com ]
    83      topics: [ meow1, meow2 ]
    84  `), 0o644))
    85  
    86  	conf := config.New()
    87  	rdr := iconfig.NewReader(fullPath, nil, iconfig.OptAddOverrides(
    88  		"input.kafka.addresses.0=nope1.com",
    89  		"input.kafka.addresses.1=nope2.com",
    90  		"input.kafka.topics=justthis",
    91  		"output.type=kafka",
    92  		"output.kafka.addresses=nope3.com",
    93  		"output.kafka.topic=foobar",
    94  	))
    95  
    96  	lints, err := rdr.Read(&conf)
    97  	require.NoError(t, err)
    98  	assert.Empty(t, lints)
    99  
   100  	assert.Equal(t, "kafka", conf.Input.Type)
   101  	assert.Equal(t, []string{"nope1.com", "nope2.com"}, conf.Input.Kafka.Addresses)
   102  	assert.Equal(t, []string{"justthis"}, conf.Input.Kafka.Topics)
   103  
   104  	assert.Equal(t, "kafka", conf.Output.Type)
   105  	assert.Equal(t, []string{"nope3.com"}, conf.Output.Kafka.Addresses)
   106  	assert.Equal(t, "foobar", conf.Output.Kafka.Topic)
   107  }
   108  
   109  func TestResources(t *testing.T) {
   110  	dir := t.TempDir()
   111  
   112  	fullPath := filepath.Join(dir, "main.yaml")
   113  	require.NoError(t, os.WriteFile(fullPath, []byte(`
   114  input:
   115    kafka:
   116      addresses: [ foobar.com, barbaz.com ]
   117      topics: [ meow1, meow2 ]
   118  `), 0o644))
   119  
   120  	resourceOnePath := filepath.Join(dir, "res1.yaml")
   121  	require.NoError(t, os.WriteFile(resourceOnePath, []byte(`
   122  cache_resources:
   123    - label: foo
   124      memory:
   125        ttl: 12
   126  
   127  tests:
   128    - name: huh
   129  `), 0o644))
   130  
   131  	resourceTwoPath := filepath.Join(dir, "res2.yaml")
   132  	require.NoError(t, os.WriteFile(resourceTwoPath, []byte(`
   133  cache_resources:
   134    - label: bar
   135      memory:
   136        ttl: 13
   137  `), 0o644))
   138  
   139  	resourceThreePath := filepath.Join(dir, "res3.yaml")
   140  	require.NoError(t, os.WriteFile(resourceThreePath, []byte(`
   141  tests:
   142    - name: whut
   143  `), 0o644))
   144  
   145  	conf := config.New()
   146  	rdr := iconfig.NewReader(fullPath, []string{resourceOnePath, resourceTwoPath, resourceThreePath})
   147  
   148  	lints, err := rdr.Read(&conf)
   149  	require.NoError(t, err)
   150  	assert.Empty(t, lints)
   151  
   152  	assert.Equal(t, "kafka", conf.Input.Type)
   153  	assert.Equal(t, []string{"foobar.com", "barbaz.com"}, conf.Input.Kafka.Addresses)
   154  	assert.Equal(t, []string{"meow1", "meow2"}, conf.Input.Kafka.Topics)
   155  
   156  	require.Len(t, conf.ResourceCaches, 2)
   157  
   158  	assert.Equal(t, "foo", conf.ResourceCaches[0].Label)
   159  	assert.Equal(t, "memory", conf.ResourceCaches[0].Type)
   160  	assert.Equal(t, 12, conf.ResourceCaches[0].Memory.TTL)
   161  
   162  	assert.Equal(t, "bar", conf.ResourceCaches[1].Label)
   163  	assert.Equal(t, "memory", conf.ResourceCaches[1].Type)
   164  	assert.Equal(t, 13, conf.ResourceCaches[1].Memory.TTL)
   165  }
   166  
   167  func TestLints(t *testing.T) {
   168  	dir := t.TempDir()
   169  
   170  	fullPath := filepath.Join(dir, "main.yaml")
   171  	require.NoError(t, os.WriteFile(fullPath, []byte(`
   172  input:
   173    meow1: not this
   174    kafka:
   175      addresses: [ foobar.com, barbaz.com ]
   176      topics: [ meow1, meow2 ]
   177  `), 0o644))
   178  
   179  	resourceOnePath := filepath.Join(dir, "res1.yaml")
   180  	require.NoError(t, os.WriteFile(resourceOnePath, []byte(`
   181  cache_resources:
   182    - label: foo
   183      memory:
   184        meow2: or this
   185        ttl: 12
   186  `), 0o644))
   187  
   188  	resourceTwoPath := filepath.Join(dir, "res2.yaml")
   189  	require.NoError(t, os.WriteFile(resourceTwoPath, []byte(`
   190  cache_resources:
   191    - label: bar
   192      memory:
   193        meow3: or also this
   194        ttl: 13
   195  `), 0o644))
   196  
   197  	conf := config.New()
   198  	rdr := iconfig.NewReader(fullPath, []string{resourceOnePath, resourceTwoPath})
   199  
   200  	lints, err := rdr.Read(&conf)
   201  	require.NoError(t, err)
   202  	require.Len(t, lints, 3)
   203  	assert.Contains(t, lints[0], "/main.yaml: line 3: field meow1 ")
   204  	assert.Contains(t, lints[1], "/res1.yaml: line 5: field meow2 ")
   205  	assert.Contains(t, lints[2], "/res2.yaml: line 5: field meow3 ")
   206  
   207  	assert.Equal(t, "kafka", conf.Input.Type)
   208  	assert.Equal(t, []string{"foobar.com", "barbaz.com"}, conf.Input.Kafka.Addresses)
   209  	assert.Equal(t, []string{"meow1", "meow2"}, conf.Input.Kafka.Topics)
   210  
   211  	require.Len(t, conf.ResourceCaches, 2)
   212  
   213  	assert.Equal(t, "foo", conf.ResourceCaches[0].Label)
   214  	assert.Equal(t, "memory", conf.ResourceCaches[0].Type)
   215  	assert.Equal(t, 12, conf.ResourceCaches[0].Memory.TTL)
   216  
   217  	assert.Equal(t, "bar", conf.ResourceCaches[1].Label)
   218  	assert.Equal(t, "memory", conf.ResourceCaches[1].Type)
   219  	assert.Equal(t, 13, conf.ResourceCaches[1].Memory.TTL)
   220  }
   221  
   222  func TestLintsOfOldPlugins(t *testing.T) {
   223  	dir := t.TempDir()
   224  
   225  	fullPath := filepath.Join(dir, "main.yaml")
   226  	require.NoError(t, os.WriteFile(fullPath, []byte(`
   227  input:
   228    type: custom_plugin_for_config_old_plugins_test
   229    plugin:
   230      addresses: [ foobar.com, barbaz.com ]
   231      topics: [ meow1, meow2 ]
   232  `), 0o644))
   233  
   234  	input.RegisterPlugin("custom_plugin_for_config_old_plugins_test", func() interface{} {
   235  		v := struct{}{}
   236  		return &v
   237  	}, func(config interface{}, manager types.Manager, logger log.Modular, metrics metrics.Type) (types.Input, error) {
   238  		return nil, errors.New("nope")
   239  	})
   240  
   241  	conf := config.New()
   242  	rdr := iconfig.NewReader(fullPath, nil)
   243  
   244  	lints, err := rdr.Read(&conf)
   245  	require.NoError(t, err)
   246  	require.Len(t, lints, 0)
   247  }