2026-04-06 15:48:37 +05:30
|
|
|
// Copyright IBM Corp. 2020, 2026
|
2023-02-02 17:15:17 -08:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
2021-09-08 10:06:38 -07:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/hashicorp/boundary/internal/db"
|
2023-04-24 19:27:41 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/kms"
|
2021-09-08 10:06:38 -07:00
|
|
|
"github.com/hashicorp/boundary/internal/plugin/store"
|
|
|
|
|
"github.com/hashicorp/boundary/internal/types/scope"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// A typeless plugin used for tests.
|
|
|
|
|
type plugin struct {
|
|
|
|
|
*store.Plugin
|
|
|
|
|
tableName string `gorm:"-"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// newPlugin is used in tests and creates a typeless plugin in the global scope.
|
|
|
|
|
func newPlugin(name string) *plugin {
|
|
|
|
|
p := &plugin{
|
|
|
|
|
Plugin: &store.Plugin{
|
|
|
|
|
ScopeId: scope.Global.String(),
|
|
|
|
|
Name: name,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
return p
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TableName returns the table name for the host plugin.
|
|
|
|
|
func (c *plugin) TableName() string {
|
|
|
|
|
if c.tableName != "" {
|
|
|
|
|
return c.tableName
|
|
|
|
|
}
|
|
|
|
|
return "plugin"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetTableName sets the table name. If the caller attempts to
|
|
|
|
|
// set the name to "" the name will be reset to the default name.
|
|
|
|
|
func (c *plugin) SetTableName(n string) {
|
|
|
|
|
c.tableName = n
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-24 19:27:41 -04:00
|
|
|
// getTestOpts - iterate the inbound TestOptions and return a struct
|
|
|
|
|
func getTestOpts(opt ...TestOption) testOptions {
|
|
|
|
|
opts := getDefaultTestOptions()
|
|
|
|
|
for _, o := range opt {
|
|
|
|
|
o(&opts)
|
|
|
|
|
}
|
|
|
|
|
return opts
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TestOption - how Options are passed as arguments
|
|
|
|
|
type TestOption func(*testOptions)
|
|
|
|
|
|
|
|
|
|
// options = how options are represented
|
|
|
|
|
type testOptions struct {
|
|
|
|
|
withHostFlag bool
|
|
|
|
|
withStorageFlag bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getDefaultTestOptions() testOptions {
|
|
|
|
|
return testOptions{
|
|
|
|
|
withHostFlag: true,
|
|
|
|
|
withStorageFlag: false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithHostFlag determines whether or not to enable the host flag for a test plugin
|
|
|
|
|
func WithHostFlag(flag bool) TestOption {
|
|
|
|
|
return func(o *testOptions) {
|
|
|
|
|
o.withHostFlag = flag
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithStorageFlag determines whether or not to enable the storage flag for a test plugin
|
|
|
|
|
func WithStorageFlag(flag bool) TestOption {
|
|
|
|
|
return func(o *testOptions) {
|
|
|
|
|
o.withStorageFlag = flag
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TestPlugin creates a plugin and inserts it into the database. enables the "host" flag by default
|
|
|
|
|
//
|
|
|
|
|
// Supported options: WithHostFlag, WithStorageFlag
|
|
|
|
|
func TestPlugin(t testing.TB, conn *db.DB, name string, opt ...TestOption) *Plugin {
|
|
|
|
|
opts := getTestOpts(opt...)
|
2021-09-08 10:06:38 -07:00
|
|
|
t.Helper()
|
2023-06-06 17:09:08 -04:00
|
|
|
ctx := context.Background()
|
2023-04-24 19:27:41 -04:00
|
|
|
p := NewPlugin(WithName(name))
|
2023-06-06 17:09:08 -04:00
|
|
|
id, err := newPluginId(ctx)
|
2021-09-08 10:06:38 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
p.PublicId = id
|
|
|
|
|
|
|
|
|
|
w := db.New(conn)
|
|
|
|
|
require.NoError(t, w.Create(context.Background(), p))
|
2023-04-24 19:27:41 -04:00
|
|
|
wrapper := db.TestWrapper(t)
|
|
|
|
|
kmsCache := kms.TestKms(t, conn, wrapper)
|
|
|
|
|
repo, err := NewRepository(ctx, w, w, kmsCache)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
if opts.withHostFlag {
|
|
|
|
|
// add the host supported flag
|
2024-02-16 08:30:16 -05:00
|
|
|
require.NoError(t, repo.AddSupportFlag(context.Background(), p, PluginTypeHost))
|
2023-04-24 19:27:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if opts.withStorageFlag {
|
|
|
|
|
// add the storage supported flag
|
2024-02-16 08:30:16 -05:00
|
|
|
require.NoError(t, repo.AddSupportFlag(context.Background(), p, PluginTypeStorage))
|
2023-04-24 19:27:41 -04:00
|
|
|
}
|
|
|
|
|
|
2021-09-08 10:06:38 -07:00
|
|
|
return p
|
|
|
|
|
}
|