Include https://github.com/mattermost/mattermost-plugin-api into the mono repo Co-authored-by: Jesse Hallam <jesse.hallam@gmail.com> Co-authored-by: Michael Kochell <mjkochell@gmail.com> Co-authored-by: Alejandro García Montoro <alejandro.garciamontoro@gmail.com> Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com> Co-authored-by: Alex Dovenmuehle <alex.dovenmuehle@mattermost.com> Co-authored-by: Michael Kochell <6913320+mickmister@users.noreply.github.com> Co-authored-by: Christopher Poile <cpoile@gmail.com> Co-authored-by: İlker Göktuğ Öztürk <ilkergoktugozturk@gmail.com> Co-authored-by: Shota Gvinepadze <wineson@gmail.com> Co-authored-by: Ali Farooq <ali.farooq0@pm.me> Co-authored-by: Maria A Nunez <maria.nunez@mattermost.com> Co-authored-by: Daniel Espino García <larkox@gmail.com> Co-authored-by: Christopher Speller <crspeller@gmail.com> Co-authored-by: Alex Dovenmuehle <adovenmuehle@gmail.com> Co-authored-by: Szymon Gibała <szymongib@gmail.com> Co-authored-by: Lev <1187448+levb@users.noreply.github.com> Co-authored-by: Jason Frerich <jason.frerich@mattermost.com> Co-authored-by: Agniva De Sarker <agnivade@yahoo.co.in> Co-authored-by: Artur M. Wolff <artur.m.wolff@gmail.com> Co-authored-by: Madhav Hugar <16546715+madhavhugar@users.noreply.github.com> Co-authored-by: Joe <security.joe@pm.me> Co-authored-by: Ibrahim Serdar Acikgoz <serdaracikgoz86@gmail.com> Co-authored-by: José Peso <trilopin@users.noreply.github.com>
110 строки
3.0 KiB
Go
110 строки
3.0 KiB
Go
package pluginapi_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/public/plugin/plugintest"
|
|
"github.com/mattermost/mattermost/server/public/pluginapi"
|
|
)
|
|
|
|
func TestStore(t *testing.T) {
|
|
t.Run("master db singleton", func(t *testing.T) {
|
|
config := &model.Config{
|
|
SqlSettings: model.SqlSettings{
|
|
DriverName: model.NewString("test"),
|
|
DataSource: model.NewString("TestStore-master-db"),
|
|
},
|
|
}
|
|
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
api.On("GetUnsanitizedConfig").Return(config)
|
|
|
|
driver := &plugintest.Driver{}
|
|
driver.On("Conn", true).Return("test", nil)
|
|
driver.On("ConnPing", "test").Return(nil)
|
|
driver.On("ConnClose", "test").Return(nil)
|
|
|
|
store := pluginapi.NewClient(api, driver).Store
|
|
|
|
db1, err := store.GetMasterDB()
|
|
require.NoError(t, err)
|
|
require.NotNil(t, db1)
|
|
|
|
db2, err := store.GetMasterDB()
|
|
require.NoError(t, err)
|
|
require.NotNil(t, db2)
|
|
|
|
require.Same(t, db1, db2)
|
|
require.NoError(t, store.Close())
|
|
})
|
|
|
|
t.Run("master db fallback", func(t *testing.T) {
|
|
config := &model.Config{
|
|
SqlSettings: model.SqlSettings{
|
|
DriverName: model.NewString("ramsql"),
|
|
DataSource: model.NewString("TestStore-master-db"),
|
|
ConnMaxLifetimeMilliseconds: model.NewInt(2),
|
|
},
|
|
}
|
|
|
|
driver := &plugintest.Driver{}
|
|
driver.On("Conn", true).Return("test", nil)
|
|
driver.On("ConnPing", "test").Return(nil)
|
|
driver.On("ConnClose", "test").Return(nil)
|
|
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
store := pluginapi.NewClient(api, driver).Store
|
|
|
|
api.On("GetUnsanitizedConfig").Return(config)
|
|
masterDB, err := store.GetMasterDB()
|
|
require.NoError(t, err)
|
|
require.NotNil(t, masterDB)
|
|
|
|
// No replica is set up, should fallback to master
|
|
replicaDB, err := store.GetReplicaDB()
|
|
require.NoError(t, err)
|
|
require.Same(t, replicaDB, masterDB)
|
|
|
|
require.NoError(t, store.Close())
|
|
})
|
|
|
|
t.Run("replica db singleton", func(t *testing.T) {
|
|
config := &model.Config{
|
|
SqlSettings: model.SqlSettings{
|
|
DriverName: model.NewString("ramsql"),
|
|
DataSource: model.NewString("TestStore-master-db"),
|
|
DataSourceReplicas: []string{"TestStore-master-db"},
|
|
ConnMaxLifetimeMilliseconds: model.NewInt(2),
|
|
},
|
|
}
|
|
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
api.On("GetUnsanitizedConfig").Return(config)
|
|
|
|
driver := &plugintest.Driver{}
|
|
driver.On("Conn", true).Return("test", nil)
|
|
driver.On("Conn", false).Return("test", nil)
|
|
driver.On("ConnPing", "test").Return(nil)
|
|
driver.On("ConnClose", "test").Return(nil)
|
|
|
|
store := pluginapi.NewClient(api, driver).Store
|
|
|
|
db1, err := store.GetReplicaDB()
|
|
require.NoError(t, err)
|
|
require.NotNil(t, db1)
|
|
|
|
db2, err := store.GetReplicaDB()
|
|
require.NoError(t, err)
|
|
require.NotNil(t, db2)
|
|
|
|
require.Same(t, db1, db2)
|
|
require.NoError(t, store.Close())
|
|
})
|
|
}
|