Skip to content

Commit

Permalink
opts: ParseRestartPolicy: improve validation of max restart-counts
Browse files Browse the repository at this point in the history
Use the new container.ValidateRestartPolicy utility to verify if a max-restart-count
is allowed for the given restart-policy.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
thaJeztah committed Aug 28, 2023
1 parent ba64618 commit d40f601
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
15 changes: 10 additions & 5 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: container.RestartPolicyDisabled,
},
},
{
input: "no:1",
expectedErr: "invalid restart policy: maximum retry count cannot be used with restart policy 'no'",
},
{
input: ":1",
expectedErr: "invalid restart policy format: no policy provided before colon",
Expand All @@ -725,11 +729,8 @@ func TestParseRestartPolicy(t *testing.T) {
},
},
{
input: "always:1",
expected: container.RestartPolicy{
Name: container.RestartPolicyAlways,
MaximumRetryCount: 1,
},
input: "always:1",
expectedErr: "invalid restart policy: maximum retry count cannot be used with restart policy 'always'",
},
{
input: "always:2:3",
Expand All @@ -752,6 +753,10 @@ func TestParseRestartPolicy(t *testing.T) {
Name: container.RestartPolicyUnlessStopped,
},
},
{
input: "unless-stopped:1",
expectedErr: "invalid restart policy: maximum retry count cannot be used with restart policy 'unless-stopped'",
},
{
input: "unless-stopped:invalid",
expectedErr: "invalid restart policy format: maximum retry count must be an integer",
Expand Down
3 changes: 3 additions & 0 deletions opts/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,8 @@ func ParseRestartPolicy(policy string) (container.RestartPolicy, error) {
}

p.Name = container.RestartPolicyMode(k)
if err := container.ValidateRestartPolicy(p); err != nil {
return container.RestartPolicy{}, err
}
return p, nil
}

0 comments on commit d40f601

Please sign in to comment.