[GH-21224] return error in plugin cmd (#26305)

This commit is contained in:
Anna 2024-03-05 13:18:30 +01:00 committed by GitHub
parent 09f78c240a
commit 3efcef45ff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 32 additions and 26 deletions

View file

@ -5,6 +5,7 @@ package commands
import (
"context"
"fmt"
"os"
"github.com/mattermost/mattermost/server/v8/cmd/mmctl/client"
@ -94,6 +95,7 @@ func init() {
func pluginAddCmdF(c client.Client, cmd *cobra.Command, args []string) error {
force, _ := cmd.Flags().GetBool("force")
var multiErr *multierror.Error
for i, plugin := range args {
fileReader, err := os.Open(plugin)
@ -109,13 +111,14 @@ func pluginAddCmdF(c client.Client, cmd *cobra.Command, args []string) error {
if err != nil {
printer.PrintError("Unable to add plugin: " + args[i] + ". Error: " + err.Error())
multiErr = multierror.Append(multiErr, err)
} else {
printer.Print("Added plugin: " + plugin)
}
fileReader.Close()
}
return nil
return multiErr.ErrorOrNil()
}
func pluginInstallURLCmdF(c client.Client, cmd *cobra.Command, args []string) error {
@ -136,39 +139,45 @@ func pluginInstallURLCmdF(c client.Client, cmd *cobra.Command, args []string) er
}
func pluginDeleteCmdF(c client.Client, cmd *cobra.Command, args []string) error {
var multiErr *multierror.Error
for _, plugin := range args {
if _, err := c.RemovePlugin(context.TODO(), plugin); err != nil {
printer.PrintError("Unable to delete plugin: " + plugin + ". Error: " + err.Error())
multiErr = multierror.Append(multiErr, fmt.Errorf("Unable to delete plugin. %w", err))
} else {
printer.Print("Deleted plugin: " + plugin)
}
}
return nil
return multiErr.ErrorOrNil()
}
func pluginEnableCmdF(c client.Client, cmd *cobra.Command, args []string) error {
var multiErr *multierror.Error
for _, plugin := range args {
if _, err := c.EnablePlugin(context.TODO(), plugin); err != nil {
printer.PrintError("Unable to enable plugin: " + plugin + ". Error: " + err.Error())
multiErr = multierror.Append(multiErr, err)
} else {
printer.Print("Enabled plugin: " + plugin)
}
}
return nil
return multiErr.ErrorOrNil()
}
func pluginDisableCmdF(c client.Client, cmd *cobra.Command, args []string) error {
var multiErr *multierror.Error
for _, plugin := range args {
if _, err := c.DisablePlugin(context.TODO(), plugin); err != nil {
printer.PrintError("Unable to disable plugin: " + plugin + ". Error: " + err.Error())
multiErr = multierror.Append(multiErr, err)
} else {
printer.Print("Disabled plugin: " + plugin)
}
}
return nil
return multiErr.ErrorOrNil()
}
func pluginListCmdF(c client.Client, cmd *cobra.Command, args []string) error {

View file

@ -46,7 +46,7 @@ func (s *MmctlE2ETestSuite) TestPluginAddCmd() {
printer.Clean()
err = pluginAddCmdF(c, &cobra.Command{}, []string{pluginPath})
s.Require().Nil(err)
s.Require().ErrorContains(err, "Unable to install plugin. A plugin with the same ID is already installed.")
s.Require().Equal(0, len(printer.GetLines()))
s.Require().Equal(1, len(printer.GetErrorLines()))
@ -108,7 +108,7 @@ func (s *MmctlE2ETestSuite) TestPluginAddCmd() {
printer.Clean()
err := pluginAddCmdF(c, &cobra.Command{}, []string{pluginPath})
s.Require().Nil(err)
s.Require().ErrorContains(err, "Plugins and/or plugin uploads have been disabled.")
s.Require().Equal(1, len(printer.GetErrorLines()))
s.Require().Contains(printer.GetErrorLines()[0], "Plugins and/or plugin uploads have been disabled.")
})
@ -156,7 +156,7 @@ func (s *MmctlE2ETestSuite) TestPluginAddCmd() {
})
err := pluginAddCmdF(s.th.Client, &cobra.Command{}, []string{pluginPath})
s.Require().Nil(err)
s.Require().ErrorContains(err, "You do not have the appropriate permissions")
s.Require().Equal(1, len(printer.GetErrorLines()))
s.Require().Contains(printer.GetErrorLines()[0], "You do not have the appropriate permissions")
})
@ -201,7 +201,7 @@ func (s *MmctlE2ETestSuite) TestPluginInstallURLCmd() {
var expected error
expected = multierror.Append(expected, errors.New("You do not have the appropriate permissions.")) //nolint:revive
err := pluginInstallURLCmdF(s.th.Client, &cobra.Command{}, []string{jiraURL})
s.Require().EqualError(err, expected.Error())
s.Require().ErrorContains(err, expected.Error())
s.Require().Len(printer.GetLines(), 0)
s.Require().Len(printer.GetErrorLines(), 1)
s.Require().Contains(printer.GetErrorLines()[0], fmt.Sprintf("Unable to install plugin from URL \"%s\".", jiraURL))
@ -221,7 +221,7 @@ func (s *MmctlE2ETestSuite) TestPluginInstallURLCmd() {
expected = multierror.Append(expected, errors.New("An error occurred while downloading the plugin.")) //nolint:revive
err := pluginInstallURLCmdF(c, &cobra.Command{}, []string{pluginURL})
s.Require().EqualError(err, expected.Error())
s.Require().ErrorContains(err, expected.Error())
s.Require().Len(printer.GetLines(), 0)
s.Require().Len(printer.GetErrorLines(), 1)
s.Require().Contains(printer.GetErrorLines()[0], fmt.Sprintf("Unable to install plugin from URL \"%s\".", pluginURL))
@ -246,7 +246,7 @@ func (s *MmctlE2ETestSuite) TestPluginInstallURLCmd() {
var expected error
expected = multierror.Append(expected, errors.New("Unable to install plugin. A plugin with the same ID is already installed.")) //nolint:revive
err = pluginInstallURLCmdF(c, &cobra.Command{}, []string{jiraURL})
s.Require().EqualError(err, expected.Error())
s.Require().ErrorContains(err, expected.Error())
s.Require().Len(printer.GetLines(), 1)
s.Require().Len(printer.GetErrorLines(), 1)
s.Require().Contains(printer.GetErrorLines()[0], fmt.Sprintf("Unable to install plugin from URL \"%s\".", jiraURL))
@ -329,7 +329,7 @@ func (s *MmctlE2ETestSuite) TestPluginDeleteCmd() {
printer.Clean()
err := pluginDeleteCmdF(c, &cobra.Command{}, []string{dummyPluginID})
s.Require().Nil(err)
s.Require().ErrorContains(err, "Plugins have been disabled.")
s.Require().Len(printer.GetLines(), 0)
s.Require().Len(printer.GetErrorLines(), 1)
s.Require().Contains(printer.GetErrorLines()[0], fmt.Sprintf("Unable to delete plugin: %s.", dummyPluginID))
@ -367,7 +367,7 @@ func (s *MmctlE2ETestSuite) TestPluginDeleteCmd() {
// Delete Test
err := pluginDeleteCmdF(s.th.Client, &cobra.Command{}, []string{jiraPluginID})
s.Require().Nil(err)
s.Require().ErrorContains(err, "You do not have the appropriate permissions.")
s.Require().Len(printer.GetLines(), 1)
s.Require().Len(printer.GetErrorLines(), 1)
s.Require().Contains(printer.GetErrorLines()[0], fmt.Sprintf("Unable to delete plugin: %s.", jiraPluginID))

View file

@ -94,7 +94,7 @@ func (s *MmctlE2ETestSuite) TestPluginMarketplaceInstallCmd() {
func removePluginIfInstalled(c client.Client, s *MmctlE2ETestSuite, pluginID string) {
appErr := pluginDeleteCmdF(c, &cobra.Command{}, []string{pluginID})
if appErr != nil {
s.Require().Contains(appErr.Error(), "Plugin is not installed.")
s.Require().Contains(appErr.Error(), "Unable to delete plugin.")
}
}

View file

@ -10,7 +10,6 @@ import (
"strings"
"github.com/golang/mock/gomock"
"github.com/hashicorp/go-multierror"
"github.com/mattermost/mattermost/server/public/model"
"github.com/pkg/errors"
@ -86,7 +85,7 @@ func (s *MmctlUnitTestSuite) TestPluginAddCmd() {
Times(1)
err = pluginAddCmdF(s.client, &cobra.Command{}, []string{pluginName})
s.Require().NoError(err)
s.Require().ErrorContains(err, "plugin add error")
s.Require().Len(printer.GetErrorLines(), 1)
s.Require().Equal(printer.GetErrorLines()[0], "Unable to add plugin: "+pluginName+". Error: "+mockError.Error())
})
@ -117,7 +116,7 @@ func (s *MmctlUnitTestSuite) TestPluginAddCmd() {
}
err := pluginAddCmdF(s.client, &cobra.Command{}, args)
s.Require().NoError(err)
s.Require().ErrorContains(err, "plugin add error")
s.Require().Len(printer.GetLines(), 1)
s.Require().Equal(printer.GetLines()[0], "Added plugin: "+args[1])
s.Require().Len(printer.GetErrorLines(), 2)
@ -198,11 +197,8 @@ func (s *MmctlUnitTestSuite) TestPluginInstallUrlCmd() {
Return(nil, &model.Response{}, errors.New("mock error")).
Times(1)
var expected error
expected = multierror.Append(expected, errors.New("mock error"))
err := pluginInstallURLCmdF(s.client, &cobra.Command{}, args)
s.Require().EqualError(err, expected.Error())
s.Require().ErrorContains(err, "mock error")
s.Require().Len(printer.GetErrorLines(), 1)
s.Require().Equal("Unable to install plugin from URL \"https://example.com/plugin2.tar.gz\". Error: mock error", printer.GetErrorLines()[0])
s.Require().Len(printer.GetLines(), 1)
@ -240,7 +236,7 @@ func (s *MmctlUnitTestSuite) TestPluginDisableCmd() {
Times(1)
err := pluginDisableCmdF(s.client, &cobra.Command{}, []string{arg})
s.Require().Nil(err)
s.Require().ErrorContains(err, "mock error")
s.Require().Len(printer.GetLines(), 0)
s.Require().Len(printer.GetErrorLines(), 1)
s.Require().Equal(printer.GetErrorLines()[0], "Unable to disable plugin: "+arg+". Error: "+mockError.Error())
@ -268,7 +264,7 @@ func (s *MmctlUnitTestSuite) TestPluginDisableCmd() {
}
err := pluginDisableCmdF(s.client, &cobra.Command{}, args)
s.Require().Nil(err)
s.Require().ErrorContains(err, "mock error")
s.Require().Len(printer.GetLines(), 2)
s.Require().Equal(printer.GetLines()[0], "Disabled plugin: "+args[1])
s.Require().Equal(printer.GetLines()[1], "Disabled plugin: "+args[2])
@ -329,7 +325,7 @@ func (s *MmctlUnitTestSuite) TestPluginEnableCmd() {
Times(1)
err := pluginEnableCmdF(s.client, &cobra.Command{}, []string{pluginArg})
s.Require().Nil(err)
s.Require().ErrorContains(err, "mock error")
s.Require().Len(printer.GetLines(), 0)
s.Require().Len(printer.GetErrorLines(), 1)
s.Require().Equal(printer.GetErrorLines()[0], "Unable to enable plugin: "+pluginArg+". Error: "+mockErr.Error())
@ -361,7 +357,7 @@ func (s *MmctlUnitTestSuite) TestPluginEnableCmd() {
}
err := pluginEnableCmdF(s.client, &cobra.Command{}, allPlugins)
s.Require().Nil(err)
s.Require().ErrorContains(err, "mock error")
s.Require().Len(printer.GetLines(), 2)
s.Require().Equal(printer.GetLines()[0], "Enabled plugin: "+okPlugins[0])
s.Require().Equal(printer.GetLines()[1], "Enabled plugin: "+okPlugins[1])
@ -533,6 +529,7 @@ func (s *MmctlUnitTestSuite) TestPluginListCmd() {
err := pluginListCmdF(s.client, &cobra.Command{}, nil)
s.Require().NotNil(err)
s.Require().Len(printer.GetLines(), 0)
s.Require().EqualError(err, "Unable to list plugins. Error: "+mockError.Error())
})
}
@ -550,7 +547,7 @@ func (s *MmctlUnitTestSuite) TestPluginDeleteCmd() {
Times(1)
err := pluginDeleteCmdF(s.client, &cobra.Command{}, []string{args})
s.Require().NoError(err)
s.Require().ErrorContains(err, "mock error")
s.Require().Len(printer.GetLines(), 0)
s.Require().Len(printer.GetErrorLines(), 1)
s.Require().Equal("Unable to delete plugin: "+args+". Error: "+mockError.Error(), printer.GetErrorLines()[0])
@ -611,7 +608,7 @@ func (s *MmctlUnitTestSuite) TestPluginDeleteCmd() {
Times(1)
err := pluginDeleteCmdF(s.client, &cobra.Command{}, args)
s.Require().NoError(err)
s.Require().ErrorContains(err, "mock error")
s.Require().Len(printer.GetLines(), 2)
s.Require().Equal("Deleted plugin: "+args[0], printer.GetLines()[0])
s.Require().Equal("Deleted plugin: "+args[3], printer.GetLines()[1])