Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions cli/command/container/opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,10 @@ func TestParseRestartPolicy(t *testing.T) {
Name: container.RestartPolicyDisabled,
},
},
{
input: "no:1",
expectedErr: "invalid restart policy: maximum retry count can only be used with 'on-failure'",
},
{
input: ":1",
expectedErr: "invalid restart policy format: no policy provided before colon",
Expand All @@ -830,11 +834,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 can only be used with 'on-failure'",
},
{
input: "always:2:3",
Expand All @@ -857,6 +858,10 @@ func TestParseRestartPolicy(t *testing.T) {
Name: container.RestartPolicyUnlessStopped,
},
},
{
input: "unless-stopped:1",
expectedErr: "invalid restart policy: maximum retry count can only be used with 'on-failure'",
},
{
input: "unless-stopped:invalid",
expectedErr: "invalid restart policy format: maximum retry count must be an integer",
Expand Down
5 changes: 2 additions & 3 deletions cli/compose/convert/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ func Service(
return swarm.ServiceSpec{}, err
}

restartPolicy, err := convertRestartPolicy(
service.Restart, service.Deploy.RestartPolicy)
restartPolicy, err := convertRestartPolicy(service.Restart, service.Deploy.RestartPolicy)
if err != nil {
return swarm.ServiceSpec{}, err
}
Expand Down Expand Up @@ -484,7 +483,7 @@ func convertRestartPolicy(restart string, source *composetypes.RestartPolicy) (*
MaxAttempts: &attempts,
}, nil
default:
return nil, fmt.Errorf("unknown restart policy: %s", restart)
return nil, fmt.Errorf("invalid restart policy: unknown policy '%s'", restart)
}
}

Expand Down
2 changes: 1 addition & 1 deletion cli/compose/convert/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestConvertRestartPolicyFromNone(t *testing.T) {

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

func TestConvertRestartPolicyFromAlways(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions opts/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,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
}
Loading