open-nomad/plugins/base/plugin_test.go

160 lines
3.6 KiB
Go
Raw Normal View History

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"
"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) {
t.Parallel()
require := require.New(t)
const (
apiVersion = "v0.1.0"
pluginVersion = "v0.2.1"
pluginName = "mock"
)
knownType := func() (*PluginInfoResponse, error) {
info := &PluginInfoResponse{
2018-08-13 17:52:59 +00:00
Type: PluginTypeDriver,
2018-08-10 17:50:09 +00:00
PluginApiVersion: apiVersion,
PluginVersion: pluginVersion,
Name: pluginName,
}
return info, nil
}
unknownType := func() (*PluginInfoResponse, error) {
info := &PluginInfoResponse{
Type: "bad",
PluginApiVersion: apiVersion,
PluginVersion: pluginVersion,
Name: pluginName,
}
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)
require.Equal(apiVersion, resp.PluginApiVersion)
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) {
t.Parallel()
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) {
t.Parallel()
require := require.New(t)
var receivedData []byte
mock := &MockPlugin{
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
},
SetConfigF: func(data []byte, cfg *ClientAgentConfig) error {
2018-08-10 17:50:09 +00:00
receivedData = data
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)
require.NoError(impl.SetConfig(cdata, &ClientAgentConfig{}))
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)
}