github.com/yandex/pandora@v0.5.32/core/plugin/ptest_test.go (about) 1 package plugin 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/pkg/errors" 8 "github.com/stretchr/testify/assert" 9 "github.com/yandex/pandora/core/config" 10 ) 11 12 // ptest contains examples and utils for testing plugin pkg 13 14 const ( 15 ptestPluginName = "ptest_name" 16 17 ptestInitValue = "ptest_INITIAL" 18 ptestDefaultValue = "ptest_DEFAULT_CONFIG" 19 ptestFilledValue = "ptest_FILLED" 20 ) 21 22 func (r *Registry) ptestRegister(constructor interface{}, newDefaultConfigOptional ...interface{}) { 23 r.Register(ptestType(), ptestPluginName, constructor, newDefaultConfigOptional...) 24 } 25 func (r *Registry) ptestNew(fillConfOptional ...func(conf interface{}) error) (plugin interface{}, err error) { 26 return r.New(ptestType(), ptestPluginName, fillConfOptional...) 27 } 28 func (r *Registry) ptestNewFactory(fillConfOptional ...func(conf interface{}) error) (plugin interface{}, err error) { 29 factory, err := r.NewFactory(ptestNewErrType(), ptestPluginName, fillConfOptional...) 30 if err != nil { 31 return 32 } 33 typedFactory := factory.(func() (ptestPlugin, error)) 34 return typedFactory() 35 } 36 37 var ( 38 ptestCreateFailedErr = errors.New("test plugin create failed") 39 ptestConfigurationFailedErr = errors.New("test plugin configuration failed") 40 ) 41 42 type ptestPlugin interface { 43 DoSomething() 44 } 45 type ptestMoreThanPlugin interface { 46 ptestPlugin 47 DoSomethingElse() 48 } 49 type ptestImpl struct{ Value string } 50 type ptestConfig struct{ Value string } 51 52 func (p *ptestImpl) DoSomething() {} 53 func (p *ptestImpl) DoSomethingElse() {} 54 55 func ptestNew() ptestPlugin { return ptestNewImpl() } 56 func ptestNewMoreThan() ptestMoreThanPlugin { return ptestNewImpl() } 57 func ptestNewImpl() *ptestImpl { return &ptestImpl{Value: ptestInitValue} } 58 func ptestNewConf(c ptestConfig) ptestPlugin { return &ptestImpl{c.Value} } 59 func ptestNewPtrConf(c *ptestConfig) ptestPlugin { return &ptestImpl{c.Value} } 60 func ptestNewErr() (ptestPlugin, error) { return &ptestImpl{Value: ptestInitValue}, nil } 61 func ptestNewErrFailing() (ptestPlugin, error) { return nil, ptestCreateFailedErr } 62 63 func ptestNewFactory() func() ptestPlugin { return ptestNew } 64 func ptestNewFactoryMoreThan() func() ptestMoreThanPlugin { return ptestNewMoreThan } 65 func ptestNewFactoryImpl() func() *ptestImpl { return ptestNewImpl } 66 func ptestNewFactoryConf(c ptestConfig) func() ptestPlugin { 67 return func() ptestPlugin { 68 return ptestNewConf(c) 69 } 70 } 71 func ptestNewFactoryPtrConf(c *ptestConfig) func() ptestPlugin { 72 return func() ptestPlugin { 73 return ptestNewPtrConf(c) 74 } 75 } 76 func ptestNewFactoryErr() (func() ptestPlugin, error) { return ptestNew, nil } 77 func ptestNewFactoryErrFailing() (func() ptestPlugin, error) { return nil, ptestCreateFailedErr } 78 func ptestNewFactoryFactoryErr() func() (ptestPlugin, error) { return ptestNewErr } 79 func ptestNewFactoryFactoryErrFailing() func() (ptestPlugin, error) { return ptestNewErrFailing } 80 81 func ptestDefaultConf() ptestConfig { return ptestConfig{ptestDefaultValue} } 82 func ptestNewDefaultPtrConf() *ptestConfig { return &ptestConfig{ptestDefaultValue} } 83 84 func ptestType() reflect.Type { return PtrType((*ptestPlugin)(nil)) } 85 func ptestNewErrType() reflect.Type { return reflect.TypeOf(ptestNewErr) } 86 func ptestNewType() reflect.Type { return reflect.TypeOf(ptestNew) } 87 88 func ptestFillConf(conf interface{}) error { 89 return config.Decode(map[string]interface{}{"Value": ptestFilledValue}, conf) 90 } 91 92 func ptestExpectConfigValue(t *testing.T, conf interface{}, val string) { 93 conf.(ptestConfChecker).expectConfValue(t, val) 94 } 95 96 type ptestConfChecker interface { 97 expectConfValue(t *testing.T, val string) 98 } 99 100 var _ ptestConfChecker = ptestConfig{} 101 var _ ptestConfChecker = &ptestImpl{} 102 103 func (c ptestConfig) expectConfValue(t *testing.T, val string) { assert.Equal(t, val, c.Value) } 104 func (p *ptestImpl) expectConfValue(t *testing.T, val string) { assert.Equal(t, val, p.Value) }