get.porter.sh/porter@v1.3.0/pkg/plugins/pluggable/loader_test.go (about) 1 package pluggable 2 3 import ( 4 "context" 5 "testing" 6 7 "get.porter.sh/porter/pkg/config" 8 "get.porter.sh/porter/pkg/plugins" 9 "get.porter.sh/porter/tests" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestPluginLoader_SelectPlugin(t *testing.T) { 15 c := config.NewTestConfig(t) 16 l := NewPluginLoader(c.Config) 17 18 pluginCfg := PluginTypeConfig{ 19 GetDefaultPluggable: func(c *config.Config) string { 20 return c.Data.DefaultStorage 21 }, 22 GetPluggable: func(c *config.Config, name string) (Entry, error) { 23 return c.GetStorage(name) 24 }, 25 GetDefaultPlugin: func(c *config.Config) string { 26 return c.Data.DefaultStoragePlugin 27 }, 28 } 29 30 t.Run("internal plugin", func(t *testing.T) { 31 c.Data.DefaultStoragePlugin = "mongodb-docker" 32 33 err := l.selectPlugin(context.Background(), pluginCfg) 34 require.NoError(t, err, "error selecting plugin") 35 36 assert.Equal(t, &plugins.PluginKey{Binary: "porter", Implementation: "mongodb-docker", IsInternal: true}, l.selectedPluginKey) 37 assert.Nil(t, l.selectedPluginConfig) 38 }) 39 40 t.Run("external plugin", func(t *testing.T) { 41 c.Data.DefaultStoragePlugin = "azure.blob" 42 43 err := l.selectPlugin(context.Background(), pluginCfg) 44 require.NoError(t, err, "error selecting plugin") 45 46 assert.Equal(t, &plugins.PluginKey{Binary: "azure", Implementation: "blob", IsInternal: false}, l.selectedPluginKey) 47 assert.Nil(t, l.selectedPluginConfig) 48 }) 49 50 t.Run("configured plugin", func(t *testing.T) { 51 c.Data.DefaultStorage = "azure" 52 c.Data.StoragePlugins = []config.StoragePlugin{ 53 { 54 PluginConfig: config.PluginConfig{ 55 Name: "azure", 56 PluginSubKey: "azure.blob", 57 Config: map[string]interface{}{ 58 "env": "MyAzureConnString", 59 }, 60 }, 61 }, 62 } 63 64 err := l.selectPlugin(context.Background(), pluginCfg) 65 require.NoError(t, err, "error selecting plugin") 66 67 assert.Equal(t, &plugins.PluginKey{Binary: "azure", Implementation: "blob", IsInternal: false}, l.selectedPluginKey) 68 assert.Equal(t, c.Data.StoragePlugins[0].Config, l.selectedPluginConfig) 69 }) 70 } 71 72 func TestPluginLoader_IdentifyRecursiveLoad(t *testing.T) { 73 // The plugin loader should proactively identify when a plugin is also trying to load another plugin 74 // and then return an error to avoid a recursive call that will go up in flames and bork your computer 75 76 ctx := context.Background() 77 c := config.NewTestConfig(t) 78 79 // Set Porter's context to indicate we are in an internal plugin right now 80 c.IsInternalPlugin = true 81 c.InternalPluginKey = "filesystem" 82 83 l := NewPluginLoader(c.Config) 84 85 pluginCfg := PluginTypeConfig{ 86 GetDefaultPluggable: func(c *config.Config) string { 87 return c.Data.DefaultStorage 88 }, 89 GetPluggable: func(c *config.Config, name string) (Entry, error) { 90 return c.GetStorage(name) 91 }, 92 GetDefaultPlugin: func(c *config.Config) string { 93 return c.Data.DefaultSecretsPlugin 94 }, 95 } 96 97 conn, err := l.Load(ctx, pluginCfg) 98 if conn != nil { 99 conn.Close(ctx) 100 } 101 tests.RequireErrorContains(t, err, "the internal plugin filesystem tried to load the .porter.host plugin") 102 }