Skip to content

Commit 1fb63eb

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 <[email protected]>
1 parent 1a1a4fc commit 1fb63eb

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
@@ -819,6 +819,10 @@ func TestParseRestartPolicy(t *testing.T) {
819819
Name: container.RestartPolicyDisabled,
820820
},
821821
},
822+
{
823+
input: "no:1",
824+
expectedErr: "invalid restart policy: maximum retry count can only be used with 'on-failure'",
825+
},
822826
{
823827
input: ":1",
824828
expectedErr: "invalid restart policy format: no policy provided before colon",
@@ -830,11 +834,8 @@ func TestParseRestartPolicy(t *testing.T) {
830834
},
831835
},
832836
{
833-
input: "always:1",
834-
expected: container.RestartPolicy{
835-
Name: container.RestartPolicyAlways,
836-
MaximumRetryCount: 1,
837-
},
837+
input: "always:1",
838+
expectedErr: "invalid restart policy: maximum retry count can only be used with 'on-failure'",
838839
},
839840
{
840841
input: "always:2:3",
@@ -857,6 +858,10 @@ func TestParseRestartPolicy(t *testing.T) {
857858
Name: container.RestartPolicyUnlessStopped,
858859
},
859860
},
861+
{
862+
input: "unless-stopped:1",
863+
expectedErr: "invalid restart policy: maximum retry count can only be used with 'on-failure'",
864+
},
860865
{
861866
input: "unless-stopped:invalid",
862867
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
@@ -80,8 +80,7 @@ func Service(
8080
return swarm.ServiceSpec{}, err
8181
}
8282

83-
restartPolicy, err := convertRestartPolicy(
84-
service.Restart, service.Deploy.RestartPolicy)
83+
restartPolicy, err := convertRestartPolicy(service.Restart, service.Deploy.RestartPolicy)
8584
if err != nil {
8685
return swarm.ServiceSpec{}, err
8786
}
@@ -484,7 +483,7 @@ func convertRestartPolicy(restart string, source *composetypes.RestartPolicy) (*
484483
MaxAttempts: &attempts,
485484
}, nil
486485
default:
487-
return nil, fmt.Errorf("unknown restart policy: %s", restart)
486+
return nil, fmt.Errorf("invalid restart policy: unknown policy '%s'", restart)
488487
}
489488
}
490489

cli/compose/convert/service_test.go

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

2727
func TestConvertRestartPolicyFromUnknown(t *testing.T) {
2828
_, err := convertRestartPolicy("unknown", nil)
29-
assert.Error(t, err, "unknown restart policy: unknown")
29+
assert.Error(t, err, "invalid restart policy: unknown policy 'unknown'; use one of 'no', 'always', 'on-failure', or 'unless-stopped'")
3030
}
3131

3232
func TestConvertRestartPolicyFromAlways(t *testing.T) {

opts/parse.go

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

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

0 commit comments

Comments
 (0)