github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/plugin/rpcplugin/process_test.go (about)

     1  package rpcplugin
     2  
     3  import (
     4  	"context"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/mattermost/mattermost-server/plugin/rpcplugin/rpcplugintest"
    14  )
    15  
    16  func TestProcess(t *testing.T) {
    17  	dir, err := ioutil.TempDir("", "")
    18  	require.NoError(t, err)
    19  	defer os.RemoveAll(dir)
    20  
    21  	ping := filepath.Join(dir, "ping.exe")
    22  	rpcplugintest.CompileGo(t, `
    23  		package main
    24  
    25  		import (
    26  			"log"
    27  
    28  			"github.com/mattermost/mattermost-server/plugin/rpcplugin"
    29  		)
    30  
    31  		func main() {
    32  			ipc, err := rpcplugin.InheritedProcessIPC()
    33  			if err != nil {
    34  				log.Fatal("unable to get inherited ipc")
    35  			}
    36  			defer ipc.Close()
    37  			_, err = ipc.Write([]byte("ping"))
    38  			if err != nil {
    39  				log.Fatal("unable to write to ipc")
    40  			}
    41  		}
    42  	`, ping)
    43  
    44  	p, ipc, err := NewProcess(context.Background(), ping)
    45  	require.NoError(t, err)
    46  	defer ipc.Close()
    47  	b := make([]byte, 10)
    48  	n, err := ipc.Read(b)
    49  	require.NoError(t, err)
    50  	assert.Equal(t, 4, n)
    51  	assert.Equal(t, "ping", string(b[:4]))
    52  	require.NoError(t, p.Wait())
    53  }
    54  
    55  func TestInvalidProcess(t *testing.T) {
    56  	p, ipc, err := NewProcess(context.Background(), "thisfileshouldnotexist")
    57  	require.Nil(t, p)
    58  	require.Nil(t, ipc)
    59  	require.Error(t, err)
    60  }