Skip to content

Commit d40f601

Browse files
committed
opts: ParseRestartPolicy: improve validation of max restart-counts
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 <github@gone.nl>
1 parent ba64618 commit d40f601

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

cli/command/container/opts_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,10 @@ func TestParseRestartPolicy(t *testing.T) {
714714
Name: container.RestartPolicyDisabled,
715715
},
716716
},
717+
{
718+
input: "no:1",
719+
expectedErr: "invalid restart policy: maximum retry count cannot be used with restart policy 'no'",
720+
},
717721
{
718722
input: ":1",
719723
expectedErr: "invalid restart policy format: no policy provided before colon",
@@ -725,11 +729,8 @@ func TestParseRestartPolicy(t *testing.T) {
725729
},
726730
},
727731
{
728-
input: "always:1",
729-
expected: container.RestartPolicy{
730-
Name: container.RestartPolicyAlways,
731-
MaximumRetryCount: 1,
732-
},
732+
input: "always:1",
733+
expectedErr: "invalid restart policy: maximum retry count cannot be used with restart policy 'always'",
733734
},
734735
{
735736
input: "always:2:3",
@@ -752,6 +753,10 @@ func TestParseRestartPolicy(t *testing.T) {
752753
Name: container.RestartPolicyUnlessStopped,
753754
},
754755
},
756+
{
757+
input: "unless-stopped:1",
758+
expectedErr: "invalid restart policy: maximum retry count cannot be used with restart policy 'unless-stopped'",
759+
},
755760
{
756761
input: "unless-stopped:invalid",
757762
expectedErr: "invalid restart policy format: maximum retry count must be an integer",

opts/parse.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,8 @@ func ParseRestartPolicy(policy string) (container.RestartPolicy, error) {
9292
}
9393

9494
p.Name = container.RestartPolicyMode(k)
95+
if err := container.ValidateRestartPolicy(p); err != nil {
96+
return container.RestartPolicy{}, err
97+
}
9598
return p, nil
9699
}

0 commit comments

Comments
 (0)