Browse code

Merge pull request #33541 from aaronlehmann/lock-unlock-tests-poll

integration-cli: Replace sleeps with polling in swarm lock/unlock tests

Sebastiaan van Stijn authored on 2017/07/08 08:41:01
Showing 1 changed files
... ...
@@ -4,7 +4,9 @@ package main
4 4
 
5 5
 import (
6 6
 	"bytes"
7
+	"crypto/x509"
7 8
 	"encoding/json"
9
+	"encoding/pem"
8 10
 	"fmt"
9 11
 	"io/ioutil"
10 12
 	"net/http"
... ...
@@ -992,32 +994,35 @@ func getNodeStatus(c *check.C, d *daemon.Swarm) swarm.LocalNodeState {
992 992
 	return info.LocalNodeState
993 993
 }
994 994
 
995
-func checkSwarmLockedToUnlocked(c *check.C, d *daemon.Swarm, unlockKey string) {
996
-	d.Restart(c)
997
-	status := getNodeStatus(c, d)
998
-	if status == swarm.LocalNodeStateLocked {
999
-		// it must not have updated to be unlocked in time - unlock, wait 3 seconds, and try again
1000
-		cmd := d.Command("swarm", "unlock")
1001
-		cmd.Stdin = bytes.NewBufferString(unlockKey)
1002
-		icmd.RunCmd(cmd).Assert(c, icmd.Success)
995
+func checkKeyIsEncrypted(d *daemon.Swarm) func(*check.C) (interface{}, check.CommentInterface) {
996
+	return func(c *check.C) (interface{}, check.CommentInterface) {
997
+		keyBytes, err := ioutil.ReadFile(filepath.Join(d.Folder, "root", "swarm", "certificates", "swarm-node.key"))
998
+		if err != nil {
999
+			return fmt.Errorf("error reading key: %v", err), nil
1000
+		}
1003 1001
 
1004
-		c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateActive)
1002
+		keyBlock, _ := pem.Decode(keyBytes)
1003
+		if keyBlock == nil {
1004
+			return fmt.Errorf("invalid PEM-encoded private key"), nil
1005
+		}
1005 1006
 
1006
-		time.Sleep(3 * time.Second)
1007
-		d.Restart(c)
1007
+		return x509.IsEncryptedPEMBlock(keyBlock), nil
1008 1008
 	}
1009
+}
1010
+
1011
+func checkSwarmLockedToUnlocked(c *check.C, d *daemon.Swarm, unlockKey string) {
1012
+	// Wait for the PEM file to become unencrypted
1013
+	waitAndAssert(c, defaultReconciliationTimeout, checkKeyIsEncrypted(d), checker.Equals, false)
1009 1014
 
1015
+	d.Restart(c)
1010 1016
 	c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateActive)
1011 1017
 }
1012 1018
 
1013 1019
 func checkSwarmUnlockedToLocked(c *check.C, d *daemon.Swarm) {
1020
+	// Wait for the PEM file to become encrypted
1021
+	waitAndAssert(c, defaultReconciliationTimeout, checkKeyIsEncrypted(d), checker.Equals, true)
1022
+
1014 1023
 	d.Restart(c)
1015
-	status := getNodeStatus(c, d)
1016
-	if status == swarm.LocalNodeStateActive {
1017
-		// it must not have updated to be unlocked in time - wait 3 seconds, and try again
1018
-		time.Sleep(3 * time.Second)
1019
-		d.Restart(c)
1020
-	}
1021 1024
 	c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateLocked)
1022 1025
 }
1023 1026