mattermost/server/public/plugin/plugintest/example_reattach_test.go
Jesse Hallam 2230fb6f5f
MM-57018: support reattaching plugins (#26421)
* ProfileImageBytes for EnsureBotOptions

* leverage plugintest.NewAPI

* fix linting

* add UpdateUserRoles to plugin api

* MM-57018: support reattaching plugins

Expose a local-only API for reattaching plugins: instead of the server starting and managing the process itself, allow the plugin to be launched externally (eg within a unit test) and reattach to an existing server instance to provide the unit test with a fully functional RPC API, sidestepping the need for mocking the plugin API in most cases.

In the future, this may become the basis for running plugins in a sidecar container.

Fixes: https://mattermost.atlassian.net/browse/MM-57018

* drop unused supervisor.pid

* factor out checkMinServerVersion

* factor out startPluginServer

* restore missing setPluginState on successful reattach

* avoid passing around a stale registeredPlugin

* inline initializePluginImplementation

* have IsValid return an error

* explicitly close rpcClient

In the case of reattached plugins, the Unix socket won't necessarily disappear leaving the muxBrokers blocked indefinitely. And `Kill()` doesn't do anything if there's no process being managed.

* explicitly detachPlugin

* emphasize gRPC not being supported

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
2024-04-11 11:10:25 -04:00

85 lines
2.4 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package plugintest_test
import (
"context"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
goPlugin "github.com/hashicorp/go-plugin"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/plugin"
)
type UnitTestedPlugin struct {
plugin.MattermostPlugin
}
// This example demonstrates a plugin that's launched during a unit test and reattached to an
// existing server instance to obtain a real PluginAPI.
func Example_unitTestingPlugins() {
t := &testing.T{}
// The manifest is usually generated dynamically.
manifest := &model.Manifest{
Id: "reattach-plugin-test",
}
// ctx, and specifically cancel, gives us control over the plugin lifecycle
ctx, cancel := context.WithCancel(context.Background())
// reattachConfigCh is the means by which we get the Unix socket information to relay back
// to the server and finish the reattachment.
reattachConfigCh := make(chan *goPlugin.ReattachConfig)
// closeCh tells us when the plugin exits and allows for cleanup.
closeCh := make(chan struct{})
// plugin.ClientMain with options allows for reattachment.
go plugin.ClientMain(
&UnitTestedPlugin{},
plugin.WithTestContext(ctx),
plugin.WithTestReattachConfigCh(reattachConfigCh),
plugin.WithTestCloseCh(closeCh),
)
// Make sure the plugin shuts down normally with the test
t.Cleanup(func() {
cancel()
select {
case <-closeCh:
case <-time.After(5 * time.Second):
panic("plugin failed to close after 5 seconds")
}
})
// Wait for the plugin to start and then reattach to the server.
var reattachConfig *goPlugin.ReattachConfig
select {
case reattachConfig = <-reattachConfigCh:
case <-time.After(5 * time.Second):
t.Fatal("failed to get reattach config")
}
// Reattaching requires a local mode client.
socketPath := os.Getenv("MM_LOCALSOCKETPATH")
if socketPath == "" {
socketPath = model.LocalModeSocketPath
}
clientLocal := model.NewAPIv4SocketClient(socketPath)
_, err := clientLocal.ReattachPlugin(ctx, &model.PluginReattachRequest{
Manifest: manifest,
PluginReattachConfig: model.NewPluginReattachConfig(reattachConfig),
})
require.NoError(t, err)
// At this point, the plugin is ready for unit testing and will be cleaned up automatically
// with the testing.T instance.
}