From 24cbb9897193894f4716583d1861091ab2fa1ae2 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Mon, 29 Oct 2018 13:46:21 -0700 Subject: [PATCH] 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 --- integration-cli/docker_cli_swarm_test.go | 108 ++++------------------- 1 file changed, 19 insertions(+), 89 deletions(-) diff --git a/integration-cli/docker_cli_swarm_test.go b/integration-cli/docker_cli_swarm_test.go index 1ced7879f9..02d922e100 100644 --- a/integration-cli/docker_cli_swarm_test.go +++ b/integration-cli/docker_cli_swarm_test.go @@ -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++ { @@ -1380,22 +1320,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++ { @@ -1462,21 +1387,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") @@ -2065,3 +1982,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 +}