[MM-19153] Add server version check in helper methods for plugins (#12675)

Этот коммит содержится в:
Rajat Varyani
2019-11-06 22:12:46 +05:30
коммит произвёл Ben Schumacher
родитель a39f4f1064
Коммит 14b1777959
5 изменённых файлов: 172 добавлений и 5 удалений

Просмотреть файл

@@ -10,10 +10,26 @@ import (
)
func TestKVGetJSON(t *testing.T) {
t.Run("incompatible server version", func(t *testing.T) {
api := &plugintest.API{}
api.On("GetServerVersion").Return("5.1.0")
p := &plugin.HelpersImpl{API: api}
var dat map[string]interface{}
ok, err := p.KVGetJSON("test-key", dat)
api.AssertExpectations(t)
assert.False(t, ok)
assert.Error(t, err)
assert.Equal(t, "incompatible server version for plugin, minimum required version: 5.2.0, current version: 5.1.0", err.Error())
})
t.Run("KVGet error", func(t *testing.T) {
p := &plugin.HelpersImpl{}
api := &plugintest.API{}
api.On("GetServerVersion").Return("5.2.0")
api.On("KVGet", "test-key").Return(nil, &model.AppError{})
p.API = api
@@ -30,6 +46,7 @@ func TestKVGetJSON(t *testing.T) {
p := &plugin.HelpersImpl{}
api := &plugintest.API{}
api.On("GetServerVersion").Return("5.2.0")
api.On("KVGet", "test-key").Return(nil, nil)
p.API = api
@@ -46,6 +63,7 @@ func TestKVGetJSON(t *testing.T) {
p := &plugin.HelpersImpl{}
api := &plugintest.API{}
api.On("GetServerVersion").Return("5.2.0")
api.On("KVGet", "test-key").Return([]byte(`{{:}"val-a": 10}`), nil)
p.API = api
@@ -62,6 +80,7 @@ func TestKVGetJSON(t *testing.T) {
p := &plugin.HelpersImpl{}
api := &plugintest.API{}
api.On("GetServerVersion").Return("5.2.0")
api.On("KVGet", "test-key").Return([]byte(`{"val-a": 10}`), nil)
p.API = api
@@ -78,9 +97,25 @@ func TestKVGetJSON(t *testing.T) {
}
func TestKVSetJSON(t *testing.T) {
t.Run("incompatible server version", func(t *testing.T) {
api := &plugintest.API{}
api.On("GetServerVersion").Return("5.1.0")
p := &plugin.HelpersImpl{API: api}
err := p.KVSetJSON("test-key", map[string]interface{}{
"val-a": float64(10),
})
api.AssertExpectations(t)
assert.Error(t, err)
assert.Equal(t, "incompatible server version for plugin, minimum required version: 5.2.0, current version: 5.1.0", err.Error())
})
t.Run("JSON marshal error", func(t *testing.T) {
api := &plugintest.API{}
api.AssertNotCalled(t, "KVSet")
api.On("GetServerVersion").Return("5.2.0")
p := &plugin.HelpersImpl{API: api}
@@ -92,6 +127,7 @@ func TestKVSetJSON(t *testing.T) {
t.Run("KVSet error", func(t *testing.T) {
api := &plugintest.API{}
api.On("KVSet", "test-key", []byte(`{"val-a":10}`)).Return(&model.AppError{})
api.On("GetServerVersion").Return("5.2.0")
p := &plugin.HelpersImpl{API: api}
@@ -106,6 +142,7 @@ func TestKVSetJSON(t *testing.T) {
t.Run("marshallable struct", func(t *testing.T) {
api := &plugintest.API{}
api.On("KVSet", "test-key", []byte(`{"val-a":10}`)).Return(nil)
api.On("GetServerVersion").Return("5.2.0")
p := &plugin.HelpersImpl{API: api}
@@ -119,9 +156,24 @@ func TestKVSetJSON(t *testing.T) {
}
func TestKVCompareAndSetJSON(t *testing.T) {
t.Run("incompatible server version", func(t *testing.T) {
api := &plugintest.API{}
api.On("GetServerVersion").Return("5.10.0")
p := &plugin.HelpersImpl{API: api}
ok, err := p.KVCompareAndSetJSON("test-key", nil, map[string]interface{}{
"val-b": 20,
})
assert.Equal(t, false, ok)
assert.Error(t, err)
assert.Equal(t, "incompatible server version for plugin, minimum required version: 5.12.0, current version: 5.10.0", err.Error())
})
t.Run("old value JSON marshal error", func(t *testing.T) {
api := &plugintest.API{}
api.AssertNotCalled(t, "KVCompareAndSet")
api.On("GetServerVersion").Return("5.12.0")
p := &plugin.HelpersImpl{API: api}
ok, err := p.KVCompareAndSetJSON("test-key", func() {}, map[string]interface{}{})
@@ -133,6 +185,7 @@ func TestKVCompareAndSetJSON(t *testing.T) {
t.Run("new value JSON marshal error", func(t *testing.T) {
api := &plugintest.API{}
api.On("GetServerVersion").Return("5.12.0")
api.AssertNotCalled(t, "KVCompareAndSet")
p := &plugin.HelpersImpl{API: api}
@@ -146,6 +199,7 @@ func TestKVCompareAndSetJSON(t *testing.T) {
t.Run("KVCompareAndSet error", func(t *testing.T) {
api := &plugintest.API{}
api.On("GetServerVersion").Return("5.12.0")
api.On("KVCompareAndSet", "test-key", []byte(`{"val-a":10}`), []byte(`{"val-b":20}`)).Return(false, &model.AppError{})
p := &plugin.HelpersImpl{API: api}
@@ -162,6 +216,7 @@ func TestKVCompareAndSetJSON(t *testing.T) {
t.Run("old value nil", func(t *testing.T) {
api := &plugintest.API{}
api.On("GetServerVersion").Return("5.12.0")
api.On("KVCompareAndSet", "test-key", []byte(nil), []byte(`{"val-b":20}`)).Return(true, nil)
p := &plugin.HelpersImpl{API: api}
@@ -176,6 +231,7 @@ func TestKVCompareAndSetJSON(t *testing.T) {
t.Run("old value non-nil", func(t *testing.T) {
api := &plugintest.API{}
api.On("GetServerVersion").Return("5.12.0")
api.On("KVCompareAndSet", "test-key", []byte(`{"val-a":10}`), []byte(`{"val-b":20}`)).Return(true, nil)
p := &plugin.HelpersImpl{API: api}
@@ -192,6 +248,7 @@ func TestKVCompareAndSetJSON(t *testing.T) {
t.Run("new value nil", func(t *testing.T) {
api := &plugintest.API{}
api.On("GetServerVersion").Return("5.12.0")
api.On("KVCompareAndSet", "test-key", []byte(`{"val-a":10}`), []byte(nil)).Return(true, nil)
p := &plugin.HelpersImpl{API: api}
@@ -206,8 +263,23 @@ func TestKVCompareAndSetJSON(t *testing.T) {
}
func TestKVCompareAndDeleteJSON(t *testing.T) {
t.Run("incompatible server version", func(t *testing.T) {
api := &plugintest.API{}
api.On("GetServerVersion").Return("5.10.0")
p := &plugin.HelpersImpl{API: api}
ok, err := p.KVCompareAndDeleteJSON("test-key", map[string]interface{}{
"val-a": 10,
})
assert.Equal(t, false, ok)
assert.Error(t, err)
assert.Equal(t, "incompatible server version for plugin, minimum required version: 5.16.0, current version: 5.10.0", err.Error())
})
t.Run("old value JSON marshal error", func(t *testing.T) {
api := &plugintest.API{}
api.On("GetServerVersion").Return("5.16.0")
api.AssertNotCalled(t, "KVCompareAndDelete")
p := &plugin.HelpersImpl{API: api}
@@ -220,6 +292,7 @@ func TestKVCompareAndDeleteJSON(t *testing.T) {
t.Run("KVCompareAndDelete error", func(t *testing.T) {
api := &plugintest.API{}
api.On("GetServerVersion").Return("5.16.0")
api.On("KVCompareAndDelete", "test-key", []byte(`{"val-a":10}`)).Return(false, &model.AppError{})
p := &plugin.HelpersImpl{API: api}
@@ -234,6 +307,7 @@ func TestKVCompareAndDeleteJSON(t *testing.T) {
t.Run("old value nil", func(t *testing.T) {
api := &plugintest.API{}
api.On("GetServerVersion").Return("5.16.0")
api.On("KVCompareAndDelete", "test-key", []byte(nil)).Return(true, nil)
p := &plugin.HelpersImpl{API: api}
@@ -246,6 +320,7 @@ func TestKVCompareAndDeleteJSON(t *testing.T) {
t.Run("old value non-nil", func(t *testing.T) {
api := &plugintest.API{}
api.On("GetServerVersion").Return("5.16.0")
api.On("KVCompareAndDelete", "test-key", []byte(`{"val-a":10}`)).Return(true, nil)
p := &plugin.HelpersImpl{API: api}
@@ -260,8 +335,24 @@ func TestKVCompareAndDeleteJSON(t *testing.T) {
}
func TestKVSetWithExpiryJSON(t *testing.T) {
t.Run("incompatible server version", func(t *testing.T) {
api := &plugintest.API{}
api.On("GetServerVersion").Return("5.4.0")
p := &plugin.HelpersImpl{API: api}
err := p.KVSetWithExpiryJSON("test-key", map[string]interface{}{
"val-a": float64(10),
}, 100)
api.AssertExpectations(t)
assert.Error(t, err)
assert.Equal(t, "incompatible server version for plugin, minimum required version: 5.6.0, current version: 5.4.0", err.Error())
})
t.Run("JSON marshal error", func(t *testing.T) {
api := &plugintest.API{}
api.On("GetServerVersion").Return("5.6.0")
api.AssertNotCalled(t, "KVSetWithExpiry")
p := &plugin.HelpersImpl{API: api}
@@ -274,6 +365,7 @@ func TestKVSetWithExpiryJSON(t *testing.T) {
t.Run("KVSetWithExpiry error", func(t *testing.T) {
api := &plugintest.API{}
api.On("GetServerVersion").Return("5.6.0")
api.On("KVSetWithExpiry", "test-key", []byte(`{"val-a":10}`), int64(100)).Return(&model.AppError{})
p := &plugin.HelpersImpl{API: api}
@@ -287,6 +379,7 @@ func TestKVSetWithExpiryJSON(t *testing.T) {
t.Run("wellformed JSON", func(t *testing.T) {
api := &plugintest.API{}
api.On("GetServerVersion").Return("5.6.0")
api.On("KVSetWithExpiry", "test-key", []byte(`{"val-a":10}`), int64(100)).Return(nil)
p := &plugin.HelpersImpl{API: api}