mirror of
https://github.com/mattermost/mattermost.git
synced 2026-02-18 18:18:23 -05:00
* 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>
143 lines
3.8 KiB
Go
143 lines
3.8 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package plugin
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/public/plugin/utils"
|
|
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
|
)
|
|
|
|
func TestPluginHealthCheck(t *testing.T) {
|
|
for name, f := range map[string]func(*testing.T){
|
|
"PluginHealthCheck_Success": testPluginHealthCheckSuccess,
|
|
"PluginHealthCheck_Panic": testPluginHealthCheckPanic,
|
|
} {
|
|
t.Run(name, f)
|
|
}
|
|
}
|
|
|
|
func testPluginHealthCheckSuccess(t *testing.T) {
|
|
dir, err := os.MkdirTemp("", "")
|
|
require.NoError(t, err)
|
|
defer os.RemoveAll(dir)
|
|
|
|
backend := filepath.Join(dir, "backend.exe")
|
|
utils.CompileGo(t, `
|
|
package main
|
|
|
|
import (
|
|
"github.com/mattermost/mattermost/server/public/plugin"
|
|
)
|
|
|
|
type MyPlugin struct {
|
|
plugin.MattermostPlugin
|
|
}
|
|
|
|
func main() {
|
|
plugin.ClientMain(&MyPlugin{})
|
|
}
|
|
`, backend)
|
|
|
|
err = os.WriteFile(filepath.Join(dir, "plugin.json"), []byte(`{"id": "foo", "server": {"executable": "backend.exe"}}`), 0600)
|
|
require.NoError(t, err)
|
|
|
|
bundle := model.BundleInfoForPath(dir)
|
|
logger := mlog.CreateConsoleTestLogger(t)
|
|
supervisor, err := newSupervisor(bundle, nil, nil, logger, nil, WithExecutableFromManifest(bundle))
|
|
require.NoError(t, err)
|
|
require.NotNil(t, supervisor)
|
|
defer supervisor.Shutdown()
|
|
|
|
err = supervisor.PerformHealthCheck()
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func testPluginHealthCheckPanic(t *testing.T) {
|
|
dir, err := os.MkdirTemp("", "")
|
|
require.NoError(t, err)
|
|
defer os.RemoveAll(dir)
|
|
|
|
backend := filepath.Join(dir, "backend.exe")
|
|
utils.CompileGo(t, `
|
|
package main
|
|
|
|
import (
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/public/plugin"
|
|
)
|
|
|
|
type MyPlugin struct {
|
|
plugin.MattermostPlugin
|
|
}
|
|
|
|
func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
|
|
panic("Uncaught error")
|
|
}
|
|
|
|
func main() {
|
|
plugin.ClientMain(&MyPlugin{})
|
|
}
|
|
`, backend)
|
|
|
|
err = os.WriteFile(filepath.Join(dir, "plugin.json"), []byte(`{"id": "foo", "server": {"executable": "backend.exe"}}`), 0600)
|
|
require.NoError(t, err)
|
|
|
|
bundle := model.BundleInfoForPath(dir)
|
|
logger := mlog.CreateConsoleTestLogger(t)
|
|
supervisor, err := newSupervisor(bundle, nil, nil, logger, nil, WithExecutableFromManifest(bundle))
|
|
require.NoError(t, err)
|
|
require.NotNil(t, supervisor)
|
|
defer supervisor.Shutdown()
|
|
|
|
err = supervisor.PerformHealthCheck()
|
|
require.NoError(t, err)
|
|
|
|
supervisor.hooks.MessageWillBePosted(&Context{}, &model.Post{})
|
|
|
|
err = supervisor.PerformHealthCheck()
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestShouldDeactivatePlugin(t *testing.T) {
|
|
// No failures, don't restart
|
|
ftime := []time.Time{}
|
|
result := shouldDeactivatePlugin(ftime)
|
|
require.Equal(t, false, result)
|
|
|
|
now := time.Now()
|
|
|
|
// Failures are recent enough to restart
|
|
ftime = []time.Time{}
|
|
ftime = append(ftime, now.Add(-HealthCheckDeactivationWindow/10*2))
|
|
ftime = append(ftime, now.Add(-HealthCheckDeactivationWindow/10))
|
|
ftime = append(ftime, now)
|
|
|
|
result = shouldDeactivatePlugin(ftime)
|
|
require.Equal(t, true, result)
|
|
|
|
// Failures are too spaced out to warrant a restart
|
|
ftime = []time.Time{}
|
|
ftime = append(ftime, now.Add(-HealthCheckDeactivationWindow*2))
|
|
ftime = append(ftime, now.Add(-HealthCheckDeactivationWindow*1))
|
|
ftime = append(ftime, now)
|
|
|
|
result = shouldDeactivatePlugin(ftime)
|
|
require.Equal(t, false, result)
|
|
|
|
// Not enough failures are present to warrant a restart
|
|
ftime = []time.Time{}
|
|
ftime = append(ftime, now.Add(-HealthCheckDeactivationWindow/10))
|
|
ftime = append(ftime, now)
|
|
|
|
result = shouldDeactivatePlugin(ftime)
|
|
require.Equal(t, false, result)
|
|
}
|