From a2ea2347ba3b31a0cb4ebc97f6817c5e6d082e32 Mon Sep 17 00:00:00 2001
From: Guillaume Tardif <guillaume.tardif@docker.com>
Date: Tue, 7 Jul 2020 16:25:30 +0200
Subject: [PATCH] Remove It() in e2e tests, simplified step declaration. Nicer
 test output for all tests

---
 tests/aci-e2e/e2e-aci_test.go             | 36 +++++++++++------------
 tests/e2e/e2e_test.go                     | 26 ++++++++--------
 tests/framework/helper.go                 |  7 -----
 tests/framework/suite.go                  |  8 +++++
 tests/skip-win-ci-e2e/skip_win_ci_test.go |  2 +-
 5 files changed, 40 insertions(+), 39 deletions(-)

diff --git a/tests/aci-e2e/e2e-aci_test.go b/tests/aci-e2e/e2e-aci_test.go
index dfd6d3835..990daaf0a 100644
--- a/tests/aci-e2e/e2e-aci_test.go
+++ b/tests/aci-e2e/e2e-aci_test.go
@@ -61,7 +61,7 @@ type E2eACISuite struct {
 }
 
 func (s *E2eACISuite) TestContextDefault() {
-	s.T().Run("should be initialized with default context", func(t *testing.T) {
+	s.Step("should be initialized with default context", func() {
 		_, err := s.NewCommand("docker", "context", "rm", "-f", contextName).Exec()
 		if err == nil {
 			log.Println("Cleaning existing test context")
@@ -75,7 +75,7 @@ func (s *E2eACISuite) TestContextDefault() {
 }
 
 func (s *E2eACISuite) TestACIBackend() {
-	s.T().Run("Logs in azure using service principal credentials", func(t *testing.T) {
+	s.Step("Logs in azure using service principal credentials", func() {
 		login, err := login.NewAzureLoginService()
 		Expect(err).To(BeNil())
 		// in order to create new service principal and get these 3 values : `az ad sp create-for-rbac --name 'TestServicePrincipal' --sdk-auth`
@@ -86,7 +86,7 @@ func (s *E2eACISuite) TestACIBackend() {
 		Expect(err).To(BeNil())
 	})
 
-	s.T().Run("creates a new aci context for tests", func(t *testing.T) {
+	s.Step("creates a new aci context for tests", func() {
 		setupTestResourceGroup(resourceGroupName)
 		helper := azure.NewACIResourceGroupHelper()
 		models, err := helper.GetSubscriptionIDs(context.TODO())
@@ -98,20 +98,20 @@ func (s *E2eACISuite) TestACIBackend() {
 
 	defer deleteResourceGroup(resourceGroupName)
 
-	s.T().Run("uses the aci context", func(t *testing.T) {
+	s.Step("uses the aci context", func() {
 		currentContext := s.NewCommand("docker", "context", "use", contextName).ExecOrDie()
 		Expect(currentContext).To(ContainSubstring(contextName))
 		output := s.NewCommand("docker", "context", "ls").ExecOrDie()
 		Expect(output).To(ContainSubstring("acitest *"))
 	})
 
-	It("ensures no container is running initially", func() {
+	s.Step("ensures no container is running initially", func() {
 		output := s.NewDockerCommand("ps").ExecOrDie()
 		Expect(len(Lines(output))).To(Equal(1))
 	})
 
 	var nginxExposedURL string
-	s.T().Run("runs nginx on port 80", func(t *testing.T) {
+	s.Step("runs nginx on port 80", func() {
 		aciContext := store.AciContext{
 			SubscriptionID: subscriptionID,
 			Location:       location,
@@ -150,13 +150,13 @@ func (s *E2eACISuite) TestACIBackend() {
 		Expect(output).To(ContainSubstring("GET"))
 	})
 
-	s.T().Run("exec command", func(t *testing.T) {
+	s.Step("exec command", func() {
 		_, err := s.NewDockerCommand("exec", testContainerName, "echo", "fail_with_argument").Exec()
 		Expect(err.Error()).To(ContainSubstring("ACI exec command does not accept arguments to the command. " +
 			"Only the binary should be specified"))
 	})
 
-	s.T().Run("follow logs from nginx", func(t *testing.T) {
+	s.Step("follow logs from nginx", func() {
 		timeChan := make(chan time.Time)
 
 		ctx := s.NewDockerCommand("logs", "--follow", testContainerName).WithTimeout(timeChan)
@@ -178,12 +178,12 @@ func (s *E2eACISuite) TestACIBackend() {
 		Expect(output).To(ContainSubstring("/test"))
 	})
 
-	s.T().Run("removes container nginx", func(t *testing.T) {
+	s.Step("removes container nginx", func() {
 		output := s.NewDockerCommand("rm", testContainerName).ExecOrDie()
 		Expect(Lines(output)[0]).To(Equal(testContainerName))
 	})
 
-	s.T().Run("re-run nginx with modified cpu/mem, and without --detach and follow logs", func(t *testing.T) {
+	s.Step("re-run nginx with modified cpu/mem, and without --detach and follow logs", func() {
 		shutdown := make(chan time.Time)
 		errs := make(chan error)
 		outChan := make(chan string)
@@ -224,7 +224,7 @@ func (s *E2eACISuite) TestACIBackend() {
 		Expect(output).To(ContainSubstring("/test"))
 	})
 
-	s.T().Run("removes container nginx", func(t *testing.T) {
+	s.Step("removes container nginx", func() {
 		output := s.NewDockerCommand("rm", testContainerName).ExecOrDie()
 		Expect(Lines(output)[0]).To(Equal(testContainerName))
 	})
@@ -236,7 +236,7 @@ func (s *E2eACISuite) TestACIBackend() {
 	const serverContainer = composeProjectName + "_web"
 	const wordsContainer = composeProjectName + "_words"
 
-	s.T().Run("deploys a compose app", func(t *testing.T) {
+	s.Step("deploys a compose app", func() {
 		// specifically do not specify project name here, it will be derived from current folder "acie2e"
 		s.NewDockerCommand("compose", "up", "-f", composeFile).ExecOrDie()
 		output := s.NewDockerCommand("ps").ExecOrDie()
@@ -263,12 +263,12 @@ func (s *E2eACISuite) TestACIBackend() {
 		Expect(webChecked).To(BeTrue())
 	})
 
-	s.T().Run("get logs from web service", func(t *testing.T) {
+	s.Step("get logs from web service", func() {
 		output := s.NewDockerCommand("logs", serverContainer).ExecOrDie()
 		Expect(output).To(ContainSubstring("Listening on port 80"))
 	})
 
-	s.T().Run("updates a compose app", func(t *testing.T) {
+	s.Step("updates a compose app", func() {
 		s.NewDockerCommand("compose", "up", "-f", composeFileMultiplePorts, "--project-name", composeProjectName).ExecOrDie()
 		// Expect(output).To(ContainSubstring("Successfully deployed"))
 		output := s.NewDockerCommand("ps").ExecOrDie()
@@ -304,11 +304,11 @@ func (s *E2eACISuite) TestACIBackend() {
 		Expect(wordsChecked).To(BeTrue())
 	})
 
-	s.T().Run("shutdown compose app", func(t *testing.T) {
+	s.Step("shutdown compose app", func() {
 		s.NewDockerCommand("compose", "down", "--project-name", composeProjectName).ExecOrDie()
 	})
 
-	s.T().Run("runs mysql with env variables", func(t *testing.T) {
+	s.Step("runs mysql with env variables", func() {
 		err := os.Setenv("MYSQL_USER", "user1")
 		Expect(err).To(BeNil())
 		s.NewDockerCommand("run", "-d", "mysql:5.7", "-e", "MYSQL_ROOT_PASSWORD=rootpwd", "-e", "MYSQL_DATABASE=mytestdb", "-e", "MYSQL_USER", "-e", "MYSQL_PASSWORD=userpwd").ExecOrDie()
@@ -330,12 +330,12 @@ func (s *E2eACISuite) TestACIBackend() {
 		Expect(err).To(BeNil())
 	})
 
-	s.T().Run("switches back to default context", func(t *testing.T) {
+	s.Step("switches back to default context", func() {
 		output := s.NewCommand("docker", "context", "use", "default").ExecOrDie()
 		Expect(output).To(ContainSubstring("default"))
 	})
 
-	s.T().Run("deletes test context", func(t *testing.T) {
+	s.Step("deletes test context", func() {
 		output := s.NewCommand("docker", "context", "rm", contextName).ExecOrDie()
 		Expect(output).To(ContainSubstring(contextName))
 	})
diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go
index b7a044d23..12b62d368 100644
--- a/tests/e2e/e2e_test.go
+++ b/tests/e2e/e2e_test.go
@@ -99,7 +99,7 @@ func (s *E2eSuite) TestContextLsFormat() {
 }
 
 func (s *E2eSuite) TestContextCreateParseErrorDoesNotDelegateToLegacy() {
-	It("should dispay new cli error when parsing context create flags", func() {
+	s.Step("should dispay new cli error when parsing context create flags", func() {
 		_, err := s.NewDockerCommand("context", "create", "aci", "--subscription-id", "titi").Exec()
 		Expect(err.Error()).NotTo(ContainSubstring("unknown flag"))
 		Expect(err.Error()).To(ContainSubstring("accepts 1 arg(s), received 0"))
@@ -154,7 +154,7 @@ func (s *E2eSuite) TestCloudLogin() {
 }
 
 func (s *E2eSuite) TestSetupError() {
-	It("should display an error if cannot shell out to com.docker.cli", func() {
+	s.Step("should display an error if cannot shell out to com.docker.cli", func() {
 		err := os.Setenv("PATH", s.BinDir)
 		Expect(err).To(BeNil())
 		err = os.Remove(filepath.Join(s.BinDir, DockerClassicExecutable()))
@@ -167,23 +167,23 @@ func (s *E2eSuite) TestSetupError() {
 }
 
 func (s *E2eSuite) TestLegacy() {
-	It("should list all legacy commands", func() {
+	s.Step("should list all legacy commands", func() {
 		output := s.NewDockerCommand("--help").ExecOrDie()
 		Expect(output).To(ContainSubstring("swarm"))
 	})
 
-	It("should execute legacy commands", func() {
+	s.Step("should execute legacy commands", func() {
 		output, _ := s.NewDockerCommand("swarm", "join").Exec()
 		Expect(output).To(ContainSubstring("\"docker swarm join\" requires exactly 1 argument."))
 	})
 
-	It("should run local container in less than 10 secs", func() {
+	s.Step("should run local container in less than 10 secs", func() {
 		s.NewDockerCommand("pull", "hello-world").ExecOrDie()
 		output := s.NewDockerCommand("run", "--rm", "hello-world").WithTimeout(time.NewTimer(20 * time.Second).C).ExecOrDie()
 		Expect(output).To(ContainSubstring("Hello from Docker!"))
 	})
 
-	It("should execute legacy commands in other moby contexts", func() {
+	s.Step("should execute legacy commands in other moby contexts", func() {
 		s.NewDockerCommand("context", "create", "mobyCtx", "--from=default").ExecOrDie()
 		s.NewDockerCommand("context", "use", "mobyCtx").ExecOrDie()
 		output, _ := s.NewDockerCommand("swarm", "join").Exec()
@@ -256,12 +256,12 @@ func (s *E2eSuite) TestAllowsFormatFlagInVersion() {
 }
 
 func (s *E2eSuite) TestMockBackend() {
-	It("creates a new test context to hardcoded example backend", func() {
+	s.Step("creates a new test context to hardcoded example backend", func() {
 		s.NewDockerCommand("context", "create", "example", "test-example").ExecOrDie()
 		// Expect(output).To(ContainSubstring("test-example context acitest created"))
 	})
 
-	It("uses the test context", func() {
+	s.Step("uses the test context", func() {
 		currentContext := s.NewDockerCommand("context", "use", "test-example").ExecOrDie()
 		Expect(currentContext).To(ContainSubstring("test-example"))
 		output := s.NewDockerCommand("context", "ls").ExecOrDie()
@@ -270,14 +270,14 @@ func (s *E2eSuite) TestMockBackend() {
 		Expect(output).To(ContainSubstring("test-example"))
 	})
 
-	It("can run ps command", func() {
+	s.Step("can run ps command", func() {
 		output := s.NewDockerCommand("ps").ExecOrDie()
 		lines := Lines(output)
 		Expect(len(lines)).To(Equal(3))
 		Expect(lines[2]).To(ContainSubstring("1234                alpine"))
 	})
 
-	It("can run quiet ps command", func() {
+	s.Step("can run quiet ps command", func() {
 		output := s.NewDockerCommand("ps", "-q").ExecOrDie()
 		lines := Lines(output)
 		Expect(len(lines)).To(Equal(2))
@@ -285,7 +285,7 @@ func (s *E2eSuite) TestMockBackend() {
 		Expect(lines[1]).To(Equal("1234"))
 	})
 
-	It("can run ps command with all ", func() {
+	s.Step("can run ps command with all ", func() {
 		output := s.NewDockerCommand("ps", "-q", "--all").ExecOrDie()
 		lines := Lines(output)
 		Expect(len(lines)).To(Equal(3))
@@ -294,11 +294,11 @@ func (s *E2eSuite) TestMockBackend() {
 		Expect(lines[2]).To(Equal("stopped"))
 	})
 
-	It("can run inspect command on container", func() {
+	s.Step("can run inspect command on container", func() {
 		golden.Assert(s.T(), s.NewDockerCommand("inspect", "id").ExecOrDie(), "inspect-id.golden")
 	})
 
-	It("can run 'run' command", func() {
+	s.Step("can run 'run' command", func() {
 		output := s.NewDockerCommand("run", "-d", "nginx", "-p", "80:80").ExecOrDie()
 		Expect(output).To(ContainSubstring("Running container \"nginx\" with name"))
 	})
diff --git a/tests/framework/helper.go b/tests/framework/helper.go
index 1eaddc6ec..d930b49aa 100644
--- a/tests/framework/helper.go
+++ b/tests/framework/helper.go
@@ -21,7 +21,6 @@ import (
 	"strings"
 
 	"github.com/robpike/filter"
-	"github.com/sirupsen/logrus"
 )
 
 func nonEmptyString(s string) bool {
@@ -50,9 +49,3 @@ func GoldenFile(name string) string {
 func IsWindows() bool {
 	return runtime.GOOS == "windows"
 }
-
-// It runs func
-func It(description string, test func()) {
-	test()
-	logrus.Print("Passed: ", description)
-}
diff --git a/tests/framework/suite.go b/tests/framework/suite.go
index a3908705a..9648e8267 100644
--- a/tests/framework/suite.go
+++ b/tests/framework/suite.go
@@ -22,6 +22,7 @@ import (
 	"os"
 	"os/exec"
 	"path/filepath"
+	"testing"
 	"time"
 
 	"github.com/onsi/gomega"
@@ -133,6 +134,13 @@ func (s *Suite) NewCommand(command string, args ...string) *CmdContext {
 	}
 }
 
+// Step runs a step in a test, with an identified name and output in test results
+func (s *Suite) Step(name string, test func()) {
+	s.T().Run(name, func(t *testing.T) {
+		test()
+	})
+}
+
 func dockerExecutable() string {
 	if IsWindows() {
 		return "docker.exe"
diff --git a/tests/skip-win-ci-e2e/skip_win_ci_test.go b/tests/skip-win-ci-e2e/skip_win_ci_test.go
index 38195a349..b8de44377 100644
--- a/tests/skip-win-ci-e2e/skip_win_ci_test.go
+++ b/tests/skip-win-ci-e2e/skip_win_ci_test.go
@@ -35,7 +35,7 @@ type NonWinCIE2eSuite struct {
 }
 
 func (s *NonWinCIE2eSuite) TestKillChildOnCancel() {
-	It("should kill com.docker.cli if parent command is cancelled", func() {
+	s.Step("should kill com.docker.cli if parent command is cancelled", func() {
 		imageName := "test-sleep-image"
 		out := s.ListProcessesCommand().ExecOrDie()
 		Expect(out).NotTo(ContainSubstring(imageName))