github.com/Jeffail/benthos/v3@v3.65.0/lib/manager/plugin_test.go (about)

     1  package manager
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/Jeffail/benthos/v3/lib/log"
     9  	"github.com/Jeffail/benthos/v3/lib/metrics"
    10  	"github.com/Jeffail/benthos/v3/lib/types"
    11  	yaml "gopkg.in/yaml.v3"
    12  )
    13  
    14  type mockPluginConf struct {
    15  	Foo string `json:"foo" yaml:"foo"`
    16  	Bar string `json:"bar" yaml:"bar"`
    17  	Baz int    `json:"baz" yaml:"baz"`
    18  }
    19  
    20  func newMockPluginConf() interface{} {
    21  	return &mockPluginConf{
    22  		Foo: "default",
    23  		Bar: "change this",
    24  		Baz: 10,
    25  	}
    26  }
    27  
    28  func TestYAMLPlugin(t *testing.T) {
    29  	errTest := errors.New("test err")
    30  
    31  	RegisterPlugin("foo", newMockPluginConf,
    32  		func(conf interface{}, mgr types.Manager, logger log.Modular, stats metrics.Type) (interface{}, error) {
    33  			mConf, ok := conf.(*mockPluginConf)
    34  			if !ok {
    35  				t.Fatalf("failed to cast config: %T", conf)
    36  			}
    37  			if exp, act := "default", mConf.Foo; exp != act {
    38  				t.Errorf("Wrong config value: %v != %v", act, exp)
    39  			}
    40  			if exp, act := "custom", mConf.Bar; exp != act {
    41  				t.Errorf("Wrong config value: %v != %v", act, exp)
    42  			}
    43  			if exp, act := 10, mConf.Baz; exp != act {
    44  				t.Errorf("Wrong config value: %v != %v", act, exp)
    45  			}
    46  			return nil, errTest
    47  		})
    48  
    49  	confStr := `plugins:
    50    foobar:
    51      type: foo
    52      plugin:
    53        bar: custom`
    54  
    55  	conf := NewConfig()
    56  	if err := yaml.Unmarshal([]byte(confStr), &conf); err != nil {
    57  		t.Fatal(err)
    58  	}
    59  
    60  	exp := "failed to create plugin resource 'foobar' of type 'foo': test err"
    61  	_, err := New(conf, nil, log.Noop(), metrics.Noop())
    62  	if act := err.Error(); act != exp {
    63  		t.Errorf("Wrong error returned: %v != %v", act, exp)
    64  	}
    65  }
    66  
    67  func TestPluginDescriptions(t *testing.T) {
    68  	RegisterPlugin("foo", newMockPluginConf, nil)
    69  	RegisterPlugin("bar", newMockPluginConf, nil)
    70  	DocumentPlugin("bar", "This is a bar plugin.", func(conf interface{}) interface{} {
    71  		mConf, ok := conf.(*mockPluginConf)
    72  		if !ok {
    73  			t.Fatalf("failed to cast config: %T", conf)
    74  		}
    75  		return map[string]interface{}{
    76  			"foo": mConf.Foo,
    77  			"bar": mConf.Bar,
    78  		}
    79  	})
    80  
    81  	exp := `Resource Plugins
    82  ================
    83  
    84  This document has been generated, do not edit it directly.
    85  
    86  This document lists any resource plugins that this flavour of Benthos offers.
    87  
    88  ### Contents
    89  
    90  1. [` + "`bar`" + `](#bar)
    91  2. [` + "`foo`" + `](#foo)
    92  
    93  ## ` + "`bar`" + `
    94  
    95  ` + "``` yaml" + `
    96  caches: {}
    97  conditions: {}
    98  inputs: {}
    99  outputs: {}
   100  plugins:
   101    example:
   102      type: bar
   103      plugin:
   104        bar: change this
   105        foo: default
   106  processors: {}
   107  rate_limits: {}
   108  ` + "```" + `
   109  
   110  This is a bar plugin.
   111  
   112  ## ` + "`foo`" + `
   113  
   114  ` + "``` yaml" + `
   115  caches: {}
   116  conditions: {}
   117  inputs: {}
   118  outputs: {}
   119  plugins:
   120    example:
   121      type: foo
   122      plugin:
   123        foo: default
   124        bar: change this
   125        baz: 10
   126  processors: {}
   127  rate_limits: {}
   128  ` + "```" + `
   129  `
   130  
   131  	act := PluginDescriptions()
   132  	if exp != act {
   133  		t.Logf("Expected:\n%v\n", exp)
   134  		t.Logf("Actual:\n%v\n", act)
   135  		t.Error("Wrong descriptions")
   136  	}
   137  }
   138  
   139  func TestJSONPlugin(t *testing.T) {
   140  	errTest := errors.New("test err")
   141  
   142  	RegisterPlugin("foo", newMockPluginConf,
   143  		func(conf interface{}, mgr types.Manager, logger log.Modular, stats metrics.Type) (interface{}, error) {
   144  			mConf, ok := conf.(*mockPluginConf)
   145  			if !ok {
   146  				t.Fatalf("failed to cast config: %T", conf)
   147  			}
   148  			if exp, act := "default", mConf.Foo; exp != act {
   149  				t.Errorf("Wrong config value: %v != %v", act, exp)
   150  			}
   151  			if exp, act := "custom", mConf.Bar; exp != act {
   152  				t.Errorf("Wrong config value: %v != %v", act, exp)
   153  			}
   154  			if exp, act := 10, mConf.Baz; exp != act {
   155  				t.Errorf("Wrong config value: %v != %v", act, exp)
   156  			}
   157  			return nil, errTest
   158  		})
   159  
   160  	confStr := `{
   161    "plugins": {
   162      "foobar": {
   163        "type": "foo",
   164        "plugin": {
   165          "bar": "custom"
   166        }
   167      }
   168    }
   169  }`
   170  
   171  	conf := NewConfig()
   172  	if err := json.Unmarshal([]byte(confStr), &conf); err != nil {
   173  		t.Fatal(err)
   174  	}
   175  
   176  	exp := "failed to create plugin resource 'foobar' of type 'foo': test err"
   177  	_, err := New(conf, nil, log.Noop(), metrics.Noop())
   178  	if act := err.Error(); act != exp {
   179  		t.Errorf("Wrong error returned: %v != %v", act, exp)
   180  	}
   181  }
   182  
   183  func TestYAMLPluginNilConf(t *testing.T) {
   184  	errTest := errors.New("test err")
   185  
   186  	RegisterPlugin("foo", func() interface{} { return &struct{}{} },
   187  		func(conf interface{}, mgr types.Manager, logger log.Modular, stats metrics.Type) (interface{}, error) {
   188  			return nil, errTest
   189  		})
   190  
   191  	confStr := `plugins:
   192    foobar:
   193      type: foo
   194      plugin:
   195        foo: this will be ignored`
   196  
   197  	conf := NewConfig()
   198  	if err := yaml.Unmarshal([]byte(confStr), &conf); err != nil {
   199  		t.Fatal(err)
   200  	}
   201  
   202  	exp := "failed to create plugin resource 'foobar' of type 'foo': test err"
   203  	_, err := New(conf, nil, log.Noop(), metrics.Noop())
   204  	if act := err.Error(); act != exp {
   205  		t.Errorf("Wrong error returned: %v != %v", act, exp)
   206  	}
   207  }
   208  
   209  func TestJSONPluginNilConf(t *testing.T) {
   210  	errTest := errors.New("test err")
   211  
   212  	RegisterPlugin("foo", func() interface{} { return &struct{}{} },
   213  		func(conf interface{}, mgr types.Manager, logger log.Modular, stats metrics.Type) (interface{}, error) {
   214  			return nil, errTest
   215  		})
   216  
   217  	confStr := `{
   218    "plugins": {
   219      "foobar": {
   220        "type": "foo",
   221        "plugin": {
   222          "foo": "this will be ignored"
   223        }
   224      }
   225    }
   226  }`
   227  
   228  	conf := NewConfig()
   229  	if err := json.Unmarshal([]byte(confStr), &conf); err != nil {
   230  		t.Fatal(err)
   231  	}
   232  
   233  	exp := "failed to create plugin resource 'foobar' of type 'foo': test err"
   234  	_, err := New(conf, nil, log.Noop(), metrics.Noop())
   235  	if act := err.Error(); act != exp {
   236  		t.Errorf("Wrong error returned: %v != %v", act, exp)
   237  	}
   238  }