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

     1  package cache_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/Jeffail/benthos/v3/lib/cache"
    10  	"github.com/Jeffail/benthos/v3/lib/log"
    11  	"github.com/Jeffail/benthos/v3/lib/metrics"
    12  	"github.com/Jeffail/benthos/v3/lib/types"
    13  	yaml "gopkg.in/yaml.v3"
    14  
    15  	_ "github.com/Jeffail/benthos/v3/public/components/all"
    16  )
    17  
    18  type mockPluginConf struct {
    19  	Foo string `json:"foo" yaml:"foo"`
    20  	Bar string `json:"bar" yaml:"bar"`
    21  	Baz int    `json:"baz" yaml:"baz"`
    22  }
    23  
    24  func newMockPluginConf() interface{} {
    25  	return &mockPluginConf{
    26  		Foo: "default",
    27  		Bar: "change this",
    28  		Baz: 10,
    29  	}
    30  }
    31  
    32  func TestYAMLPlugin(t *testing.T) {
    33  	errTest := errors.New("test err")
    34  
    35  	cache.RegisterPlugin("foo", newMockPluginConf,
    36  		func(conf interface{}, mgr types.Manager, logger log.Modular, stats metrics.Type) (types.Cache, error) {
    37  			mConf, ok := conf.(*mockPluginConf)
    38  			if !ok {
    39  				t.Fatalf("failed to cast config: %T", conf)
    40  			}
    41  			if exp, act := "default", mConf.Foo; exp != act {
    42  				t.Errorf("Wrong config value: %v != %v", act, exp)
    43  			}
    44  			if exp, act := "custom", mConf.Bar; exp != act {
    45  				t.Errorf("Wrong config value: %v != %v", act, exp)
    46  			}
    47  			if exp, act := 10, mConf.Baz; exp != act {
    48  				t.Errorf("Wrong config value: %v != %v", act, exp)
    49  			}
    50  			return nil, errTest
    51  		})
    52  
    53  	confStr := `type: foo
    54  plugin:
    55    bar: custom`
    56  
    57  	conf := cache.NewConfig()
    58  	if err := yaml.Unmarshal([]byte(confStr), &conf); err != nil {
    59  		t.Fatal(err)
    60  	}
    61  
    62  	_, err := cache.New(conf, nil, log.Noop(), metrics.Noop())
    63  	if !strings.Contains(err.Error(), "test err") {
    64  		t.Errorf("Wrong error returned: %v != %v", err, errTest)
    65  	}
    66  }
    67  
    68  func TestYAMLPluginNilConf(t *testing.T) {
    69  	errTest := errors.New("test err")
    70  
    71  	cache.RegisterPlugin("foo", func() interface{} { return &struct{}{} },
    72  		func(conf interface{}, mgr types.Manager, logger log.Modular, stats metrics.Type) (types.Cache, error) {
    73  			return nil, errTest
    74  		})
    75  
    76  	confStr := `type: foo
    77  plugin:
    78    foo: this will be ignored`
    79  
    80  	conf := cache.NewConfig()
    81  	if err := yaml.Unmarshal([]byte(confStr), &conf); err != nil {
    82  		t.Fatal(err)
    83  	}
    84  
    85  	_, err := cache.New(conf, nil, log.Noop(), metrics.Noop())
    86  	if !strings.Contains(err.Error(), "test err") {
    87  		t.Errorf("Wrong error returned: %v != %v", err, errTest)
    88  	}
    89  }
    90  
    91  func TestJSONPluginNilConf(t *testing.T) {
    92  	errTest := errors.New("test err")
    93  
    94  	cache.RegisterPlugin("foo", func() interface{} { return &struct{}{} },
    95  		func(conf interface{}, mgr types.Manager, logger log.Modular, stats metrics.Type) (types.Cache, error) {
    96  			return nil, errTest
    97  		})
    98  
    99  	confStr := `{
   100    "type": "foo",
   101    "plugin": {
   102      "foo": "this will be ignored"
   103    }
   104  }`
   105  
   106  	conf := cache.NewConfig()
   107  	if err := json.Unmarshal([]byte(confStr), &conf); err != nil {
   108  		t.Fatal(err)
   109  	}
   110  
   111  	_, err := cache.New(conf, nil, log.Noop(), metrics.Noop())
   112  	if !strings.Contains(err.Error(), "test err") {
   113  		t.Errorf("Wrong error returned: %v != %v", err, errTest)
   114  	}
   115  }