Browse code

Merge pull request #29198 from allencloud/return-no-swarm-when-unlock-normal-node

return node is not a swarm when unlock a normal node

Aaron Lehmann authored on 2016/12/14 11:59:06
Showing 2 changed files
... ...
@@ -446,11 +446,24 @@ func (c *Cluster) UnlockSwarm(req types.UnlockRequest) error {
446 446
 
447 447
 	c.mu.RLock()
448 448
 	state := c.currentNodeState()
449
-	nr := c.nr
450
-	c.mu.RUnlock()
451
-	if nr == nil || errors.Cause(state.err) != errSwarmLocked {
449
+
450
+	if !state.IsActiveManager() {
451
+		// when manager is not active,
452
+		// unless it is locked, otherwise return error.
453
+		if err := c.errNoManager(state); err != errSwarmLocked {
454
+			c.mu.RUnlock()
455
+			return err
456
+		}
457
+	} else {
458
+		// when manager is active, return an error of "not locked"
459
+		c.mu.RUnlock()
452 460
 		return errors.New("swarm is not locked")
453 461
 	}
462
+
463
+	// only when swarm is locked, code running reaches here
464
+	nr := c.nr
465
+	c.mu.RUnlock()
466
+
454 467
 	key, err := encryption.ParseHumanReadableKey(req.UnlockKey)
455 468
 	if err != nil {
456 469
 		return err
... ...
@@ -835,6 +835,29 @@ func checkSwarmUnlockedToLocked(c *check.C, d *daemon.Swarm) {
835 835
 	c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateLocked)
836 836
 }
837 837
 
838
+func (s *DockerSwarmSuite) TestUnlockEngineAndUnlockedSwarm(c *check.C) {
839
+	d := s.AddDaemon(c, false, false)
840
+
841
+	// unlocking a normal engine should return an error
842
+	cmd := d.Command("swarm", "unlock")
843
+	cmd.Stdin = bytes.NewBufferString("wrong-secret-key")
844
+	outs, err := cmd.CombinedOutput()
845
+
846
+	c.Assert(err, checker.NotNil, check.Commentf("out: %v", string(outs)))
847
+	c.Assert(string(outs), checker.Contains, "This node is not a swarm manager.")
848
+
849
+	_, err = d.Cmd("swarm", "init")
850
+	c.Assert(err, checker.IsNil)
851
+
852
+	// unlocking an unlocked swarm should return an error
853
+	cmd = d.Command("swarm", "unlock")
854
+	cmd.Stdin = bytes.NewBufferString("wrong-secret-key")
855
+	outs, err = cmd.CombinedOutput()
856
+
857
+	c.Assert(err, checker.NotNil, check.Commentf("out: %v", string(outs)))
858
+	c.Assert(string(outs), checker.Contains, "swarm is not locked")
859
+}
860
+
838 861
 func (s *DockerSwarmSuite) TestSwarmInitLocked(c *check.C) {
839 862
 	d := s.AddDaemon(c, false, false)
840 863