return not a swarm when unlock

Signed-off-by: allencloud <allen.sun@daocloud.io>
This commit is contained in:
allencloud 2016-12-07 13:48:01 +08:00
parent e9076c0f00
commit 0270645c13
2 changed files with 39 additions and 3 deletions

View File

@ -446,11 +446,24 @@ func (c *Cluster) UnlockSwarm(req types.UnlockRequest) error {
c.mu.RLock()
state := c.currentNodeState()
nr := c.nr
c.mu.RUnlock()
if nr == nil || errors.Cause(state.err) != errSwarmLocked {
if !state.IsActiveManager() {
// when manager is not active,
// unless it is locked, otherwise return error.
if err := c.errNoManager(state); err != errSwarmLocked {
c.mu.RUnlock()
return err
}
} else {
// when manager is active, return an error of "not locked"
c.mu.RUnlock()
return errors.New("swarm is not locked")
}
// only when swarm is locked, code running reaches here
nr := c.nr
c.mu.RUnlock()
key, err := encryption.ParseHumanReadableKey(req.UnlockKey)
if err != nil {
return err

View File

@ -835,6 +835,29 @@ func checkSwarmUnlockedToLocked(c *check.C, d *daemon.Swarm) {
c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateLocked)
}
func (s *DockerSwarmSuite) TestUnlockEngineAndUnlockedSwarm(c *check.C) {
d := s.AddDaemon(c, false, false)
// unlocking a normal engine should return an error
cmd := d.Command("swarm", "unlock")
cmd.Stdin = bytes.NewBufferString("wrong-secret-key")
outs, err := cmd.CombinedOutput()
c.Assert(err, checker.NotNil, check.Commentf("out: %v", string(outs)))
c.Assert(string(outs), checker.Contains, "This node is not a swarm manager.")
_, err = d.Cmd("swarm", "init")
c.Assert(err, checker.IsNil)
// unlocking an unlocked swarm should return an error
cmd = d.Command("swarm", "unlock")
cmd.Stdin = bytes.NewBufferString("wrong-secret-key")
outs, err = cmd.CombinedOutput()
c.Assert(err, checker.NotNil, check.Commentf("out: %v", string(outs)))
c.Assert(string(outs), checker.Contains, "swarm is not locked")
}
func (s *DockerSwarmSuite) TestSwarmInitLocked(c *check.C) {
d := s.AddDaemon(c, false, false)