2018-08-13 17:52:59 +00:00
|
|
|
package base
|
2018-08-10 17:50:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
pb "github.com/golang/protobuf/proto"
|
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2018-08-10 17:50:09 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/plugins/shared/hclspec"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
"github.com/zclconf/go-cty/cty/msgpack"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBasePlugin_PluginInfo_GRPC(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-08-10 17:50:09 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
2018-12-14 23:03:31 +00:00
|
|
|
var (
|
|
|
|
apiVersions = []string{"v0.1.0", "v0.1.1"}
|
|
|
|
)
|
|
|
|
|
2018-08-10 17:50:09 +00:00
|
|
|
const (
|
|
|
|
pluginVersion = "v0.2.1"
|
|
|
|
pluginName = "mock"
|
|
|
|
)
|
|
|
|
|
|
|
|
knownType := func() (*PluginInfoResponse, error) {
|
|
|
|
info := &PluginInfoResponse{
|
2018-12-14 23:03:31 +00:00
|
|
|
Type: PluginTypeDriver,
|
|
|
|
PluginApiVersions: apiVersions,
|
|
|
|
PluginVersion: pluginVersion,
|
|
|
|
Name: pluginName,
|
2018-08-10 17:50:09 +00:00
|
|
|
}
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
unknownType := func() (*PluginInfoResponse, error) {
|
|
|
|
info := &PluginInfoResponse{
|
2018-12-14 23:03:31 +00:00
|
|
|
Type: "bad",
|
|
|
|
PluginApiVersions: apiVersions,
|
|
|
|
PluginVersion: pluginVersion,
|
|
|
|
Name: pluginName,
|
2018-08-10 17:50:09 +00:00
|
|
|
}
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mock := &MockPlugin{
|
|
|
|
PluginInfoF: knownType,
|
|
|
|
}
|
|
|
|
|
2018-08-13 17:52:59 +00:00
|
|
|
client, server := plugin.TestPluginGRPCConn(t, map[string]plugin.Plugin{
|
2018-08-13 17:29:29 +00:00
|
|
|
PluginTypeBase: &PluginBase{Impl: mock},
|
2018-08-10 17:50:09 +00:00
|
|
|
})
|
|
|
|
defer server.Stop()
|
2018-08-13 17:52:59 +00:00
|
|
|
defer client.Close()
|
2018-08-10 17:50:09 +00:00
|
|
|
|
2018-08-13 17:29:29 +00:00
|
|
|
raw, err := client.Dispense(PluginTypeBase)
|
2018-08-13 17:52:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl, ok := raw.(BasePlugin)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("bad: %#v", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := impl.PluginInfo()
|
2018-08-10 17:50:09 +00:00
|
|
|
require.NoError(err)
|
2018-12-14 23:03:31 +00:00
|
|
|
require.Equal(apiVersions, resp.PluginApiVersions)
|
2018-08-10 17:50:09 +00:00
|
|
|
require.Equal(pluginVersion, resp.PluginVersion)
|
|
|
|
require.Equal(pluginName, resp.Name)
|
2018-08-13 17:52:59 +00:00
|
|
|
require.Equal(PluginTypeDriver, resp.Type)
|
2018-08-10 17:50:09 +00:00
|
|
|
|
|
|
|
// Swap the implementation to return an unknown type
|
|
|
|
mock.PluginInfoF = unknownType
|
2018-08-13 17:52:59 +00:00
|
|
|
_, err = impl.PluginInfo()
|
2018-08-10 17:50:09 +00:00
|
|
|
require.Error(err)
|
|
|
|
require.Contains(err.Error(), "unknown type")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBasePlugin_ConfigSchema(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-08-10 17:50:09 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
mock := &MockPlugin{
|
|
|
|
ConfigSchemaF: func() (*hclspec.Spec, error) {
|
2018-08-13 17:29:29 +00:00
|
|
|
return TestSpec, nil
|
2018-08-10 17:50:09 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-08-13 17:52:59 +00:00
|
|
|
client, server := plugin.TestPluginGRPCConn(t, map[string]plugin.Plugin{
|
2018-08-13 17:29:29 +00:00
|
|
|
PluginTypeBase: &PluginBase{Impl: mock},
|
2018-08-10 17:50:09 +00:00
|
|
|
})
|
|
|
|
defer server.Stop()
|
2018-08-13 17:52:59 +00:00
|
|
|
defer client.Close()
|
|
|
|
|
2018-08-13 17:29:29 +00:00
|
|
|
raw, err := client.Dispense(PluginTypeBase)
|
2018-08-13 17:52:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-08-10 17:50:09 +00:00
|
|
|
|
2018-08-13 17:52:59 +00:00
|
|
|
impl, ok := raw.(BasePlugin)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("bad: %#v", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
specOut, err := impl.ConfigSchema()
|
2018-08-10 17:50:09 +00:00
|
|
|
require.NoError(err)
|
2018-08-13 17:29:29 +00:00
|
|
|
require.True(pb.Equal(TestSpec, specOut))
|
2018-08-10 17:50:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBasePlugin_SetConfig(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-08-10 17:50:09 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
var receivedData []byte
|
|
|
|
mock := &MockPlugin{
|
2018-10-19 03:31:01 +00:00
|
|
|
PluginInfoF: func() (*PluginInfoResponse, error) {
|
|
|
|
return &PluginInfoResponse{Type: PluginTypeDriver}, nil
|
|
|
|
},
|
2018-08-10 17:50:09 +00:00
|
|
|
ConfigSchemaF: func() (*hclspec.Spec, error) {
|
2018-08-13 17:29:29 +00:00
|
|
|
return TestSpec, nil
|
2018-08-10 17:50:09 +00:00
|
|
|
},
|
2018-12-14 23:03:31 +00:00
|
|
|
SetConfigF: func(cfg *Config) error {
|
|
|
|
receivedData = cfg.PluginConfig
|
2018-08-10 17:50:09 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-08-13 17:52:59 +00:00
|
|
|
client, server := plugin.TestPluginGRPCConn(t, map[string]plugin.Plugin{
|
2018-08-13 17:29:29 +00:00
|
|
|
PluginTypeBase: &PluginBase{Impl: mock},
|
2018-08-10 17:50:09 +00:00
|
|
|
})
|
|
|
|
defer server.Stop()
|
2018-08-13 17:52:59 +00:00
|
|
|
defer client.Close()
|
|
|
|
|
2018-08-13 17:29:29 +00:00
|
|
|
raw, err := client.Dispense(PluginTypeBase)
|
2018-08-13 17:52:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl, ok := raw.(BasePlugin)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("bad: %#v", raw)
|
|
|
|
}
|
2018-08-10 17:50:09 +00:00
|
|
|
|
|
|
|
config := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"foo": cty.StringVal("v1"),
|
|
|
|
"bar": cty.NumberIntVal(1337),
|
|
|
|
"baz": cty.BoolVal(true),
|
|
|
|
})
|
|
|
|
cdata, err := msgpack.Marshal(config, config.Type())
|
|
|
|
require.NoError(err)
|
2018-12-14 23:03:31 +00:00
|
|
|
require.NoError(impl.SetConfig(&Config{PluginConfig: cdata}))
|
2018-08-10 17:50:09 +00:00
|
|
|
require.Equal(cdata, receivedData)
|
|
|
|
|
|
|
|
// Decode the value back
|
2018-08-13 17:29:29 +00:00
|
|
|
var actual TestConfig
|
2018-08-10 17:50:09 +00:00
|
|
|
require.NoError(structs.Decode(receivedData, &actual))
|
|
|
|
require.Equal("v1", actual.Foo)
|
|
|
|
require.EqualValues(1337, actual.Bar)
|
|
|
|
require.True(actual.Baz)
|
|
|
|
}
|