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 <aaron.lehmann@docker.com>
This commit is contained in:
Aaron Lehmann 2017-04-13 15:45:37 -07:00 committed by Vincent Demeester
parent d160d98970
commit eb366ae039
4 changed files with 98 additions and 93 deletions

View File

@ -5,7 +5,9 @@ import (
"testing" "testing"
mounttypes "github.com/docker/docker/api/types/mount" mounttypes "github.com/docker/docker/api/types/mount"
"github.com/docker/docker/pkg/testutil/assert" "github.com/docker/docker/pkg/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestMountOptString(t *testing.T) { func TestMountOptString(t *testing.T) {
@ -24,7 +26,7 @@ func TestMountOptString(t *testing.T) {
}, },
} }
expected := "bind /home/path /target, volume foo /target/foo" expected := "bind /home/path /target, volume foo /target/foo"
assert.Equal(t, mount.String(), expected) assert.Equal(t, expected, mount.String())
} }
func TestMountOptSetBindNoErrorBind(t *testing.T) { func TestMountOptSetBindNoErrorBind(t *testing.T) {
@ -37,15 +39,15 @@ func TestMountOptSetBindNoErrorBind(t *testing.T) {
} { } {
var mount MountOpt var mount MountOpt
assert.NilError(t, mount.Set(testcase)) assert.NoError(t, mount.Set(testcase))
mounts := mount.Value() mounts := mount.Value()
assert.Equal(t, len(mounts), 1) require.Len(t, mounts, 1)
assert.Equal(t, mounts[0], mounttypes.Mount{ assert.Equal(t, mounttypes.Mount{
Type: mounttypes.TypeBind, Type: mounttypes.TypeBind,
Source: "/source", Source: "/source",
Target: "/target", Target: "/target",
}) }, mounts[0])
} }
} }
@ -59,15 +61,15 @@ func TestMountOptSetVolumeNoError(t *testing.T) {
} { } {
var mount MountOpt var mount MountOpt
assert.NilError(t, mount.Set(testcase)) assert.NoError(t, mount.Set(testcase))
mounts := mount.Value() mounts := mount.Value()
assert.Equal(t, len(mounts), 1) require.Len(t, mounts, 1)
assert.Equal(t, mounts[0], mounttypes.Mount{ assert.Equal(t, mounttypes.Mount{
Type: mounttypes.TypeVolume, Type: mounttypes.TypeVolume,
Source: "/source", Source: "/source",
Target: "/target", Target: "/target",
}) }, mounts[0])
} }
} }
@ -75,82 +77,82 @@ func TestMountOptSetVolumeNoError(t *testing.T) {
// volume mount. // volume mount.
func TestMountOptDefaultType(t *testing.T) { func TestMountOptDefaultType(t *testing.T) {
var mount MountOpt var mount MountOpt
assert.NilError(t, mount.Set("target=/target,source=/foo")) assert.NoError(t, mount.Set("target=/target,source=/foo"))
assert.Equal(t, mount.values[0].Type, mounttypes.TypeVolume) assert.Equal(t, mounttypes.TypeVolume, mount.values[0].Type)
} }
func TestMountOptSetErrorNoTarget(t *testing.T) { func TestMountOptSetErrorNoTarget(t *testing.T) {
var mount MountOpt var mount MountOpt
assert.Error(t, mount.Set("type=volume,source=/foo"), "target is required") assert.EqualError(t, mount.Set("type=volume,source=/foo"), "target is required")
} }
func TestMountOptSetErrorInvalidKey(t *testing.T) { func TestMountOptSetErrorInvalidKey(t *testing.T) {
var mount MountOpt var mount MountOpt
assert.Error(t, mount.Set("type=volume,bogus=foo"), "unexpected key 'bogus'") assert.EqualError(t, mount.Set("type=volume,bogus=foo"), "unexpected key 'bogus' in 'bogus=foo'")
} }
func TestMountOptSetErrorInvalidField(t *testing.T) { func TestMountOptSetErrorInvalidField(t *testing.T) {
var mount MountOpt var mount MountOpt
assert.Error(t, mount.Set("type=volume,bogus"), "invalid field 'bogus'") assert.EqualError(t, mount.Set("type=volume,bogus"), "invalid field 'bogus' must be a key=value pair")
} }
func TestMountOptSetErrorInvalidReadOnly(t *testing.T) { func TestMountOptSetErrorInvalidReadOnly(t *testing.T) {
var mount MountOpt var mount MountOpt
assert.Error(t, mount.Set("type=volume,readonly=no"), "invalid value for readonly: no") assert.EqualError(t, mount.Set("type=volume,readonly=no"), "invalid value for readonly: no")
assert.Error(t, mount.Set("type=volume,readonly=invalid"), "invalid value for readonly: invalid") assert.EqualError(t, mount.Set("type=volume,readonly=invalid"), "invalid value for readonly: invalid")
} }
func TestMountOptDefaultEnableReadOnly(t *testing.T) { func TestMountOptDefaultEnableReadOnly(t *testing.T) {
var m MountOpt var m MountOpt
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo")) assert.NoError(t, m.Set("type=bind,target=/foo,source=/foo"))
assert.Equal(t, m.values[0].ReadOnly, false) assert.False(t, m.values[0].ReadOnly)
m = MountOpt{} m = MountOpt{}
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly")) assert.NoError(t, m.Set("type=bind,target=/foo,source=/foo,readonly"))
assert.Equal(t, m.values[0].ReadOnly, true) assert.True(t, m.values[0].ReadOnly)
m = MountOpt{} m = MountOpt{}
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=1")) assert.NoError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=1"))
assert.Equal(t, m.values[0].ReadOnly, true) assert.True(t, m.values[0].ReadOnly)
m = MountOpt{} m = MountOpt{}
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=true")) assert.NoError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=true"))
assert.Equal(t, m.values[0].ReadOnly, true) assert.True(t, m.values[0].ReadOnly)
m = MountOpt{} m = MountOpt{}
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=0")) assert.NoError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=0"))
assert.Equal(t, m.values[0].ReadOnly, false) assert.False(t, m.values[0].ReadOnly)
} }
func TestMountOptVolumeNoCopy(t *testing.T) { func TestMountOptVolumeNoCopy(t *testing.T) {
var m MountOpt var m MountOpt
assert.NilError(t, m.Set("type=volume,target=/foo,volume-nocopy")) assert.NoError(t, m.Set("type=volume,target=/foo,volume-nocopy"))
assert.Equal(t, m.values[0].Source, "") assert.Equal(t, "", m.values[0].Source)
m = MountOpt{} m = MountOpt{}
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo")) assert.NoError(t, m.Set("type=volume,target=/foo,source=foo"))
assert.Equal(t, m.values[0].VolumeOptions == nil, true) assert.True(t, m.values[0].VolumeOptions == nil)
m = MountOpt{} m = MountOpt{}
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy=true")) assert.NoError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy=true"))
assert.Equal(t, m.values[0].VolumeOptions != nil, true) assert.True(t, m.values[0].VolumeOptions != nil)
assert.Equal(t, m.values[0].VolumeOptions.NoCopy, true) assert.True(t, m.values[0].VolumeOptions.NoCopy)
m = MountOpt{} m = MountOpt{}
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy")) assert.NoError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy"))
assert.Equal(t, m.values[0].VolumeOptions != nil, true) assert.True(t, m.values[0].VolumeOptions != nil)
assert.Equal(t, m.values[0].VolumeOptions.NoCopy, true) assert.True(t, m.values[0].VolumeOptions.NoCopy)
m = MountOpt{} m = MountOpt{}
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy=1")) assert.NoError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy=1"))
assert.Equal(t, m.values[0].VolumeOptions != nil, true) assert.True(t, m.values[0].VolumeOptions != nil)
assert.Equal(t, m.values[0].VolumeOptions.NoCopy, true) assert.True(t, m.values[0].VolumeOptions.NoCopy)
} }
func TestMountOptTypeConflict(t *testing.T) { func TestMountOptTypeConflict(t *testing.T) {
var m MountOpt var m MountOpt
assert.Error(t, m.Set("type=bind,target=/foo,source=/foo,volume-nocopy=true"), "cannot mix") testutil.ErrorContains(t, m.Set("type=bind,target=/foo,source=/foo,volume-nocopy=true"), "cannot mix")
assert.Error(t, m.Set("type=volume,target=/foo,source=/foo,bind-propagation=rprivate"), "cannot mix") testutil.ErrorContains(t, m.Set("type=volume,target=/foo,source=/foo,bind-propagation=rprivate"), "cannot mix")
} }
func TestMountOptSetTmpfsNoError(t *testing.T) { func TestMountOptSetTmpfsNoError(t *testing.T) {
@ -161,24 +163,24 @@ func TestMountOptSetTmpfsNoError(t *testing.T) {
} { } {
var mount MountOpt var mount MountOpt
assert.NilError(t, mount.Set(testcase)) assert.NoError(t, mount.Set(testcase))
mounts := mount.Value() mounts := mount.Value()
assert.Equal(t, len(mounts), 1) require.Len(t, mounts, 1)
assert.DeepEqual(t, mounts[0], mounttypes.Mount{ assert.Equal(t, mounttypes.Mount{
Type: mounttypes.TypeTmpfs, Type: mounttypes.TypeTmpfs,
Target: "/target", Target: "/target",
TmpfsOptions: &mounttypes.TmpfsOptions{ TmpfsOptions: &mounttypes.TmpfsOptions{
SizeBytes: 1024 * 1024, // not 1000 * 1000 SizeBytes: 1024 * 1024, // not 1000 * 1000
Mode: os.FileMode(0700), Mode: os.FileMode(0700),
}, },
}) }, mounts[0])
} }
} }
func TestMountOptSetTmpfsError(t *testing.T) { func TestMountOptSetTmpfsError(t *testing.T) {
var m MountOpt var m MountOpt
assert.Error(t, m.Set("type=tmpfs,target=/foo,tmpfs-size=foo"), "invalid value for tmpfs-size") testutil.ErrorContains(t, m.Set("type=tmpfs,target=/foo,tmpfs-size=foo"), "invalid value for tmpfs-size")
assert.Error(t, m.Set("type=tmpfs,target=/foo,tmpfs-mode=foo"), "invalid value for tmpfs-mode") testutil.ErrorContains(t, m.Set("type=tmpfs,target=/foo,tmpfs-mode=foo"), "invalid value for tmpfs-mode")
assert.Error(t, m.Set("type=tmpfs"), "target is required") testutil.ErrorContains(t, m.Set("type=tmpfs"), "target is required")
} }

