github.com/mattermost/mattermost-plugin-api@v0.1.4/cluster/mock_plugin_api_test.go (about)

     1  package cluster
     2  
     3  import (
     4  	"bytes"
     5  	"sort"
     6  	"strings"
     7  	"sync"
     8  	"testing"
     9  
    10  	"github.com/mattermost/mattermost-server/v6/model"
    11  )
    12  
    13  type mockPluginAPI struct {
    14  	t *testing.T
    15  
    16  	lock              sync.Mutex
    17  	keyValues         map[string][]byte
    18  	failing           bool
    19  	failingWithPrefix string
    20  }
    21  
    22  func newMockPluginAPI(t *testing.T) *mockPluginAPI {
    23  	return &mockPluginAPI{
    24  		t:         t,
    25  		keyValues: make(map[string][]byte),
    26  	}
    27  }
    28  
    29  func (pluginAPI *mockPluginAPI) setFailing(failing bool) {
    30  	pluginAPI.lock.Lock()
    31  	defer pluginAPI.lock.Unlock()
    32  
    33  	pluginAPI.failing = failing
    34  }
    35  
    36  func (pluginAPI *mockPluginAPI) setFailingWithPrefix(prefix string) {
    37  	pluginAPI.lock.Lock()
    38  	defer pluginAPI.lock.Unlock()
    39  
    40  	pluginAPI.failingWithPrefix = prefix
    41  }
    42  
    43  func (pluginAPI *mockPluginAPI) clear() {
    44  	pluginAPI.lock.Lock()
    45  	defer pluginAPI.lock.Unlock()
    46  
    47  	for k := range pluginAPI.keyValues {
    48  		delete(pluginAPI.keyValues, k)
    49  	}
    50  }
    51  
    52  func (pluginAPI *mockPluginAPI) KVGet(key string) ([]byte, *model.AppError) {
    53  	pluginAPI.lock.Lock()
    54  	defer pluginAPI.lock.Unlock()
    55  
    56  	if pluginAPI.failing {
    57  		return nil, &model.AppError{Message: "fake error"}
    58  	}
    59  
    60  	if pluginAPI.failingWithPrefix != "" && strings.HasPrefix(key, pluginAPI.failingWithPrefix) {
    61  		return nil, &model.AppError{Message: "fake error for prefix " + pluginAPI.failingWithPrefix}
    62  	}
    63  
    64  	return pluginAPI.keyValues[key], nil
    65  }
    66  
    67  func (pluginAPI *mockPluginAPI) KVDelete(key string) *model.AppError {
    68  	pluginAPI.lock.Lock()
    69  	defer pluginAPI.lock.Unlock()
    70  
    71  	if pluginAPI.failing {
    72  		return &model.AppError{Message: "fake error"}
    73  	}
    74  
    75  	if pluginAPI.failingWithPrefix != "" && strings.HasPrefix(key, pluginAPI.failingWithPrefix) {
    76  		return &model.AppError{Message: "fake error for prefix " + pluginAPI.failingWithPrefix}
    77  	}
    78  
    79  	delete(pluginAPI.keyValues, key)
    80  
    81  	return nil
    82  }
    83  
    84  func (pluginAPI *mockPluginAPI) KVList(page, count int) ([]string, *model.AppError) {
    85  	pluginAPI.lock.Lock()
    86  	defer pluginAPI.lock.Unlock()
    87  
    88  	if pluginAPI.failing {
    89  		return nil, &model.AppError{Message: "fake error"}
    90  	}
    91  
    92  	keys := make([]string, 0, len(pluginAPI.keyValues))
    93  	for k := range pluginAPI.keyValues {
    94  		keys = append(keys, k)
    95  	}
    96  
    97  	// have to sort, because we're paging below
    98  	sort.Strings(keys)
    99  
   100  	start := min(page*count, len(keys))
   101  	end := min((page+1)*count, len(keys))
   102  	return keys[start:end], nil
   103  }
   104  
   105  func min(a, b int) int {
   106  	if a < b {
   107  		return a
   108  	}
   109  	return b
   110  }
   111  
   112  func (pluginAPI *mockPluginAPI) KVSetWithOptions(key string, value []byte, options model.PluginKVSetOptions) (bool, *model.AppError) {
   113  	pluginAPI.lock.Lock()
   114  	defer pluginAPI.lock.Unlock()
   115  
   116  	if pluginAPI.failing {
   117  		return false, &model.AppError{Message: "fake error"}
   118  	}
   119  
   120  	if pluginAPI.failingWithPrefix != "" && strings.HasPrefix(key, pluginAPI.failingWithPrefix) {
   121  		return false, &model.AppError{Message: "fake error for prefix " + pluginAPI.failingWithPrefix}
   122  	}
   123  
   124  	if options.Atomic {
   125  		if actualValue := pluginAPI.keyValues[key]; !bytes.Equal(actualValue, options.OldValue) {
   126  			return false, nil
   127  		}
   128  	}
   129  
   130  	if value == nil {
   131  		delete(pluginAPI.keyValues, key)
   132  	} else {
   133  		pluginAPI.keyValues[key] = value
   134  	}
   135  
   136  	return true, nil
   137  }
   138  
   139  func (pluginAPI *mockPluginAPI) LogError(msg string, keyValuePairs ...interface{}) {
   140  	if pluginAPI.t == nil {
   141  		return
   142  	}
   143  
   144  	pluginAPI.t.Helper()
   145  
   146  	params := []interface{}{msg}
   147  	params = append(params, keyValuePairs...)
   148  
   149  	pluginAPI.t.Log(params...)
   150  }