From 50e74272d11d49010552a94b0bd1468b15573e37 Mon Sep 17 00:00:00 2001 From: Aaron Lehmann Date: Thu, 13 Apr 2017 15:45:37 -0700 Subject: [PATCH] Remove pkg/testutil/assert in favor of testify I noticed that we're using a homegrown package for assertions. The functions are extremely similar to testify, but with enough slight differences to be confusing (for example, Equal takes its arguments in a different order). We already vendor testify, and it's used in a few places by tests. I also found some problems with pkg/testutil/assert. For example, the NotNil function seems to be broken. It checks the argument against "nil", which only works for an interface. If you pass in a nil map or slice, the equality check will fail. In the interest of avoiding NIH, I'm proposing replacing pkg/testutil/assert with testify. The test code looks almost the same, but we avoid the confusion of having two similar but slightly different assertion packages, and having to maintain our own package instead of using a commonly-used one. In the process, I found a few places where the tests should halt if an assertion fails, so I've made those cases (that I noticed) use "require" instead of "assert", and I've vendored the "require" package from testify alongside the already-present "assert" package. Signed-off-by: Aaron Lehmann --- container_prune_test.go | 12 ++++++------ image_prune_test.go | 12 ++++++------ network_prune_test.go | 8 ++++---- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/container_prune_test.go b/container_prune_test.go index 55e551bb4b..8a1c63897b 100644 --- a/container_prune_test.go +++ b/container_prune_test.go @@ -11,7 +11,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" - "github.com/docker/docker/pkg/testutil/assert" + "github.com/stretchr/testify/assert" "golang.org/x/net/context" ) @@ -24,7 +24,7 @@ func TestContainersPruneError(t *testing.T) { filters := filters.NewArgs() _, err := client.ContainersPrune(context.Background(), filters) - assert.Error(t, err, "Error response from daemon: Server error") + assert.EqualError(t, err, "Error response from daemon: Server error") } func TestContainersPrune(t *testing.T) { @@ -99,7 +99,7 @@ func TestContainersPrune(t *testing.T) { query := req.URL.Query() for key, expected := range listCase.expectedQueryParams { actual := query.Get(key) - assert.Equal(t, actual, expected) + assert.Equal(t, expected, actual) } content, err := json.Marshal(types.ContainersPruneReport{ ContainersDeleted: []string{"container_id1", "container_id2"}, @@ -117,8 +117,8 @@ func TestContainersPrune(t *testing.T) { } report, err := client.ContainersPrune(context.Background(), listCase.filters) - assert.NilError(t, err) - assert.Equal(t, len(report.ContainersDeleted), 2) - assert.Equal(t, report.SpaceReclaimed, uint64(9999)) + assert.NoError(t, err) + assert.Len(t, report.ContainersDeleted, 2) + assert.Equal(t, uint64(9999), report.SpaceReclaimed) } } diff --git a/image_prune_test.go b/image_prune_test.go index 5dfb173acc..453f84adee 100644 --- a/image_prune_test.go +++ b/image_prune_test.go @@ -11,7 +11,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" - "github.com/docker/docker/pkg/testutil/assert" + "github.com/stretchr/testify/assert" "golang.org/x/net/context" ) @@ -24,7 +24,7 @@ func TestImagesPruneError(t *testing.T) { filters := filters.NewArgs() _, err := client.ImagesPrune(context.Background(), filters) - assert.Error(t, err, "Error response from daemon: Server error") + assert.EqualError(t, err, "Error response from daemon: Server error") } func TestImagesPrune(t *testing.T) { @@ -87,7 +87,7 @@ func TestImagesPrune(t *testing.T) { query := req.URL.Query() for key, expected := range listCase.expectedQueryParams { actual := query.Get(key) - assert.Equal(t, actual, expected) + assert.Equal(t, expected, actual) } content, err := json.Marshal(types.ImagesPruneReport{ ImagesDeleted: []types.ImageDeleteResponseItem{ @@ -112,8 +112,8 @@ func TestImagesPrune(t *testing.T) { } report, err := client.ImagesPrune(context.Background(), listCase.filters) - assert.NilError(t, err) - assert.Equal(t, len(report.ImagesDeleted), 2) - assert.Equal(t, report.SpaceReclaimed, uint64(9999)) + assert.NoError(t, err) + assert.Len(t, report.ImagesDeleted, 2) + assert.Equal(t, uint64(9999), report.SpaceReclaimed) } } diff --git a/network_prune_test.go b/network_prune_test.go index 2bf4af4716..3e4f5d0415 100644 --- a/network_prune_test.go +++ b/network_prune_test.go @@ -11,7 +11,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" - "github.com/docker/docker/pkg/testutil/assert" + "github.com/stretchr/testify/assert" "golang.org/x/net/context" ) @@ -89,7 +89,7 @@ func TestNetworksPrune(t *testing.T) { query := req.URL.Query() for key, expected := range listCase.expectedQueryParams { actual := query.Get(key) - assert.Equal(t, actual, expected) + assert.Equal(t, expected, actual) } content, err := json.Marshal(types.NetworksPruneReport{ NetworksDeleted: []string{"network_id1", "network_id2"}, @@ -106,7 +106,7 @@ func TestNetworksPrune(t *testing.T) { } report, err := client.NetworksPrune(context.Background(), listCase.filters) - assert.NilError(t, err) - assert.Equal(t, len(report.NetworksDeleted), 2) + assert.NoError(t, err) + assert.Len(t, report.NetworksDeleted, 2) } }