From 8c4ab22e5db088d040a1c6625afedc9a7d08e976 Mon Sep 17 00:00:00 2001 From: Joshua Williams Date: Mon, 7 Sep 2020 23:50:40 -0400 Subject: [PATCH] Increased test coverage for the plugin package --- agent/builder.go | 10 +- .../builtin/input/generate/generate_test.go | 5 +- operator/config.go | 7 +- pipeline/config.go | 11 +- pipeline/config_test.go | 35 ++--- .../parameter.go | 24 ++-- plugin/parameter_test.go | 126 ++++++++++++++++++ {operator => plugin}/plugin.go | 53 ++++---- {operator => plugin}/plugin_test.go | 59 +++++--- testutil/util.go | 5 +- 10 files changed, 242 insertions(+), 93 deletions(-) rename operator/plugin_parameter.go => plugin/parameter.go (85%) create mode 100644 plugin/parameter_test.go rename {operator => plugin}/plugin.go (72%) rename {operator => plugin}/plugin_test.go (90%) diff --git a/agent/builder.go b/agent/builder.go index c261580d2..a3b3636fb 100644 --- a/agent/builder.go +++ b/agent/builder.go @@ -4,6 +4,7 @@ import ( "github.com/observiq/stanza/database" "github.com/observiq/stanza/errors" "github.com/observiq/stanza/operator" + "github.com/observiq/stanza/plugin" "go.uber.org/zap" ) @@ -49,18 +50,17 @@ func (b *LogAgentBuilder) Build() (*LogAgent, error) { return nil, errors.Wrap(err, "open database") } - registry, err := operator.NewPluginRegistry(b.pluginDir) + registry, err := plugin.NewPluginRegistry(b.pluginDir) if err != nil { return nil, errors.Wrap(err, "load plugin registry") } buildContext := operator.BuildContext{ - Logger: b.logger, - PluginRegistry: registry, - Database: db, + Logger: b.logger, + Database: db, } - pipeline, err := b.cfg.Pipeline.BuildPipeline(buildContext, b.defaultOutput) + pipeline, err := b.cfg.Pipeline.BuildPipeline(buildContext, registry, b.defaultOutput) if err != nil { return nil, err } diff --git a/operator/builtin/input/generate/generate_test.go b/operator/builtin/input/generate/generate_test.go index a36e8588c..5485f81eb 100644 --- a/operator/builtin/input/generate/generate_test.go +++ b/operator/builtin/input/generate/generate_test.go @@ -8,6 +8,7 @@ import ( "github.com/observiq/stanza/entry" "github.com/observiq/stanza/operator" "github.com/observiq/stanza/operator/helper" + "github.com/observiq/stanza/plugin" "github.com/observiq/stanza/testutil" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" @@ -66,7 +67,7 @@ pipeline: tmpl, err := template.New("my_generator").Parse(templateText) require.NoError(t, err) - registry := operator.PluginRegistry{ + registry := plugin.Registry{ "sample": tmpl, } @@ -76,7 +77,7 @@ pipeline: config, err := registry.Render("sample", params) require.NoError(t, err) - expectedConfig := operator.PluginConfig{ + expectedConfig := plugin.Plugin{ Pipeline: []operator.Config{ { Builder: &GenerateInputConfig{ diff --git a/operator/config.go b/operator/config.go index 74eb566dd..9ae23f53d 100644 --- a/operator/config.go +++ b/operator/config.go @@ -25,10 +25,9 @@ type Builder interface { // BuildContext supplies contextual resources when building an operator. type BuildContext struct { - PluginRegistry PluginRegistry - Database database.Database - Parameters map[string]interface{} - Logger *zap.SugaredLogger + Database database.Database + Parameters map[string]interface{} + Logger *zap.SugaredLogger } // registry is a global registry of operator types to operator builders. diff --git a/pipeline/config.go b/pipeline/config.go index b2e7c843e..73ed6f63d 100644 --- a/pipeline/config.go +++ b/pipeline/config.go @@ -7,6 +7,7 @@ import ( "github.com/observiq/stanza/errors" "github.com/observiq/stanza/operator" "github.com/observiq/stanza/operator/helper" + "github.com/observiq/stanza/plugin" yaml "gopkg.in/yaml.v2" ) @@ -14,8 +15,8 @@ import ( type Config []Params // BuildPipeline will build a pipeline from the config. -func (c Config) BuildPipeline(context operator.BuildContext, defaultOutput operator.Operator) (*DirectedPipeline, error) { - operatorConfigs, err := c.buildOperatorConfigs(context.PluginRegistry) +func (c Config) BuildPipeline(context operator.BuildContext, pluginRegistry plugin.Registry, defaultOutput operator.Operator) (*DirectedPipeline, error) { + operatorConfigs, err := c.buildOperatorConfigs(pluginRegistry) if err != nil { return nil, err } @@ -37,7 +38,7 @@ func (c Config) BuildPipeline(context operator.BuildContext, defaultOutput opera return pipeline, nil } -func (c Config) buildOperatorConfigs(pluginRegistry operator.PluginRegistry) ([]operator.Config, error) { +func (c Config) buildOperatorConfigs(pluginRegistry plugin.Registry) ([]operator.Config, error) { operatorConfigs := make([]operator.Config, 0, len(c)) for i, params := range c { @@ -194,7 +195,7 @@ func (p Params) getStringArray(key string) []string { } // BuildConfigs will build operator configs from a params map. -func (p Params) BuildConfigs(pluginRegistry operator.PluginRegistry, namespace string, defaultOutput []string) ([]operator.Config, error) { +func (p Params) BuildConfigs(pluginRegistry plugin.Registry, namespace string, defaultOutput []string) ([]operator.Config, error) { if operator.IsDefined(p.Type()) { return p.buildAsBuiltin(namespace) } @@ -232,7 +233,7 @@ func (p Params) buildAsBuiltin(namespace string) ([]operator.Config, error) { } // buildPlugin will build a plugin config from a params map. -func (p Params) buildPlugin(pluginRegistry operator.PluginRegistry, namespace string, defaultOutput []string) ([]operator.Config, error) { +func (p Params) buildPlugin(pluginRegistry plugin.Registry, namespace string, defaultOutput []string) ([]operator.Config, error) { templateParams := map[string]interface{}{} for key, value := range p { templateParams[key] = value diff --git a/pipeline/config_test.go b/pipeline/config_test.go index 1f93a591c..2daeda78a 100644 --- a/pipeline/config_test.go +++ b/pipeline/config_test.go @@ -9,6 +9,7 @@ import ( _ "github.com/observiq/stanza/operator/builtin/input/generate" "github.com/observiq/stanza/operator/builtin/output/drop" _ "github.com/observiq/stanza/operator/builtin/transformer/noop" + "github.com/observiq/stanza/plugin" "github.com/observiq/stanza/testutil" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" @@ -201,7 +202,8 @@ pipeline: message: test output: {{.output}} ` - err := context.PluginRegistry.Add("plugin", pluginTemplate) + registry := plugin.Registry{} + err := registry.Add("plugin", pluginTemplate) require.NoError(t, err) pipelineConfig := Config{ @@ -216,7 +218,7 @@ pipeline: }, } - _, err = pipelineConfig.BuildPipeline(context, nil) + _, err = pipelineConfig.BuildPipeline(context, registry, nil) require.NoError(t, err) } @@ -239,7 +241,7 @@ func TestBuildValidPipelineDefaultOutput(t *testing.T) { defaultOutput, err := drop.NewDropOutputConfig("$.drop_it").Build(context) require.NoError(t, err) - pl, err := pipelineConfig.BuildPipeline(context, defaultOutput) + pl, err := pipelineConfig.BuildPipeline(context, nil, defaultOutput) require.NoError(t, err) require.True(t, pl.Graph.HasEdgeFromTo(createNodeID("$.generate_input"), createNodeID("$.drop_it"))) } @@ -267,7 +269,7 @@ func TestBuildValidPipelineNextOutputAndDefaultOutput(t *testing.T) { defaultOutput, err := drop.NewDropOutputConfig("$.drop_it").Build(context) require.NoError(t, err) - pl, err := pipelineConfig.BuildPipeline(context, defaultOutput) + pl, err := pipelineConfig.BuildPipeline(context, nil, defaultOutput) require.NoError(t, err) require.True(t, pl.Graph.HasEdgeFromTo(createNodeID("$.generate_input"), createNodeID("$.noop"))) require.True(t, pl.Graph.HasEdgeFromTo(createNodeID("$.noop"), createNodeID("$.drop_it"))) @@ -284,7 +286,8 @@ pipeline: record: message: test ` - err := context.PluginRegistry.Add("plugin", pluginTemplate) + registry := plugin.Registry{} + err := registry.Add("plugin", pluginTemplate) require.NoError(t, err) pipelineConfig := Config{ @@ -297,7 +300,7 @@ pipeline: defaultOutput, err := drop.NewDropOutputConfig("$.drop_it").Build(context) require.NoError(t, err) - pl, err := pipelineConfig.BuildPipeline(context, defaultOutput) + pl, err := pipelineConfig.BuildPipeline(context, registry, defaultOutput) require.NoError(t, err) require.True(t, pl.Graph.HasEdgeFromTo(createNodeID("$.plugin.plugin_generate"), createNodeID("$.drop_it"))) } @@ -317,7 +320,7 @@ func TestBuildInvalidPipelineInvalidType(t *testing.T) { }, } - _, err := pipelineConfig.BuildPipeline(context, nil) + _, err := pipelineConfig.BuildPipeline(context, nil, nil) require.Error(t, err) require.Contains(t, err.Error(), "unsupported `type` for operator config") } @@ -333,7 +336,8 @@ pipeline: message: test output: {{.output}} ` - err := context.PluginRegistry.Add("plugin", pluginTemplate) + registry := plugin.Registry{} + err := registry.Add("plugin", pluginTemplate) require.NoError(t, err) pipelineConfig := Config{ @@ -348,7 +352,7 @@ pipeline: }, } - _, err = pipelineConfig.BuildPipeline(context, nil) + _, err = pipelineConfig.BuildPipeline(context, registry, nil) require.Error(t, err) require.Contains(t, err.Error(), "build operator configs") } @@ -368,7 +372,7 @@ func TestBuildInvalidPipelineInvalidOperator(t *testing.T) { } context := testutil.NewBuildContext(t) - _, err := pipelineConfig.BuildPipeline(context, nil) + _, err := pipelineConfig.BuildPipeline(context, nil, nil) require.Error(t, err) require.Contains(t, err.Error(), "field number not found") } @@ -393,7 +397,7 @@ func TestBuildInvalidPipelineInvalidGraph(t *testing.T) { } context := testutil.NewBuildContext(t) - _, err := pipelineConfig.BuildPipeline(context, nil) + _, err := pipelineConfig.BuildPipeline(context, nil, nil) require.Error(t, err) require.Contains(t, err.Error(), "does not exist") } @@ -413,7 +417,8 @@ pipeline: record: test output: {{.output}} ` - err := context.PluginRegistry.Add("plugin", pluginTemplate) + registry := plugin.Registry{} + err := registry.Add("plugin", pluginTemplate) require.NoError(t, err) config := Config{ @@ -427,7 +432,7 @@ pipeline: }, } - configs, err := config.buildOperatorConfigs(context.PluginRegistry) + configs, err := config.buildOperatorConfigs(registry) require.NoError(t, err) require.Len(t, configs, 3) @@ -526,7 +531,7 @@ func TestBuildPipelineWithFailingOperator(t *testing.T) { config := Config{ {"type": "invalid_operator"}, } - _, err := config.BuildPipeline(ctx, nil) + _, err := config.BuildPipeline(ctx, nil, nil) require.Error(t, err) require.Contains(t, err.Error(), "failed to build operator") } @@ -536,7 +541,7 @@ func TestBuildPipelineWithInvalidParam(t *testing.T) { config := Config{ {"missing": "type"}, } - _, err := config.BuildPipeline(ctx, nil) + _, err := config.BuildPipeline(ctx, nil, nil) require.Error(t, err) require.Contains(t, err.Error(), "missing required `type` field") } diff --git a/operator/plugin_parameter.go b/plugin/parameter.go similarity index 85% rename from operator/plugin_parameter.go rename to plugin/parameter.go index 0481ce39b..ce5106d20 100644 --- a/operator/plugin_parameter.go +++ b/plugin/parameter.go @@ -1,4 +1,4 @@ -package operator +package plugin import ( "fmt" @@ -6,8 +6,8 @@ import ( "github.com/observiq/stanza/errors" ) -// PluginParameter is a basic description of a plugin's parameter. -type PluginParameter struct { +// Parameter is a basic description of a plugin's parameter. +type Parameter struct { Label string Description string Required bool @@ -16,7 +16,7 @@ type PluginParameter struct { Default interface{} // Must be valid according to Type & ValidValues } -func (param PluginParameter) validate() error { +func (param Parameter) validate() error { if param.Required && param.Default != nil { return errors.NewError( "required parameter cannot have a default value", @@ -39,7 +39,7 @@ func (param PluginParameter) validate() error { return nil } -func (param PluginParameter) validateType() error { +func (param Parameter) validateType() error { switch param.Type { case "string", "int", "bool", "strings", "enum": // ok default: @@ -51,7 +51,7 @@ func (param PluginParameter) validateType() error { return nil } -func (param PluginParameter) validateValidValues() error { +func (param Parameter) validateValidValues() error { switch param.Type { case "string", "int", "bool", "strings": if len(param.ValidValues) > 0 { @@ -71,7 +71,7 @@ func (param PluginParameter) validateValidValues() error { return nil } -func (param PluginParameter) validateDefault() error { +func (param Parameter) validateDefault() error { if param.Default == nil { return nil } @@ -96,7 +96,7 @@ func (param PluginParameter) validateDefault() error { } } -func validateStringDefault(param PluginParameter) error { +func validateStringDefault(param Parameter) error { if _, ok := param.Default.(string); !ok { return errors.NewError( "default value for a parameter of type 'string' must be a string", @@ -106,7 +106,7 @@ func validateStringDefault(param PluginParameter) error { return nil } -func validateIntDefault(param PluginParameter) error { +func validateIntDefault(param Parameter) error { switch param.Default.(type) { case int, int32, int64: return nil @@ -118,7 +118,7 @@ func validateIntDefault(param PluginParameter) error { } } -func validateBoolDefault(param PluginParameter) error { +func validateBoolDefault(param Parameter) error { if _, ok := param.Default.(bool); !ok { return errors.NewError( "default value for a parameter of type 'bool' must be a boolean", @@ -128,7 +128,7 @@ func validateBoolDefault(param PluginParameter) error { return nil } -func validateStringArrayDefault(param PluginParameter) error { +func validateStringArrayDefault(param Parameter) error { defaultList, ok := param.Default.([]interface{}) if !ok { return errors.NewError( @@ -147,7 +147,7 @@ func validateStringArrayDefault(param PluginParameter) error { return nil } -func validateEnumDefault(param PluginParameter) error { +func validateEnumDefault(param Parameter) error { def, ok := param.Default.(string) if !ok { return errors.NewError( diff --git a/plugin/parameter_test.go b/plugin/parameter_test.go new file mode 100644 index 000000000..723e4de24 --- /dev/null +++ b/plugin/parameter_test.go @@ -0,0 +1,126 @@ +package plugin + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestValidateDefault(t *testing.T) { + testCases := []struct { + name string + expectErr bool + param Parameter + }{ + { + "ValidStringDefault", + false, + Parameter{ + Type: "string", + Default: "test", + }, + }, + { + "InvalidStringDefault", + true, + Parameter{ + Type: "string", + Default: 5, + }, + }, + { + "ValidIntDefault", + false, + Parameter{ + Type: "int", + Default: 5, + }, + }, + { + "InvalidStringDefault", + true, + Parameter{ + Type: "int", + Default: "test", + }, + }, + { + "ValidBoolDefault", + false, + Parameter{ + Type: "bool", + Default: true, + }, + }, + { + "InvalidBoolDefault", + true, + Parameter{ + Type: "bool", + Default: "test", + }, + }, + { + "ValidStringsDefault", + false, + Parameter{ + Type: "strings", + Default: []interface{}{"test"}, + }, + }, + { + "InvalidStringsDefault", + true, + Parameter{ + Type: "strings", + Default: []interface{}{5}, + }, + }, + { + "ValidEnumDefault", + false, + Parameter{ + Type: "enum", + ValidValues: []string{"test"}, + Default: "test", + }, + }, + { + "InvalidEnumDefault", + true, + Parameter{ + Type: "enum", + ValidValues: []string{"test"}, + Default: "invalid", + }, + }, + { + "NonStringEnumDefault", + true, + Parameter{ + Type: "enum", + ValidValues: []string{"test"}, + Default: 5, + }, + }, + { + "InvalidTypeDefault", + true, + Parameter{ + Type: "float", + Default: 5, + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + err := tc.param.validateDefault() + if tc.expectErr { + require.Error(t, err) + } else { + require.NoError(t, err) + } + }) + } +} diff --git a/operator/plugin.go b/plugin/plugin.go similarity index 72% rename from operator/plugin.go rename to plugin/plugin.go index ea14a73e9..53b7c2257 100644 --- a/operator/plugin.go +++ b/plugin/plugin.go @@ -1,4 +1,4 @@ -package operator +package plugin import ( "bytes" @@ -9,26 +9,27 @@ import ( "text/template" "github.com/observiq/stanza/errors" + "github.com/observiq/stanza/operator" yaml "gopkg.in/yaml.v2" ) -// PluginConfig is the rendered config of a plugin. -type PluginConfig struct { +// Plugin is the rendered result of a plugin template. +type Plugin struct { Version string Title string Description string - Parameters map[string]PluginParameter - Pipeline []Config + Parameters map[string]Parameter + Pipeline []operator.Config } -// PluginRegistry is a registry of plugin templates. -type PluginRegistry map[string]*template.Template +// Registry is a registry of plugin templates. +type Registry map[string]*template.Template -// Render will render a plugin config using the params and plugin type. -func (r PluginRegistry) Render(pluginType string, params map[string]interface{}) (PluginConfig, error) { +// Render will render a plugin using the params and plugin type. +func (r Registry) Render(pluginType string, params map[string]interface{}) (Plugin, error) { template, ok := r[pluginType] if !ok { - return PluginConfig{}, errors.NewError( + return Plugin{}, errors.NewError( "plugin type does not exist", "ensure that all plugins are defined with a registered type", "plugin_type", pluginType, @@ -37,7 +38,7 @@ func (r PluginRegistry) Render(pluginType string, params map[string]interface{}) var writer bytes.Buffer if err := template.Execute(&writer, params); err != nil { - return PluginConfig{}, errors.NewError( + return Plugin{}, errors.NewError( "failed to render template for plugin", "ensure that all parameters are valid for the plugin", "plugin_type", pluginType, @@ -45,10 +46,10 @@ func (r PluginRegistry) Render(pluginType string, params map[string]interface{}) ) } - var config PluginConfig - if err := yaml.UnmarshalStrict(writer.Bytes(), &config); err != nil { - return PluginConfig{}, errors.NewError( - "failed to unmarshal plugin template to plugin config", + var plugin Plugin + if err := yaml.UnmarshalStrict(writer.Bytes(), &plugin); err != nil { + return Plugin{}, errors.NewError( + "failed to unmarshal plugin template to plugin", "ensure that the plugin template renders a valid pipeline", "plugin_type", pluginType, "rendered_config", writer.String(), @@ -56,10 +57,10 @@ func (r PluginRegistry) Render(pluginType string, params map[string]interface{}) ) } - for name, param := range config.Parameters { + for name, param := range plugin.Parameters { if err := param.validate(); err != nil { - return PluginConfig{}, errors.NewError( - "invalid parameter found in plugin config", + return Plugin{}, errors.NewError( + "invalid parameter found in plugin", "ensure that all parameters are valid for the plugin", "plugin_type", pluginType, "plugin_parameter", name, @@ -69,17 +70,17 @@ func (r PluginRegistry) Render(pluginType string, params map[string]interface{}) } } - return config, nil + return plugin, nil } // IsDefined returns a boolean indicating if a plugin is defined and registered. -func (r PluginRegistry) IsDefined(pluginType string) bool { +func (r Registry) IsDefined(pluginType string) bool { _, ok := r[pluginType] return ok } // LoadAll will load all plugin templates contained in a directory. -func (r PluginRegistry) LoadAll(dir string, pattern string) error { +func (r Registry) LoadAll(dir string, pattern string) error { glob := filepath.Join(dir, pattern) filePaths, err := filepath.Glob(glob) if err != nil { @@ -109,7 +110,7 @@ func (r PluginRegistry) LoadAll(dir string, pattern string) error { } // Load will load a plugin template from a file path. -func (r PluginRegistry) Load(path string) error { +func (r Registry) Load(path string) error { fileName := filepath.Base(path) pluginType := strings.TrimSuffix(fileName, filepath.Ext(fileName)) @@ -122,8 +123,8 @@ func (r PluginRegistry) Load(path string) error { } // Add will add a plugin to the registry. -func (r PluginRegistry) Add(pluginType string, contents string) error { - if IsDefined(pluginType) { +func (r Registry) Add(pluginType string, contents string) error { + if operator.IsDefined(pluginType) { return fmt.Errorf("plugin type %s already exists as a builtin plugin", pluginType) } @@ -137,8 +138,8 @@ func (r PluginRegistry) Add(pluginType string, contents string) error { } // NewPluginRegistry creates a new plugin registry from a plugin directory. -func NewPluginRegistry(dir string) (PluginRegistry, error) { - registry := PluginRegistry{} +func NewPluginRegistry(dir string) (Registry, error) { + registry := Registry{} if err := registry.LoadAll(dir, "*.yaml"); err != nil { return registry, err } diff --git a/operator/plugin_test.go b/plugin/plugin_test.go similarity index 90% rename from operator/plugin_test.go rename to plugin/plugin_test.go index a0b39bea2..3c85dbfee 100644 --- a/operator/plugin_test.go +++ b/plugin/plugin_test.go @@ -1,4 +1,4 @@ -package operator +package plugin import ( "io/ioutil" @@ -7,6 +7,7 @@ import ( "testing" "text/template" + "github.com/observiq/stanza/operator" "github.com/stretchr/testify/require" ) @@ -23,12 +24,8 @@ func NewTempDir(t *testing.T) string { return tempDir } -func TestPluginRegistry_LoadAll(t *testing.T) { - tempDir, err := ioutil.TempDir("", "") - require.NoError(t, err) - t.Cleanup(func() { - os.RemoveAll(tempDir) - }) +func TestNewRegistry(t *testing.T) { + tempDir := NewTempDir(t) test1 := []byte(` id: my_generator @@ -46,21 +43,31 @@ record: message2: {{ .message }} `) - err = ioutil.WriteFile(filepath.Join(tempDir, "test1.yaml"), test1, 0666) + err := ioutil.WriteFile(filepath.Join(tempDir, "test1.yaml"), test1, 0666) require.NoError(t, err) err = ioutil.WriteFile(filepath.Join(tempDir, "test2.yaml"), test2, 0666) require.NoError(t, err) - pluginRegistry := PluginRegistry{} - err = pluginRegistry.LoadAll(tempDir, "*.yaml") + registry, err := NewPluginRegistry(tempDir) require.NoError(t, err) - require.Equal(t, 2, len(pluginRegistry)) + require.Equal(t, 2, len(registry)) + require.True(t, registry.IsDefined("test1")) + require.True(t, registry.IsDefined("test2")) } -func TestPluginRegistryRender(t *testing.T) { +func TestNewRegistryFailure(t *testing.T) { + tempDir := NewTempDir(t) + err := ioutil.WriteFile(filepath.Join(tempDir, "invalid.yaml"), []byte("pipeline:"), 0111) + require.NoError(t, err) + + _, err = NewPluginRegistry(tempDir) + require.Error(t, err) +} + +func TestRegistryRender(t *testing.T) { t.Run("ErrorTypeDoesNotExist", func(t *testing.T) { - reg := PluginRegistry{} + reg := Registry{} _, err := reg.Render("unknown", map[string]interface{}{}) require.Error(t, err) require.Contains(t, err.Error(), "does not exist") @@ -70,7 +77,7 @@ func TestPluginRegistryRender(t *testing.T) { tmpl, err := template.New("plugintype").Parse(`{{ .panicker }}`) require.NoError(t, err) - reg := PluginRegistry{ + reg := Registry{ "plugintype": tmpl, } params := map[string]interface{}{ @@ -83,24 +90,24 @@ func TestPluginRegistryRender(t *testing.T) { }) } -func TestPluginRegistryLoad(t *testing.T) { +func TestRegistryLoad(t *testing.T) { t.Run("LoadAllBadGlob", func(t *testing.T) { - reg := PluginRegistry{} + reg := Registry{} err := reg.LoadAll("", `[]`) require.Error(t, err) require.Contains(t, err.Error(), "with glob pattern") }) t.Run("AddDuplicate", func(t *testing.T) { - reg := PluginRegistry{} - Register("copy", func() Builder { return nil }) + reg := Registry{} + operator.Register("copy", func() operator.Builder { return nil }) err := reg.Add("copy", "pipeline:\n") require.Error(t, err) require.Contains(t, err.Error(), "already exists") }) t.Run("AddBadTemplate", func(t *testing.T) { - reg := PluginRegistry{} + reg := Registry{} err := reg.Add("new", "{{ nofunc }") require.Error(t, err) require.Contains(t, err.Error(), "as a plugin template") @@ -112,7 +119,7 @@ func TestPluginRegistryLoad(t *testing.T) { err := ioutil.WriteFile(pluginPath, []byte("pipeline:\n"), 0755) require.NoError(t, err) - reg := PluginRegistry{} + reg := Registry{} err = reg.LoadAll(tempDir, "*.yaml") require.Error(t, err) }) @@ -615,7 +622,7 @@ pipeline: for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - reg := PluginRegistry{} + reg := Registry{} err := reg.Add(tc.name, tc.template) require.NoError(t, err) _, err = reg.Render(tc.name, map[string]interface{}{}) @@ -627,3 +634,13 @@ pipeline: }) } } + +func TestDefaultPluginFuncWithValue(t *testing.T) { + result := defaultPluginFunc("default_value", "supplied_value") + require.Equal(t, "supplied_value", result) +} + +func TestDefaultPluginFuncWithoutValue(t *testing.T) { + result := defaultPluginFunc("default_value", nil) + require.Equal(t, "default_value", result) +} diff --git a/testutil/util.go b/testutil/util.go index fdabbbf5a..2754e14bd 100644 --- a/testutil/util.go +++ b/testutil/util.go @@ -55,9 +55,8 @@ func NewTestDatabase(t testing.TB) *bbolt.DB { // NewBuildContext will return a new build context for testing func NewBuildContext(t testing.TB) operator.BuildContext { return operator.BuildContext{ - PluginRegistry: make(operator.PluginRegistry), - Database: NewTestDatabase(t), - Logger: zaptest.NewLogger(t).Sugar(), + Database: NewTestDatabase(t), + Logger: zaptest.NewLogger(t).Sugar(), } }