plugin: fix InstallPlugin() API by manually creating RPC code (#13041)

Flugin: fix InstallPlugin() API by manually creating RPC code

previous implementation of InstallPlugin()-#12232 's RPC funcs wasn't working because `io.Reader` isn't supported by the RPC code generation tool.

RPC does not support streaming data and RPC code generation tool does not handle this exception.

thus, RPC funcs are now implemented manually to stream `io.Reader` through a separate multiplexed connection.
This commit is contained in:
İlker Göktuğ Öztürk
2019-11-29 14:41:17 +03:00
committed by Ben Schumacher
parent e03411795c
commit 0adbfa8478
4 changed files with 177 additions and 31 deletions

View File

@@ -11,6 +11,8 @@ import (
"image/color"
"image/png"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
@@ -731,6 +733,128 @@ func TestPluginAPIInstallPlugin(t *testing.T) {
assert.True(t, found)
}
func TestInstallPlugin(t *testing.T) {
// TODO(ilgooz): remove this setup func to use existent setupPluginApiTest().
// following setupTest() func is a modified version of setupPluginApiTest().
// we need a modified version of setupPluginApiTest() because it wasn't possible to use it directly here
// since it removes plugin dirs right after it returns, does not update App configs with the plugin
// dirs and this behavior tends to break this test as a result.
setupTest := func(t *testing.T, pluginCode string, pluginManifest string, pluginID string, app *App) (func(), string) {
pluginDir, err := ioutil.TempDir("", "")
require.NoError(t, err)
webappPluginDir, err := ioutil.TempDir("", "")
require.NoError(t, err)
app.UpdateConfig(func(cfg *model.Config) {
*cfg.PluginSettings.Directory = pluginDir
*cfg.PluginSettings.ClientDirectory = webappPluginDir
})
env, err := plugin.NewEnvironment(app.NewPluginAPI, pluginDir, webappPluginDir, app.Log)
require.NoError(t, err)
app.SetPluginsEnvironment(env)
backend := filepath.Join(pluginDir, pluginID, "backend.exe")
utils.CompileGo(t, pluginCode, backend)
ioutil.WriteFile(filepath.Join(pluginDir, pluginID, "plugin.json"), []byte(pluginManifest), 0600)
manifest, activated, reterr := env.Activate(pluginID)
require.Nil(t, reterr)
require.NotNil(t, manifest)
require.True(t, activated)
return func() {
os.RemoveAll(pluginDir)
os.RemoveAll(webappPluginDir)
}, pluginDir
}
th := Setup(t).InitBasic()
defer th.TearDown()
// start an http server to serve plugin's tarball to the test.
path, _ := fileutils.FindDir("tests")
ts := httptest.NewServer(http.FileServer(http.Dir(path)))
defer ts.Close()
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.PluginSettings.Enable = true
*cfg.PluginSettings.EnableUploads = true
cfg.PluginSettings.Plugins["testinstallplugin"] = map[string]interface{}{
"DownloadURL": ts.URL + "/testplugin.tar.gz",
}
})
tearDown, _ := setupTest(t,
`
package main
import (
"net/http"
"github.com/pkg/errors"
"github.com/mattermost/mattermost-server/v5/plugin"
)
type configuration struct {
DownloadURL string
}
type Plugin struct {
plugin.MattermostPlugin
configuration configuration
}
func (p *Plugin) OnConfigurationChange() error {
if err := p.API.LoadPluginConfiguration(&p.configuration); err != nil {
return err
}
return nil
}
func (p *Plugin) OnActivate() error {
resp, err := http.Get(p.configuration.DownloadURL)
if err != nil {
return err
}
defer resp.Body.Close()
_, aerr := p.API.InstallPlugin(resp.Body, true)
if aerr != nil {
return errors.Wrap(aerr, "cannot install plugin")
}
return nil
}
func main() {
plugin.ClientMain(&Plugin{})
}
`,
`{"id": "testinstallplugin", "backend": {"executable": "backend.exe"}, "settings_schema": {
"settings": [
{
"key": "DownloadURL",
"type": "text"
}
]
}}`, "testinstallplugin", th.App)
defer tearDown()
hooks, err := th.App.GetPluginsEnvironment().HooksForPlugin("testinstallplugin")
require.NoError(t, err)
err = hooks.OnActivate()
require.NoError(t, err)
plugins, aerr := th.App.GetPlugins()
require.Nil(t, aerr)
require.Len(t, plugins.Inactive, 1)
require.Equal(t, "testplugin", plugins.Inactive[0].Id)
}
func TestPluginAPIGetTeamIcon(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()