6052f2b396
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>
80 lines
1.8 KiB
Go
80 lines
1.8 KiB
Go
package opts
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestSecretOptionsSimple(t *testing.T) {
|
|
var opt SecretOpt
|
|
|
|
testCase := "app-secret"
|
|
assert.NoError(t, opt.Set(testCase))
|
|
|
|
reqs := opt.Value()
|
|
require.Len(t, reqs, 1)
|
|
req := reqs[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.NoError(t, opt.Set(testCase))
|
|
|
|
reqs := opt.Value()
|
|
require.Len(t, reqs, 1)
|
|
req := reqs[0]
|
|
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.NoError(t, opt.Set(testCase))
|
|
|
|
reqs := opt.Value()
|
|
require.Len(t, reqs, 1)
|
|
req := reqs[0]
|
|
assert.Equal(t, "foo", req.SecretName)
|
|
}
|
|
|
|
func TestSecretOptionsCustomUidGid(t *testing.T) {
|
|
var opt SecretOpt
|
|
|
|
testCase := "source=foo,target=testing,uid=1000,gid=1001"
|
|
assert.NoError(t, opt.Set(testCase))
|
|
|
|
reqs := opt.Value()
|
|
require.Len(t, reqs, 1)
|
|
req := reqs[0]
|
|
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.NoError(t, opt.Set(testCase))
|
|
|
|
reqs := opt.Value()
|
|
require.Len(t, reqs, 1)
|
|
req := reqs[0]
|
|
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)
|
|
}
|