mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #29198 from allencloud/return-no-swarm-when-unlock-normal-node
return node is not a swarm when unlock a normal node
This commit is contained in:
commit
96f7fedc1e
2 changed files with 39 additions and 3 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue