mirror of https://github.com/docker/cli.git
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:
parent
d160d98970
commit
eb366ae039
|
@ -5,7 +5,9 @@ import (
|
|||
"testing"
|
||||
|
||||
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) {
|
||||
|
@ -24,7 +26,7 @@ func TestMountOptString(t *testing.T) {
|
|||
},
|
||||
}
|
||||
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) {
|
||||
|
@ -37,15 +39,15 @@ func TestMountOptSetBindNoErrorBind(t *testing.T) {
|
|||
} {
|
||||
var mount MountOpt
|
||||
|
||||
assert.NilError(t, mount.Set(testcase))
|
||||
assert.NoError(t, mount.Set(testcase))
|
||||
|
||||
mounts := mount.Value()
|
||||
assert.Equal(t, len(mounts), 1)
|
||||
assert.Equal(t, mounts[0], mounttypes.Mount{
|
||||
require.Len(t, mounts, 1)
|
||||
assert.Equal(t, mounttypes.Mount{
|
||||
Type: mounttypes.TypeBind,
|
||||
Source: "/source",
|
||||
Target: "/target",
|
||||
})
|
||||
}, mounts[0])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -59,15 +61,15 @@ func TestMountOptSetVolumeNoError(t *testing.T) {
|
|||
} {
|
||||
var mount MountOpt
|
||||
|
||||
assert.NilError(t, mount.Set(testcase))
|
||||
assert.NoError(t, mount.Set(testcase))
|
||||
|
||||
mounts := mount.Value()
|
||||
assert.Equal(t, len(mounts), 1)
|
||||
assert.Equal(t, mounts[0], mounttypes.Mount{
|
||||
require.Len(t, mounts, 1)
|
||||
assert.Equal(t, mounttypes.Mount{
|
||||
Type: mounttypes.TypeVolume,
|
||||
Source: "/source",
|
||||
Target: "/target",
|
||||
})
|
||||
}, mounts[0])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -75,82 +77,82 @@ func TestMountOptSetVolumeNoError(t *testing.T) {
|
|||
// volume mount.
|
||||
func TestMountOptDefaultType(t *testing.T) {
|
||||
var mount MountOpt
|
||||
assert.NilError(t, mount.Set("target=/target,source=/foo"))
|
||||
assert.Equal(t, mount.values[0].Type, mounttypes.TypeVolume)
|
||||
assert.NoError(t, mount.Set("target=/target,source=/foo"))
|
||||
assert.Equal(t, mounttypes.TypeVolume, mount.values[0].Type)
|
||||
}
|
||||
|
||||
func TestMountOptSetErrorNoTarget(t *testing.T) {
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
var mount MountOpt
|
||||
assert.Error(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=no"), "invalid value for readonly: no")
|
||||
assert.EqualError(t, mount.Set("type=volume,readonly=invalid"), "invalid value for readonly: invalid")
|
||||
}
|
||||
|
||||
func TestMountOptDefaultEnableReadOnly(t *testing.T) {
|
||||
var m MountOpt
|
||||
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo"))
|
||||
assert.Equal(t, m.values[0].ReadOnly, false)
|
||||
assert.NoError(t, m.Set("type=bind,target=/foo,source=/foo"))
|
||||
assert.False(t, m.values[0].ReadOnly)
|
||||
|
||||
m = MountOpt{}
|
||||
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly"))
|
||||
assert.Equal(t, m.values[0].ReadOnly, true)
|
||||
assert.NoError(t, m.Set("type=bind,target=/foo,source=/foo,readonly"))
|
||||
assert.True(t, m.values[0].ReadOnly)
|
||||
|
||||
m = MountOpt{}
|
||||
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=1"))
|
||||
assert.Equal(t, m.values[0].ReadOnly, true)
|
||||
assert.NoError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=1"))
|
||||
assert.True(t, m.values[0].ReadOnly)
|
||||
|
||||
m = MountOpt{}
|
||||
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=true"))
|
||||
assert.Equal(t, m.values[0].ReadOnly, true)
|
||||
assert.NoError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=true"))
|
||||
assert.True(t, m.values[0].ReadOnly)
|
||||
|
||||
m = MountOpt{}
|
||||
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=0"))
|
||||
assert.Equal(t, m.values[0].ReadOnly, false)
|
||||
assert.NoError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=0"))
|
||||
assert.False(t, m.values[0].ReadOnly)
|
||||
}
|
||||
|
||||
func TestMountOptVolumeNoCopy(t *testing.T) {
|
||||
var m MountOpt
|
||||
assert.NilError(t, m.Set("type=volume,target=/foo,volume-nocopy"))
|
||||
assert.Equal(t, m.values[0].Source, "")
|
||||
assert.NoError(t, m.Set("type=volume,target=/foo,volume-nocopy"))
|
||||
assert.Equal(t, "", m.values[0].Source)
|
||||
|
||||
m = MountOpt{}
|
||||
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo"))
|
||||
assert.Equal(t, m.values[0].VolumeOptions == nil, true)
|
||||
assert.NoError(t, m.Set("type=volume,target=/foo,source=foo"))
|
||||
assert.True(t, m.values[0].VolumeOptions == nil)
|
||||
|
||||
m = MountOpt{}
|
||||
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy=true"))
|
||||
assert.Equal(t, m.values[0].VolumeOptions != nil, true)
|
||||
assert.Equal(t, m.values[0].VolumeOptions.NoCopy, true)
|
||||
assert.NoError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy=true"))
|
||||
assert.True(t, m.values[0].VolumeOptions != nil)
|
||||
assert.True(t, m.values[0].VolumeOptions.NoCopy)
|
||||
|
||||
m = MountOpt{}
|
||||
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy"))
|
||||
assert.Equal(t, m.values[0].VolumeOptions != nil, true)
|
||||
assert.Equal(t, m.values[0].VolumeOptions.NoCopy, true)
|
||||
assert.NoError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy"))
|
||||
assert.True(t, m.values[0].VolumeOptions != nil)
|
||||
assert.True(t, m.values[0].VolumeOptions.NoCopy)
|
||||
|
||||
m = MountOpt{}
|
||||
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy=1"))
|
||||
assert.Equal(t, m.values[0].VolumeOptions != nil, true)
|
||||
assert.Equal(t, m.values[0].VolumeOptions.NoCopy, true)
|
||||
assert.NoError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy=1"))
|
||||
assert.True(t, m.values[0].VolumeOptions != nil)
|
||||
assert.True(t, m.values[0].VolumeOptions.NoCopy)
|
||||
}
|
||||
|
||||
func TestMountOptTypeConflict(t *testing.T) {
|
||||
var m MountOpt
|
||||
assert.Error(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=bind,target=/foo,source=/foo,volume-nocopy=true"), "cannot mix")
|
||||
testutil.ErrorContains(t, m.Set("type=volume,target=/foo,source=/foo,bind-propagation=rprivate"), "cannot mix")
|
||||
}
|
||||
|
||||
func TestMountOptSetTmpfsNoError(t *testing.T) {
|
||||
|
@ -161,24 +163,24 @@ func TestMountOptSetTmpfsNoError(t *testing.T) {
|
|||
} {
|
||||
var mount MountOpt
|
||||
|
||||
assert.NilError(t, mount.Set(testcase))
|
||||
assert.NoError(t, mount.Set(testcase))
|
||||
|
||||
mounts := mount.Value()
|
||||
assert.Equal(t, len(mounts), 1)
|
||||
assert.DeepEqual(t, mounts[0], mounttypes.Mount{
|
||||
require.Len(t, mounts, 1)
|
||||
assert.Equal(t, mounttypes.Mount{
|
||||
Type: mounttypes.TypeTmpfs,
|
||||
Target: "/target",
|
||||
TmpfsOptions: &mounttypes.TmpfsOptions{
|
||||
SizeBytes: 1024 * 1024, // not 1000 * 1000
|
||||
Mode: os.FileMode(0700),
|
||||
},
|
||||
})
|
||||
}, mounts[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestMountOptSetTmpfsError(t *testing.T) {
|
||||
var m MountOpt
|
||||
assert.Error(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")
|
||||
assert.Error(t, m.Set("type=tmpfs"), "target is required")
|
||||
testutil.ErrorContains(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-mode=foo"), "invalid value for tmpfs-mode")
|
||||
testutil.ErrorContains(t, m.Set("type=tmpfs"), "target is required")
|
||||
}
|
||||
|
|
|
@ -4,7 +4,8 @@ import (
|
|||
"testing"
|
||||
|
||||
"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) {
|
||||
|
@ -98,8 +99,8 @@ func TestPortOptValidSimpleSyntax(t *testing.T) {
|
|||
}
|
||||
for _, tc := range testCases {
|
||||
var port PortOpt
|
||||
assert.NilError(t, port.Set(tc.value))
|
||||
assert.Equal(t, len(port.Value()), len(tc.expected))
|
||||
assert.NoError(t, port.Set(tc.value))
|
||||
assert.Len(t, port.Value(), len(tc.expected))
|
||||
for _, expectedPortConfig := range tc.expected {
|
||||
assertContains(t, port.Value(), expectedPortConfig)
|
||||
}
|
||||
|
@ -189,8 +190,8 @@ func TestPortOptValidComplexSyntax(t *testing.T) {
|
|||
}
|
||||
for _, tc := range testCases {
|
||||
var port PortOpt
|
||||
assert.NilError(t, port.Set(tc.value))
|
||||
assert.Equal(t, len(port.Value()), len(tc.expected))
|
||||
assert.NoError(t, port.Set(tc.value))
|
||||
assert.Len(t, port.Value(), len(tc.expected))
|
||||
for _, expectedPortConfig := range tc.expected {
|
||||
assertContains(t, port.Value(), expectedPortConfig)
|
||||
}
|
||||
|
@ -241,7 +242,7 @@ func TestPortOptInvalidComplexSyntax(t *testing.T) {
|
|||
}
|
||||
for _, tc := range testCases {
|
||||
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: "",
|
||||
expectedError: "No port specified",
|
||||
expectedError: "No port specified: <empty>",
|
||||
},
|
||||
{
|
||||
value: "1.1.1.1:80:80",
|
||||
expectedError: "HostIP is not supported",
|
||||
expectedError: "HostIP is not supported.",
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
var port PortOpt
|
||||
assert.Error(t, port.Set(tc.value), tc.expectedError)
|
||||
assert.EqualError(t, port.Set(tc.value), tc.expectedError)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,28 +1,29 @@
|
|||
package opts
|
||||
|
||||
import (
|
||||
"github.com/docker/docker/pkg/testutil/assert"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestQuotedStringSetWithQuotes(t *testing.T) {
|
||||
value := ""
|
||||
qs := NewQuotedString(&value)
|
||||
assert.NilError(t, qs.Set("\"something\""))
|
||||
assert.Equal(t, qs.String(), "something")
|
||||
assert.Equal(t, value, "something")
|
||||
assert.NoError(t, qs.Set(`"something"`))
|
||||
assert.Equal(t, "something", qs.String())
|
||||
assert.Equal(t, "something", value)
|
||||
}
|
||||
|
||||
func TestQuotedStringSetWithMismatchedQuotes(t *testing.T) {
|
||||
value := ""
|
||||
qs := NewQuotedString(&value)
|
||||
assert.NilError(t, qs.Set("\"something'"))
|
||||
assert.Equal(t, qs.String(), "\"something'")
|
||||
assert.NoError(t, qs.Set(`"something'`))
|
||||
assert.Equal(t, `"something'`, qs.String())
|
||||
}
|
||||
|
||||
func TestQuotedStringSetWithNoQuotes(t *testing.T) {
|
||||
value := ""
|
||||
qs := NewQuotedString(&value)
|
||||
assert.NilError(t, qs.Set("something"))
|
||||
assert.Equal(t, qs.String(), "something")
|
||||
assert.NoError(t, qs.Set("something"))
|
||||
assert.Equal(t, "something", qs.String())
|
||||
}
|
||||
|
|
|
@ -4,76 +4,77 @@ import (
|
|||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/pkg/testutil/assert"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSecretOptionsSimple(t *testing.T) {
|
||||
var opt SecretOpt
|
||||
|
||||
testCase := "app-secret"
|
||||
assert.NilError(t, opt.Set(testCase))
|
||||
assert.NoError(t, opt.Set(testCase))
|
||||
|
||||
reqs := opt.Value()
|
||||
assert.Equal(t, len(reqs), 1)
|
||||
require.Len(t, reqs, 1)
|
||||
req := reqs[0]
|
||||
assert.Equal(t, req.SecretName, "app-secret")
|
||||
assert.Equal(t, req.File.Name, "app-secret")
|
||||
assert.Equal(t, req.File.UID, "0")
|
||||
assert.Equal(t, req.File.GID, "0")
|
||||
assert.Equal(t, "app-secret", req.SecretName)
|
||||
assert.Equal(t, "app-secret", req.File.Name)
|
||||
assert.Equal(t, "0", req.File.UID)
|
||||
assert.Equal(t, "0", req.File.GID)
|
||||
}
|
||||
|
||||
func TestSecretOptionsSourceTarget(t *testing.T) {
|
||||
var opt SecretOpt
|
||||
|
||||
testCase := "source=foo,target=testing"
|
||||
assert.NilError(t, opt.Set(testCase))
|
||||
assert.NoError(t, opt.Set(testCase))
|
||||
|
||||
reqs := opt.Value()
|
||||
assert.Equal(t, len(reqs), 1)
|
||||
require.Len(t, reqs, 1)
|
||||
req := reqs[0]
|
||||
assert.Equal(t, req.SecretName, "foo")
|
||||
assert.Equal(t, req.File.Name, "testing")
|
||||
assert.Equal(t, "foo", req.SecretName)
|
||||
assert.Equal(t, "testing", req.File.Name)
|
||||
}
|
||||
|
||||
func TestSecretOptionsShorthand(t *testing.T) {
|
||||
var opt SecretOpt
|
||||
|
||||
testCase := "src=foo,target=testing"
|
||||
assert.NilError(t, opt.Set(testCase))
|
||||
assert.NoError(t, opt.Set(testCase))
|
||||
|
||||
reqs := opt.Value()
|
||||
assert.Equal(t, len(reqs), 1)
|
||||
require.Len(t, reqs, 1)
|
||||
req := reqs[0]
|
||||
assert.Equal(t, req.SecretName, "foo")
|
||||
assert.Equal(t, "foo", req.SecretName)
|
||||
}
|
||||
|
||||
func TestSecretOptionsCustomUidGid(t *testing.T) {
|
||||
var opt SecretOpt
|
||||
|
||||
testCase := "source=foo,target=testing,uid=1000,gid=1001"
|
||||
assert.NilError(t, opt.Set(testCase))
|
||||
assert.NoError(t, opt.Set(testCase))
|
||||
|
||||
reqs := opt.Value()
|
||||
assert.Equal(t, len(reqs), 1)
|
||||
require.Len(t, reqs, 1)
|
||||
req := reqs[0]
|
||||
assert.Equal(t, req.SecretName, "foo")
|
||||
assert.Equal(t, req.File.Name, "testing")
|
||||
assert.Equal(t, req.File.UID, "1000")
|
||||
assert.Equal(t, req.File.GID, "1001")
|
||||
assert.Equal(t, "foo", req.SecretName)
|
||||
assert.Equal(t, "testing", req.File.Name)
|
||||
assert.Equal(t, "1000", req.File.UID)
|
||||
assert.Equal(t, "1001", req.File.GID)
|
||||
}
|
||||
|
||||
func TestSecretOptionsCustomMode(t *testing.T) {
|
||||
var opt SecretOpt
|
||||
|
||||
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()
|
||||
assert.Equal(t, len(reqs), 1)
|
||||
require.Len(t, reqs, 1)
|
||||
req := reqs[0]
|
||||
assert.Equal(t, req.SecretName, "foo")
|
||||
assert.Equal(t, req.File.Name, "testing")
|
||||
assert.Equal(t, req.File.UID, "1000")
|
||||
assert.Equal(t, req.File.GID, "1001")
|
||||
assert.Equal(t, req.File.Mode, os.FileMode(0444))
|
||||
assert.Equal(t, "foo", req.SecretName)
|
||||
assert.Equal(t, "testing", req.File.Name)
|
||||
assert.Equal(t, "1000", req.File.UID)
|
||||
assert.Equal(t, "1001", req.File.GID)
|
||||
assert.Equal(t, os.FileMode(0444), req.File.Mode)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue