mirror of https://github.com/docker/cli.git
Refactor config and secret tests to table-driven
Refactors the config and secret unit tests to be table driven to remove duplication Signed-off-by: Nick Adcock <nick.adcock@docker.com>
This commit is contained in:
parent
9698b7a374
commit
3baa6d57fa
|
@ -8,86 +8,80 @@ import (
|
||||||
is "gotest.tools/assert/cmp"
|
is "gotest.tools/assert/cmp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestConfigOptionsSimple(t *testing.T) {
|
func TestConfigOptions(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
configName string
|
||||||
|
fileName string
|
||||||
|
uid string
|
||||||
|
gid string
|
||||||
|
fileMode uint
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Simple",
|
||||||
|
input: "app-config",
|
||||||
|
configName: "app-config",
|
||||||
|
fileName: "app-config",
|
||||||
|
uid: "0",
|
||||||
|
gid: "0",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Source",
|
||||||
|
input: "source=foo",
|
||||||
|
configName: "foo",
|
||||||
|
fileName: "foo",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "SourceTarget",
|
||||||
|
input: "source=foo,target=testing",
|
||||||
|
configName: "foo",
|
||||||
|
fileName: "testing",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Shorthand",
|
||||||
|
input: "src=foo,target=testing",
|
||||||
|
configName: "foo",
|
||||||
|
fileName: "testing",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "CustomUidGid",
|
||||||
|
input: "source=foo,target=testing,uid=1000,gid=1001",
|
||||||
|
configName: "foo",
|
||||||
|
fileName: "testing",
|
||||||
|
uid: "1000",
|
||||||
|
gid: "1001",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "CustomMode",
|
||||||
|
input: "source=foo,target=testing,uid=1000,gid=1001,mode=0444",
|
||||||
|
configName: "foo",
|
||||||
|
fileName: "testing",
|
||||||
|
uid: "1000",
|
||||||
|
gid: "1001",
|
||||||
|
fileMode: 0444,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
tc := tc
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
var opt ConfigOpt
|
var opt ConfigOpt
|
||||||
|
assert.NilError(t, opt.Set(tc.input))
|
||||||
testCase := "app-config"
|
|
||||||
assert.NilError(t, opt.Set(testCase))
|
|
||||||
|
|
||||||
reqs := opt.Value()
|
reqs := opt.Value()
|
||||||
assert.Assert(t, is.Len(reqs, 1))
|
assert.Assert(t, is.Len(reqs, 1))
|
||||||
req := reqs[0]
|
req := reqs[0]
|
||||||
assert.Check(t, is.Equal("app-config", req.ConfigName))
|
assert.Check(t, is.Equal(tc.configName, req.ConfigName))
|
||||||
assert.Check(t, is.Equal("app-config", req.File.Name))
|
assert.Check(t, is.Equal(tc.fileName, req.File.Name))
|
||||||
assert.Check(t, is.Equal("0", req.File.UID))
|
if tc.uid != "" {
|
||||||
assert.Check(t, is.Equal("0", req.File.GID))
|
assert.Check(t, is.Equal(tc.uid, req.File.UID))
|
||||||
}
|
}
|
||||||
|
if tc.gid != "" {
|
||||||
func TestConfigOptionsSource(t *testing.T) {
|
assert.Check(t, is.Equal(tc.gid, req.File.GID))
|
||||||
var opt ConfigOpt
|
|
||||||
|
|
||||||
testCase := "source=foo"
|
|
||||||
assert.NilError(t, opt.Set(testCase))
|
|
||||||
|
|
||||||
reqs := opt.Value()
|
|
||||||
assert.Assert(t, is.Len(reqs, 1))
|
|
||||||
req := reqs[0]
|
|
||||||
assert.Check(t, is.Equal("foo", req.ConfigName))
|
|
||||||
assert.Check(t, is.Equal("foo", req.File.Name))
|
|
||||||
}
|
}
|
||||||
|
if tc.fileMode != 0 {
|
||||||
func TestConfigOptionsSourceTarget(t *testing.T) {
|
assert.Check(t, is.Equal(os.FileMode(tc.fileMode), req.File.Mode))
|
||||||
var opt ConfigOpt
|
|
||||||
|
|
||||||
testCase := "source=foo,target=testing"
|
|
||||||
assert.NilError(t, opt.Set(testCase))
|
|
||||||
|
|
||||||
reqs := opt.Value()
|
|
||||||
assert.Assert(t, is.Len(reqs, 1))
|
|
||||||
req := reqs[0]
|
|
||||||
assert.Check(t, is.Equal("foo", req.ConfigName))
|
|
||||||
assert.Check(t, is.Equal("testing", req.File.Name))
|
|
||||||
}
|
}
|
||||||
|
})
|
||||||
func TestConfigOptionsShorthand(t *testing.T) {
|
|
||||||
var opt ConfigOpt
|
|
||||||
|
|
||||||
testCase := "src=foo,target=testing"
|
|
||||||
assert.NilError(t, opt.Set(testCase))
|
|
||||||
|
|
||||||
reqs := opt.Value()
|
|
||||||
assert.Assert(t, is.Len(reqs, 1))
|
|
||||||
req := reqs[0]
|
|
||||||
assert.Check(t, is.Equal("foo", req.ConfigName))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConfigOptionsCustomUidGid(t *testing.T) {
|
|
||||||
var opt ConfigOpt
|
|
||||||
|
|
||||||
testCase := "source=foo,target=testing,uid=1000,gid=1001"
|
|
||||||
assert.NilError(t, opt.Set(testCase))
|
|
||||||
|
|
||||||
reqs := opt.Value()
|
|
||||||
assert.Assert(t, is.Len(reqs, 1))
|
|
||||||
req := reqs[0]
|
|
||||||
assert.Check(t, is.Equal("foo", req.ConfigName))
|
|
||||||
assert.Check(t, is.Equal("testing", req.File.Name))
|
|
||||||
assert.Check(t, is.Equal("1000", req.File.UID))
|
|
||||||
assert.Check(t, is.Equal("1001", req.File.GID))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestConfigOptionsCustomMode(t *testing.T) {
|
|
||||||
var opt ConfigOpt
|
|
||||||
|
|
||||||
testCase := "source=foo,target=testing,uid=1000,gid=1001,mode=0444"
|
|
||||||
assert.NilError(t, opt.Set(testCase))
|
|
||||||
|
|
||||||
reqs := opt.Value()
|
|
||||||
assert.Assert(t, is.Len(reqs, 1))
|
|
||||||
req := reqs[0]
|
|
||||||
assert.Check(t, is.Equal("foo", req.ConfigName))
|
|
||||||
assert.Check(t, is.Equal("testing", req.File.Name))
|
|
||||||
assert.Check(t, is.Equal("1000", req.File.UID))
|
|
||||||
assert.Check(t, is.Equal("1001", req.File.GID))
|
|
||||||
assert.Check(t, is.Equal(os.FileMode(0444), req.File.Mode))
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,86 +8,80 @@ import (
|
||||||
is "gotest.tools/assert/cmp"
|
is "gotest.tools/assert/cmp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSecretOptionsSimple(t *testing.T) {
|
func TestSecretOptions(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
secretName string
|
||||||
|
fileName string
|
||||||
|
uid string
|
||||||
|
gid string
|
||||||
|
fileMode uint
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Simple",
|
||||||
|
input: "app-secret",
|
||||||
|
secretName: "app-secret",
|
||||||
|
fileName: "app-secret",
|
||||||
|
uid: "0",
|
||||||
|
gid: "0",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Source",
|
||||||
|
input: "source=foo",
|
||||||
|
secretName: "foo",
|
||||||
|
fileName: "foo",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "SourceTarget",
|
||||||
|
input: "source=foo,target=testing",
|
||||||
|
secretName: "foo",
|
||||||
|
fileName: "testing",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Shorthand",
|
||||||
|
input: "src=foo,target=testing",
|
||||||
|
secretName: "foo",
|
||||||
|
fileName: "testing",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "CustomUidGid",
|
||||||
|
input: "source=foo,target=testing,uid=1000,gid=1001",
|
||||||
|
secretName: "foo",
|
||||||
|
fileName: "testing",
|
||||||
|
uid: "1000",
|
||||||
|
gid: "1001",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "CustomMode",
|
||||||
|
input: "source=foo,target=testing,uid=1000,gid=1001,mode=0444",
|
||||||
|
secretName: "foo",
|
||||||
|
fileName: "testing",
|
||||||
|
uid: "1000",
|
||||||
|
gid: "1001",
|
||||||
|
fileMode: 0444,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
tc := tc
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
var opt SecretOpt
|
var opt SecretOpt
|
||||||
|
assert.NilError(t, opt.Set(tc.input))
|
||||||
testCase := "app-secret"
|
|
||||||
assert.NilError(t, opt.Set(testCase))
|
|
||||||
|
|
||||||
reqs := opt.Value()
|
reqs := opt.Value()
|
||||||
assert.Assert(t, is.Len(reqs, 1))
|
assert.Assert(t, is.Len(reqs, 1))
|
||||||
req := reqs[0]
|
req := reqs[0]
|
||||||
assert.Check(t, is.Equal("app-secret", req.SecretName))
|
assert.Check(t, is.Equal(tc.secretName, req.SecretName))
|
||||||
assert.Check(t, is.Equal("app-secret", req.File.Name))
|
assert.Check(t, is.Equal(tc.fileName, req.File.Name))
|
||||||
assert.Check(t, is.Equal("0", req.File.UID))
|
if tc.uid != "" {
|
||||||
assert.Check(t, is.Equal("0", req.File.GID))
|
assert.Check(t, is.Equal(tc.uid, req.File.UID))
|
||||||
}
|
}
|
||||||
|
if tc.gid != "" {
|
||||||
func TestSecretOptionsSource(t *testing.T) {
|
assert.Check(t, is.Equal(tc.gid, req.File.GID))
|
||||||
var opt SecretOpt
|
|
||||||
|
|
||||||
testCase := "source=foo"
|
|
||||||
assert.NilError(t, opt.Set(testCase))
|
|
||||||
|
|
||||||
reqs := opt.Value()
|
|
||||||
assert.Assert(t, is.Len(reqs, 1))
|
|
||||||
req := reqs[0]
|
|
||||||
assert.Check(t, is.Equal("foo", req.SecretName))
|
|
||||||
assert.Check(t, is.Equal("foo", req.File.Name))
|
|
||||||
}
|
}
|
||||||
|
if tc.fileMode != 0 {
|
||||||
func TestSecretOptionsSourceTarget(t *testing.T) {
|
assert.Check(t, is.Equal(os.FileMode(tc.fileMode), req.File.Mode))
|
||||||
var opt SecretOpt
|
|
||||||
|
|
||||||
testCase := "source=foo,target=testing"
|
|
||||||
assert.NilError(t, opt.Set(testCase))
|
|
||||||
|
|
||||||
reqs := opt.Value()
|
|
||||||
assert.Assert(t, is.Len(reqs, 1))
|
|
||||||
req := reqs[0]
|
|
||||||
assert.Check(t, is.Equal("foo", req.SecretName))
|
|
||||||
assert.Check(t, is.Equal("testing", req.File.Name))
|
|
||||||
}
|
}
|
||||||
|
})
|
||||||
func TestSecretOptionsShorthand(t *testing.T) {
|
|
||||||
var opt SecretOpt
|
|
||||||
|
|
||||||
testCase := "src=foo,target=testing"
|
|
||||||
assert.NilError(t, opt.Set(testCase))
|
|
||||||
|
|
||||||
reqs := opt.Value()
|
|
||||||
assert.Assert(t, is.Len(reqs, 1))
|
|
||||||
req := reqs[0]
|
|
||||||
assert.Check(t, is.Equal("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))
|
|
||||||
|
|
||||||
reqs := opt.Value()
|
|
||||||
assert.Assert(t, is.Len(reqs, 1))
|
|
||||||
req := reqs[0]
|
|
||||||
assert.Check(t, is.Equal("foo", req.SecretName))
|
|
||||||
assert.Check(t, is.Equal("testing", req.File.Name))
|
|
||||||
assert.Check(t, is.Equal("1000", req.File.UID))
|
|
||||||
assert.Check(t, is.Equal("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))
|
|
||||||
|
|
||||||
reqs := opt.Value()
|
|
||||||
assert.Assert(t, is.Len(reqs, 1))
|
|
||||||
req := reqs[0]
|
|
||||||
assert.Check(t, is.Equal("foo", req.SecretName))
|
|
||||||
assert.Check(t, is.Equal("testing", req.File.Name))
|
|
||||||
assert.Check(t, is.Equal("1000", req.File.UID))
|
|
||||||
assert.Check(t, is.Equal("1001", req.File.GID))
|
|
||||||
assert.Check(t, is.Equal(os.FileMode(0444), req.File.Mode))
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue