mattermost-community-enterp.../public/plugin/health_check_test.go
Claude ec1f89217a Merge: Complete Mattermost Server with Community Enterprise
Full Mattermost server source with integrated Community Enterprise features.
Includes vendor directory for offline/air-gapped builds.

Structure:
- enterprise-impl/: Enterprise feature implementations
- enterprise-community/: Init files that register implementations
- enterprise/: Bridge imports (community_imports.go)
- vendor/: All dependencies for offline builds

Build (online):
  go build ./cmd/mattermost

Build (offline/air-gapped):
  go build -mod=vendor ./cmd/mattermost

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-17 23:59:07 +09:00

144 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)
}