Skip to content

Commit

Permalink
ParseRestartPolicy: validate for missing policy-names
Browse files Browse the repository at this point in the history
Also make it slightly more clearer we're returning a default (empty)
policy if the input is empty.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
thaJeztah committed Aug 28, 2023
1 parent 4cce7bb commit 610b52d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
4 changes: 4 additions & 0 deletions cli/command/container/opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,10 @@ func TestParseRestartPolicy(t *testing.T) {
Name: "no",
},
},
{
input: ":1",
expectedErr: "invalid restart policy format: no policy provided before colon",
},
{
input: "always",
expected: container.RestartPolicy{
Expand Down
13 changes: 9 additions & 4 deletions opts/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,18 @@ func ConvertKVStringsToMapWithNil(values []string) map[string]*string {

// ParseRestartPolicy returns the parsed policy or an error indicating what is incorrect
func ParseRestartPolicy(policy string) (container.RestartPolicy, error) {
p := container.RestartPolicy{}

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 != "" {
count, err := strconv.Atoi(v)
if err != nil {
Expand Down

0 comments on commit 610b52d

Please sign in to comment.