|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/docker/infrakit/pkg/rpc/server" |
| 5 | + "github.com/docker/infrakit/pkg/spi" |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | + "io/ioutil" |
| 8 | + "net/http" |
| 9 | + "path/filepath" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +var apiSpec = spi.InterfaceSpec{ |
| 14 | + Name: "TestPlugin", |
| 15 | + Version: "0.1.0", |
| 16 | +} |
| 17 | + |
| 18 | +func startPluginServer(t *testing.T) (server.Stoppable, string) { |
| 19 | + dir, err := ioutil.TempDir("", "infrakit_handshake_test") |
| 20 | + require.NoError(t, err) |
| 21 | + |
| 22 | + name := "instance" |
| 23 | + socket := filepath.Join(dir, name) |
| 24 | + |
| 25 | + testServer, err := server.StartPluginAtPath(socket, &TestPlugin{spec: apiSpec}) |
| 26 | + require.NoError(t, err) |
| 27 | + return testServer, socket |
| 28 | +} |
| 29 | + |
| 30 | +func TestHandshakeSuccess(t *testing.T) { |
| 31 | + testServer, socket := startPluginServer(t) |
| 32 | + defer testServer.Stop() |
| 33 | + |
| 34 | + client := rpcClient{client: New(socket, apiSpec)} |
| 35 | + require.NoError(t, client.DoSomething()) |
| 36 | +} |
| 37 | + |
| 38 | +func TestHandshakeFailVersion(t *testing.T) { |
| 39 | + testServer, socket := startPluginServer(t) |
| 40 | + defer testServer.Stop() |
| 41 | + |
| 42 | + client := rpcClient{client: New(socket, spi.InterfaceSpec{Name: "TestPlugin", Version: "0.2.0"})} |
| 43 | + err := client.DoSomething() |
| 44 | + require.Error(t, err) |
| 45 | + require.Equal(t, "Plugin supports TestPlugin interface version 0.1.0, client requires 0.2.0", err.Error()) |
| 46 | +} |
| 47 | + |
| 48 | +func TestHandshakeFailWrongAPI(t *testing.T) { |
| 49 | + testServer, socket := startPluginServer(t) |
| 50 | + defer testServer.Stop() |
| 51 | + |
| 52 | + client := rpcClient{client: New(socket, spi.InterfaceSpec{Name: "OtherPlugin", Version: "0.1.0"})} |
| 53 | + err := client.DoSomething() |
| 54 | + require.Error(t, err) |
| 55 | + require.Equal(t, "Plugin does not support interface {OtherPlugin 0.1.0}", err.Error()) |
| 56 | +} |
| 57 | + |
| 58 | +type rpcClient struct { |
| 59 | + client Client |
| 60 | +} |
| 61 | + |
| 62 | +func (c rpcClient) DoSomething() error { |
| 63 | + req := EmptyMessage{} |
| 64 | + resp := EmptyMessage{} |
| 65 | + return c.client.Call("TestPlugin.DoSomething", req, &resp) |
| 66 | +} |
| 67 | + |
| 68 | +// TestPlugin is an RPC service for this unit test. |
| 69 | +type TestPlugin struct { |
| 70 | + spec spi.InterfaceSpec |
| 71 | +} |
| 72 | + |
| 73 | +// ImplementedInterface returns the interface implemented by this RPC service. |
| 74 | +func (p *TestPlugin) ImplementedInterface() spi.InterfaceSpec { |
| 75 | + return p.spec |
| 76 | +} |
| 77 | + |
| 78 | +// EmptyMessage is an empty test message. |
| 79 | +type EmptyMessage struct { |
| 80 | +} |
| 81 | + |
| 82 | +// DoSomething is an empty test RPC. |
| 83 | +func (p *TestPlugin) DoSomething(_ *http.Request, req *EmptyMessage, resp *EmptyMessage) error { |
| 84 | + return nil |
| 85 | +} |
0 commit comments