View File

@ -4,7 +4,8 @@ import (
"testing" "testing"
"github.com/docker/docker/api/types/swarm" "github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/pkg/testutil/assert" "github.com/docker/docker/pkg/testutil"
"github.com/stretchr/testify/assert"
) )
func TestPortOptValidSimpleSyntax(t *testing.T) { func TestPortOptValidSimpleSyntax(t *testing.T) {
@ -98,8 +99,8 @@ func TestPortOptValidSimpleSyntax(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
var port PortOpt var port PortOpt
assert.NilError(t, port.Set(tc.value)) assert.NoError(t, port.Set(tc.value))
assert.Equal(t, len(port.Value()), len(tc.expected)) assert.Len(t, port.Value(), len(tc.expected))
for _, expectedPortConfig := range tc.expected { for _, expectedPortConfig := range tc.expected {
assertContains(t, port.Value(), expectedPortConfig) assertContains(t, port.Value(), expectedPortConfig)
} }
@ -189,8 +190,8 @@ func TestPortOptValidComplexSyntax(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
var port PortOpt var port PortOpt
assert.NilError(t, port.Set(tc.value)) assert.NoError(t, port.Set(tc.value))
assert.Equal(t, len(port.Value()), len(tc.expected)) assert.Len(t, port.Value(), len(tc.expected))
for _, expectedPortConfig := range tc.expected { for _, expectedPortConfig := range tc.expected {
assertContains(t, port.Value(), expectedPortConfig) assertContains(t, port.Value(), expectedPortConfig)
} }
@ -241,7 +242,7 @@ func TestPortOptInvalidComplexSyntax(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
var port PortOpt var port PortOpt
assert.Error(t, port.Set(tc.value), tc.expectedError) testutil.ErrorContains(t, port.Set(tc.value), tc.expectedError)
} }
} }
@ -268,16 +269,16 @@ func TestPortOptInvalidSimpleSyntax(t *testing.T) {
}, },
{ {
value: "", value: "",
expectedError: "No port specified", expectedError: "No port specified: <empty>",
}, },
{ {
value: "1.1.1.1:80:80", value: "1.1.1.1:80:80",
expectedError: "HostIP is not supported", expectedError: "HostIP is not supported.",
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
var port PortOpt var port PortOpt
assert.Error(t, port.Set(tc.value), tc.expectedError) assert.EqualError(t, port.Set(tc.value), tc.expectedError)
} }
} }

