docker_cli_swarm_test: factor out common code
This is repeated 6 times in different tests, with slight minor variations. Let's factor it out, for clarity. While at it, simplify the code: instead of more complex parsing of "docker swarm init|update --autolock" output (1) and checking if the key is also present in "docker swarm unlock-key" output (2), get the key from (2) and check it is present in (1). Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com> (cherry picked from commit 24cbb9897193894f4716583d1861091ab2fa1ae2) Signed-off-by: Sebastiaan van Stijn <github@gone.nl> Upstream-commit: 6ec991ec8371bdba94480c587eea6ea24f3e9d43 Component: engine
This commit is contained in:
committed by
Sebastiaan van Stijn
parent
d81b3ef73b
commit
3a2688e1e5
@ -1055,22 +1055,7 @@ func (s *DockerSwarmSuite) TestSwarmInitLocked(c *check.C) {
|
||||
|
||||
outs, err := d.Cmd("swarm", "init", "--autolock")
|
||||
c.Assert(err, checker.IsNil, check.Commentf("%s", outs))
|
||||
|
||||
c.Assert(outs, checker.Contains, "docker swarm unlock")
|
||||
|
||||
var unlockKey string
|
||||
for _, line := range strings.Split(outs, "\n") {
|
||||
if strings.Contains(line, "SWMKEY") {
|
||||
unlockKey = strings.TrimSpace(line)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
c.Assert(unlockKey, checker.Not(checker.Equals), "")
|
||||
|
||||
outs, err = d.Cmd("swarm", "unlock-key", "-q")
|
||||
c.Assert(err, checker.IsNil, check.Commentf("%s", outs))
|
||||
c.Assert(outs, checker.Equals, unlockKey+"\n")
|
||||
unlockKey := getUnlockKey(d, c, outs)
|
||||
|
||||
c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateActive)
|
||||
|
||||
@ -1155,22 +1140,7 @@ func (s *DockerSwarmSuite) TestSwarmLockUnlockCluster(c *check.C) {
|
||||
// enable autolock
|
||||
outs, err := d1.Cmd("swarm", "update", "--autolock")
|
||||
c.Assert(err, checker.IsNil, check.Commentf("%s", outs))
|
||||
|
||||
c.Assert(outs, checker.Contains, "docker swarm unlock")
|
||||
|
||||
var unlockKey string
|
||||
for _, line := range strings.Split(outs, "\n") {
|
||||
if strings.Contains(line, "SWMKEY") {
|
||||
unlockKey = strings.TrimSpace(line)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
c.Assert(unlockKey, checker.Not(checker.Equals), "")
|
||||
|
||||
outs, err = d1.Cmd("swarm", "unlock-key", "-q")
|
||||
c.Assert(err, checker.IsNil)
|
||||
c.Assert(outs, checker.Equals, unlockKey+"\n")
|
||||
unlockKey := getUnlockKey(d1, c, outs)
|
||||
|
||||
// The ones that got the cluster update should be set to locked
|
||||
for _, d := range []*daemon.Daemon{d1, d3} {
|
||||
@ -1222,22 +1192,7 @@ func (s *DockerSwarmSuite) TestSwarmJoinPromoteLocked(c *check.C) {
|
||||
// enable autolock
|
||||
outs, err := d1.Cmd("swarm", "update", "--autolock")
|
||||
c.Assert(err, checker.IsNil, check.Commentf("out: %v", outs))
|
||||
|
||||
c.Assert(outs, checker.Contains, "docker swarm unlock")
|
||||
|
||||
var unlockKey string
|
||||
for _, line := range strings.Split(outs, "\n") {
|
||||
if strings.Contains(line, "SWMKEY") {
|
||||
unlockKey = strings.TrimSpace(line)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
c.Assert(unlockKey, checker.Not(checker.Equals), "")
|
||||
|
||||
outs, err = d1.Cmd("swarm", "unlock-key", "-q")
|
||||
c.Assert(err, checker.IsNil)
|
||||
c.Assert(outs, checker.Equals, unlockKey+"\n")
|
||||
unlockKey := getUnlockKey(d1, c, outs)
|
||||
|
||||
// joined workers start off unlocked
|
||||
d2 := s.AddDaemon(c, true, false)
|
||||
@ -1295,22 +1250,7 @@ func (s *DockerSwarmSuite) TestSwarmRotateUnlockKey(c *check.C) {
|
||||
|
||||
outs, err := d.Cmd("swarm", "update", "--autolock")
|
||||
c.Assert(err, checker.IsNil, check.Commentf("out: %v", outs))
|
||||
|
||||
c.Assert(outs, checker.Contains, "docker swarm unlock")
|
||||
|
||||
var unlockKey string
|
||||
for _, line := range strings.Split(outs, "\n") {
|
||||
if strings.Contains(line, "SWMKEY") {
|
||||
unlockKey = strings.TrimSpace(line)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
c.Assert(unlockKey, checker.Not(checker.Equals), "")
|
||||
|
||||
outs, err = d.Cmd("swarm", "unlock-key", "-q")
|
||||
c.Assert(err, checker.IsNil)
|
||||
c.Assert(outs, checker.Equals, unlockKey+"\n")
|
||||
unlockKey := getUnlockKey(d, c, outs)
|
||||
|
||||
// Rotate multiple times
|
||||
for i := 0; i != 3; i++ {
|
||||
@ -1387,22 +1327,7 @@ func (s *DockerSwarmSuite) TestSwarmClusterRotateUnlockKey(c *check.C) {
|
||||
|
||||
outs, err := d1.Cmd("swarm", "update", "--autolock")
|
||||
c.Assert(err, checker.IsNil, check.Commentf("%s", outs))
|
||||
|
||||
c.Assert(outs, checker.Contains, "docker swarm unlock")
|
||||
|
||||
var unlockKey string
|
||||
for _, line := range strings.Split(outs, "\n") {
|
||||
if strings.Contains(line, "SWMKEY") {
|
||||
unlockKey = strings.TrimSpace(line)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
c.Assert(unlockKey, checker.Not(checker.Equals), "")
|
||||
|
||||
outs, err = d1.Cmd("swarm", "unlock-key", "-q")
|
||||
c.Assert(err, checker.IsNil, check.Commentf("%s", outs))
|
||||
c.Assert(outs, checker.Equals, unlockKey+"\n")
|
||||
unlockKey := getUnlockKey(d1, c, outs)
|
||||
|
||||
// Rotate multiple times
|
||||
for i := 0; i != 3; i++ {
|
||||
@ -1469,21 +1394,13 @@ func (s *DockerSwarmSuite) TestSwarmClusterRotateUnlockKey(c *check.C) {
|
||||
func (s *DockerSwarmSuite) TestSwarmAlternateLockUnlock(c *check.C) {
|
||||
d := s.AddDaemon(c, true, true)
|
||||
|
||||
var unlockKey string
|
||||
for i := 0; i < 2; i++ {
|
||||
// set to lock
|
||||
outs, err := d.Cmd("swarm", "update", "--autolock")
|
||||
c.Assert(err, checker.IsNil, check.Commentf("out: %v", outs))
|
||||
c.Assert(outs, checker.Contains, "docker swarm unlock")
|
||||
unlockKey := getUnlockKey(d, c, outs)
|
||||
|
||||
for _, line := range strings.Split(outs, "\n") {
|
||||
if strings.Contains(line, "SWMKEY") {
|
||||
unlockKey = strings.TrimSpace(line)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
c.Assert(unlockKey, checker.Not(checker.Equals), "")
|
||||
checkSwarmUnlockedToLocked(c, d)
|
||||
|
||||
cmd := d.Command("swarm", "unlock")
|
||||
@ -2071,3 +1988,16 @@ func (s *DockerSwarmSuite) TestSwarmClusterEventsConfig(c *check.C) {
|
||||
// filtered by config
|
||||
waitForEvent(c, d, t1, "-f type=config", "config remove "+id, defaultRetryCount)
|
||||
}
|
||||
|
||||
func getUnlockKey(d *daemon.Daemon, c *check.C, autolockOutput string) string {
|
||||
unlockKey, err := d.Cmd("swarm", "unlock-key", "-q")
|
||||
c.Assert(err, checker.IsNil, check.Commentf("%s", unlockKey))
|
||||
unlockKey = strings.TrimSuffix(unlockKey, "\n")
|
||||
|
||||
// Check that "docker swarm init --autolock" or "docker swarm update --autolock"
|
||||
// contains all the expected strings, including the unlock key
|
||||
c.Assert(autolockOutput, checker.Contains, "docker swarm unlock")
|
||||
c.Assert(autolockOutput, checker.Contains, unlockKey)
|
||||
|
||||
return unlockKey
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user