From 9699e81a0624c8505b63d9ac5b5a30980c9f5b46 Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Wed, 17 Feb 2021 21:18:39 +0300 Subject: API: fix libpod's container wait endpoint condition conversion Signed-off-by: Nikolay Edigaryev --- pkg/api/handlers/decoder.go | 15 +++++++++++++++ test/apiv2/20-containers.at | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pkg/api/handlers/decoder.go b/pkg/api/handlers/decoder.go index 54087168a..123d325aa 100644 --- a/pkg/api/handlers/decoder.go +++ b/pkg/api/handlers/decoder.go @@ -6,6 +6,7 @@ import ( "syscall" "time" + "github.com/containers/podman/v2/libpod/define" "github.com/containers/podman/v2/pkg/util" "github.com/gorilla/schema" "github.com/sirupsen/logrus" @@ -19,6 +20,7 @@ func NewAPIDecoder() *schema.Decoder { d.IgnoreUnknownKeys(true) d.RegisterConverter(map[string][]string{}, convertURLValuesString) d.RegisterConverter(time.Time{}, convertTimeString) + d.RegisterConverter(define.ContainerStatus(0), convertContainerStatusString) var Signal syscall.Signal d.RegisterConverter(Signal, convertSignal) @@ -46,6 +48,19 @@ func convertURLValuesString(query string) reflect.Value { return reflect.ValueOf(f) } +func convertContainerStatusString(query string) reflect.Value { + result, err := define.StringToContainerStatus(query) + if err != nil { + logrus.Infof("convertContainerStatusString: Failed to parse %s: %s", query, err.Error()) + + // We return nil here instead of result because reflect.ValueOf().IsValid() will be true + // in github.com/gorilla/schema's decoder, which means there's no parsing error + return reflect.ValueOf(nil) + } + + return reflect.ValueOf(result) +} + // isZero() can be used to determine if parsing failed. func convertTimeString(query string) reflect.Value { var ( diff --git a/test/apiv2/20-containers.at b/test/apiv2/20-containers.at index 0da196e46..292562934 100644 --- a/test/apiv2/20-containers.at +++ b/test/apiv2/20-containers.at @@ -63,7 +63,7 @@ cid=$(jq -r '.Id' <<<"$output") # Prior to the fix in #6835, this would fail 500 "args must not be empty" t POST libpod/containers/${cid}/start '' 204 # Container should exit almost immediately. Wait for it, confirm successful run -t POST libpod/containers/${cid}/wait '' 200 '0' +t POST libpod/containers/${cid}/wait?condition=stopped&condition=exited '' 200 '0' t GET libpod/containers/${cid}/json 200 \ .Id=$cid \ .State.Status~\\\(exited\\\|stopped\\\) \ -- cgit v1.2.3-54-g00ecf From b3f9559c87421701a026f62ca277e568158cccc0 Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Thu, 18 Feb 2021 01:09:21 +0300 Subject: bindings: support simple types that implement fmt.Stringer interface Signed-off-by: Nikolay Edigaryev --- pkg/bindings/util/util.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkg/bindings/util/util.go b/pkg/bindings/util/util.go index 6296fc22f..2d65ae9fd 100644 --- a/pkg/bindings/util/util.go +++ b/pkg/bindings/util/util.go @@ -2,6 +2,7 @@ package util import ( "errors" + "fmt" "net/url" "reflect" "strconv" @@ -11,14 +12,25 @@ import ( ) func IsSimpleType(f reflect.Value) bool { + switch f.Interface().(type) { + case fmt.Stringer: + return true + } + switch f.Kind() { case reflect.Bool, reflect.Int, reflect.Int64, reflect.Uint, reflect.Uint64, reflect.String: return true } + return false } func SimpleTypeToParam(f reflect.Value) string { + switch cast := f.Interface().(type) { + case fmt.Stringer: + return cast.String() + } + switch f.Kind() { case reflect.Bool: return strconv.FormatBool(f.Bool()) @@ -31,6 +43,7 @@ func SimpleTypeToParam(f reflect.Value) string { case reflect.String: return f.String() } + panic("the input parameter is not a simple type") } -- cgit v1.2.3-54-g00ecf From 3e168b19f23d3e9e4e9f25d0368b4d607968abb4 Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Thu, 18 Feb 2021 01:40:41 +0300 Subject: Quote URL Signed-off-by: Nikolay Edigaryev --- test/apiv2/20-containers.at | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/apiv2/20-containers.at b/test/apiv2/20-containers.at index 292562934..a99e9a184 100644 --- a/test/apiv2/20-containers.at +++ b/test/apiv2/20-containers.at @@ -63,7 +63,7 @@ cid=$(jq -r '.Id' <<<"$output") # Prior to the fix in #6835, this would fail 500 "args must not be empty" t POST libpod/containers/${cid}/start '' 204 # Container should exit almost immediately. Wait for it, confirm successful run -t POST libpod/containers/${cid}/wait?condition=stopped&condition=exited '' 200 '0' +t POST "libpod/containers/${cid}/wait?condition=stopped&condition=exited" '' 200 '0' t GET libpod/containers/${cid}/json 200 \ .Id=$cid \ .State.Status~\\\(exited\\\|stopped\\\) \ -- cgit v1.2.3-54-g00ecf From e022c19753182cfb85f8f49354d493ee3a3147a3 Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Thu, 18 Feb 2021 01:43:23 +0300 Subject: Don't switch on a single case Signed-off-by: Nikolay Edigaryev --- pkg/bindings/util/util.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pkg/bindings/util/util.go b/pkg/bindings/util/util.go index 2d65ae9fd..c1961308e 100644 --- a/pkg/bindings/util/util.go +++ b/pkg/bindings/util/util.go @@ -12,8 +12,7 @@ import ( ) func IsSimpleType(f reflect.Value) bool { - switch f.Interface().(type) { - case fmt.Stringer: + if _, ok := f.Interface().(fmt.Stringer); ok { return true } @@ -26,9 +25,8 @@ func IsSimpleType(f reflect.Value) bool { } func SimpleTypeToParam(f reflect.Value) string { - switch cast := f.Interface().(type) { - case fmt.Stringer: - return cast.String() + if s, ok := f.Interface().(fmt.Stringer); ok { + return s.String() } switch f.Kind() { -- cgit v1.2.3-54-g00ecf