View File

@ -1,28 +1,29 @@
package opts package opts
import ( import (
"github.com/docker/docker/pkg/testutil/assert"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func TestQuotedStringSetWithQuotes(t *testing.T) { func TestQuotedStringSetWithQuotes(t *testing.T) {
value := "" value := ""
qs := NewQuotedString(&value) qs := NewQuotedString(&value)
assert.NilError(t, qs.Set("\"something\"")) assert.NoError(t, qs.Set(`"something"`))
assert.Equal(t, qs.String(), "something") assert.Equal(t, "something", qs.String())
assert.Equal(t, value, "something") assert.Equal(t, "something", value)
} }
func TestQuotedStringSetWithMismatchedQuotes(t *testing.T) { func TestQuotedStringSetWithMismatchedQuotes(t *testing.T) {
value := "" value := ""
qs := NewQuotedString(&value) qs := NewQuotedString(&value)
assert.NilError(t, qs.Set("\"something'")) assert.NoError(t, qs.Set(`"something'`))
assert.Equal(t, qs.String(), "\"something'") assert.Equal(t, `"something'`, qs.String())
} }
func TestQuotedStringSetWithNoQuotes(t *testing.T) { func TestQuotedStringSetWithNoQuotes(t *testing.T) {
value := "" value := ""
qs := NewQuotedString(&value) qs := NewQuotedString(&value)
assert.NilError(t, qs.Set("something")) assert.NoError(t, qs.Set("something"))
assert.Equal(t, qs.String(), "something") assert.Equal(t, "something", qs.String())
} }

View File

@ -4,76 +4,77 @@ import (
"os" "os"
"testing" "testing"
"github.com/docker/docker/pkg/testutil/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestSecretOptionsSimple(t *testing.T) { func TestSecretOptionsSimple(t *testing.T) {
var opt SecretOpt var opt SecretOpt
testCase := "app-secret" testCase := "app-secret"
assert.NilError(t, opt.Set(testCase)) assert.NoError(t, opt.Set(testCase))
reqs := opt.Value() reqs := opt.Value()
assert.Equal(t, len(reqs), 1) require.Len(t, reqs, 1)
req := reqs[0] req := reqs[0]
assert.Equal(t, req.SecretName, "app-secret") assert.Equal(t, "app-secret", req.SecretName)
assert.Equal(t, req.File.Name, "app-secret") assert.Equal(t, "app-secret", req.File.Name)
assert.Equal(t, req.File.UID, "0") assert.Equal(t, "0", req.File.UID)
assert.Equal(t, req.File.GID, "0") assert.Equal(t, "0", req.File.GID)
} }
func TestSecretOptionsSourceTarget(t *testing.T) { func TestSecretOptionsSourceTarget(t *testing.T) {
var opt SecretOpt var opt SecretOpt
testCase := "source=foo,target=testing" testCase := "source=foo,target=testing"
assert.NilError(t, opt.Set(testCase)) assert.NoError(t, opt.Set(testCase))
reqs := opt.Value() reqs := opt.Value()
assert.Equal(t, len(reqs), 1) require.Len(t, reqs, 1)
req := reqs[0] req := reqs[0]
assert.Equal(t, req.SecretName, "foo") assert.Equal(t, "foo", req.SecretName)
assert.Equal(t, req.File.Name, "testing") assert.Equal(t, "testing", req.File.Name)
} }
func TestSecretOptionsShorthand(t *testing.T) { func TestSecretOptionsShorthand(t *testing.T) {
var opt SecretOpt var opt SecretOpt
testCase := "src=foo,target=testing" testCase := "src=foo,target=testing"
assert.NilError(t, opt.Set(testCase)) assert.NoError(t, opt.Set(testCase))
reqs := opt.Value() reqs := opt.Value()
assert.Equal(t, len(reqs), 1) require.Len(t, reqs, 1)
req := reqs[0] req := reqs[0]
assert.Equal(t, req.SecretName, "foo") assert.Equal(t, "foo", req.SecretName)
} }
func TestSecretOptionsCustomUidGid(t *testing.T) { func TestSecretOptionsCustomUidGid(t *testing.T) {
var opt SecretOpt var opt SecretOpt
testCase := "source=foo,target=testing,uid=1000,gid=1001" testCase := "source=foo,target=testing,uid=1000,gid=1001"
assert.NilError(t, opt.Set(testCase)) assert.NoError(t, opt.Set(testCase))
reqs := opt.Value() reqs := opt.Value()
assert.Equal(t, len(reqs), 1) require.Len(t, reqs, 1)
req := reqs[0] req := reqs[0]
assert.Equal(t, req.SecretName, "foo") assert.Equal(t, "foo", req.SecretName)
assert.Equal(t, req.File.Name, "testing") assert.Equal(t, "testing", req.File.Name)
assert.Equal(t, req.File.UID, "1000") assert.Equal(t, "1000", req.File.UID)
assert.Equal(t, req.File.GID, "1001") assert.Equal(t, "1001", req.File.GID)
} }
func TestSecretOptionsCustomMode(t *testing.T) { func TestSecretOptionsCustomMode(t *testing.T) {
var opt SecretOpt var opt SecretOpt
testCase := "source=foo,target=testing,uid=1000,gid=1001,mode=0444" testCase := "source=foo,target=testing,uid=1000,gid=1001,mode=0444"
assert.NilError(t, opt.Set(testCase)) assert.NoError(t, opt.Set(testCase))
reqs := opt.Value() reqs := opt.Value()
assert.Equal(t, len(reqs), 1) require.Len(t, reqs, 1)
req := reqs[0] req := reqs[0]
assert.Equal(t, req.SecretName, "foo") assert.Equal(t, "foo", req.SecretName)
assert.Equal(t, req.File.Name, "testing") assert.Equal(t, "testing", req.File.Name)
assert.Equal(t, req.File.UID, "1000") assert.Equal(t, "1000", req.File.UID)
assert.Equal(t, req.File.GID, "1001") assert.Equal(t, "1001", req.File.GID)
assert.Equal(t, req.File.Mode, os.FileMode(0444)) assert.Equal(t, os.FileMode(0444), req.File.Mode)
} }