2017-08-16 17:23:38 -05:00
|
|
|
package rpcplugin
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
2018-01-15 11:21:06 -06:00
|
|
|
"github.com/mattermost/mattermost-server/plugin/rpcplugin/rpcplugintest"
|
|
|
|
|
)
|
2017-08-16 17:23:38 -05:00
|
|
|
|
|
|
|
|
func TestProcess(t *testing.T) {
|
|
|
|
|
dir, err := ioutil.TempDir("", "")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
2017-08-18 14:21:01 -05:00
|
|
|
ping := filepath.Join(dir, "ping.exe")
|
2018-01-15 11:21:06 -06:00
|
|
|
rpcplugintest.CompileGo(t, `
|
2017-08-16 17:23:38 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"log"
|
|
|
|
|
|
2017-09-06 23:05:10 -07:00
|
|
|
"github.com/mattermost/mattermost-server/plugin/rpcplugin"
|
2017-08-16 17:23:38 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
ipc, err := rpcplugin.InheritedProcessIPC()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal("unable to get inherited ipc")
|
|
|
|
|
}
|
|
|
|
|
defer ipc.Close()
|
|
|
|
|
_, err = ipc.Write([]byte("ping"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal("unable to write to ipc")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`, ping)
|
|
|
|
|
|
|
|
|
|
p, ipc, err := NewProcess(context.Background(), ping)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
defer ipc.Close()
|
|
|
|
|
b := make([]byte, 10)
|
|
|
|
|
n, err := ipc.Read(b)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Equal(t, 4, n)
|
|
|
|
|
assert.Equal(t, "ping", string(b[:4]))
|
|
|
|
|
require.NoError(t, p.Wait())
|
|
|
|
|
}
|
2017-11-15 16:08:02 -06:00
|
|
|
|
|
|
|
|
func TestInvalidProcess(t *testing.T) {
|
|
|
|
|
p, ipc, err := NewProcess(context.Background(), "thisfileshouldnotexist")
|
|
|
|
|
require.Nil(t, p)
|
|
|
|
|
require.Nil(t, ipc)
|
|
|
|
|
require.Error(t, err)
|
|
|
|
|
}
|