mattermost-community-enterp.../public/pluginapi/cluster/mock_plugin_api_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.2 KiB
Go

package cluster
import (
"bytes"
"sort"
"strings"
"sync"
"testing"
"github.com/mattermost/mattermost/server/public/model"
)
type mockPluginAPI struct {
t *testing.T
lock sync.Mutex
keyValues map[string][]byte
failing bool
failingWithPrefix string
}
func newMockPluginAPI(t *testing.T) *mockPluginAPI {
return &mockPluginAPI{
t: t,
keyValues: make(map[string][]byte),
}
}
func (pluginAPI *mockPluginAPI) setFailing(failing bool) {
pluginAPI.lock.Lock()
defer pluginAPI.lock.Unlock()
pluginAPI.failing = failing
}
func (pluginAPI *mockPluginAPI) setFailingWithPrefix(prefix string) {
pluginAPI.lock.Lock()
defer pluginAPI.lock.Unlock()
pluginAPI.failingWithPrefix = prefix
}
func (pluginAPI *mockPluginAPI) clear() {
pluginAPI.lock.Lock()
defer pluginAPI.lock.Unlock()
for k := range pluginAPI.keyValues {
delete(pluginAPI.keyValues, k)
}
}
func (pluginAPI *mockPluginAPI) KVGet(key string) ([]byte, *model.AppError) {
pluginAPI.lock.Lock()
defer pluginAPI.lock.Unlock()
if pluginAPI.failing {
return nil, &model.AppError{Message: "fake error"}
}
if pluginAPI.failingWithPrefix != "" && strings.HasPrefix(key, pluginAPI.failingWithPrefix) {
return nil, &model.AppError{Message: "fake error for prefix " + pluginAPI.failingWithPrefix}
}
return pluginAPI.keyValues[key], nil
}
func (pluginAPI *mockPluginAPI) KVDelete(key string) *model.AppError {
pluginAPI.lock.Lock()
defer pluginAPI.lock.Unlock()
if pluginAPI.failing {
return &model.AppError{Message: "fake error"}
}
if pluginAPI.failingWithPrefix != "" && strings.HasPrefix(key, pluginAPI.failingWithPrefix) {
return &model.AppError{Message: "fake error for prefix " + pluginAPI.failingWithPrefix}
}
delete(pluginAPI.keyValues, key)
return nil
}
func (pluginAPI *mockPluginAPI) KVList(page, count int) ([]string, *model.AppError) {
pluginAPI.lock.Lock()
defer pluginAPI.lock.Unlock()
if pluginAPI.failing {
return nil, &model.AppError{Message: "fake error"}
}
keys := make([]string, 0, len(pluginAPI.keyValues))
for k := range pluginAPI.keyValues {
keys = append(keys, k)
}
// have to sort, because we're paging below
sort.Strings(keys)
start := min(page*count, len(keys))
end := min((page+1)*count, len(keys))
return keys[start:end], nil
}
func (pluginAPI *mockPluginAPI) KVSetWithOptions(key string, value []byte, options model.PluginKVSetOptions) (bool, *model.AppError) {
pluginAPI.lock.Lock()
defer pluginAPI.lock.Unlock()
if pluginAPI.failing {
return false, &model.AppError{Message: "fake error"}
}
if pluginAPI.failingWithPrefix != "" && strings.HasPrefix(key, pluginAPI.failingWithPrefix) {
return false, &model.AppError{Message: "fake error for prefix " + pluginAPI.failingWithPrefix}
}
if options.Atomic {
if actualValue := pluginAPI.keyValues[key]; !bytes.Equal(actualValue, options.OldValue) {
return false, nil
}
}
if value == nil {
delete(pluginAPI.keyValues, key)
} else {
pluginAPI.keyValues[key] = value
}
return true, nil
}
func (pluginAPI *mockPluginAPI) LogError(msg string, keyValuePairs ...any) {
if pluginAPI.t == nil {
return
}
pluginAPI.t.Helper()
params := []any{msg}
params = append(params, keyValuePairs...)
pluginAPI.t.Log(params...)
}