mirror of https://github.com/docker/cli.git
Merge pull request #4533 from thaJeztah/test_table_TestParseRestartPolicy
rewrite TestParseRestartPolicy to use sub-tests
This commit is contained in:
commit
be9e42ee00
|
@ -700,34 +700,75 @@ func TestRunFlagsParseShmSize(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseRestartPolicy(t *testing.T) {
|
func TestParseRestartPolicy(t *testing.T) {
|
||||||
invalids := map[string]string{
|
tests := []struct {
|
||||||
"always:2:3": "invalid restart policy format: maximum retry count must be an integer",
|
input string
|
||||||
"on-failure:invalid": "invalid restart policy format: maximum retry count must be an integer",
|
expected container.RestartPolicy
|
||||||
}
|
expectedErr string
|
||||||
valids := map[string]container.RestartPolicy{
|
}{
|
||||||
"": {},
|
{
|
||||||
"always": {
|
input: "",
|
||||||
Name: "always",
|
|
||||||
MaximumRetryCount: 0,
|
|
||||||
},
|
},
|
||||||
"on-failure:1": {
|
{
|
||||||
Name: "on-failure",
|
input: "no",
|
||||||
MaximumRetryCount: 1,
|
expected: container.RestartPolicy{
|
||||||
|
Name: "no",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: ":1",
|
||||||
|
expectedErr: "invalid restart policy format: no policy provided before colon",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "always",
|
||||||
|
expected: container.RestartPolicy{
|
||||||
|
Name: "always",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "always:1",
|
||||||
|
expected: container.RestartPolicy{
|
||||||
|
Name: "always",
|
||||||
|
MaximumRetryCount: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "always:2:3",
|
||||||
|
expectedErr: "invalid restart policy format: maximum retry count must be an integer",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "on-failure:1",
|
||||||
|
expected: container.RestartPolicy{
|
||||||
|
Name: "on-failure",
|
||||||
|
MaximumRetryCount: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "on-failure:invalid",
|
||||||
|
expectedErr: "invalid restart policy format: maximum retry count must be an integer",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "unless-stopped",
|
||||||
|
expected: container.RestartPolicy{
|
||||||
|
Name: "unless-stopped",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "unless-stopped:invalid",
|
||||||
|
expectedErr: "invalid restart policy format: maximum retry count must be an integer",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for restart, expectedError := range invalids {
|
for _, tc := range tests {
|
||||||
if _, _, _, err := parseRun([]string{fmt.Sprintf("--restart=%s", restart), "img", "cmd"}); err == nil || err.Error() != expectedError {
|
tc := tc
|
||||||
t.Fatalf("Expected an error with message '%v' for %v, got %v", expectedError, restart, err)
|
t.Run(tc.input, func(t *testing.T) {
|
||||||
}
|
_, hostConfig, _, err := parseRun([]string{"--restart=" + tc.input, "img", "cmd"})
|
||||||
}
|
if tc.expectedErr != "" {
|
||||||
for restart, expected := range valids {
|
assert.Check(t, is.Error(err, tc.expectedErr))
|
||||||
_, hostconfig, _, err := parseRun([]string{fmt.Sprintf("--restart=%v", restart), "img", "cmd"})
|
assert.Check(t, is.Nil(hostConfig))
|
||||||
if err != nil {
|
} else {
|
||||||
t.Fatal(err)
|
assert.NilError(t, err)
|
||||||
}
|
assert.Check(t, is.DeepEqual(hostConfig.RestartPolicy, tc.expected))
|
||||||
if hostconfig.RestartPolicy != expected {
|
}
|
||||||
t.Fatalf("Expected %v, got %v", expected, hostconfig.RestartPolicy)
|
})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -71,17 +71,22 @@ func ConvertKVStringsToMapWithNil(values []string) map[string]*string {
|
||||||
|
|
||||||
// ParseRestartPolicy returns the parsed policy or an error indicating what is incorrect
|
// ParseRestartPolicy returns the parsed policy or an error indicating what is incorrect
|
||||||
func ParseRestartPolicy(policy string) (container.RestartPolicy, error) {
|
func ParseRestartPolicy(policy string) (container.RestartPolicy, error) {
|
||||||
p := container.RestartPolicy{}
|
|
||||||
|
|
||||||
if policy == "" {
|
if policy == "" {
|
||||||
return p, nil
|
// for backward-compatibility, we don't set the default ("no")
|
||||||
|
// policy here, because older versions of the engine may not
|
||||||
|
// support it.
|
||||||
|
return container.RestartPolicy{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
k, v, _ := strings.Cut(policy, ":")
|
p := container.RestartPolicy{}
|
||||||
|
k, v, ok := strings.Cut(policy, ":")
|
||||||
|
if ok && k == "" {
|
||||||
|
return container.RestartPolicy{}, fmt.Errorf("invalid restart policy format: no policy provided before colon")
|
||||||
|
}
|
||||||
if v != "" {
|
if v != "" {
|
||||||
count, err := strconv.Atoi(v)
|
count, err := strconv.Atoi(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return p, fmt.Errorf("invalid restart policy format: maximum retry count must be an integer")
|
return container.RestartPolicy{}, fmt.Errorf("invalid restart policy format: maximum retry count must be an integer")
|
||||||
}
|
}
|
||||||
p.MaximumRetryCount = count
|
p.MaximumRetryCount = count
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue