-
Notifications
You must be signed in to change notification settings - Fork 456
/
load.go
76 lines (66 loc) · 1.82 KB
/
load.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package plugintest
import (
"context"
"io"
"testing"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/spiffe/spire/pkg/common/catalog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type Plugin interface {
catalog.Configurer
io.Closer
}
// Load loads a built-in plugin for testing with the given options. The plugin
// facade can be nil. If one of the Configure* options is given, the plugin
// will also be configured. The plugin will unload when the test is over. The
// function returns a plugin interface that can be closed to unload the
// built-in before the test is finished or used to reconfigure the plugin, but
// can otherwise be ignored.
func Load(t *testing.T, builtIn catalog.BuiltIn, pluginFacade catalog.Facade, options ...Option) Plugin {
conf := &config{
builtInConfig: catalog.BuiltInConfig{
Log: nullLogger(),
},
}
for _, opt := range options {
opt.setOption(conf)
}
conn, err := catalog.LoadBuiltIn(context.Background(), builtIn, conf.builtInConfig)
if conf.loadErr != nil {
*conf.loadErr = err
if err != nil {
return nil
}
}
require.NoError(t, err)
t.Cleanup(func() { assert.NoError(t, conn.Close()) })
var facades []catalog.Facade
if pluginFacade != nil {
facades = append(facades, pluginFacade)
}
facades = append(facades, conf.serviceFacades...)
configurer, err := conn.Bind(facades...)
require.NoError(t, err)
if conf.doConfigure {
err := configurer.Configure(context.Background(), conf.coreConfig, conf.makeConfigData(t))
if conf.configureErr != nil {
*conf.configureErr = err
} else {
require.NoError(t, err)
}
}
return struct {
catalog.Configurer
io.Closer
}{
Configurer: configurer,
Closer: conn,
}
}
func nullLogger() logrus.FieldLogger {
log, _ := test.NewNullLogger()
return log
}