Files
docker-cli/opts/network_test.go
Sebastiaan van Stijn 201e44907d opts: Using the variable on range scope tc in function literal (scopelint)
```
opts/network_test.go:74:35: Using the variable on range scope `tc` in function literal (scopelint)
			assert.NilError(t, network.Set(tc.value))
			                               ^
opts/network_test.go:102:40: Using the variable on range scope `tc` in function literal (scopelint)
			assert.ErrorContains(t, network.Set(tc.value), tc.expectedError)
			                                    ^
opts/opts_test.go:270:30: Using the variable on range scope `tc` in function literal (scopelint)
			val, err := ValidateLabel(tc.value)
			                          ^
opts/opts_test.go:271:7: Using the variable on range scope `tc` in function literal (scopelint)
			if tc.expectedErr != "" {
			   ^
```

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit c2b069f4db)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-01-06 13:17:02 +01:00

107 lines
2.2 KiB
Go

package opts
import (
"testing"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
)
func TestNetworkOptLegacySyntax(t *testing.T) {
testCases := []struct {
value string
expected []NetworkAttachmentOpts
}{
{
value: "docknet1",
expected: []NetworkAttachmentOpts{
{
Target: "docknet1",
},
},
},
}
for _, tc := range testCases {
var network NetworkOpt
assert.NilError(t, network.Set(tc.value))
assert.Check(t, is.DeepEqual(tc.expected, network.Value()))
}
}
func TestNetworkOptAdvancedSyntax(t *testing.T) {
testCases := []struct {
value string
expected []NetworkAttachmentOpts
}{
{
value: "name=docknet1,alias=web,driver-opt=field1=value1",
expected: []NetworkAttachmentOpts{
{
Target: "docknet1",
Aliases: []string{"web"},
DriverOpts: map[string]string{
"field1": "value1",
},
},
},
},
{
value: "name=docknet1,alias=web1,alias=web2,driver-opt=field1=value1,driver-opt=field2=value2",
expected: []NetworkAttachmentOpts{
{
Target: "docknet1",
Aliases: []string{"web1", "web2"},
DriverOpts: map[string]string{
"field1": "value1",
"field2": "value2",
},
},
},
},
{
value: "name=docknet1",
expected: []NetworkAttachmentOpts{
{
Target: "docknet1",
Aliases: []string{},
},
},
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.value, func(t *testing.T) {
var network NetworkOpt
assert.NilError(t, network.Set(tc.value))
assert.Check(t, is.DeepEqual(tc.expected, network.Value()))
})
}
}
func TestNetworkOptAdvancedSyntaxInvalid(t *testing.T) {
testCases := []struct {
value string
expectedError string
}{
{
value: "invalidField=docknet1",
expectedError: "invalid field",
},
{
value: "network=docknet1,invalid=web",
expectedError: "invalid field",
},
{
value: "driver-opt=field1=value1,driver-opt=field2=value2",
expectedError: "network name/id is not specified",
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.value, func(t *testing.T) {
var network NetworkOpt
assert.ErrorContains(t, network.Set(tc.value), tc.expectedError)
})
}
}