mirror of https://github.com/docker/cli.git
ParseRestartPolicy: validate for missing policy-names
Also make it slightly more clearer we're returning a default (empty) policy if the input is empty. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
4cce7bb2fc
commit
261c18f9ee
|
@ -714,6 +714,10 @@ func TestParseRestartPolicy(t *testing.T) {
|
||||||
Name: "no",
|
Name: "no",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
input: ":1",
|
||||||
|
expectedErr: "invalid restart policy format: no policy provided before colon",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
input: "always",
|
input: "always",
|
||||||
expected: container.RestartPolicy{
|
expected: container.RestartPolicy{
|
||||||
|
@ -758,11 +762,11 @@ func TestParseRestartPolicy(t *testing.T) {
|
||||||
t.Run(tc.input, func(t *testing.T) {
|
t.Run(tc.input, func(t *testing.T) {
|
||||||
_, hostConfig, _, err := parseRun([]string{"--restart=" + tc.input, "img", "cmd"})
|
_, hostConfig, _, err := parseRun([]string{"--restart=" + tc.input, "img", "cmd"})
|
||||||
if tc.expectedErr != "" {
|
if tc.expectedErr != "" {
|
||||||
assert.Check(t, is.Nil(hostConfig))
|
|
||||||
assert.Check(t, is.Error(err, tc.expectedErr))
|
assert.Check(t, is.Error(err, tc.expectedErr))
|
||||||
|
assert.Check(t, is.Nil(hostConfig))
|
||||||
} else {
|
} else {
|
||||||
|
assert.NilError(t, err)
|
||||||
assert.Check(t, is.DeepEqual(hostConfig.RestartPolicy, tc.expected))
|
assert.Check(t, is.DeepEqual(hostConfig.RestartPolicy, tc.expected))
|
||||||
assert.Check(t, err)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -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