From 53f4bf1ec1dba61c7ccb17c94dbb2ba64a6f6b4e Mon Sep 17 00:00:00 2001 From: Maria A Nunez Date: Thu, 22 Aug 2019 13:33:28 -0400 Subject: [PATCH] MM-17688 - Updated http timeout for plugin install from URL (#11887) * Bumped http request timeout for plugin install from URL * Added unit test for timeout higher than 30 seconds. Other minor PR feedback * Fixed spacing --- api4/plugin.go | 6 ++++++ api4/plugin_test.go | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/api4/plugin.go b/api4/plugin.go index 4ce44f9746..1f045afc71 100644 --- a/api4/plugin.go +++ b/api4/plugin.go @@ -10,6 +10,7 @@ import ( "io/ioutil" "net/http" "net/url" + "time" "github.com/mattermost/mattermost-server/mlog" "github.com/mattermost/mattermost-server/model" @@ -17,6 +18,9 @@ import ( const ( MAXIMUM_PLUGIN_FILE_SIZE = 50 * 1024 * 1024 + // INSTALL_PLUGIN_FROM_URL_HTTP_REQUEST_TIMEOUT defines a high timeout for installing plugins + // from an external URL to avoid slow connections or large plugins from failing to install. + INSTALL_PLUGIN_FROM_URL_HTTP_REQUEST_TIMEOUT = 60 * time.Minute ) func (api *API) InitPlugin() { @@ -115,6 +119,8 @@ func installPluginFromUrl(c *Context, w http.ResponseWriter, r *http.Request) { } client := c.App.HTTPService.MakeClient(true) + client.Timeout = INSTALL_PLUGIN_FROM_URL_HTTP_REQUEST_TIMEOUT + resp, err := client.Get(downloadUrl) if err != nil { c.Err = model.NewAppError("installPluginFromUrl", "api.plugin.install.download_failed.app_error", nil, err.Error(), http.StatusBadRequest) diff --git a/api4/plugin_test.go b/api4/plugin_test.go index 482ad32f6f..ded44cb9e4 100644 --- a/api4/plugin_test.go +++ b/api4/plugin_test.go @@ -12,6 +12,7 @@ import ( "os" "path/filepath" "testing" + "time" "github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/testlib" @@ -59,6 +60,24 @@ func TestPlugin(t *testing.T) { CheckNoError(t, resp) assert.Equal(t, "testplugin", manifest.Id) + t.Run("install plugin from URL with slow response time", func(t *testing.T) { + if testing.Short() { + t.Skip("skipping test to install plugin from a slow response server") + } + + // Install from URL - slow server to simulate longer bundle download times + slowTestServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { + time.Sleep(60 * time.Second) // Wait longer than the previous default 30 seconds timeout + res.WriteHeader(http.StatusOK) + res.Write(tarData) + })) + defer func() { slowTestServer.Close() }() + + manifest, resp = th.SystemAdminClient.InstallPluginFromUrl(slowTestServer.URL, true) + CheckNoError(t, resp) + assert.Equal(t, "testplugin", manifest.Id) + }) + // Stored in File Store: Install Plugin from URL case pluginStored, err := th.App.FileExists("./plugins/" + manifest.Id + ".tar.gz") assert.Nil(t, err)