github.com/yandex/pandora@v0.5.32/core/plugin/pluginconfig/hooks_test.go (about)

     1  package pluginconfig
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  	"github.com/yandex/pandora/core/config"
    12  	"github.com/yandex/pandora/core/plugin"
    13  )
    14  
    15  func init() {
    16  	AddHooks()
    17  }
    18  
    19  type TestPlugin interface {
    20  	GetData() string
    21  }
    22  
    23  type testPluginImpl struct {
    24  	testPluginConf
    25  }
    26  
    27  type testPluginConf struct {
    28  	Data string `validate:"max=20"`
    29  }
    30  
    31  func (v *testPluginImpl) GetData() string { return v.Data }
    32  
    33  func TestPluginHooks(t *testing.T) {
    34  	dataConf := func(conf interface{}) map[string]interface{} {
    35  		return map[string]interface{}{
    36  			"plugin": conf,
    37  		}
    38  	}
    39  	const pluginName = "test_hook_plugin"
    40  	plugin.Register(reflect.TypeOf((*TestPlugin)(nil)).Elem(), pluginName, func(c testPluginConf) TestPlugin { return &testPluginImpl{c} })
    41  
    42  	const expectedData = "expected data"
    43  
    44  	validConfig := func() interface{} {
    45  		return dataConf(map[interface{}]interface{}{
    46  			PluginNameKey: pluginName,
    47  			"data":        expectedData,
    48  		})
    49  	}
    50  	invalidConfigs := []map[interface{}]interface{}{
    51  		{},
    52  		{
    53  			PluginNameKey:                  pluginName,
    54  			strings.ToUpper(PluginNameKey): pluginName,
    55  		},
    56  		{
    57  			PluginNameKey: pluginName,
    58  			"data":        expectedData,
    59  			"unused":      "wtf",
    60  		},
    61  		{
    62  			PluginNameKey: pluginName,
    63  			"data":        "invalid because is toooooo looooong",
    64  		},
    65  	}
    66  	testInvalid := func(t *testing.T, data interface{}) {
    67  		for _, tc := range invalidConfigs {
    68  			t.Run(fmt.Sprintf("Invalid conf: %v", tc), func(t *testing.T) {
    69  				err := config.Decode(dataConf(tc), data)
    70  				assert.Error(t, err)
    71  			})
    72  		}
    73  	}
    74  
    75  	t.Run("plugin", func(t *testing.T) {
    76  		var data struct {
    77  			Plugin TestPlugin
    78  		}
    79  		err := config.Decode(validConfig(), &data)
    80  		require.NoError(t, err)
    81  		assert.Equal(t, expectedData, data.Plugin.GetData(), expectedData)
    82  
    83  		testInvalid(t, data)
    84  	})
    85  
    86  	t.Run("factory", func(t *testing.T) {
    87  		var data struct {
    88  			Plugin func() (TestPlugin, error)
    89  		}
    90  		require.True(t, plugin.LookupFactory(plugin.PtrType(&data.Plugin)))
    91  		err := config.Decode(validConfig(), &data)
    92  		require.NoError(t, err)
    93  		plugin, err := data.Plugin()
    94  		require.NoError(t, err)
    95  		assert.Equal(t, expectedData, plugin.GetData(), expectedData)
    96  
    97  		testInvalid(t, data)
    98  	})
    99  }