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

     1  package config_test
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	iconfig "github.com/Jeffail/benthos/v3/internal/config"
     9  	"github.com/Jeffail/benthos/v3/lib/config"
    10  	"github.com/Jeffail/benthos/v3/lib/stream"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  
    14  	_ "github.com/Jeffail/benthos/v3/public/components/all"
    15  )
    16  
    17  func TestStreamsLints(t *testing.T) {
    18  	dir := t.TempDir()
    19  
    20  	streamOnePath := filepath.Join(dir, "first.yaml")
    21  	require.NoError(t, os.WriteFile(streamOnePath, []byte(`
    22  input:
    23    meow1: not this
    24    kafka:
    25      addresses: [ foobar.com, barbaz.com ]
    26      topics: [ meow1, meow2 ]
    27  `), 0o644))
    28  
    29  	streamTwoPath := filepath.Join(dir, "second.yaml")
    30  	require.NoError(t, os.WriteFile(streamTwoPath, []byte(`
    31  pipeline:
    32    processors:
    33      - bloblang: 'root = this.lowercase()'
    34  
    35  cache_resources:
    36    - label: this_shouldnt_be_here
    37      memory:
    38        ttl: 13
    39  `), 0o644))
    40  
    41  	rdr := iconfig.NewReader("", nil, iconfig.OptSetStreamPaths(streamOnePath, streamTwoPath))
    42  
    43  	conf := config.New()
    44  	lints, err := rdr.Read(&conf)
    45  	require.NoError(t, err)
    46  	require.Len(t, lints, 0)
    47  
    48  	streamConfs := map[string]stream.Config{}
    49  	lints, err = rdr.ReadStreams(streamConfs)
    50  	require.NoError(t, err)
    51  
    52  	require.Len(t, lints, 2)
    53  	assert.Contains(t, lints[0], "/first.yaml: line 3: field meow1 ")
    54  	assert.Contains(t, lints[1], "/second.yaml: line 6: field cache_resources not recognised")
    55  
    56  	require.Len(t, streamConfs, 2)
    57  
    58  	assert.Equal(t, "kafka", streamConfs["first"].Input.Type)
    59  	assert.Equal(t, []string{"foobar.com", "barbaz.com"}, streamConfs["first"].Input.Kafka.Addresses)
    60  	assert.Equal(t, []string{"meow1", "meow2"}, streamConfs["first"].Input.Kafka.Topics)
    61  }
    62  
    63  func TestStreamsDirectoryWalk(t *testing.T) {
    64  	dir := t.TempDir()
    65  
    66  	streamOnePath := filepath.Join(dir, "first.yaml")
    67  	require.NoError(t, os.WriteFile(streamOnePath, []byte(`
    68  pipeline:
    69    processors:
    70      - bloblang: 'root = "first"'
    71  `), 0o644))
    72  
    73  	require.NoError(t, os.MkdirAll(filepath.Join(dir, "nested", "inner"), 0o755))
    74  
    75  	streamTwoPath := filepath.Join(dir, "nested", "inner", "second.yaml")
    76  	require.NoError(t, os.WriteFile(streamTwoPath, []byte(`
    77  pipeline:
    78    processors:
    79      - bloblang: 'root = "second"'
    80  `), 0o644))
    81  
    82  	streamThreePath := filepath.Join(dir, "nested", "inner", "third.yaml")
    83  	require.NoError(t, os.WriteFile(streamThreePath, []byte(`
    84  pipeline:
    85    processors:
    86      - bloblang: 'root = "third"'
    87  `), 0o644))
    88  
    89  	rdr := iconfig.NewReader("", nil, iconfig.OptSetStreamPaths(streamOnePath, filepath.Join(dir, "nested")))
    90  
    91  	conf := config.New()
    92  	lints, err := rdr.Read(&conf)
    93  	require.NoError(t, err)
    94  	require.Len(t, lints, 0)
    95  
    96  	streamConfs := map[string]stream.Config{}
    97  	lints, err = rdr.ReadStreams(streamConfs)
    98  	require.NoError(t, err)
    99  	require.Len(t, lints, 0)
   100  
   101  	require.Len(t, streamConfs, 3)
   102  	require.Contains(t, streamConfs, "first")
   103  	require.Contains(t, streamConfs, "inner_second")
   104  	require.Contains(t, streamConfs, "inner_third")
   105  
   106  	assert.Equal(t, `root = "first"`, string(streamConfs["first"].Pipeline.Processors[0].Bloblang))
   107  	assert.Equal(t, `root = "second"`, string(streamConfs["inner_second"].Pipeline.Processors[0].Bloblang))
   108  	assert.Equal(t, `root = "third"`, string(streamConfs["inner_third"].Pipeline.Processors[0].Bloblang))
   109  }