Adding interplugin communication. (#12829)

* Adding interplugin communication.

* Naming changes and moving ResponseTransfer to own file.

* Fix.

* Tests and moving to buffering bytes.

* Switching API to passing plugin ID through path rather than a header.

* Review feedback.
Этот коммит содержится в:
Christopher Speller
2019-11-04 17:35:58 -08:00
коммит произвёл GitHub
родитель 501da809f3
Коммит 428454cee4
9 изменённых файлов: 351 добавлений и 19 удалений

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

@@ -26,7 +26,7 @@ import (
"github.com/stretchr/testify/require"
)
func setupPluginApiTest(t *testing.T, pluginCode string, pluginManifest string, pluginId string, app *App) string {
func setupMultiPluginApiTest(t *testing.T, pluginCodes []string, pluginManifests []string, pluginIds []string, app *App) string {
pluginDir, err := ioutil.TempDir("", "")
require.NoError(t, err)
webappPluginDir, err := ioutil.TempDir("", "")
@@ -37,20 +37,29 @@ func setupPluginApiTest(t *testing.T, pluginCode string, pluginManifest string,
env, err := plugin.NewEnvironment(app.NewPluginAPI, pluginDir, webappPluginDir, app.Log)
require.NoError(t, err)
backend := filepath.Join(pluginDir, pluginId, "backend.exe")
utils.CompileGo(t, pluginCode, backend)
require.Equal(t, len(pluginCodes), len(pluginIds))
require.Equal(t, len(pluginManifests), len(pluginIds))
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)
for i, pluginId := range pluginIds {
backend := filepath.Join(pluginDir, pluginId, "backend.exe")
utils.CompileGo(t, pluginCodes[i], backend)
ioutil.WriteFile(filepath.Join(pluginDir, pluginId, "plugin.json"), []byte(pluginManifests[i]), 0600)
manifest, activated, reterr := env.Activate(pluginId)
require.Nil(t, reterr)
require.NotNil(t, manifest)
require.True(t, activated)
}
app.SetPluginsEnvironment(env)
return pluginDir
}
func setupPluginApiTest(t *testing.T, pluginCode string, pluginManifest string, pluginId string, app *App) string {
return setupMultiPluginApiTest(t, []string{pluginCode}, []string{pluginManifest}, []string{pluginId}, app)
}
func TestPublicFilesPathConfiguration(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
@@ -1462,3 +1471,98 @@ func TestPluginAddUserToChannel(t *testing.T) {
require.Equal(t, th.BasicChannel.Id, member.ChannelId)
require.Equal(t, th.BasicUser.Id, member.UserId)
}
func TestInterpluginPluginHTTP(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
setupMultiPluginApiTest(t,
[]string{`
package main
import (
"github.com/mattermost/mattermost-server/plugin"
"bytes"
"net/http"
)
type MyPlugin struct {
plugin.MattermostPlugin
}
func (p *MyPlugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v2/test" {
return
}
buf := bytes.Buffer{}
buf.ReadFrom(r.Body)
resp := "we got:" + buf.String()
w.WriteHeader(598)
w.Write([]byte(resp))
}
func main() {
plugin.ClientMain(&MyPlugin{})
}
`,
`
package main
import (
"github.com/mattermost/mattermost-server/plugin"
"github.com/mattermost/mattermost-server/model"
"bytes"
"net/http"
"io/ioutil"
)
type MyPlugin struct {
plugin.MattermostPlugin
}
func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
buf := bytes.Buffer{}
buf.WriteString("This is the request")
req, err := http.NewRequest("GET", "/testplugininterserver/api/v2/test", &buf)
if err != nil {
return nil, err.Error()
}
req.Header.Add("Mattermost-User-Id", "userid")
resp := p.API.PluginHTTP(req)
if resp == nil {
return nil, "Nil resp"
}
if resp.Body == nil {
return nil, "Nil body"
}
respbody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err.Error()
}
if resp.StatusCode != 598 {
return nil, "wrong status " + string(respbody)
}
return nil, string(respbody)
}
func main() {
plugin.ClientMain(&MyPlugin{})
}
`,
},
[]string{
`{"id": "testplugininterserver", "backend": {"executable": "backend.exe"}}`,
`{"id": "testplugininterclient", "backend": {"executable": "backend.exe"}}`,
},
[]string{
"testplugininterserver",
"testplugininterclient",
},
th.App,
)
hooks, err := th.App.GetPluginsEnvironment().HooksForPlugin("testplugininterclient")
require.NoError(t, err)
_, ret := hooks.MessageWillBePosted(nil, nil)
assert.Equal(t, "we got:This is the request", ret)
}