github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/plugin/rpcplugin/main_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/mlog"
    14  	"github.com/mattermost/mattermost-server/plugin/plugintest"
    15  	"github.com/mattermost/mattermost-server/plugin/rpcplugin/rpcplugintest"
    16  )
    17  
    18  func TestMain(t *testing.T) {
    19  	// Setup a global logger to catch tests logging outside of app context
    20  	// The global logger will be stomped by apps initalizing but that's fine for testing. Ideally this won't happen.
    21  	mlog.InitGlobalLogger(mlog.NewLogger(&mlog.LoggerConfiguration{
    22  		EnableConsole: true,
    23  		ConsoleJson:   true,
    24  		ConsoleLevel:  "error",
    25  		EnableFile:    false,
    26  	}))
    27  
    28  	dir, err := ioutil.TempDir("", "")
    29  	require.NoError(t, err)
    30  	defer os.RemoveAll(dir)
    31  
    32  	plugin := filepath.Join(dir, "plugin.exe")
    33  	rpcplugintest.CompileGo(t, `
    34  		package main
    35  
    36  		import (
    37  			"github.com/mattermost/mattermost-server/plugin/rpcplugin"
    38  		)
    39  
    40  		type MyPlugin struct {}
    41  
    42  		func main() {
    43  			rpcplugin.Main(&MyPlugin{})
    44  		}
    45  	`, plugin)
    46  
    47  	ctx, cancel := context.WithCancel(context.Background())
    48  	p, ipc, err := NewProcess(ctx, plugin)
    49  	require.NoError(t, err)
    50  	defer p.Wait()
    51  
    52  	muxer := NewMuxer(ipc, false)
    53  	defer muxer.Close()
    54  
    55  	defer cancel()
    56  
    57  	var api plugintest.API
    58  
    59  	hooks, err := ConnectMain(muxer, "plugin_id")
    60  	require.NoError(t, err)
    61  	assert.NoError(t, hooks.OnActivate(&api))
    62  	assert.NoError(t, hooks.OnDeactivate())
    63  }