Check for plugin state before enable and disable.

This prevents unnecessary API call to containerd.

Signed-off-by: Anusha Ragunathan <anusha@docker.com>
This commit is contained in:
Anusha Ragunathan 2016-07-25 14:06:27 -07:00
parent 8f11896a45
commit b867f6c6e1
2 changed files with 34 additions and 0 deletions

View File

@ -68,3 +68,24 @@ func (s *DockerSuite) TestPluginInstallImage(c *check.C) {
c.Assert(err, checker.NotNil)
c.Assert(out, checker.Contains, "content is not a plugin")
}
func (s *DockerSuite) TestPluginEnableDisableNegative(c *check.C) {
testRequires(c, DaemonIsLinux, ExperimentalDaemon)
out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pName)
c.Assert(err, checker.IsNil)
c.Assert(strings.TrimSpace(out), checker.Contains, pName)
out, _, err = dockerCmdWithError("plugin", "enable", pName)
c.Assert(err, checker.NotNil)
c.Assert(strings.TrimSpace(out), checker.Contains, "already enabled")
_, _, err = dockerCmdWithError("plugin", "disable", pName)
c.Assert(err, checker.IsNil)
out, _, err = dockerCmdWithError("plugin", "disable", pName)
c.Assert(err, checker.NotNil)
c.Assert(strings.TrimSpace(out), checker.Contains, "already disabled")
_, _, err = dockerCmdWithError("plugin", "remove", pName)
c.Assert(err, checker.IsNil)
}

View File

@ -3,6 +3,7 @@
package plugin
import (
"fmt"
"os"
"path/filepath"
"syscall"
@ -20,6 +21,9 @@ import (
)
func (pm *Manager) enable(p *plugin) error {
if p.P.Active {
return fmt.Errorf("plugin %s is already enabled", p.Name())
}
spec, err := pm.initSpec(p)
if err != nil {
return err
@ -27,12 +31,18 @@ func (pm *Manager) enable(p *plugin) error {
p.restartManager = restartmanager.New(container.RestartPolicy{Name: "always"}, 0)
if err := pm.containerdClient.Create(p.P.ID, libcontainerd.Spec(*spec), libcontainerd.WithRestartManager(p.restartManager)); err != nil { // POC-only
if err := p.restartManager.Cancel(); err != nil {
logrus.Errorf("enable: restartManager.Cancel failed due to %v", err)
}
return err
}
socket := p.P.Manifest.Interface.Socket
p.client, err = plugins.NewClient("unix://"+filepath.Join(p.runtimeSourcePath, socket), nil)
if err != nil {
if err := p.restartManager.Cancel(); err != nil {
logrus.Errorf("enable: restartManager.Cancel failed due to %v", err)
}
return err
}
@ -114,6 +124,9 @@ func (pm *Manager) initSpec(p *plugin) (*specs.Spec, error) {
}
func (pm *Manager) disable(p *plugin) error {
if !p.P.Active {
return fmt.Errorf("plugin %s is already disabled", p.Name())
}
if err := p.restartManager.Cancel(); err != nil {
logrus.Error(err)
}