[MM-20353] Change KVSetWithOptions to accept a byte slice (#13213)
* Drop EncodeJSON from PluginKVSetOptions * Add unit tests for KVSetWithOptions with nil values
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
3543fcce6c
Коммит
d0d6ce0a70
@@ -697,7 +697,7 @@ func (api *PluginAPI) InstallPlugin(file io.Reader, replace bool) (*model.Manife
|
||||
|
||||
// KV Store Section
|
||||
|
||||
func (api *PluginAPI) KVSetWithOptions(key string, value interface{}, options model.PluginKVSetOptions) (bool, *model.AppError) {
|
||||
func (api *PluginAPI) KVSetWithOptions(key string, value []byte, options model.PluginKVSetOptions) (bool, *model.AppError) {
|
||||
return api.app.SetPluginKeyWithOptions(api.id, key, value, options)
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ func (a *App) CompareAndSetPluginKey(pluginId string, key string, oldValue, newV
|
||||
return a.SetPluginKeyWithOptions(pluginId, key, newValue, options)
|
||||
}
|
||||
|
||||
func (a *App) SetPluginKeyWithOptions(pluginId string, key string, value interface{}, options model.PluginKVSetOptions) (bool, *model.AppError) {
|
||||
func (a *App) SetPluginKeyWithOptions(pluginId string, key string, value []byte, options model.PluginKVSetOptions) (bool, *model.AppError) {
|
||||
if err := options.IsValid(); err != nil {
|
||||
mlog.Error("Failed to set plugin key value with options", mlog.String("plugin_id", pluginId), mlog.String("key", key), mlog.Err(err))
|
||||
return false, err
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -197,60 +198,24 @@ func TestPluginKeyValueStoreSetWithOptionsJSON(t *testing.T) {
|
||||
assert.Nil(t, th.App.DeletePluginKey(pluginId, "key"))
|
||||
}()
|
||||
|
||||
t.Run("fails with a non-serializable object as the new value", func(t *testing.T) {
|
||||
result, err := th.App.SetPluginKeyWithOptions(pluginId, "key", func() {}, model.PluginKVSetOptions{
|
||||
EncodeJSON: true,
|
||||
})
|
||||
assert.False(t, result)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
// verify that after the failure it was not set
|
||||
ret, err := th.App.GetPluginKey(pluginId, "key")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, []byte(nil), ret)
|
||||
})
|
||||
|
||||
t.Run("fails with a non-serializable object as the old value", func(t *testing.T) {
|
||||
result, err := th.App.SetPluginKeyWithOptions(pluginId, "key", map[string]interface{}{
|
||||
"val-a": 10,
|
||||
}, model.PluginKVSetOptions{
|
||||
EncodeJSON: true,
|
||||
Atomic: true,
|
||||
OldValue: func() {},
|
||||
})
|
||||
assert.False(t, result)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
// verify that after the failure it was not set
|
||||
ret, err := th.App.GetPluginKey(pluginId, "key")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, []byte(nil), ret)
|
||||
})
|
||||
|
||||
t.Run("storing a value json encoded works", func(t *testing.T) {
|
||||
result, err := th.App.SetPluginKeyWithOptions(pluginId, "key", map[string]interface{}{
|
||||
"val-a": 10,
|
||||
}, model.PluginKVSetOptions{
|
||||
EncodeJSON: true,
|
||||
})
|
||||
t.Run("storing a value without providing options works", func(t *testing.T) {
|
||||
result, err := th.App.SetPluginKeyWithOptions(pluginId, "key", []byte("value-1"), model.PluginKVSetOptions{})
|
||||
assert.True(t, result)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// and I can get it back!
|
||||
ret, err := th.App.GetPluginKey(pluginId, "key")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, []byte(`{"val-a":10}`), ret)
|
||||
assert.Equal(t, []byte(`value-1`), ret)
|
||||
})
|
||||
|
||||
t.Run("test that setting it atomic when it doesn't match doesn't change anything", func(t *testing.T) {
|
||||
result, err := th.App.SetPluginKeyWithOptions(pluginId, "key", map[string]interface{}{
|
||||
"val-a": 30,
|
||||
}, model.PluginKVSetOptions{
|
||||
EncodeJSON: true,
|
||||
Atomic: true,
|
||||
OldValue: map[string]interface{}{
|
||||
"val-a": 20,
|
||||
},
|
||||
err := th.App.SetPluginKey(pluginId, "key", []byte("value-1"))
|
||||
require.Nil(t, err)
|
||||
|
||||
result, err := th.App.SetPluginKeyWithOptions(pluginId, "key", []byte("value-3"), model.PluginKVSetOptions{
|
||||
Atomic: true,
|
||||
OldValue: []byte("value-2"),
|
||||
})
|
||||
assert.False(t, result)
|
||||
assert.Nil(t, err)
|
||||
@@ -258,18 +223,16 @@ func TestPluginKeyValueStoreSetWithOptionsJSON(t *testing.T) {
|
||||
// test that the value didn't change
|
||||
ret, err := th.App.GetPluginKey(pluginId, "key")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, []byte(`{"val-a":10}`), ret)
|
||||
assert.Equal(t, []byte(`value-1`), ret)
|
||||
})
|
||||
|
||||
t.Run("test the atomic change with the proper old value", func(t *testing.T) {
|
||||
result, err := th.App.SetPluginKeyWithOptions(pluginId, "key", map[string]interface{}{
|
||||
"val-a": 30,
|
||||
}, model.PluginKVSetOptions{
|
||||
EncodeJSON: true,
|
||||
Atomic: true,
|
||||
OldValue: map[string]interface{}{
|
||||
"val-a": 10,
|
||||
},
|
||||
err := th.App.SetPluginKey(pluginId, "key", []byte("value-2"))
|
||||
require.Nil(t, err)
|
||||
|
||||
result, err := th.App.SetPluginKeyWithOptions(pluginId, "key", []byte("value-3"), model.PluginKVSetOptions{
|
||||
Atomic: true,
|
||||
OldValue: []byte("value-2"),
|
||||
})
|
||||
assert.True(t, result)
|
||||
assert.Nil(t, err)
|
||||
@@ -277,55 +240,76 @@ func TestPluginKeyValueStoreSetWithOptionsJSON(t *testing.T) {
|
||||
// test that the value did change
|
||||
ret, err := th.App.GetPluginKey(pluginId, "key")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, []byte(`{"val-a":30}`), ret)
|
||||
assert.Equal(t, []byte(`value-3`), ret)
|
||||
})
|
||||
}
|
||||
|
||||
func TestPluginKeyValueStoreSetWithOptionsByteArray(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
t.Run("when new value is nil and old value matches with the current, it should delete the currently set value", func(t *testing.T) {
|
||||
// first set a value.
|
||||
result, err := th.App.SetPluginKeyWithOptions(pluginId, "nil-test-key-2", []byte("value-1"), model.PluginKVSetOptions{})
|
||||
require.Nil(t, err)
|
||||
require.True(t, result)
|
||||
|
||||
pluginId := "testpluginid"
|
||||
// now it should delete the set value.
|
||||
result, err = th.App.SetPluginKeyWithOptions(pluginId, "nil-test-key-2", nil, model.PluginKVSetOptions{
|
||||
Atomic: true,
|
||||
OldValue: []byte("value-1"),
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, result)
|
||||
|
||||
defer func() {
|
||||
assert.Nil(t, th.App.DeletePluginKey(pluginId, "key"))
|
||||
}()
|
||||
|
||||
// storing a value works
|
||||
result, err := th.App.SetPluginKeyWithOptions(pluginId, "key", []byte(`myvalue`), model.PluginKVSetOptions{})
|
||||
assert.True(t, result)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// and I can get it back!
|
||||
ret, err := th.App.GetPluginKey(pluginId, "key")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, []byte(`myvalue`), ret)
|
||||
|
||||
// test that setting it atomic when it doesn't match doesn't change anything
|
||||
result, err = th.App.SetPluginKeyWithOptions(pluginId, "key", []byte(`newvalue`), model.PluginKVSetOptions{
|
||||
Atomic: true,
|
||||
OldValue: []byte(`differentvalue`),
|
||||
ret, err := th.App.GetPluginKey(pluginId, "nil-test-key-2")
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, ret)
|
||||
})
|
||||
assert.False(t, result)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// test that the value didn't change
|
||||
ret, err = th.App.GetPluginKey(pluginId, "key")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, []byte(`myvalue`), ret)
|
||||
t.Run("when new value is nil and there is a value set for the key already, it should delete the currently set value", func(t *testing.T) {
|
||||
// first set a value.
|
||||
result, err := th.App.SetPluginKeyWithOptions(pluginId, "nil-test-key-3", []byte("value-1"), model.PluginKVSetOptions{})
|
||||
require.Nil(t, err)
|
||||
require.True(t, result)
|
||||
|
||||
// now do the atomic change with the proper old value
|
||||
result, err = th.App.SetPluginKeyWithOptions(pluginId, "key", []byte(`newvalue`), model.PluginKVSetOptions{
|
||||
Atomic: true,
|
||||
OldValue: []byte(`myvalue`),
|
||||
// now it should delete the set value.
|
||||
result, err = th.App.SetPluginKeyWithOptions(pluginId, "nil-test-key-3", nil, model.PluginKVSetOptions{})
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, result)
|
||||
|
||||
ret, err := th.App.GetPluginKey(pluginId, "nil-test-key-3")
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, ret)
|
||||
})
|
||||
assert.True(t, result)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// test that the value did change
|
||||
ret, err = th.App.GetPluginKey(pluginId, "key")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, []byte(`newvalue`), ret)
|
||||
t.Run("when old value is nil and there is no value set for the key before, it should set the new value", func(t *testing.T) {
|
||||
result, err := th.App.SetPluginKeyWithOptions(pluginId, "nil-test-key-4", []byte("value-1"), model.PluginKVSetOptions{
|
||||
Atomic: true,
|
||||
OldValue: nil,
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, result)
|
||||
|
||||
ret, err := th.App.GetPluginKey(pluginId, "nil-test-key-4")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, []byte("value-1"), ret)
|
||||
})
|
||||
|
||||
t.Run("test that value is set and unset with ExpireInSeconds", func(t *testing.T) {
|
||||
result, err := th.App.SetPluginKeyWithOptions(pluginId, "key", []byte("value-1"), model.PluginKVSetOptions{
|
||||
ExpireInSeconds: 1,
|
||||
})
|
||||
assert.True(t, result)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// test that the value is set
|
||||
ret, err := th.App.GetPluginKey(pluginId, "key")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, []byte(`value-1`), ret)
|
||||
|
||||
// test that the value is not longer
|
||||
time.Sleep(1500 * time.Millisecond)
|
||||
|
||||
ret, err = th.App.GetPluginKey(pluginId, "key")
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, ret)
|
||||
})
|
||||
}
|
||||
|
||||
func TestServePluginRequest(t *testing.T) {
|
||||
|
||||
Ссылка в новой задаче
Block a user