Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect single-value advanced syntax #2243

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion opts/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (o *ConfigOpt) Set(value string) error {
}

// support a simple syntax of --config foo
if len(fields) == 1 {
if len(fields) == 1 && !strings.Contains(fields[0], "=") {
options.File.Name = fields[0]
options.ConfigName = fields[0]
o.values = append(o.values, options)
Expand Down Expand Up @@ -72,6 +72,9 @@ func (o *ConfigOpt) Set(value string) error {
if options.ConfigName == "" {
return fmt.Errorf("source is required")
}
if options.File.Name == "" {
options.File.Name = options.ConfigName
}

o.values = append(o.values, options)
return nil
Expand Down
87 changes: 87 additions & 0 deletions opts/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package opts

import (
"os"
"testing"

"gotest.tools/assert"
is "gotest.tools/assert/cmp"
)

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this to

Suggested change
t.Run(tc.name, func(t *testing.T) {
tc := tc
t.Run(tc.name, func(t *testing.T) {

see moby/moby#40115 (comment) (and https://blog.golang.org/subtests)

var opt ConfigOpt
assert.NilError(t, opt.Set(tc.input))
reqs := opt.Value()
assert.Assert(t, is.Len(reqs, 1))
req := reqs[0]
assert.Check(t, is.Equal(tc.configName, req.ConfigName))
assert.Check(t, is.Equal(tc.fileName, req.File.Name))
if tc.uid != "" {
assert.Check(t, is.Equal(tc.uid, req.File.UID))
}
if tc.gid != "" {
assert.Check(t, is.Equal(tc.gid, req.File.GID))
}
if tc.fileMode != 0 {
assert.Check(t, is.Equal(os.FileMode(tc.fileMode), req.File.Mode))
}
})
}
}
5 changes: 4 additions & 1 deletion opts/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (o *SecretOpt) Set(value string) error {
}

// support a simple syntax of --secret foo
if len(fields) == 1 {
if len(fields) == 1 && !strings.Contains(fields[0], "=") {
options.File.Name = fields[0]
options.SecretName = fields[0]
o.values = append(o.values, options)
Expand Down Expand Up @@ -72,6 +72,9 @@ func (o *SecretOpt) Set(value string) error {
if options.SecretName == "" {
return fmt.Errorf("source is required")
}
if options.File.Name == "" {
options.File.Name = options.SecretName
}

o.values = append(o.values, options)
return nil
Expand Down
145 changes: 76 additions & 69 deletions opts/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,73 +8,80 @@ import (
is "gotest.tools/assert/cmp"
)

func TestSecretOptionsSimple(t *testing.T) {
var opt SecretOpt

testCase := "app-secret"
assert.NilError(t, opt.Set(testCase))

reqs := opt.Value()
assert.Assert(t, is.Len(reqs, 1))
req := reqs[0]
assert.Check(t, is.Equal("app-secret", req.SecretName))
assert.Check(t, is.Equal("app-secret", req.File.Name))
assert.Check(t, is.Equal("0", req.File.UID))
assert.Check(t, is.Equal("0", req.File.GID))
}

func TestSecretOptionsSourceTarget(t *testing.T) {
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))
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Suggested change
t.Run(tc.name, func(t *testing.T) {
tc := tc
t.Run(tc.name, func(t *testing.T) {

see moby/moby#40115 (comment) (and https://blog.golang.org/subtests)

var opt SecretOpt
assert.NilError(t, opt.Set(tc.input))
reqs := opt.Value()
assert.Assert(t, is.Len(reqs, 1))
req := reqs[0]
assert.Check(t, is.Equal(tc.secretName, req.SecretName))
assert.Check(t, is.Equal(tc.fileName, req.File.Name))
if tc.uid != "" {
assert.Check(t, is.Equal(tc.uid, req.File.UID))
}
if tc.gid != "" {
assert.Check(t, is.Equal(tc.gid, req.File.GID))
}
if tc.fileMode != 0 {
assert.Check(t, is.Equal(os.FileMode(tc.fileMode), req.File.Mode))
}
})
}
}