瀏覽代碼

cli: Move to gotest.tools

Signed-off-by: Chris Crone <[email protected]>
Chris Crone 5 年之前
父節點
當前提交
f8ce7a4780
共有 7 個文件被更改,包括 83 次插入129 次删除
  1. 6 14
      cli/cmd/inspect_test.go
  2. 11 18
      cli/cmd/ps_test.go
  3. 1 2
      cli/formatter/container_test.go
  4. 15 16
      cli/main_test.go
  5. 4 15
      cli/mobycli/exec_test.go
  6. 16 21
      cli/options/run/opts_test.go
  7. 30 43
      tests/framework/cli.go

+ 6 - 14
cli/cmd/inspect_test.go

@@ -19,24 +19,16 @@ package cmd
 import (
 	"testing"
 
-	"github.com/stretchr/testify/require"
-	"github.com/stretchr/testify/suite"
+	"gotest.tools/v3/assert"
 	"gotest.tools/v3/golden"
 
 	_ "github.com/docker/api/example"
 	"github.com/docker/api/tests/framework"
 )
 
-type InspectSuite struct {
-	framework.CliSuite
-}
-
-func (sut *InspectSuite) TestInspectId() {
-	err := runInspect(sut.Context(), "id")
-	require.Nil(sut.T(), err)
-	golden.Assert(sut.T(), sut.GetStdOut(), "inspect-out-id.golden")
-}
-
-func TestInspect(t *testing.T) {
-	suite.Run(t, new(InspectSuite))
+func TestInspectId(t *testing.T) {
+	c := framework.NewTestCLI(t)
+	err := runInspect(c.Context(), "id")
+	assert.NilError(t, err)
+	golden.Assert(t, c.GetStdOut(), "inspect-out-id.golden")
 }

+ 11 - 18
cli/cmd/ps_test.go

@@ -19,40 +19,33 @@ package cmd
 import (
 	"testing"
 
-	"github.com/stretchr/testify/require"
-	"github.com/stretchr/testify/suite"
+	"gotest.tools/v3/assert"
 	"gotest.tools/v3/golden"
 
 	_ "github.com/docker/api/example"
 	"github.com/docker/api/tests/framework"
 )
 
-type PsSuite struct {
-	framework.CliSuite
-}
-
-func (sut *PsSuite) TestPs() {
+func TestPs(t *testing.T) {
+	c := framework.NewTestCLI(t)
 	opts := psOpts{
 		quiet: false,
 	}
 
-	err := runPs(sut.Context(), opts)
-	require.Nil(sut.T(), err)
+	err := runPs(c.Context(), opts)
+	assert.NilError(t, err)
 
-	golden.Assert(sut.T(), sut.GetStdOut(), "ps-out.golden")
+	golden.Assert(t, c.GetStdOut(), "ps-out.golden")
 }
 
-func (sut *PsSuite) TestPsQuiet() {
+func TestPsQuiet(t *testing.T) {
+	c := framework.NewTestCLI(t)
 	opts := psOpts{
 		quiet: true,
 	}
 
-	err := runPs(sut.Context(), opts)
-	require.Nil(sut.T(), err)
+	err := runPs(c.Context(), opts)
+	assert.NilError(t, err)
 
-	golden.Assert(sut.T(), sut.GetStdOut(), "ps-out-quiet.golden")
-}
-
-func TestPs(t *testing.T) {
-	suite.Run(t, new(PsSuite))
+	golden.Assert(t, c.GetStdOut(), "ps-out-quiet.golden")
 }

+ 1 - 2
cli/formatter/container_test.go

@@ -19,7 +19,6 @@ package formatter
 import (
 	"testing"
 
-	"github.com/stretchr/testify/require"
 	"gotest.tools/v3/assert"
 
 	"github.com/docker/api/cli/options/run"
@@ -69,7 +68,7 @@ func TestDisplayPorts(t *testing.T) {
 				Publish: testCase.in,
 			}
 			containerConfig, err := runOpts.ToContainerConfig("test")
-			require.Nil(t, err)
+			assert.NilError(t, err)
 
 			out := PortsString(containerConfig.Ports)
 			assert.Equal(t, testCase.expected, out)

+ 15 - 16
cli/main_test.go

@@ -22,13 +22,12 @@ import (
 	"path/filepath"
 	"testing"
 
+	"gotest.tools/v3/assert"
+
 	"github.com/docker/api/cli/cmd"
 	"github.com/docker/api/cli/cmd/context"
 	"github.com/docker/api/cli/cmd/login"
 	"github.com/docker/api/cli/cmd/run"
-
-	"github.com/stretchr/testify/require"
-
 	"github.com/docker/api/config"
 )
 
@@ -40,33 +39,33 @@ func TestDetermineCurrentContext(t *testing.T) {
 	d, err := ioutil.TempDir("", "")
 	// nolint errcheck
 	defer os.RemoveAll(d)
-	require.NoError(t, err)
+	assert.NilError(t, err)
 	err = ioutil.WriteFile(filepath.Join(d, config.ConfigFileName), contextSetConfig, 0644)
-	require.NoError(t, err)
+	assert.NilError(t, err)
 
 	// If nothing set, fallback to default
 	c := determineCurrentContext("", "")
-	require.Equal(t, "default", c)
+	assert.Equal(t, c, "default")
 
 	// If context flag set, use that
 	c = determineCurrentContext("other-context", "")
-	require.Equal(t, "other-context", c)
+	assert.Equal(t, c, "other-context")
 
 	// If no context flag, use config
 	c = determineCurrentContext("", d)
-	require.Equal(t, "some-context", c)
+	assert.Equal(t, c, "some-context")
 
 	// Ensure context flag overrides config
 	c = determineCurrentContext("other-context", d)
-	require.Equal(t, "other-context", c)
+	assert.Equal(t, "other-context", c)
 }
 
 func TestCheckOwnCommand(t *testing.T) {
-	require.True(t, isOwnCommand(login.Command()))
-	require.True(t, isOwnCommand(context.Command()))
-	require.True(t, isOwnCommand(cmd.ServeCommand()))
-	require.False(t, isOwnCommand(run.Command()))
-	require.False(t, isOwnCommand(cmd.ExecCommand()))
-	require.False(t, isOwnCommand(cmd.LogsCommand()))
-	require.False(t, isOwnCommand(cmd.PsCommand()))
+	assert.Assert(t, isOwnCommand(login.Command()))
+	assert.Assert(t, isOwnCommand(context.Command()))
+	assert.Assert(t, isOwnCommand(cmd.ServeCommand()))
+	assert.Assert(t, !isOwnCommand(run.Command()))
+	assert.Assert(t, !isOwnCommand(cmd.ExecCommand()))
+	assert.Assert(t, !isOwnCommand(cmd.LogsCommand()))
+	assert.Assert(t, !isOwnCommand(cmd.PsCommand()))
 }

+ 4 - 15
cli/mobycli/exec_test.go

@@ -3,18 +3,12 @@ package mobycli
 import (
 	"testing"
 
-	. "github.com/onsi/gomega"
-	"github.com/stretchr/testify/suite"
+	"gotest.tools/v3/assert"
 
 	"github.com/docker/api/context/store"
-	"github.com/docker/api/tests/framework"
 )
 
-type MobyExecSuite struct {
-	framework.CliSuite
-}
-
-func (sut *MobyExecSuite) TestDelegateContextTypeToMoby() {
+func TestDelegateContextTypeToMoby(t *testing.T) {
 
 	isDelegated := func(val string) bool {
 		for _, ctx := range delegatedContextTypes {
@@ -28,14 +22,9 @@ func (sut *MobyExecSuite) TestDelegateContextTypeToMoby() {
 	allCtx := []string{store.AciContextType, store.EcsContextType, store.AwsContextType, store.DefaultContextType}
 	for _, ctx := range allCtx {
 		if isDelegated(ctx) {
-			Expect(mustDelegateToMoby(ctx)).To(BeTrue())
+			assert.Assert(t, mustDelegateToMoby(ctx))
 			continue
 		}
-		Expect(mustDelegateToMoby(ctx)).To(BeFalse())
+		assert.Assert(t, !mustDelegateToMoby(ctx))
 	}
 }
-
-func TestExec(t *testing.T) {
-	RegisterTestingT(t)
-	suite.Run(t, new(MobyExecSuite))
-}

+ 16 - 21
cli/options/run/opts_test.go

@@ -21,17 +21,12 @@ import (
 	"regexp"
 	"testing"
 
-	"github.com/stretchr/testify/assert"
-	"github.com/stretchr/testify/require"
-	"github.com/stretchr/testify/suite"
+	"gotest.tools/v3/assert"
+	"gotest.tools/v3/assert/cmp"
 
 	"github.com/docker/api/containers"
 )
 
-type RunOptsSuite struct {
-	suite.Suite
-}
-
 var (
 	// AzureNameRegex is used to validate container names
 	// Regex was taken from server side error:
@@ -40,14 +35,14 @@ var (
 )
 
 // TestAzureRandomName ensures compliance with Azure naming requirements
-func (s *RunOptsSuite) TestAzureRandomName() {
+func TestAzureRandomName(t *testing.T) {
 	n := getRandomName()
-	require.Less(s.T(), len(n), 64)
-	require.Greater(s.T(), len(n), 1)
-	require.Regexp(s.T(), AzureNameRegex, n)
+	assert.Assert(t, len(n) < 64)
+	assert.Assert(t, len(n) > 1)
+	assert.Assert(t, cmp.Regexp(AzureNameRegex, n))
 }
 
-func (s *RunOptsSuite) TestPortParse() {
+func TestPortParse(t *testing.T) {
 	testCases := []struct {
 		in       string
 		expected []containers.Port
@@ -125,12 +120,12 @@ func (s *RunOptsSuite) TestPortParse() {
 			Publish: []string{testCase.in},
 		}
 		result, err := opts.toPorts()
-		require.Nil(s.T(), err)
-		assert.ElementsMatch(s.T(), testCase.expected, result)
+		assert.NilError(t, err)
+		assert.DeepEqual(t, result, testCase.expected)
 	}
 }
 
-func (s *RunOptsSuite) TestLabels() {
+func TestLabels(t *testing.T) {
 	testCases := []struct {
 		in            []string
 		expected      map[string]string
@@ -167,11 +162,11 @@ func (s *RunOptsSuite) TestLabels() {
 
 	for _, testCase := range testCases {
 		result, err := toLabels(testCase.in)
-		assert.Equal(s.T(), testCase.expectedError, err)
-		assert.Equal(s.T(), testCase.expected, result)
+		if testCase.expectedError == nil {
+			assert.NilError(t, err)
+		} else {
+			assert.Error(t, err, testCase.expectedError.Error())
+		}
+		assert.DeepEqual(t, result, testCase.expected)
 	}
 }
-
-func TestExampleTestSuite(t *testing.T) {
-	suite.Run(t, new(RunOptsSuite))
-}

+ 30 - 43
tests/framework/clisuite.go → tests/framework/cli.go

@@ -20,72 +20,59 @@ import (
 	"context"
 	"io/ioutil"
 	"os"
+	"testing"
 
-	"github.com/stretchr/testify/require"
-	"github.com/stretchr/testify/suite"
+	"gotest.tools/v3/assert"
+	"gotest.tools/v3/assert/cmp"
 
 	apicontext "github.com/docker/api/context"
 	"github.com/docker/api/context/store"
 )
 
-// CliSuite is a helper struct that creates a configured context
-// and captures the output of a command. it should be used in the
-// same way as testify.suite.Suite
-type CliSuite struct {
-	suite.Suite
-	ctx            context.Context
-	writer         *os.File
-	reader         *os.File
-	OriginalStdout *os.File
-	storeRoot      string
+// TestCLI is a helper struct for CLI tests.
+type TestCLI struct {
+	ctx    context.Context
+	writer *os.File
+	reader *os.File
 }
 
-// BeforeTest is called by testify.suite
-func (sut *CliSuite) BeforeTest(suiteName, testName string) {
-	ctx := context.Background()
-	ctx = apicontext.WithCurrentContext(ctx, "example")
+// NewTestCLI returns a CLI testing helper.
+func NewTestCLI(t *testing.T) *TestCLI {
 	dir, err := ioutil.TempDir("", "store")
-	require.Nil(sut.T(), err)
+	assert.Check(t, cmp.Nil(err))
+
+	originalStdout := os.Stdout
+
+	t.Cleanup(func() {
+		os.Stdout = originalStdout
+		_ = os.RemoveAll(dir)
+	})
+
 	s, err := store.New(
 		store.WithRoot(dir),
 	)
-	require.Nil(sut.T(), err)
-
+	assert.Check(t, cmp.Nil(err))
 	err = s.Create("example", "example", "", store.ContextMetadata{})
-	require.Nil(sut.T(), err)
-
-	sut.storeRoot = dir
+	assert.Check(t, cmp.Nil(err))
 
+	ctx := context.Background()
 	ctx = store.WithContextStore(ctx, s)
-	sut.ctx = ctx
+	ctx = apicontext.WithCurrentContext(ctx, "example")
 
-	sut.OriginalStdout = os.Stdout
 	r, w, err := os.Pipe()
-	require.Nil(sut.T(), err)
-
 	os.Stdout = w
-	sut.writer = w
-	sut.reader = r
+	assert.Check(t, cmp.Nil(err))
+	return &TestCLI{ctx, w, r}
 }
 
 // Context returns a configured context
-func (sut *CliSuite) Context() context.Context {
-	return sut.ctx
+func (c *TestCLI) Context() context.Context {
+	return c.ctx
 }
 
 // GetStdOut returns the output of the command
-func (sut *CliSuite) GetStdOut() string {
-	err := sut.writer.Close()
-	require.Nil(sut.T(), err)
-
-	out, _ := ioutil.ReadAll(sut.reader)
-
+func (c *TestCLI) GetStdOut() string {
+	_ = c.writer.Close()
+	out, _ := ioutil.ReadAll(c.reader)
 	return string(out)
 }
-
-// AfterTest is called by testify.suite
-func (sut *CliSuite) AfterTest(suiteName, testName string) {
-	os.Stdout = sut.OriginalStdout
-	err := os.RemoveAll(sut.storeRoot)
-	require.Nil(sut.T(), err)
-}