Skip to content

Commit 618bbfe

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 618bbfe

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
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",

cli/compose/convert/service.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ func Service(
8383
return swarm.ServiceSpec{}, err
8484
}
8585

86-
restartPolicy, err := convertRestartPolicy(
87-
service.Restart, service.Deploy.RestartPolicy)
86+
restartPolicy, err := convertRestartPolicy(service.Restart, service.Deploy.RestartPolicy)
8887
if err != nil {
8988
return swarm.ServiceSpec{}, err
9089
}
@@ -490,7 +489,7 @@ func convertRestartPolicy(restart string, source *composetypes.RestartPolicy) (*
490489
MaxAttempts: &attempts,
491490
}, nil
492491
default:
493-
return nil, errors.Errorf("unknown restart policy: %s", restart)
492+
return nil, errors.Errorf("invalid restart policy: '%s'", restart)
494493
}
495494
}
496495

cli/compose/convert/service_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestConvertRestartPolicyFromNone(t *testing.T) {
2525

2626
func TestConvertRestartPolicyFromUnknown(t *testing.T) {
2727
_, err := convertRestartPolicy("unknown", nil)
28-
assert.Error(t, err, "unknown restart policy: unknown")
28+
assert.Error(t, err, "invalid restart policy: 'unknown'")
2929
}
3030

3131
func TestConvertRestartPolicyFromAlways(t *testing.T) {

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)