From 0d4791f838fa8f4c40cb9f40ff4efffa5ab49732 Mon Sep 17 00:00:00 2001 From: Lei Jitang Date: Fri, 3 Jul 2015 17:33:33 +0800 Subject: [PATCH] Validate restart policy. Fixes #14351 Signed-off-by: Lei Jitang Upstream-commit: f3faf599250e6de3dff340a2e9a72c89c31ba9db Component: engine --- components/engine/runconfig/parse.go | 5 ++++- components/engine/runconfig/parse_test.go | 10 +++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/components/engine/runconfig/parse.go b/components/engine/runconfig/parse.go index 552a648330..d512bd3078 100644 --- a/components/engine/runconfig/parse.go +++ b/components/engine/runconfig/parse.go @@ -428,12 +428,15 @@ func ParseRestartPolicy(policy string) (RestartPolicy, error) { p.Name = name switch name { case "always": - if len(parts) == 2 { + if len(parts) > 1 { return p, fmt.Errorf("maximum restart count not valid with restart policy of \"always\"") } case "no": // do nothing case "on-failure": + if len(parts) > 2 { + return p, fmt.Errorf("restart count format is not valid, usage: 'on-failure:N' or 'on-failure'") + } if len(parts) == 2 { count, err := strconv.Atoi(parts[1]) if err != nil { diff --git a/components/engine/runconfig/parse_test.go b/components/engine/runconfig/parse_test.go index d858c2570a..d1f3c69eb0 100644 --- a/components/engine/runconfig/parse_test.go +++ b/components/engine/runconfig/parse_test.go @@ -456,12 +456,13 @@ func TestParseRestartPolicy(t *testing.T) { invalids := map[string]string{ "something": "invalid restart policy something", "always:2": "maximum restart count not valid with restart policy of \"always\"", + "always:2:3": "maximum restart count not valid with restart policy of \"always\"", "on-failure:invalid": `strconv.ParseInt: parsing "invalid": invalid syntax`, + "on-failure:2:5": "restart count format is not valid, usage: 'on-failure:N' or 'on-failure'", } valids := map[string]RestartPolicy{ "": {}, - // FIXME This feels not right - "always:1:2": { + "always": { Name: "always", MaximumRetryCount: 0, }, @@ -469,11 +470,6 @@ func TestParseRestartPolicy(t *testing.T) { Name: "on-failure", MaximumRetryCount: 1, }, - // FIXME This doesn't feel right - "on-failure:1:2": { - Name: "on-failure", - MaximumRetryCount: 0, - }, } for restart, expectedError := range invalids { if _, _, _, err := parseRun([]string{fmt.Sprintf("--restart=%s", restart), "img", "cmd"}); err == nil || err.Error() != expectedError {