Skip to content

Commit dd46c30

Browse files
authored
fix(test): enable template support in test command (#358)
* fix(test): enable template support in test command Templates loaded via the -t flag were not available during test execution because the manager defaulted to bundle.GlobalEnvironment instead of using the environment where templates were registered. This fix explicitly passes the environment to the manager, matching the pattern used by the run command (internal/cli/common/manager.go:107). The issue only affects RPK, which uses a cloned environment rather than bundle.GlobalEnvironment directly. Standalone Benthos is unaffected. Changes: - Add manager.OptSetEnvironment(p.env) in processors_provider.go - Add integration test verifying templates work with ProcessorsProvider ---------
1 parent c0ce014 commit dd46c30

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

internal/cli/test/processors_provider.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@ func (b *bloblangProc) Close(context.Context) error {
135135
//------------------------------------------------------------------------------
136136

137137
func (p *ProcessorsProvider) initProcs(confs cachedConfig) ([]processor.V1, error) {
138-
mgr, err := manager.New(confs.mgr, manager.OptSetLogger(p.logger))
138+
mgr, err := manager.New(confs.mgr,
139+
manager.OptSetLogger(p.logger),
140+
manager.OptSetEnvironment(p.env),
141+
)
139142
if err != nil {
140143
return nil, fmt.Errorf("failed to initialise resources: %v", err)
141144
}

internal/cli/test/processors_provider_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ import (
1313
"github.com/stretchr/testify/require"
1414
yaml "gopkg.in/yaml.v3"
1515

16+
"github.com/redpanda-data/benthos/v4/internal/bloblang"
1617
"github.com/redpanda-data/benthos/v4/internal/bundle"
1718
"github.com/redpanda-data/benthos/v4/internal/cli/test"
1819
"github.com/redpanda-data/benthos/v4/internal/component/processor"
1920
"github.com/redpanda-data/benthos/v4/internal/config"
2021
"github.com/redpanda-data/benthos/v4/internal/log"
2122
"github.com/redpanda-data/benthos/v4/internal/message"
23+
"github.com/redpanda-data/benthos/v4/internal/template"
2224

2325
_ "github.com/redpanda-data/benthos/v4/public/components/io"
2426
_ "github.com/redpanda-data/benthos/v4/public/components/pure"
@@ -413,3 +415,55 @@ pipeline:
413415
_, err = provider.Provide("/pipeline/processors", nil, nil)
414416
require.EqualError(t, err, "failed to initialise resources: cache resource label 'barcache' collides with a previously defined resource")
415417
}
418+
419+
func TestProcessorsProviderWithTemplate(t *testing.T) {
420+
env := bundle.GlobalEnvironment.Clone()
421+
bloblEnv := bloblang.GlobalEnvironment()
422+
423+
// Register a simple test template
424+
templateYAML := []byte(`
425+
name: test_uppercase_processor
426+
type: processor
427+
fields:
428+
- name: prefix
429+
type: string
430+
default: "PREFIX: "
431+
mapping: |
432+
root.mapping = """root = "%s" + content().string().uppercase()""".format(this.prefix)
433+
`)
434+
435+
err := template.RegisterTemplateYAML(env, bloblEnv, templateYAML)
436+
require.NoError(t, err)
437+
438+
// Create test config using the template
439+
files := map[string]string{
440+
"config.yaml": `
441+
pipeline:
442+
processors:
443+
- test_uppercase_processor:
444+
prefix: "TEST: "
445+
`,
446+
}
447+
448+
testDir, err := initTestFiles(t, files)
449+
require.NoError(t, err)
450+
defer os.RemoveAll(testDir)
451+
452+
// Create ProcessorsProvider with the environment containing the template
453+
configPath := filepath.Join(testDir, "config.yaml")
454+
provider := test.NewProcessorsProvider(configPath, nil, config.Spec(), env, log.Noop())
455+
456+
// Extract processors - this should work now that we pass the environment
457+
procs, err := provider.Provide("/pipeline/processors", nil, nil)
458+
require.NoError(t, err)
459+
require.Len(t, procs, 1)
460+
461+
// Test the processor works correctly
462+
msg := message.Batch{message.NewPart([]byte("hello"))}
463+
results, err := procs[0].ProcessBatch(context.Background(), msg)
464+
require.NoError(t, err)
465+
require.Len(t, results, 1)
466+
require.Len(t, results[0], 1)
467+
468+
assert.Equal(t, "TEST: HELLO", string(results[0][0].AsBytes()))
469+
}

0 commit comments

Comments
 (0)