2021-09-23 08:33:54 -04:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
2023-02-02 17:15:17 -08:00
|
|
|
|
2023-04-19 11:27:00 -07:00
|
|
|
package loopback
|
2021-09-23 08:33:54 -04:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
plgpb "github.com/hashicorp/boundary/sdk/pbs/plugin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const loopbackPluginHostInfoAttrField = "host_info"
|
|
|
|
|
|
2023-04-19 11:27:00 -07:00
|
|
|
var (
|
2023-04-18 21:12:05 -07:00
|
|
|
_ plgpb.HostPluginServiceServer = (*LoopbackPlugin)(nil)
|
|
|
|
|
_ plgpb.StoragePluginServiceServer = (*LoopbackPlugin)(nil)
|
|
|
|
|
_ plgpb.HostPluginServiceServer = (*TestPluginServer)(nil)
|
|
|
|
|
_ plgpb.StoragePluginServiceServer = (*TestPluginServer)(nil)
|
2023-04-19 11:27:00 -07:00
|
|
|
)
|
|
|
|
|
|
2023-04-18 21:12:05 -07:00
|
|
|
// TestPluginServer provides a host and storage plugin service server where each method can be overwritten for tests.
|
2023-04-19 11:27:00 -07:00
|
|
|
type TestPluginServer struct {
|
2023-04-18 21:12:05 -07:00
|
|
|
TestPluginHostServer
|
|
|
|
|
TestPluginStorageServer
|
2023-04-19 11:27:00 -07:00
|
|
|
}
|
|
|
|
|
|
2023-04-18 21:12:05 -07:00
|
|
|
// LoopbackPlugin provides a host and storage plugin with functionality useful for certain
|
|
|
|
|
// kinds of testing.
|
|
|
|
|
//
|
|
|
|
|
// It is not (currently) thread-safe.
|
2021-09-23 08:33:54 -04:00
|
|
|
//
|
|
|
|
|
// Over time, if useful, it can be enhanced to do things like handle multiple
|
|
|
|
|
// hosts per set.
|
2023-04-19 11:27:00 -07:00
|
|
|
type LoopbackPlugin struct {
|
2021-09-23 08:33:54 -04:00
|
|
|
*TestPluginServer
|
|
|
|
|
|
2023-04-18 21:12:05 -07:00
|
|
|
*LoopbackHost
|
|
|
|
|
*LoopbackStorage
|
2021-09-23 08:33:54 -04:00
|
|
|
}
|
|
|
|
|
|
2023-04-08 16:15:14 -07:00
|
|
|
// NewLoopbackPlugin returns a new loopback pluginre
|
|
|
|
|
// For storage service testings NewLoopbackPlugin Supports WithMockErrors
|
|
|
|
|
// and WithMockBuckets as options. If no mock buckets are provided,
|
|
|
|
|
// a bucket named `default` will be created.
|
2023-04-18 21:12:05 -07:00
|
|
|
func NewLoopbackPlugin(opt ...TestOption) (*LoopbackPlugin, error) {
|
|
|
|
|
opts, err := getTestOpts(opt...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 11:27:00 -07:00
|
|
|
ret := &LoopbackPlugin{
|
2021-09-23 08:33:54 -04:00
|
|
|
TestPluginServer: new(TestPluginServer),
|
2023-04-18 21:12:05 -07:00
|
|
|
LoopbackHost: &LoopbackHost{
|
|
|
|
|
hostMap: make(map[string][]*loopbackPluginHostInfo),
|
|
|
|
|
},
|
|
|
|
|
LoopbackStorage: &LoopbackStorage{
|
2023-05-15 11:57:07 -07:00
|
|
|
chunksSize: opts.withChunkSize,
|
|
|
|
|
buckets: map[BucketName]Bucket{"default": {}},
|
|
|
|
|
errs: make([]PluginMockError, 0),
|
|
|
|
|
putObjectResponse: make([]PluginMockPutObjectResponse, 0),
|
2023-04-18 21:12:05 -07:00
|
|
|
},
|
2021-09-23 08:33:54 -04:00
|
|
|
}
|
2023-04-18 21:12:05 -07:00
|
|
|
|
|
|
|
|
// Set host methods
|
2021-09-23 08:33:54 -04:00
|
|
|
ret.OnCreateCatalogFn = ret.onCreateCatalog
|
2021-11-12 09:46:40 -07:00
|
|
|
ret.OnUpdateCatalogFn = ret.onUpdateCatalog
|
2021-11-13 17:18:20 -05:00
|
|
|
ret.OnCreateSetFn = ret.onCreateSet
|
|
|
|
|
ret.OnUpdateSetFn = ret.onUpdateSet
|
2021-09-23 08:33:54 -04:00
|
|
|
ret.OnDeleteSetFn = ret.onDeleteSet
|
|
|
|
|
ret.ListHostsFn = ret.listHosts
|
2023-04-19 11:27:00 -07:00
|
|
|
|
2023-04-18 21:12:05 -07:00
|
|
|
// Set storage methods
|
|
|
|
|
ret.OnCreateStorageBucketFn = ret.onCreateStorageBucket
|
|
|
|
|
ret.OnUpdateStorageBucketFn = ret.onUpdateStorageBucket
|
|
|
|
|
ret.OnDeleteStorageBucketFn = ret.onDeleteStorageBucket
|
|
|
|
|
ret.ValidatePermissionsFn = ret.validatePermissions
|
|
|
|
|
ret.HeadObjectFn = ret.headObject
|
|
|
|
|
ret.GetObjectFn = ret.getObject
|
|
|
|
|
ret.PutObjectFn = ret.putObject
|
|
|
|
|
if len(opts.withMockBuckets) > 0 {
|
|
|
|
|
ret.buckets = opts.withMockBuckets
|
2021-09-23 08:33:54 -04:00
|
|
|
}
|
2023-04-18 21:12:05 -07:00
|
|
|
if len(opts.withMockError) > 0 {
|
|
|
|
|
ret.errs = opts.withMockError
|
2021-09-23 08:33:54 -04:00
|
|
|
}
|
2023-05-15 11:57:07 -07:00
|
|
|
if len(opts.withMockPutObjectResponse) > 0 {
|
|
|
|
|
ret.putObjectResponse = opts.withMockPutObjectResponse
|
|
|
|
|
}
|
2021-11-12 09:46:40 -07:00
|
|
|
|
2023-04-18 21:12:05 -07:00
|
|
|
return ret, nil
|
2021-09-23 08:33:54 -04:00
|
|
|
}
|